urbiGetSound

PURPOSE ^

Gets 'duration' ms of sound recorded by the server (micro.val by default),

SYNOPSIS ^

function sound = urbiGetSound(con,duration, deviceName)

DESCRIPTION ^

 Gets 'duration' ms of sound recorded by the server (micro.val by default),

 The result is a Sound struct:
   sound.samples   : vector of samples
   sound.Fs        : sampling frequency used by the server
   sound.channel   : number of channels used by the server
   sound.nbits     : number of bits used by the server
   sound.length    : number of samples in the sound
   sound.duration  : duration in ms

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function sound = urbiGetSound(con,duration, deviceName)
0002 % Gets 'duration' ms of sound recorded by the server (micro.val by default),
0003 %
0004 % The result is a Sound struct:
0005 %   sound.samples   : vector of samples
0006 %   sound.Fs        : sampling frequency used by the server
0007 %   sound.channel   : number of channels used by the server
0008 %   sound.nbits     : number of bits used by the server
0009 %   sound.length    : number of samples in the sound
0010 %   sound.duration  : duration in ms
0011 
0012 if (nargin==2)
0013     deviceName='micro.val';
0014 end
0015 
0016 %urbiClearConnection(con,1);
0017 
0018 % Get the sound informations
0019 soundUnit = urbiGetSoundUnit(con,deviceName);
0020 nb_to_get = floor(duration / (soundUnit.duration));
0021 
0022 
0023 % command to get the right number of sound samples
0024 ref_tag = 'gsnd_tag';
0025 urbiClearConnection(con,0);
0026 urbiSend(con, [ 'for(i=0;i<' num2str(nb_to_get) ';i++) gsnd_tag:' deviceName ';' ]);
0027 
0028 % Reception of the samples
0029 length = 0;
0030 samples = [];
0031 
0032 for i=1:(nb_to_get)
0033 % getting header
0034 o_tag = '';
0035 while ~strcmp (o_tag, ref_tag)
0036   r = pnet(con, 'readline', 'noblock');
0037   while isempty(r)
0038     r = pnet(con, 'readline', 'noblock');
0039   end;
0040   ans_string = strread (r, '%s');
0041   [timeStamp, o_tag] = strread(ans_string{1}, '[%u:%s]', 'whitespace', ']');
0042   o_tag = char(o_tag);
0043 end;
0044 
0045 % Data stream length is in bytes
0046 length =  length + str2num ( ans_string{3} );
0047 
0048 % getting samples
0049 samples = [samples pnet(con, 'read', soundUnit.length) ];
0050 
0051 end;
0052 
0053 % duration
0054 duration = length/(soundUnit.Fs*soundUnit.channel*(soundUnit.nbits/8))*1000;
0055 
0056 
0057 % constructing output
0058 sound = struct('samples',samples,'Fs',soundUnit.Fs,'nbits', soundUnit.nbits, 'length', length, 'channel', soundUnit.channel, 'duration', duration);
0059 
0060

Generated on Tue 20-Dec-2005 19:05:34 by m2html © 2003