Search code examples
javascriptaudiotone.js

Create sound effect with Tone.js notes?


How can I create one of these sound effects with Tone.js notes

Is this even possible? When given are these notes: "C","C#","Db","D","D#","Eb","E","E#","Fb","F","F#","Gb","G","G#","Ab","A","A#","Bb","B","B#","Cb"... Can I now somehow use tone.js to create a sound effect like "Tada!"? I think it needs more than just the notes/tones, it needs also somehow pitching and time manimulation?

Simple C tone played for 400ms:

polySynth.triggerAttack("C");
setTimeout(x=>polySynth.triggerRelease("C"),400);

Here a working Jsfiddle with Tone.js to experiment.


Solution

  • I don't have a very experienced ear, but most of these sound like major chords (base, third, fifth) to me, some with an added octave. For example, C4, E4, G4, C5:

    const chord = ["C4", "E4", "G4", "C5"];
    const duration = 0.5;
    const delay = 0.05;
    const now = Tone.now();
    for (let i = 0; i < chord.length; i++) {
      const note = chord[i];
      polySynth.triggerAttackRelease(note, duration, now + i * delay);
    }
    

    If you want to randomize the root note, it might be useful to work with frequencies directly, instead of note names. The A above middle C is usually taken as 440 Hz, and each successive semi-tone above that is a factor of Math.pow(2, 1/12) higher:

    const rootFrequency = 440;
    const chordSemitones = [0, 4, 7, 12];
    const duration = 0.5;
    const delay = 0.1;
    const now = Tone.now();
    for (let i = 0; i < chordSemitones.length; i++) {
      const pitch = rootFrequency * Math.pow(2, chordSemitones[i] / 12);
      polySynth.triggerAttackRelease(pitch, duration, now + i * delay);
    }