No BSD License  

Highlights from
osprey

osprey

by

 

Osprey is a program for viewing and printing spectrograms of sound files.

opPlay(cmd, ch)
function [factor,var] = opPlay(cmd, ch)
% opPlay('button')
%    Play the selection (if there is one) or the screen (if not).
%    This is called when the user clicks on the Play button.
%
% opPlay('all')
%    Play the whole sound.
%
% opPlay('disptext', ch)
%    Display the popup text according to whether there's a selection or not.
%
% opPlay('setratetext', ch)
%    Set the popup (opPlayRateMenu) to the set of possible play rate factors.
%
% opPlay('setrate')
%    Set the rate to the given value.  This is the menu callback.
%
% factor = opPlay('getinterp', ch)
%    Return the current playback rate multiply factor.  If it's negative,
%    it means the machine has variable playback rate hardware or software 
%    at a lower level, and that -factor is multiplicative factor to pass down.
%
% opPlay('othercallback')
%    Internal function for handling the uiInput() callback.

global opc opNsamp opSrate opT0 opT1 opSelT0 opSelT1 opSelF0 opSelF1
global opPlayBut opFig opTMax opPlayRateMenu opPlayRate opPlayOthers 
global uiInputButton uiInput1 opChannelNum

if (~exist('opPlayOthers')), opPlayOthers = []; end	% used only in MATLAB 4
if (nargin < 2), ch = opc; end
issel = opSelect(ch);

if (strcmp(cmd, 'disptext'))
  if (issel), set(opPlayBut(ch), 'String', 'Play Selection');
  else        set(opPlayBut(ch), 'String', 'Play Screen');
  end
  return

elseif (  strcmp(cmd,'getinterp')...
        | strcmp(cmd,'setratetext') ...
	| strcmp(cmd,'setrate') ...
	| strcmp(cmd,'othercallback')...
       )
  [def,var] = defaultsrate;
  mult = def(1) / opSrate(ch);			% multiplier factor for 1x
  if (var)  z = [20 10 5 2 1 1/2 1/5 1/10 1/20];
  else      z = mult * 0.5 .^ (0 : max(5, round(log2(mult))));
  end
  z = fliplr(uniq(sort([z, reduce(opPlayOthers, opPlayOthers < mult)])));

  if (strcmp(cmd, 'getinterp'))			% return an interpolation factor
    factor = max(1, round(mult / z(opPlayRate(ch))));
    if (var) factor = -factor; end

  elseif (strcmp(cmd, 'setratetext'))		% set popup string
    s = 'Playback rate';
    x = z > 2/3;		% 2/3 is so round(1/z) is an integer > 1
    if (any(x)),  s = [s, sprintf('|    %.3gx  ', z(x))];              end
    if (any(~x)), s = [s, sprintf('|  1/%.3gx  ', round(1 ./ z(~x)))]; end
    s = [s,'|  Other...'];
    set(opPlayRateMenu, 'String', s, 'Value', opPlayRate(ch) + 1);

  elseif (strcmp(cmd, 'setrate'))		% user chose something on popup
    x = get(opPlayRateMenu, 'value');
    if (x == 1 | x == length(z)+2)		% 'Play rate' or 'Other...'
      set(opPlayRateMenu, 'value', opPlayRate(ch)+1);
      if (x ~= 1)
	uiInput('Playback rate', 'OK|Cancel', 'opPlay(''othercallback'');', ...
	    [], 'Playback rate multiplier (expressions like 1/27 work)');
      end
    else opPlayRate(ch) = x - 1;		% the user picked a value
    end

  elseif (strcmp(cmd, 'othercallback'))		% callback from 'Other...'
    if (uiInputButton ~= 1), return; end	% Cancel
    if (~length(uiInput1)), return; end		% empty user input
    v = eval(uiInput1);
    if (~length(v)), disp('Empty playback rate value.  Nope.'); return; end
    v = v(1);
    if (v > mult)
      uiInput(['Rate cannot be greater than ' num2str(mult) '.'], 'OK', '');
    else
      opPlayOthers = [opPlayOthers, v];
      opPlayRate(ch) = length(find(v < z)) + 1;
      opPlay('setratetext', ch);
    end

  else
    error(['Internal error: Bad command ''', cmd, '''.']); 
  end
  return

elseif (strcmp(cmd, 'all'))
  t0 = 0; t1 = opTMax(opc);		% whole sound

elseif (strcmp(cmd, 'button'))
  if (issel), t0 = opSelT0(opc); t1 = opSelT1(opc);	% selection
  else        t0 = opT0(opc);    t1 = opT1(opc);	% screen
  end
else
  error(['Unknown command in opPlay: ', cmd]);
end

% Control falls through to here if there is no explicit return.

% Play the sound between t0 and t1.
s0 = max(round(t0 * opSrate(opc)), 0);
s1 = min(round(t1 * opSrate(opc)), opNsamp(opc));
if (s1 > s0)
  opPointer('watch');
  interp = opPlay('getinterp', opc);
  if (interp >= 0)
    soundPlay(opSoundIn(opc, s0, s1-s0, opChannelNum), interp);
  else
    soundPlay(opSoundIn(opc, s0, s1-s0, opChannelNum), 1, -defaultsrate/interp);
  end
  opPointer('crosshair');
end

Contact us