Skip to content

Commit 5c59266

Browse files
committed
feat(singer) add console logs
1 parent 340ab0c commit 5c59266

File tree

3 files changed

+105
-5
lines changed

3 files changed

+105
-5
lines changed
Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,99 @@
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+
172
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+
);
399
}

modules/singer/src/core/synthUtils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,11 @@ export default class SynthUtils implements ISynthUtils {
107107
}
108108

109109
const _pitch = this.samples[sampleName]['centerNote'];
110+
110111
this.samplerSynths.set(
111112
sampleName,
112113
new Tone.Sampler({
113-
_pitch: this.samples[sampleName]['data'],
114+
[_pitch]: this.samples[sampleName]['data'],
114115
}),
115116
);
116117
}
@@ -232,12 +233,12 @@ export default class SynthUtils implements ISynthUtils {
232233
) {
233234
const now = Tone.now();
234235

235-
if (instrumentName in this.builtinSynths) {
236+
if (this.builtinSynths.has(instrumentName)) {
236237
const synth = this.getBuiltinSynth(instrumentName);
237238
if (synth !== undefined) {
238239
synth.triggerAttackRelease(pitches, noteValueInSeconds, now + offset);
239240
}
240-
} else if (instrumentName in this.samplerSynths) {
241+
} else if (this.samplerSynths.has(instrumentName)) {
241242
const synth = this.samplerSynths.get(instrumentName);
242243
if (synth !== undefined) {
243244
Tone.loaded().then(() => {
@@ -252,7 +253,7 @@ export default class SynthUtils implements ISynthUtils {
252253
}
253254
});
254255
}
255-
} else if (instrumentName in this.playerSynths) {
256+
} else if (this.playerSynths.has(instrumentName)) {
256257
const synth = this.playerSynths.get(instrumentName);
257258
if (synth !== undefined) {
258259
Tone.loaded().then(() => {

modules/singer/src/core/voice.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ export class Voice implements IVoice {
446446
}
447447

448448
if (this._synthUtils !== null) {
449+
console.log('calling synthUtils trigger function');
449450
this._synthUtils.trigger(
450451
pitches,
451452
noteValue,
@@ -475,6 +476,8 @@ export class Voice implements IVoice {
475476
* @throws {InvalidArgumentError}
476477
*/
477478
public playNote(pitch: string | number, noteValue: number, instrumentName: string) {
479+
console.log('calling voice playNote function');
478480
this.playNotes([pitch], noteValue, instrumentName, 0, true);
479481
}
480482
}
483+

0 commit comments

Comments
 (0)