From sources-request at octave dot org Tue May 17 08:31:47 2005 Subject: Functions for loading and playing sound From: Svein Berge To: sources at octave dot org Date: Tue, 17 May 2005 06:18:01 -0500 --Boundary-00=_DJdiCrAo0ATRdL9 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline I am doing an audio project, and found that the existing sound functions are limited to raw sound files and raw sound devices. I've written two functions that use the sox sound package (opensource, standard in most distos) to load and play sound files of any standard format at any sample rate. Please consider including them in the distribution, as I think they could be useful to many. Svein Berge. --Boundary-00=_DJdiCrAo0ATRdL9 Content-Type: text/x-objcsrc; charset="us-ascii"; name="loadsnd.m" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="loadsnd.m" ## Copyright (C) 2005 Svein Berge ## ## This file is part of Octave. ## ## Octave is free software; you can redistribute it and/or modify it ## under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2, or (at your option) ## any later version. ## ## Octave is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, write to the Free ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA ## 02111-1307, USA. ## usage: X=loadsnd(filename) ## ## `"X"=loadsnd ("filename")' loads the contents of "filename" into ## the vector X. The file should be a mono sound file in any format that ## is recognized by the sox sound package. ## ## Author: Svein Berge ## Created: 17 May 2005 function X=loadsnd(filename) file = tmpnam (); unwind_protect system (sprintf ("sox %s -t raw -s -w %s.raw", filename, file)); X=loadaudio(file,'raw',16); unwind_protect_cleanup unlink(sprintf("%s.raw",file)); end_unwind_protect end --Boundary-00=_DJdiCrAo0ATRdL9 Content-Type: text/x-objcsrc; charset="us-ascii"; name="play.m" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="play.m" ## Copyright (C) 2005 Svein Berge ## ## This file is part of Octave. ## ## Octave is free software; you can redistribute it and/or modify it ## under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2, or (at your option) ## any later version. ## ## Octave is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, write to the Free ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA ## 02111-1307, USA. ## usage: play (X [, fs]) ## ## `play ("X" [, "fs"])' plays the audio data in vector "X". The ## "fs" argument specifies the sampling frequency in Hz, and defaults ## to 44100. The vector is scaled to maximize playback volume. This ## function uses the sox sound package. ## ## Author: Svein Berge ## Created: 17 May 2005 ## Based on: playaudio.m (see credits in playaudio.m) function play (name, fs) usage_msg = "play (X [, fs])"; if (nargin == 1) fs=44100; end ## play a vector [nr, nc] = size (name); if (nc != 1) if (nr == 1) name = name'; nr = nc; else error ("play: X must be a vector"); endif endif X=name; X = X-sum(X)/length(X); X = X*10000/max(abs(X)); unwind_protect file = tmpnam (); num = fopen (file, "wb"); c = fwrite (num, X, "short"); fclose (num); system (sprintf ("play -f s -s w -t raw -r %d %s >/dev/null", fs, file)); unwind_protect_cleanup unlink (file); end_unwind_protect endfunction --Boundary-00=_DJdiCrAo0ATRdL9--