-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Synth pre-renders into one player per note * Refactor note playing into its own class * Synth now cycles through multiple voices * Notes get quieter with more played at once, to prevent crackling. Closes #1. Also, added ghostMode false to gulpfile and increased particles count to 500. * Implement tile glow on mouseover. Closes #8 * Fix touch events again
- Loading branch information
1 parent
f867a5a
commit 4a10b69
Showing
5 changed files
with
142 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,6 +91,7 @@ function serve(done) { | |
server: { | ||
baseDir: './dist', | ||
}, | ||
ghostMode: false, | ||
}); | ||
done(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* global Tone */ | ||
// eslint-disable-next-line no-unused-vars | ||
class NotePlayer { | ||
constructor(gridWidth, gridHeight) { | ||
// Construct scale array | ||
const pentatonic = ['B#', 'D', 'F', 'G', 'A']; | ||
const octave = 3; // base octave | ||
const octaveoffset = 4; | ||
let scale = Array(gridHeight); | ||
for (let i = 0; i < gridHeight; i += 1) { | ||
scale[i] = pentatonic[i % pentatonic.length] | ||
+ (octave + Math.floor((i + octaveoffset) / pentatonic.length)); | ||
} | ||
scale = scale.reverse(); // higher notes at lower y values, near the top | ||
|
||
// Pre-render synth | ||
|
||
this.numVoices = 3; // Number of voices (players) *per note* | ||
|
||
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 }); | ||
}); | ||
}); | ||
} | ||
|
||
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ h1 { | |
|
||
canvas { | ||
margin: -4px 0; | ||
cursor: pointer; | ||
} | ||
|
||
.canvaswrap { | ||
|