2847
|
1 ## Copyright (C) 1996, 1997 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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## 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, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
2303
|
19 |
3426
|
20 ## -*- texinfo -*- |
3332
|
21 ## @deftypefn {Function File} {} saveaudio (@var{name}, @var{x}, @var{ext}, @var{bps}) |
|
22 ## Saves a vector @var{x} of audio data to the file |
|
23 ## @file{@var{name}.@var{ext}}. The optional parameters @var{ext} and |
|
24 ## @var{bps} determine the encoding and the number of bits per sample used |
|
25 ## in the audio file (see @code{loadaudio}); defaults are @file{lin} and |
|
26 ## 8, respectively. |
5642
|
27 ## @seealso{lin2mu, mu2lin, loadaudio, playaudio, setaudio, record} |
3332
|
28 ## @end deftypefn |
2311
|
29 |
2312
|
30 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at> |
|
31 ## Created: 5 September 1994 |
|
32 ## Adapted-By: jwe |
|
33 |
1636
|
34 function saveaudio (name, X, ext, bit) |
2325
|
35 |
1636
|
36 if (nargin < 2 || nargin > 4) |
6046
|
37 print_usage (); |
1636
|
38 endif |
|
39 |
|
40 if (nargin == 2) |
|
41 ext = "lin"; |
|
42 endif |
|
43 |
|
44 if (nargin < 4) |
|
45 bit = 8; |
|
46 elseif (bit != 8 && bit != 16) |
|
47 error ("saveaudio: bit must be either 8 or 16"); |
|
48 endif |
|
49 |
|
50 [nr, nc] = size (X); |
|
51 if (nc != 1) |
|
52 if (nr == 1) |
|
53 X = X'; |
|
54 nr = nc; |
|
55 else |
3458
|
56 error ("saveaudio: X must be a vector"); |
1636
|
57 endif |
|
58 endif |
|
59 |
3167
|
60 num = fopen ([name, ".", ext], "wb"); |
1636
|
61 |
2325
|
62 if (strcmp (ext, "lin") || strcmp (ext, "raw")) |
1636
|
63 if (bit == 8) |
|
64 ld = max (abs (X)); |
|
65 if (ld > 127) # convert 16 to 8 bit |
3426
|
66 if (ld < 16384) |
|
67 sc = 64 / ld; |
|
68 else |
|
69 sc = 1 / 256; |
|
70 endif |
|
71 X = fix (X * sc); |
1636
|
72 endif |
|
73 X = X + 127; |
|
74 c = fwrite (num, X, "uchar"); |
|
75 else |
|
76 c = fwrite (num, X, "short"); |
|
77 endif |
3471
|
78 elseif (strcmp (ext, "mu") || strcmp (ext, "au") |
|
79 || strcmp (ext, "snd") || strcmp (ext, "ul")) |
1636
|
80 Y = lin2mu (X); |
|
81 c = fwrite (num, Y, "uchar"); |
|
82 else |
|
83 fclose (num); |
|
84 error ("saveaudio does not support given extension"); |
|
85 endif |
|
86 |
|
87 fclose (num); |
2325
|
88 |
1636
|
89 endfunction |