|
| 1 | +// this code from https://gist.github.com/vanyle/c31dcd43d450543e423f8fa745ae67e2 |
| 2 | + |
| 3 | +// This function only generates pure frequencies but you can customize the waveform |
| 4 | +// by changing the function at line ~50 (the one with the cos) |
| 5 | + |
| 6 | +export function makeTestSound(freq, volume, duration, fadeout) { |
| 7 | + // Generate a wav binary file, convert it to a blob and play it. |
| 8 | + // https://fr.wikipedia.org/wiki/Waveform_Audio_File_Format#Structure_des_fichiers_WAV |
| 9 | + |
| 10 | + const sampling = 44100; // in hertz |
| 11 | + duration = duration || 1; // in seconds |
| 12 | + const sampleCount = Math.floor(sampling * duration); |
| 13 | + |
| 14 | + volume = volume || 0.5; |
| 15 | + if (volume > 1) volume = 1; |
| 16 | + if (volume < 0) volume = 0; // clamp |
| 17 | + volume = Math.floor(volume * 256 * 256); // convert to 16 bit integer |
| 18 | + |
| 19 | + freq = freq || 440; // nice default freq |
| 20 | + |
| 21 | + // Helper function to build binary files |
| 22 | + function toBytes(value, bytes) { |
| 23 | + let result = ""; |
| 24 | + for (; bytes > 0; bytes--) { |
| 25 | + result += String.fromCharCode(value & 255); |
| 26 | + value >>= 8; |
| 27 | + } |
| 28 | + return result; |
| 29 | + } |
| 30 | + |
| 31 | + let samples = ""; |
| 32 | + |
| 33 | + // Example of a fade: linear fade. |
| 34 | + // fadeout = fadeout || ((t,max) => (max-t)/max); |
| 35 | + |
| 36 | + // By default, be use a fancy "fast attack, slow descent" fade |
| 37 | + fadeout = |
| 38 | + fadeout || |
| 39 | + ((t, max) => { |
| 40 | + const attackRatio = 0.01; |
| 41 | + if (t / max < attackRatio) { |
| 42 | + // fast attack |
| 43 | + return t / max / attackRatio; |
| 44 | + } else { |
| 45 | + // slow descent (linear) |
| 46 | + return (1 - t / max) / (1 - attackRatio); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + for (let i = 0; i < sampleCount; i++) { |
| 51 | + samples += toBytes( |
| 52 | + ((Math.cos((2 * Math.PI * i * freq) / sampling) + 1) / 2) * |
| 53 | + volume * |
| 54 | + fadeout(i, sampleCount), |
| 55 | + 2, |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + let dataChunk = [ |
| 60 | + "fmt ", |
| 61 | + "\x10\x00\x00\x00", // 16: chunk length |
| 62 | + "\x01\x00", // audio format, PCM integer |
| 63 | + "\x01\x00", // mono |
| 64 | + toBytes(sampling, 4), |
| 65 | + toBytes(sampling * 2, 4), // bytes / sec |
| 66 | + "\x02\x00", // bytes per bloc |
| 67 | + "\x10\x00", // bytes per sample |
| 68 | + "data", |
| 69 | + toBytes(sampleCount * 2, 4), |
| 70 | + samples, |
| 71 | + ].join(""); |
| 72 | + |
| 73 | + let wav = ["RIFF", toBytes(20 + dataChunk.length, 4), "WAVE", dataChunk].join(""); |
| 74 | + |
| 75 | + let uint8 = new Uint8Array(wav.length); |
| 76 | + for (let i = 0; i < uint8.length; i++) { |
| 77 | + uint8[i] = wav.charCodeAt(i); |
| 78 | + } |
| 79 | + |
| 80 | + let blob = new Blob([uint8], { type: "audio/wav" }); |
| 81 | + return URL.createObjectURL(blob); |
| 82 | +} |
0 commit comments