comparison scripts/audio/saveaudio.m @ 1636:21fe2afb3692

[project @ 1995-11-16 19:16:11 by jwe] Initial revision
author jwe
date Thu, 16 Nov 1995 19:16:11 +0000
parents
children 5cffc4b8de57
comparison
equal deleted inserted replaced
1635:e91ac27650dc 1636:21fe2afb3692
1 function saveaudio (name, X, ext, bit)
2
3 # usage: saveaudio (name, X, [, ext [, bit]])
4 #
5 # Saves a vector X of audio data in the file "name.ext".
6 # The format of the audio file is determined by ext which has to be
7 # written without an inital "."; default value for ext is "lin".
8 #
9 # Currently, the following audio formats are supported:
10 # *) mu-law files with extension "mu", "au" or "snd"
11 # *) linearly encoded files with extension "lin" or "raw"
12 # If the data is saved linearly, the bit argument decides whether an
13 # 8-bit (default) or a 16-bit format is used.
14
15 # Written by AW (Andreas.Weingessel@ci.tuwien.ac.at) on Sep 5, 1994
16 # Last modified by AW on Oct 29, 1994
17 # Copyright Dept of Probability Theory and Statistics TU Wien
18
19 if (nargin < 2 || nargin > 4)
20 usage ("saveaudio (X, name [, ext [, bit]])");
21 endif
22
23 if (nargin == 2)
24 ext = "lin";
25 endif
26
27 if (nargin < 4)
28 bit = 8;
29 elseif (bit != 8 && bit != 16)
30 error ("saveaudio: bit must be either 8 or 16");
31 endif
32
33 [nr, nc] = size (X);
34 if (nc != 1)
35 if (nr == 1)
36 X = X';
37 nr = nc;
38 else
39 error ("saveaudio: X must be a vector.");
40 endif
41 endif
42
43 num = fopen ([name, ".", ext], "w");
44
45 if (strcmp (ext, "lin") || strcmp (ext, "raw"))
46 if (bit == 8)
47 ld = max (abs (X));
48 if (ld > 127) # convert 16 to 8 bit
49 if (ld < 16384)
50 sc = 64 / ld;
51 else
52 sc = 1 / 256;
53 endif
54 X = fix (X * sc);
55 endif
56 X = X + 127;
57 c = fwrite (num, X, "uchar");
58 else
59 c = fwrite (num, X, "short");
60 endif
61 elseif (strcmp (ext, "mu") || strcmp (ext, "au") || strcmp (ext, "snd"))
62 Y = lin2mu (X);
63 c = fwrite (num, Y, "uchar");
64 else
65 fclose (num);
66 error ("saveaudio does not support given extension");
67 endif
68
69 fclose (num);
70
71 endfunction