home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 3.ddi / DEMOS.DI$ / SOUNDEXT.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  5.9 KB  |  170 lines

  1. function soundext()
  2. %SOUNDEXT Demonstrate MATLAB V4's sound capability.  (SPARC only.)
  3.  
  4. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  5.  
  6. global SD_FIG
  7. if ~isempty(SD_FIG)
  8.     set(0,'CurrentFigure',SD_FIG);
  9.     return
  10. end
  11. SD_NAMES = [ 'chirp   '
  12.           'gong    '
  13.           'handel  '
  14.           'splat   '
  15.           'train   '
  16.           'laughter'];
  17. nfiles = size(SD_NAMES,1);
  18. global SD_NAMES
  19. global SD_PLOTCMD
  20. SD_PLOTCMD = str2mat('plot(t,y);xlabel(''Time in seconds'');',...
  21.     ['p=spectrum(y,1024);specplot(p,Fs);h=get(gca,''children'');',...
  22.     'delete(h(1:2));xlabel(''Frequency in Hertz'');'],...
  23.     ['specgram(y);axis off;xlabel(''Time in seconds'');',...
  24.     'ylabel(''Frequency'');']);
  25. global SD_DISP
  26. SD_DISP = 1;
  27. S =  brighten([[zeros(8,2) (3:10)'/10]; prism(56)],1/3);
  28. if (get(0,'ScreenDepth') == 1)
  29.    S = gray(64);
  30. end
  31. SD_FIG = figure('units','normal','position',[.13 .33 .477 .44],...
  32.                 'colormap',S);
  33. % button labels corresponding to file names
  34. sd_labels = str2mat('Bird chirps', ...
  35.          'Chinese Gong', ...
  36.          'Hallelujah', ...
  37.          'Dropping Egg', ...
  38.          'Train Whistle', ...
  39.          'Laughter');
  40. % now add labels for FUNCTIONS
  41. sd_labels = str2mat(sd_labels,...
  42.          'Beats',...
  43.          'FM');
  44. % specify functions
  45. funcs = str2mat('Fs=8192;t=0:1/Fs:2;y = sin(300*2*pi*t)+sin(330*2*pi*t);',...
  46.                 'Fs=8192;t=0:1/Fs:2;y = sin(2*pi*440*erf(t));');
  47. func_titles = str2mat('sin(a*t)+sin(b*t)',...
  48.                       'sin(c*erf(t))');
  49. nsounds = size(sd_labels,1);
  50. maxbuts = nsounds+1;
  51.  
  52. % place sound menu buttons on left side
  53. tots = 1.5*(maxbuts+5)+.5;
  54. but_h = .8/(maxbuts+5);
  55. wids = 0.26;
  56. pos = [0.01 .25/tots wids but_h];
  57.  
  58. % set close control
  59. hcl = uicontrol(SD_FIG,'units','normal','pos',pos);
  60.  
  61. hs = [];
  62. % set sound controls
  63. for k = 2:maxbuts
  64.    pos = [0.01 (1.4*(k+4)+.25)/tots wids but_h];
  65.    hs = [uicontrol(SD_FIG,'units','normal','pos',pos) ; hs];
  66. end
  67.  
  68. % set choices for display, if signal processing toolbox is there
  69. hd = [];
  70. if exist('freqz')==2
  71.    nui = 3;
  72. else
  73.    nui = 1;
  74. end
  75. for k = 1:nui
  76.    pos = [0.01 (1.4*(k+1)+.25)/tots wids but_h];
  77.    hd = [uicontrol(SD_FIG,'units','normal',...
  78.          'pos',pos,'style','radiobutton'); hd];
  79. end
  80. % place handles of related radiobuttons in UserData
  81. for k = 1:nui
  82.     hh = hd;
  83.     hh(k) = [];
  84.     set(hd(k),'UserData', hh);
  85. end
  86. set(hd(1),'value',1);
  87.  
  88. % set close callback
  89. s = ['global SD_FIG;close(SD_FIG),saxis auto;', ...
  90.     'clear global SD_FIG SD_VOLUME SD_NAMES SD_DISP SD_PLOTCMD SD_AXIS;',...
  91.     'clear SD_AXIS SD_FIG SD_NAMES SD_DISP SD_PLOTCMD SD_VOLUME'];
  92. set(hcl,'string',' Close','horiz','left','callback',s);
  93.  
  94. % set sound callbacks
  95. % Place latest sound data in figure's user data so radio button callbacks can
  96. % do their thing with the data as well.
  97. for k = 1:nsounds
  98.     s = ['global SD_AXIS SD_FIG SD_DISP SD_PLOTCMD SD_NAMES SD_VOLUME;', ...
  99.         'cf=gcf;set(0,''currentfigure'',SD_FIG);', ...
  100.         'set(SD_FIG,''CurrentAxes'',SD_AXIS);', ...
  101.         'point = get(SD_FIG,''pointer'');', ...
  102.         'set(SD_FIG,''pointer'',''watch'');drawnow;'];
  103.     if k <= nfiles
  104.         s = [s 'file = ''',SD_NAMES(k,:),''';file(file == '' '') = [];',...
  105.         'load(file);',...
  106.         'y=y/max(abs(y));t=(0:length(y)-1)/Fs;'];
  107.     else
  108.         s = [s deblank(funcs(k-nfiles,:))];
  109.     end
  110.     s = [s 'set(SD_FIG,''UserData'',[t(:) y(:)]);',...
  111.         'n = SD_DISP;eval([SD_PLOTCMD(n,:)]);'];
  112.     if k <= nfiles
  113.         s = [s 'title([int2str(length(y)) '' Samples'']),drawnow,'];
  114.     else
  115.         s = [s 'title(''' deblank(func_titles(k-nfiles,:)) ''');drawnow,'];
  116.     end
  117.     s = [s 'slim=[min(y) max(y)];saxis(slim/(get(SD_VOLUME,''value'')));', ...
  118.         'sound(y,Fs);set(SD_FIG,''pointer'',point);', ...
  119.         'if cf~=SD_FIG,set(0,''CurrentFigure'',cf),end,'];
  120.     set(hs(k),'string',[' ',sd_labels(k,:)],'horiz','left','callback',s);
  121. end
  122.  
  123. % set display callbacks
  124. dd_labels = str2mat('Time Sequence','Spectrum','Spectrogram');
  125. for k = 1:length(hd)
  126.     s = ['global SD_FIG,thisone = get(SD_FIG,''CurrentObject'');',...
  127.         'others = get(thisone,''UserData''); set(others,''value'',0);',...
  128.         'set(thisone,''value'',1);',...
  129.         'if strcmp('' Time Sequence'',deblank(get(thisone,''string''))),',...
  130.         'SD_DISP=1;',...
  131.         'elseif strcmp('' Spectrum'',deblank(get(thisone,''string''))),',...
  132.         'SD_DISP=2;',...
  133.         'elseif strcmp('' Spectrogram'',deblank(get(thisone,''string''))),',...
  134.         'SD_DISP=3;end,',...
  135.         'y = get(SD_FIG,''UserData'');',...
  136.         'if ~isempty(y),t = y(:,1); y = y(:,2);',...
  137.         'point = get(SD_FIG,''pointer'');',...
  138.         'set(SD_FIG,''pointer'',''watch'');drawnow;',...
  139.         'titl = get(get(SD_AXIS,''title''),''String'');',...
  140.         'eval([SD_PLOTCMD(SD_DISP,:)]);',...
  141.         'set(SD_FIG,''pointer'',point);',...
  142.         'title(titl),drawnow,end;'];
  143.     set(hd(k),'callback',s,'string',[' ',dd_labels(k,:)],'horiz','left');
  144. end
  145.  
  146. % add volume control
  147. SD_VOLUME = uicontrol(SD_FIG,'units','normal',...
  148.     'position',[1.5*(0.01+wids) .06 .8*(.9-wids) .03]);
  149. set(SD_VOLUME,'style','slider','min',0.01,'max',1,'value',1);
  150. global SD_VOLUME
  151.  
  152. % set up axis to draw in
  153. SD_AXIS = axes('position',[1.4*(0.01+wids) .2 .9*(.9-wids) .7]);
  154. axis off
  155. global SD_AXIS
  156. rect = [.6 .023 .2 .03] ;
  157.  
  158. % label volume control
  159. uicontrol(SD_FIG,'style','text','units','normal','position',rect,...
  160.    'string','Volume','backgroundcolor',get(gcf,'color'),...
  161.    'foregroundcolor', get(SD_AXIS,'xcolor'),...
  162.    'Horizontal','left');
  163.  
  164. % label volumn of buttons with a title
  165. rect = [0.02 (1.4*(maxbuts+5)+.25)/tots wids but_h] ;
  166. uicontrol(SD_FIG,'style','text','units','normal','position',rect,...
  167.    'string','Sounds','backgroundcolor',get(gcf,'color'),...
  168.    'foregroundcolor', get(SD_AXIS,'xcolor'),...
  169.    'Horizontal','left');
  170.