2303
|
1 ### Copyright (C) 1996 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 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 |
|
17 ### Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ### 02111-1307, USA. |
|
19 |
2311
|
20 ## usage: saveaudio (name, X, [, ext [, bit]]) |
|
21 ## |
|
22 ## Saves a vector X of audio data in the file "name.ext". |
|
23 ## The format of the audio file is determined by ext which has to be |
|
24 ## written without an inital "."; default value for ext is "lin". |
|
25 ## |
|
26 ## Currently, the following audio formats are supported: |
|
27 ## *) mu-law files with extension "mu", "au" or "snd" |
|
28 ## *) linearly encoded files with extension "lin" or "raw" |
|
29 ## If the data is saved linearly, the bit argument decides whether an |
|
30 ## 8-bit (default) or a 16-bit format is used. |
|
31 |
2312
|
32 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at> |
|
33 ## Created: 5 September 1994 |
|
34 ## Adapted-By: jwe |
|
35 |
1636
|
36 function saveaudio (name, X, ext, bit) |
|
37 |
|
38 if (nargin < 2 || nargin > 4) |
|
39 usage ("saveaudio (X, name [, ext [, bit]])"); |
|
40 endif |
|
41 |
|
42 if (nargin == 2) |
|
43 ext = "lin"; |
|
44 endif |
|
45 |
|
46 if (nargin < 4) |
|
47 bit = 8; |
|
48 elseif (bit != 8 && bit != 16) |
|
49 error ("saveaudio: bit must be either 8 or 16"); |
|
50 endif |
|
51 |
|
52 [nr, nc] = size (X); |
|
53 if (nc != 1) |
|
54 if (nr == 1) |
|
55 X = X'; |
|
56 nr = nc; |
|
57 else |
|
58 error ("saveaudio: X must be a vector."); |
|
59 endif |
|
60 endif |
|
61 |
|
62 num = fopen ([name, ".", ext], "w"); |
|
63 |
|
64 if (strcmp (ext, "lin") || strcmp (ext, "raw")) |
|
65 if (bit == 8) |
|
66 ld = max (abs (X)); |
|
67 if (ld > 127) # convert 16 to 8 bit |
|
68 if (ld < 16384) |
|
69 sc = 64 / ld; |
|
70 else |
|
71 sc = 1 / 256; |
|
72 endif |
|
73 X = fix (X * sc); |
|
74 endif |
|
75 X = X + 127; |
|
76 c = fwrite (num, X, "uchar"); |
|
77 else |
|
78 c = fwrite (num, X, "short"); |
|
79 endif |
|
80 elseif (strcmp (ext, "mu") || strcmp (ext, "au") || strcmp (ext, "snd")) |
|
81 Y = lin2mu (X); |
|
82 c = fwrite (num, Y, "uchar"); |
|
83 else |
|
84 fclose (num); |
|
85 error ("saveaudio does not support given extension"); |
|
86 endif |
|
87 |
|
88 fclose (num); |
|
89 |
|
90 endfunction |