13. Sounds

 

13.1  Playing beeps

13.2  Loading and playing other sound files

13.3  Controlling volume

13.4  Staggering or overlapping sounds and delaying sounds

13.5  Controlling volume while staggering or overlapping sounds

13.6  Creating your own sound files computationally

13.7  Writing and reading files for sound

13.8  Learning more about sound-related functions

13.9  Practicing sounds

 

 

top of page

13.1 Playing beeps

 

 

Code 13.1.1

 

close all

clear all

beep, pause(2), beep

 

 

top of page

13.2 Loading and playing other sound files

 

Code 13.2.1

 

pause(2)

load chirp

sound(y)

commandwindow

whos

figure(1)

plot(y,'k')

shg

pause(2)

 

Output 13.2.1

 

 y            13129x1                    105032  double array

 

 

 

 

top of page

13.3 Controlling volume

 

Code 13.3.1

 

pause(2)

load handel

soundsc(y,[-3.25 3.25])

figure(2)

subplot(2,2,1)

y_loud = y;

plot(y_loud,'k')

hold on

pause(6)

soundsc(y,[-15.25 15.25])

subplot(2,2,2)

y_soft = y;

plot(y_soft,'k')

subplot(2,2,3:4)

plot(y_soft,y_loud,'k')

axis square

grid on

 

Output 13.3.1

 

 

 

top of page

13.4 Staggering or overlapping sounds and delaying sounds

 

 

Code 13.4.1

 

load chirp;

y1 = y;

load gong;

% If sync, chirp finishes before gong begins

wavplay(y1,Fs,'sync')  

wavplay(y,Fs)

 

pause

load chirp;

y1 = y;

load gong;

% If async, gong starts before chirp ends

wavplay(y1,Fs,'async')

wavplay(y,Fs)

 

 

Code 13.4.2

 

load chirp

sf1 = 22050;                     

t = timer('TimerFcn',' wavplay(y, sf1, ''async'')', ...

    'StartDelay', 1.5);

start(t)

 

 

top of page

13.5  Controlling volume while staggering or overlapping sounds

 

 

Code 13.5.1

 

load gong;

for a = 1:3

    wavplay(y*a*.3,Fs,'sync')

end

 

 

top of page

13.6  Creating your own sound files computationally

 

 

Code 13.6.1

 

pause

sf = 22050;                      % sample frequency

d = 1.0;                         % duration

n = sf*d;                        % number of samples

noise = rand(1,n);              % uniform distribution

noise = noise / max(abs(noise)); % normalize

sound(noise,sf);                % play sound

figure(4)

plot(noise,'k')

xlim([1 n]);

ylim([-.5 1.5]);

 

Output 13.6.1

 

 

 

Code 13.6.2

 

pause

cf = 440;                        % carrier frequency (Hz)

sf = 22050;                      % sample frequency (Hz)

d = 1.0;                         % duration (s)

n = sf * d;                      % number of samples

s = (1:n) / sf;                  % time-dependent values

tone = sin(2 * pi * cf * s);     % sinusoidal modulation

sound(tone,sf);                  % sound presentation

figure(5)

subplot(2,1,1)

plot(tone,'k')

subplot(2,1,2)

plot(tone(1:250),'k')

 

 

Output 13.6.2

 

Code 13.6.3

 

pause

a = linspace(1/length(tone),1,length(tone));

sound(a.*tone,sf)

figure(6)

plot(a.*tone,'k')

 

Output 13.6.3

 

 

 

Code 13.6.4

 

    fs = 8000;       % sampling frequency

    t = 0:1/fs:0.25; % length of each note

    tspace = .05;    % length of pause between notes

    fr = 2^(1/12);   % frequency ratio between neighboring keys

    A4 = 440;        % reference note for others

    B4 = A4*fr^2;

    C4 = A4*fr^(-9);

    D4 = A4*fr^(-7);

    E4 = A4*fr^(-5);

    F4 = A4*fr^(-4);

    G4 = A4*fr^(-2);

    C5 = A4*fr^3;

    xspace = zeros(size(tspace));    % set pause

    x = [cos(C4*2*pi*t),xspace, ...

         cos(D4*2*pi*t),xspace, ...

         cos(E4*2*pi*t),xspace, ...

         cos(F4*2*pi*t),xspace, ...

         cos(G4*2*pi*t),xspace,...

          cos(A4*2*pi*t),xspace,...

         cos(B4*2*pi*t),xspace,...

         cos(C5*2*pi*t)];

    wavplay(x,fs);

 

 

top of page

13.7  Writing and reading files for sound

 

Code 13.7.1

 

wavwrite(x,‘scale.wav’)

z = wavread(‘scale.wav’)

sound(z)