Monday, November 07, 2011

Play Sound/Audio File such as .wav in GNU / Octave

In octave, there is not function like wavplay or sound such in Matlab. Octave has own function to play audiofile i.e playaudio, but it is never work for me. Instead of repair the playaudio function in Octave, below is simple script function to play sound file in Octave.

Please copy the code below and paste to your PC (work on Ubuntu Linux) as playsound.m

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% This function play the singal x as a sound.
% playsound(x, fs, bits)
%  x : a signal vector
%  fs: sampling frequency in Hz
%  bits: quantization bits
%
% If encounter a error, try to transpose a signal vector,
% like playsound(x',fs,bits)

function playsound(x,fs,bits)
if (nargin != 3)
  usage("playsound(x,fs,bits)");
endif

 file= strcat(tmpnam(),".wav");
 wavwrite(file,x,fs,bits); 
 system(sprintf("/usr/bin/paplay %s ; rm -f %s",file,file));
endfunction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


And then, in your playsound.m directory, run octave in terminal

[sound, fs,bit]=wavread('sound.wav');
playsound(sound, fs, bit)

To make it available in every where of your octave, just add this function to your function library. If there is not available you can make directory by the following steps

sudo mkdir ~/func

and in your playsound.m file, type the code below to copy that fucntion to destination folder.

sudo cp playsound.m ~/func

Now, the final step is to add the 'func' to your Octave path by this ways,

sudo nano ~/.octave rc

And then in octave rc type the following command;

addpath(genpath ("~/func"));
You should play audio file in Octave every where!



Related Posts Plugin for WordPress, Blogger...