comparison scripts/audio/playaudio.m @ 13831:dc685dd445b4

playaudio.m: Use modern coding standards. Add input validation tests. * playaudio.m: Use modern coding standards. Add input validation tests.
author Rik <octave@nomad.inbox5.com>
date Sat, 05 Nov 2011 07:34:01 -0700
parents f754b65f4bc5
children 72c96de7a403
comparison
equal deleted inserted replaced
13830:462b5f556346 13831:dc685dd445b4
28 ## Created: 11 April 1994 28 ## Created: 11 April 1994
29 ## Adapted-By: jwe 29 ## Adapted-By: jwe
30 30
31 function playaudio (name, ext) 31 function playaudio (name, ext)
32 32
33 if (nargin == 1 && isvector (name) && ! ischar (name)) 33 if (nargin < 1 || nargin > 2)
34 print_usage ();
35 endif
36
37 if (nargin == 1 && isnumeric (name))
34 ## play a vector 38 ## play a vector
35 [nr, nc] = size (name); 39 if (! isvector (name))
36 if (nc != 1) 40 error ("playaudio: X must be a vector");
37 if (nr == 1)
38 name = name';
39 nr = nc;
40 else
41 error ("playaudio: X must be a vector");
42 endif
43 endif 41 endif
44 X = name + 127; 42 X = name(:) + 127;
45 unwind_protect 43 unwind_protect
46 file = tmpnam (); 44 file = tmpnam ();
47 num = fopen (file, "wb"); 45 fid = fopen (file, "wb");
48 c = fwrite (num, X, "uchar"); 46 fwrite (fid, X, "uchar");
49 fclose (num); 47 fclose (fid);
50 [status, out] = system (sprintf ("cat \"%s\" > /dev/dsp", file)); 48 [status, out] = system (sprintf ('cat "%s" > /dev/dsp', file));
51 if (status != 0) 49 if (status != 0)
52 system (sprintf ("paplay --raw \"%s\"", file)) 50 system (sprintf ("paplay --raw \"%s\"", file))
53 endif 51 endif
54 unwind_protect_cleanup 52 unwind_protect_cleanup
55 unlink (file); 53 unlink (file);
56 end_unwind_protect 54 end_unwind_protect
57 elseif (nargin >= 1 && ischar (name)) 55 elseif (nargin >= 1 && ischar (name))
58 ## play a file 56 ## play a file
59 if (nargin == 1) 57 if (nargin == 1)
60 name = [name, ".lin"]; 58 name = [name ".lin"];
61 elseif (nargin == 2) 59 elseif (nargin == 2)
62 name = [name, ".", ext]; 60 name = [name "." ext];
63 else
64 print_usage ();
65 endif 61 endif
66 if (strcmp (ext, "lin") || strcmp (ext, "raw")) 62 if (any (strcmp (ext, {"lin", "raw"})))
67 [status, out] = system (sprintf ("cat \"%s\" > /dev/dsp", name)); 63 [status, out] = system (sprintf ('cat "%s" > /dev/dsp', name));
68 if (status != 0) 64 if (status != 0)
69 system (sprintf ("paplay --raw \"%s\"", name)) 65 system (sprintf ('paplay --raw "%s"', name))
70 endif 66 endif
71 elseif (strcmp (ext, "mu") || strcmp (ext, "au") 67 elseif (any (strcmp (ext, {"mu", "au" "snd", "ul"})))
72 || strcmp (ext, "snd") || strcmp (ext, "ul")) 68 [status, out] = system (sprintf ('cat "%s" > /dev/audio', name));
73 [status, out] = system (sprintf ("cat \"%s\" > /dev/audio", name));
74 if (status != 0) 69 if (status != 0)
75 system (sprintf ("paplay \"%s\"", name)) 70 system (sprintf ('paplay "%s"', name))
76 endif 71 endif
77 else 72 else
78 error ("playaudio: unsupported extension"); 73 error ("playaudio: unsupported extension '%s'", ext);
79 endif 74 endif
80 else 75 else
81 print_usage (); 76 print_usage ();
82 endif 77 endif
83 78
84 endfunction 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