|
| 1 | +import { Voice } from '@/core/voice'; |
| 2 | +import SynthUtils from '@/core/synthUtils'; |
| 3 | +import * as Tone from 'tone'; |
| 4 | +import { _state, noteValueToSeconds, _defaultSynth, _polySynth } from '@/singer'; |
| 5 | +import { setupSynthUtils } from '@/core/synthUtils'; |
| 6 | +import { injected } from '@/index'; |
| 7 | + |
| 8 | +await (async () => { |
| 9 | + const { importAssets, getAsset } = await import('@sugarlabs/mb4-assets'); |
| 10 | + const assetManifest = (await import('@sugarlabs/mb4-assets')).default; |
| 11 | + await importAssets( |
| 12 | + Object.entries(assetManifest).map(([identifier, manifest]) => ({ identifier, manifest })), |
| 13 | + () => undefined, |
| 14 | + ); |
| 15 | + |
| 16 | + injected.assets = { |
| 17 | + 'audio.guitar': getAsset('audio.guitar')!, |
| 18 | + 'audio.piano': getAsset('audio.piano')!, |
| 19 | + 'audio.snare': getAsset('audio.snare')!, |
| 20 | + }; |
| 21 | + |
| 22 | +})(); |
| 23 | + |
| 24 | +function _getSynth(synthType: string) { |
| 25 | + switch (synthType) { |
| 26 | + case 'polysynth': |
| 27 | + return _polySynth; |
| 28 | + } |
| 29 | + return _defaultSynth; |
| 30 | +} |
| 31 | + |
| 32 | +async function playSynth(synthType: string) { |
| 33 | + await Tone.start(); |
| 34 | + const synth = _getSynth(synthType); |
| 35 | + _state.notesPlayed = 0; |
| 36 | + console.log('playing c4 using', synthType); |
| 37 | + const now = Tone.now(); |
| 38 | + let offset = noteValueToSeconds(_state.notesPlayed); |
| 39 | + synth.triggerAttackRelease('c4', '4n', now + offset); |
| 40 | + _state.notesPlayed += 4; |
| 41 | +} |
| 42 | + |
| 43 | +async function voice() { |
| 44 | + const synth = new SynthUtils(); |
| 45 | + const myVoice = new Voice('myvoice', synth); |
| 46 | + |
| 47 | + await setupSynthUtils(); |
| 48 | + |
| 49 | + myVoice.playNote('g4', 1 / 4, 'piano'); |
| 50 | + |
| 51 | + // synth.trigger(['c4', 'g5'], 1/2, 'piano', 0); |
| 52 | + |
| 53 | + // const sampler = new Tone.Sampler({ |
| 54 | + // urls: { |
| 55 | + // A1: "A1.mp3", |
| 56 | + // A2: "A2.mp3", |
| 57 | + // }, |
| 58 | + // baseUrl: "https://tonejs.github.io/audio/casio/", |
| 59 | + // onload: () => { |
| 60 | + // sampler.triggerAttackRelease(["C4", "E4", "G4"], 4, 2); |
| 61 | + // } |
| 62 | + // }).toDestination(); |
| 63 | + |
| 64 | + |
| 65 | + // _state.notesPlayed = 0; |
| 66 | + // const now = Tone.now(); |
| 67 | + // let offset = noteValueToSeconds(_state.notesPlayed); |
| 68 | + // synth.trigger(['c4', 'd4'], 4, 'electronic synth', now + offset); |
| 69 | + // _state.notesPlayed += 4; |
| 70 | +} |
| 71 | + |
1 | 72 | export default function (): JSX.Element { |
2 | | - return <div>Voice Testbench</div>; |
| 73 | + return ( |
| 74 | + <div> |
| 75 | + <h1>Voice Component</h1> |
| 76 | + <button |
| 77 | + onClick={async () => { |
| 78 | + await playSynth('default'); |
| 79 | + }} |
| 80 | + > |
| 81 | + Default Synth |
| 82 | + </button> |
| 83 | + <button |
| 84 | + onClick={async () => { |
| 85 | + await playSynth('polysynth'); |
| 86 | + }} |
| 87 | + > |
| 88 | + PolySynth |
| 89 | + </button> |
| 90 | + <button |
| 91 | + onClick={async () => { |
| 92 | + await voice(); |
| 93 | + }} |
| 94 | + > |
| 95 | + Voice Sample Synth |
| 96 | + </button> |
| 97 | + </div> |
| 98 | + ); |
3 | 99 | } |
0 commit comments