Mercurial > hg > octave-nkf
annotate scripts/audio/playaudio.m @ 19840:c5270263d466 gui-release
close gui-release branch
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 30 Jan 2015 17:41:50 -0500 |
parents | d63878346099 |
children | 6ca096827123 |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
15202
diff
changeset
|
1 ## Copyright (C) 1995-2013 John W. Eaton |
2313 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
2303 | 18 |
3332 | 19 ## -*- texinfo -*- |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
20 ## @deftypefn {Function File} {} playaudio (@var{name}, @var{ext}) |
3332 | 21 ## @deftypefnx {Function File} {} playaudio (@var{x}) |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
22 ## Play the audio file @file{@var{name}.@var{ext}} or the audio data |
3332 | 23 ## stored in the vector @var{x}. |
5642 | 24 ## @seealso{lin2mu, mu2lin, loadaudio, saveaudio, setaudio, record} |
3332 | 25 ## @end deftypefn |
2311 | 26 |
2312 | 27 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at> |
28 ## Created: 11 April 1994 | |
29 ## Adapted-By: jwe | |
30 | |
1636 | 31 function playaudio (name, ext) |
2325 | 32 |
13831
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
33 if (nargin < 1 || nargin > 2) |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
34 print_usage (); |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
35 endif |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
36 |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
37 if (nargin == 1 && isnumeric (name)) |
2303 | 38 ## play a vector |
13831
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
39 if (! isvector (name)) |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
40 error ("playaudio: X must be a vector"); |
1636 | 41 endif |
13831
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
42 X = name(:) + 127; |
2458 | 43 unwind_protect |
44 file = tmpnam (); | |
13831
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
45 fid = fopen (file, "wb"); |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
46 fwrite (fid, X, "uchar"); |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
47 fclose (fid); |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
48 [status, out] = system (sprintf ('cat "%s" > /dev/dsp', file)); |
12901
f754b65f4bc5
Add a PulseAudio backend to playaudio
Fabian Deutsch <fabian.deutsch@gmx.de>
parents:
12575
diff
changeset
|
49 if (status != 0) |
15202
f3b5cadfd6d5
fix missing semicolons in various .m files
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
50 system (sprintf ("paplay --raw \"%s\"", file)); |
12901
f754b65f4bc5
Add a PulseAudio backend to playaudio
Fabian Deutsch <fabian.deutsch@gmx.de>
parents:
12575
diff
changeset
|
51 endif |
2458 | 52 unwind_protect_cleanup |
53 unlink (file); | |
54 end_unwind_protect | |
5443 | 55 elseif (nargin >= 1 && ischar (name)) |
2303 | 56 ## play a file |
1636 | 57 if (nargin == 1) |
13831
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
58 name = [name ".lin"]; |
1636 | 59 elseif (nargin == 2) |
13831
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
60 name = [name "." ext]; |
1636 | 61 endif |
13831
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
62 if (any (strcmp (ext, {"lin", "raw"}))) |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
63 [status, out] = system (sprintf ('cat "%s" > /dev/dsp', name)); |
12901
f754b65f4bc5
Add a PulseAudio backend to playaudio
Fabian Deutsch <fabian.deutsch@gmx.de>
parents:
12575
diff
changeset
|
64 if (status != 0) |
15202
f3b5cadfd6d5
fix missing semicolons in various .m files
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
65 system (sprintf ('paplay --raw "%s"', name)); |
12901
f754b65f4bc5
Add a PulseAudio backend to playaudio
Fabian Deutsch <fabian.deutsch@gmx.de>
parents:
12575
diff
changeset
|
66 endif |
13831
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
67 elseif (any (strcmp (ext, {"mu", "au" "snd", "ul"}))) |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
68 [status, out] = system (sprintf ('cat "%s" > /dev/audio', name)); |
12901
f754b65f4bc5
Add a PulseAudio backend to playaudio
Fabian Deutsch <fabian.deutsch@gmx.de>
parents:
12575
diff
changeset
|
69 if (status != 0) |
15202
f3b5cadfd6d5
fix missing semicolons in various .m files
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
70 system (sprintf ('paplay "%s"', name)); |
12901
f754b65f4bc5
Add a PulseAudio backend to playaudio
Fabian Deutsch <fabian.deutsch@gmx.de>
parents:
12575
diff
changeset
|
71 endif |
1636 | 72 else |
13831
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
73 error ("playaudio: unsupported extension '%s'", ext); |
1636 | 74 endif |
75 else | |
6046 | 76 print_usage (); |
1636 | 77 endif |
78 | |
79 endfunction | |
13831
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
80 |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
81 |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
82 %% Test input validation |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
83 %!error playaudio () |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
84 %!error playaudio (1,2,3) |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
85 %!error <X must be a vector> playaudio (magic (3)) |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
86 %!error <unsupported extension> playaudio ("file", "abc") |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
87 %!error playaudio ({"abc"}) |
dc685dd445b4
playaudio.m: Use modern coding standards. Add input validation tests.
Rik <octave@nomad.inbox5.com>
parents:
12901
diff
changeset
|
88 |