urbiGetSoundUnit

PURPOSE ^

Gets one unit of sound recorded by the server (micro.val by default),

SYNOPSIS ^

function sound = urbiGetSoundUnit(con,deviceName)

DESCRIPTION ^

 Gets one unit of sound recorded by the server (micro.val by default),
 
 This function gets a sound from the URBI server. The length of the sound
 is the duration of a urbi server cycle (e.g. 32 ms for aibo)

 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 = urbiGetSoundUnit(con,deviceName)
0002 % Gets one unit of sound recorded by the server (micro.val by default),
0003 %
0004 % This function gets a sound from the URBI server. The length of the sound
0005 % is the duration of a urbi server cycle (e.g. 32 ms for aibo)
0006 %
0007 % The result is a Sound struct:
0008 %   sound.samples   : vector of samples
0009 %   sound.Fs        : sampling frequency used by the server
0010 %   sound.channel   : number of channels used by the server
0011 %   sound.nbits     : number of bits used by the server
0012 %   sound.length    : number of samples in the sound
0013 %   sound.duration  : duration in ms
0014 
0015 if (nargin == 1)
0016     deviceName = 'micro.val';
0017 end
0018 
0019 
0020 ref_tag = 'gsnd_tag';
0021 
0022 urbiClearConnection(con,0);
0023 urbiSend(con, [ 'gsnd_tag:' deviceName ';' ]);
0024 
0025 % getting header
0026 o_tag = '';
0027 while ~strcmp (o_tag, ref_tag)
0028   r = pnet(con, 'readline', 'noblock');
0029   while isempty(r)
0030     r = pnet(con, 'readline', 'noblock');
0031   end;
0032   ans_string = strread (r, '%s');
0033   [timeStamp, o_tag] = strread(ans_string{1}, '[%u:%s]', 'whitespace', ']');
0034   o_tag = char(o_tag);
0035 end;
0036 
0037 % BIN type : ans_string{2} == 'BIN'
0038 
0039 % Data stream length is in bytes
0040 length = str2num ( ans_string{3} );
0041 
0042 % raw format : ans_string{4} == 'raw'
0043 
0044 % number of channels :
0045 channel = str2num ( ans_string{5} );
0046 
0047 % Sample Rate in HZ
0048 Fs = str2num ( ans_string{6} );
0049 
0050 % bits per sample
0051 nbits = str2num ( ans_string{7} );
0052 
0053 % getting samples
0054 samples = pnet(con, 'read', length);
0055 
0056 % duration
0057 duration = length/(Fs*channel*(nbits/8))*1000;
0058 
0059 % constructing output
0060 sound = struct('samples',samples,'Fs',Fs,'nbits', nbits, 'length', length, 'channel', channel, 'duration', duration);

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