Mercurial > hg > octave-lyh
annotate scripts/audio/saveaudio.m @ 10635:d1978e7364ad
Print name of function in error() string messages.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 16 May 2010 22:26:54 -0700 |
parents | 95c3e38098bf |
children | be55736a0783 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2004, 2005, 2006, |
2 ## 2007 John W. Eaton | |
2313 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
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") |
10549 | 79 || strcmp (ext, "snd") || strcmp (ext, "ul")) |
1636 | 80 Y = lin2mu (X); |
81 c = fwrite (num, Y, "uchar"); | |
82 else | |
83 fclose (num); | |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
84 error ("saveaudio: unsupported extension"); |
1636 | 85 endif |
86 | |
87 fclose (num); | |
2325 | 88 |
1636 | 89 endfunction |