Ron Bakker/MathLabHelp
Open MathLab ↗

Tutorial · Beginner · 5 min

Making sound

Turn signals into sound: tones, chords, envelopes and stereo — and save them as WAV files.

  1. 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.

    fs = 44100;
    t = 0:1/fs:1;
    sound(0.4*sin(2*pi*440*t), fs)    % concert A
    Try it ↗
  2. A chord with an envelope

    Add tones together and shape the loudness over time. soundsc scales the result so it is as loud as possible without clipping.

    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')
    Try it ↗
  3. Stereo and saving

    A matrix with two columns is played in stereo (left, right). audiowrite offers the result as a WAV download.

    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)
    Try it ↗