Skip to content

Commit

Permalink
Implement sound spriting
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxLaumeister committed Dec 22, 2019
1 parent 4a10b69 commit c34d8c3
Showing 1 changed file with 33 additions and 36 deletions.
69 changes: 33 additions & 36 deletions src/NotePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,45 @@ class NotePlayer {
// Pre-render synth

this.numVoices = 3; // Number of voices (players) *per note*
this.noteDuration = (Tone.Time('1m') / gridWidth) * 6; // Total note duration, including release

this.players = [];
// eslint-disable-next-line prefer-destructuring
const players = this.players;
scale.forEach((el, idx) => {
Tone.Offline(() => {
const lowPass = new Tone.Filter({
frequency: 1100,
rolloff: -12,
}).toMaster();

const synth = new Tone.PolySynth(16, Tone.Synth, {
oscillator: {
type: 'sine',
},
envelope: {
attack: 0.005,
decay: 0.1,
sustain: 0.3,
release: 1,
},
}).connect(lowPass);
synth.triggerAttackRelease(el, Tone.Time('1m') / gridWidth, 0);
}, (Tone.Time('1m') / gridWidth) * 6).then((buffer) => {
const voices = [];
for (let i = 0; i < this.numVoices; i += 1) {
voices.push(new Tone.Player(buffer).toMaster());
}
players[idx] = ({ voices, currentVoice: 0 });

this.currentPlayer = 0;

const self = this;
Tone.Offline(() => {
const lowPass = new Tone.Filter({
frequency: 1100,
rolloff: -12,
}).toMaster();

const synth = new Tone.PolySynth(16, Tone.Synth, {
oscillator: {
type: 'sine',
},
envelope: {
attack: 0.005,
decay: 0.1,
sustain: 0.3,
release: 1,
},
}).connect(lowPass);

scale.forEach((el, idx) => {
synth.triggerAttackRelease(el, Tone.Time('1m') / gridWidth, idx * self.noteDuration);
});
}, this.noteDuration * scale.length).then((buffer) => {
for (let i = 0; i < scale.length * self.numVoices; i += 1) {
this.players.push(new Tone.Player(buffer).toMaster());
}
});
}

play(index, time, volume) {
// Cycle through the note's voices
const note = this.players[index];
try {
note.voices[note.currentVoice].volume.setValueAtTime(volume, time);
note.voices[note.currentVoice].start(time);
note.currentVoice = (note.currentVoice + 1) % this.numVoices;
} catch (e) {
// Player not ready yet
}
// Cycle through the voices
this.players[this.currentPlayer].volume.setValueAtTime(volume, time);
this.players[this.currentPlayer].start(time, index * this.noteDuration, this.noteDuration);
this.currentPlayer = (this.currentPlayer + 1) % this.players.length;
}
}

0 comments on commit c34d8c3

Please sign in to comment.