%SOUNDDEMO Demonstrate MATLAB V4's sound capability.

%       Copyright (c) 1984-93 by The MathWorks, Inc.

echo on
clc
%       MATLAB has sound capability on SPARC, HP 9000/700, SGI, and
%       Macintosh computers.  On PCs, sound is available if you
%       have an MS-Windows supported sound card installed.
%
%       Continue with this demo only if you have sound available.

echo off
s = input('Do you wish to continue? ([y]/n) ','s');
if isempty(s), s = 'y'; end
if strcmp(lower(s(1)),'n')
        return
end
clf 
saxis('auto')
echo on
clc
%       MATLAB is useful for exploring audio data. Sounds can be thought
%       of as one-dimensional arrays, or vectors.  There are a number of
%       sample files with sound data in the sounds directory.  Let's see
%       what's there.

pause      % Press any key to continue.
clc
what sounds
%       The MAT-files in this directory contain data appropriate for using
%       with the sound command. For example, splat.mat contains
%       audio data of a dropping egg.  Let's load the data in.

load splat

pause      % Press any key to continue.
clc

%       Now in the workspace is vector y, with audio data, and scalar Fs
%       containing the sample rate.  Let's generate the corresponding
%       time vector and plot the time sequence.

t = (1:length(y))/Fs;
plot(t,y)

pause      % Press any key to continue.

%       Use MATLAB's SOUND command to play the sound through the audio
%       port.

sound(y,Fs)

pause      % Press any key to continue.
clc

%       The SAXIS command controls the volume at which the next sound
%       is played in much the same way that AXIS controls plot scaling.
%       First find out the range of the data.

range  = [min(y) max(y)]
%       Now replay the egg-drop sound at a lower volume.

saxis(2*range)
sound(y,Fs)

pause      % Press any key to continue.
clc

%       Finally, let's build a separate window in which buttons, made from
%       UICONTROLS, are set up.  These display choices of sounds to play,
%       a volume control, and, alternate ways to view the data graphically,
%       if the Signal Processing Toolbox is present.

%       You may view the audio graphically in three ways.
%       The default option, as a time sequence, is a straight-forward
%       two-dimensional plot of the data as a function of time.
 
%       Another way to see the information in the data is to view the
%       "spectrum", an average of the signal's frequency content for
%       the duration of the sound. The spectrum shows the signal's
%       power as a function of frequency.
 
%       The third choice is to look at the "spectrogram" of the data.
%       This is a view of the frequency content of the signal as a
%       function of time.  Time increases from left to right;
%       frequency increases from bottom to top.

echo off
soundext
if exist('freqz')~=2
	clc
end
