5565
|
1 ## Copyright (C) 2005 Michael Zeising |
|
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, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} wavwrite(@var{filename}, @var{y}) |
|
22 ## Write @var{y} to the canonical RIFF/WAVE sound file @var{filename}. A sample |
|
23 ## rate of 8000 Hz and 16-bit samples are assumed. Each column of the data |
|
24 ## represents a separate channel. |
|
25 ## |
|
26 ## @deftypefnx {Function File} {} wavwrite(@var{filename}, @var{y}, @var{fs}) |
|
27 ## Set the sample rate to @var{fs} Hz. |
|
28 ## |
|
29 ## @deftypefnx {Function File} {} wavwrite(@var{filename}, @var{y}, @var{fs}, @var{bits}) |
|
30 ## Set the sample rate to @var{fs} Hz and resolution to @var{bits} bits. |
|
31 ## |
|
32 ## @end deftypefn |
|
33 ## |
|
34 ## @seealso{wavread} |
|
35 |
|
36 ## Author: Michael Zeising <michael.zeising@stud.uni-erlangen.de> |
|
37 ## Created: 06 December 2005 |
|
38 |
|
39 function wavwrite (filename, y, samplesPerSec, bitsPerSample) |
|
40 BYTEORDER = "ieee-le"; |
|
41 |
|
42 # parse arguments |
|
43 if (exist ("samplesPerSec","var") < 1) |
|
44 warning ("wavwrite: sample rate set to 8000 Hz") |
|
45 samplesPerSec = 8000; |
|
46 endif |
|
47 if (exist ("bitsPerSample","var") < 1) |
|
48 warning ("wavwrite: sample resolution set to 16-bit") |
|
49 bitsPerSample = 16; |
|
50 endif |
|
51 |
|
52 # determine sample format |
|
53 switch bitsPerSample |
|
54 case 8 |
|
55 format = "int8"; |
|
56 case 16 |
|
57 format = "int16"; |
|
58 case 32 |
|
59 format = "int32"; |
|
60 otherwise |
|
61 fclose (fid); |
|
62 error ("wavread: sample resolution not supported"); |
|
63 endswitch |
|
64 |
|
65 # calculate filesize |
|
66 channels = size(y)(2); |
|
67 n = size(y)(1); |
|
68 |
|
69 ckSize = n*channels*(bitsPerSample/8); # size of data chunk |
|
70 |
|
71 # open file for writing binary |
|
72 [fid, msg] = fopen (filename, "wb"); |
|
73 if (fid < 0) |
|
74 error ("wavwrite: %s", msg) |
|
75 endif |
|
76 |
|
77 # write RIFF/WAVE header |
|
78 c = 0; |
|
79 c += fwrite (fid, "RIFF", "uchar"); |
|
80 c += fwrite (fid, ckSize + 36, "ulong", 0, BYTEORDER); # file size - 8 |
|
81 c += fwrite (fid, "WAVEfmt ", "uchar"); |
|
82 c += fwrite (fid, 16, "ulong", 0, BYTEORDER); # size of fmt chunk |
|
83 c += fwrite (fid, 0x0001, "short", 0, BYTEORDER); # sample format code (PCM) |
|
84 c += fwrite (fid, channels, "short", 0, BYTEORDER); # channels |
|
85 c += fwrite (fid, samplesPerSec, "ulong", 0, BYTEORDER); # sample rate |
|
86 c += fwrite (fid, samplesPerSec*channels*bitsPerSample/8, "ulong", 0, BYTEORDER); # bytes per second |
|
87 c += fwrite (fid, channels*bitsPerSample/8, "short", 0, BYTEORDER); # block align |
|
88 c += fwrite (fid, bitsPerSample, "short", 0, BYTEORDER); # bits/sample |
|
89 c += fwrite (fid, "data", "uchar"); |
|
90 c += fwrite (fid, ckSize, "ulong", 0, BYTEORDER); # size of data chunk |
|
91 |
|
92 if (c < 25) |
|
93 fclose (fid); |
|
94 error ("wavread: writing to file failed") |
|
95 endif |
|
96 |
|
97 # scale samples |
|
98 switch bitsPerSample |
|
99 case 8 |
|
100 y = floor (y*127 + 127); |
|
101 case {16,32} |
|
102 y = floor (y*((2 ** bitsPerSample) / 2 - 1)); |
|
103 endswitch |
|
104 |
|
105 # interleave samples |
|
106 l = n*channels; |
|
107 for (i = 1:channels) |
|
108 yi(i:channels:l) = y(:,i); |
|
109 endfor |
|
110 |
|
111 # write to file |
|
112 c = fwrite (fid, yi, format, 0, BYTEORDER); |
|
113 |
|
114 fclose (fid); |
|
115 |
|
116 endfunction |