Tutorial · Beginner · 5 min
Making sound
Turn signals into sound: tones, chords, envelopes and stereo — and save them as WAV files.
A tone
sound(y, fs)plays the samples y (between −1 and 1) at fs samples per second. A player with the waveform appears under the figure.
Try it ↗fs = 44100; t = 0:1/fs:1; sound(0.4*sin(2*pi*440*t), fs) % concert AA chord with an envelope
Add tones together and shape the loudness over time.
soundscscales the result so it is as loud as possible without clipping.
Try it ↗fs = 44100; t = 0:1/fs:2; chord = sin(2*pi*261.6*t) + sin(2*pi*329.6*t) + sin(2*pi*392*t); % C major env = min(t/0.02, 1) .* exp(-1.5*t); soundsc(chord .* env, fs) plot(t, chord .* env), xlabel('time (s)'), title('C major chord')Stereo and saving
A matrix with two columns is played in stereo (left, right).
audiowriteoffers the result as a WAV download.
Try it ↗fs = 22050; t = (0:1/fs:3)'; left = sin(2*pi*300*t); right = sin(2*pi*303*t); % slightly detuned: a slow wobble audiowrite('wobble.wav', 0.4*[left right], fs) sound(0.4*[left right], fs)