]> Creatis software - CreaPhase.git/blob - octave_packages/m/audio/playaudio.m
update packages
[CreaPhase.git] / octave_packages / m / audio / playaudio.m
1 ## Copyright (C) 1995-2012 John W. Eaton
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {} playaudio (@var{name}, @var{ext})
21 ## @deftypefnx {Function File} {} playaudio (@var{x})
22 ## Play the audio file @file{@var{name}.@var{ext}} or the audio data
23 ## stored in the vector @var{x}.
24 ## @seealso{lin2mu, mu2lin, loadaudio, saveaudio, setaudio, record}
25 ## @end deftypefn
26
27 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
28 ## Created: 11 April 1994
29 ## Adapted-By: jwe
30
31 function playaudio (name, ext)
32
33   if (nargin < 1 || nargin > 2)
34     print_usage ();
35   endif
36
37   if (nargin == 1 && isnumeric (name))
38     ## play a vector
39     if (! isvector (name))
40       error ("playaudio: X must be a vector");
41     endif
42     X = name(:) + 127;
43     unwind_protect
44       file = tmpnam ();
45       fid = fopen (file, "wb");
46       fwrite (fid, X, "uchar");
47       fclose (fid);
48       [status, out] = system (sprintf ('cat "%s" > /dev/dsp', file));
49       if (status != 0)
50         system (sprintf ("paplay --raw \"%s\"", file))
51       endif
52     unwind_protect_cleanup
53       unlink (file);
54     end_unwind_protect
55   elseif (nargin >= 1 && ischar (name))
56     ## play a file
57     if (nargin == 1)
58       name = [name ".lin"];
59     elseif (nargin == 2)
60       name = [name "." ext];
61     endif
62     if (any (strcmp (ext, {"lin", "raw"})))
63       [status, out] = system (sprintf ('cat "%s" > /dev/dsp', name));
64       if (status != 0)
65         system (sprintf ('paplay --raw "%s"', name))
66       endif
67     elseif (any (strcmp (ext, {"mu", "au" "snd", "ul"})))
68       [status, out] = system (sprintf ('cat "%s" > /dev/audio', name));
69       if (status != 0)
70         system (sprintf ('paplay "%s"', name))
71       endif
72     else
73       error ("playaudio: unsupported extension '%s'", ext);
74     endif
75   else
76     print_usage ();
77   endif
78
79 endfunction
80
81
82 %% Test input validation
83 %!error playaudio ()
84 %!error playaudio (1,2,3)
85 %!error <X must be a vector> playaudio (magic (3))
86 %!error <unsupported extension> playaudio ("file", "abc")
87 %!error playaudio ({"abc"})
88