function soundext()
%SOUNDEXT Demonstrate MATLAB V4's sound capability.  (SPARC only.)

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

global SD_FIG
if ~isempty(SD_FIG)
	set(0,'CurrentFigure',SD_FIG);
	return
end
SD_NAMES = [ 'chirp   '
          'gong    '
          'handel  '
          'splat   '
          'train   '
          'laughter'];
nfiles = size(SD_NAMES,1);
global SD_NAMES
global SD_PLOTCMD
SD_PLOTCMD = str2mat('plot(t,y);xlabel(''Time in seconds'');',...
    ['p=spectrum(y,1024);specplot(p,Fs);h=get(gca,''children'');',...
    'delete(h(1:2));xlabel(''Frequency in Hertz'');'],...
    ['specgram(y);axis off;xlabel(''Time in seconds'');',...
    'ylabel(''Frequency'');']);
global SD_DISP
SD_DISP = 1;
S =  brighten([[zeros(8,2) (3:10)'/10]; prism(56)],1/3);
if (get(0,'ScreenDepth') == 1)
   S = gray(64);
end
SD_FIG = figure('units','normal','position',[.13 .33 .477 .44],...
                'colormap',S);
% button labels corresponding to file names
sd_labels = str2mat('Bird chirps', ...
         'Chinese Gong', ...
         'Hallelujah', ...
         'Dropping Egg', ...
         'Train Whistle', ...
         'Laughter');
% now add labels for FUNCTIONS
sd_labels = str2mat(sd_labels,...
         'Beats',...
         'FM');
% specify functions
funcs = str2mat('Fs=8192;t=0:1/Fs:2;y = sin(300*2*pi*t)+sin(330*2*pi*t);',...
                'Fs=8192;t=0:1/Fs:2;y = sin(2*pi*440*erf(t));');
func_titles = str2mat('sin(a*t)+sin(b*t)',...
                      'sin(c*erf(t))');
nsounds = size(sd_labels,1);
maxbuts = nsounds+1;

% place sound menu buttons on left side
tots = 1.5*(maxbuts+5)+.5;
but_h = .8/(maxbuts+5);
wids = 0.26;
pos = [0.01 .25/tots wids but_h];

% set close control
hcl = uicontrol(SD_FIG,'units','normal','pos',pos);

hs = [];
% set sound controls
for k = 2:maxbuts
   pos = [0.01 (1.4*(k+4)+.25)/tots wids but_h];
   hs = [uicontrol(SD_FIG,'units','normal','pos',pos) ; hs];
end

% set choices for display, if signal processing toolbox is there
hd = [];
if exist('freqz')==2
   nui = 3;
else
   nui = 1;
end
for k = 1:nui
   pos = [0.01 (1.4*(k+1)+.25)/tots wids but_h];
   hd = [uicontrol(SD_FIG,'units','normal',...
         'pos',pos,'style','radiobutton'); hd];
end
% place handles of related radiobuttons in UserData
for k = 1:nui
    hh = hd;
    hh(k) = [];
    set(hd(k),'UserData', hh);
end
set(hd(1),'value',1);

% set close callback
s = ['global SD_FIG;close(SD_FIG),saxis auto;', ...
    'clear global SD_FIG SD_VOLUME SD_NAMES SD_DISP SD_PLOTCMD SD_AXIS;',...
    'clear SD_AXIS SD_FIG SD_NAMES SD_DISP SD_PLOTCMD SD_VOLUME'];
set(hcl,'string',' Close','horiz','left','callback',s);

% set sound callbacks
% Place latest sound data in figure's user data so radio button callbacks can
% do their thing with the data as well.
for k = 1:nsounds
    s = ['global SD_AXIS SD_FIG SD_DISP SD_PLOTCMD SD_NAMES SD_VOLUME;', ...
        'cf=gcf;set(0,''currentfigure'',SD_FIG);', ...
        'set(SD_FIG,''CurrentAxes'',SD_AXIS);', ...
        'point = get(SD_FIG,''pointer'');', ...
        'set(SD_FIG,''pointer'',''watch'');drawnow;'];
    if k <= nfiles
        s = [s 'file = ''',SD_NAMES(k,:),''';file(file == '' '') = [];',...
        'load(file);',...
        'y=y/max(abs(y));t=(0:length(y)-1)/Fs;'];
    else
        s = [s deblank(funcs(k-nfiles,:))];
    end
    s = [s 'set(SD_FIG,''UserData'',[t(:) y(:)]);',...
        'n = SD_DISP;eval([SD_PLOTCMD(n,:)]);'];
    if k <= nfiles
        s = [s 'title([int2str(length(y)) '' Samples'']),drawnow,'];
    else
        s = [s 'title(''' deblank(func_titles(k-nfiles,:)) ''');drawnow,'];
    end
    s = [s 'slim=[min(y) max(y)];saxis(slim/(get(SD_VOLUME,''value'')));', ...
        'sound(y,Fs);set(SD_FIG,''pointer'',point);', ...
        'if cf~=SD_FIG,set(0,''CurrentFigure'',cf),end,'];
    set(hs(k),'string',[' ',sd_labels(k,:)],'horiz','left','callback',s);
end

% set display callbacks
dd_labels = str2mat('Time Sequence','Spectrum','Spectrogram');
for k = 1:length(hd)
    s = ['global SD_FIG,thisone = get(SD_FIG,''CurrentObject'');',...
        'others = get(thisone,''UserData''); set(others,''value'',0);',...
        'set(thisone,''value'',1);',...
        'if strcmp('' Time Sequence'',deblank(get(thisone,''string''))),',...
        'SD_DISP=1;',...
        'elseif strcmp('' Spectrum'',deblank(get(thisone,''string''))),',...
        'SD_DISP=2;',...
        'elseif strcmp('' Spectrogram'',deblank(get(thisone,''string''))),',...
        'SD_DISP=3;end,',...
        'y = get(SD_FIG,''UserData'');',...
        'if ~isempty(y),t = y(:,1); y = y(:,2);',...
        'point = get(SD_FIG,''pointer'');',...
        'set(SD_FIG,''pointer'',''watch'');drawnow;',...
        'titl = get(get(SD_AXIS,''title''),''String'');',...
        'eval([SD_PLOTCMD(SD_DISP,:)]);',...
        'set(SD_FIG,''pointer'',point);',...
        'title(titl),drawnow,end;'];
    set(hd(k),'callback',s,'string',[' ',dd_labels(k,:)],'horiz','left');
end

% add volume control
SD_VOLUME = uicontrol(SD_FIG,'units','normal',...
    'position',[1.5*(0.01+wids) .06 .8*(.9-wids) .03]);
set(SD_VOLUME,'style','slider','min',0.01,'max',1,'value',1);
global SD_VOLUME

% set up axis to draw in
SD_AXIS = axes('position',[1.4*(0.01+wids) .2 .9*(.9-wids) .7]);
axis off
global SD_AXIS
rect = [.6 .023 .2 .03] ;

% label volume control
uicontrol(SD_FIG,'style','text','units','normal','position',rect,...
   'string','Volume','backgroundcolor',get(gcf,'color'),...
   'foregroundcolor', get(SD_AXIS,'xcolor'),...
   'Horizontal','left');

% label volumn of buttons with a title
rect = [0.02 (1.4*(maxbuts+5)+.25)/tots wids but_h] ;
uicontrol(SD_FIG,'style','text','units','normal','position',rect,...
   'string','Sounds','backgroundcolor',get(gcf,'color'),...
   'foregroundcolor', get(SD_AXIS,'xcolor'),...
   'Horizontal','left');
