1
+ function savewav(y ,filename ,fs )
2
+ % SAVEWAV saves floating point audio data in a *.wav with 32bit precision
3
+ %
4
+ % Usage: savewav(y,filename,fs)
5
+ %
6
+ % Input parameters:
7
+ % y - audio data in floating point [NFrames x NChannels]
8
+ % filename - name of *.wav file (file extension will NOT be added
9
+ % automatically)
10
+ % fs - sample rate / Hz
11
+ %
12
+ % Since wavwrite has been removed in MATLAB 2015b and audiowrite does not
13
+ % support audio data with a channel number higher than 256, this is an
14
+ % alternative to save massive multichannel. This function does only support
15
+ % floating point data and saves it 32bit floating point precision.
16
+ %
17
+ % See also: audiowrite, audioread, fwrite
18
+
19
+ % Copyright (c) 2015 Fiete Winter
20
+ %
21
+ % Permission is hereby granted, free of charge, to any person obtaining a copy
22
+ % of this software and associated documentation files (the "Software"), to deal
23
+ % in the Software without restriction, including without limitation the rights
24
+ % to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25
+ % copies of the Software, and to permit persons to whom the Software is
26
+ % furnished to do so, subject to the following conditions:
27
+ %
28
+ % The above copyright notice and this permission notice shall be included in
29
+ % all copies or substantial portions of the Software.
30
+ %
31
+ % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32
+ % IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33
+ % FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34
+ % AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35
+ % LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36
+ % OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37
+ % THE SOFTWARE.
38
+
39
+ %% Input Check
40
+ narginchk(3 ,3 );
41
+ if ~isfloat(y )
42
+ error(' %s : only floating point supported' , upper(mfilename ));
43
+ end
44
+ if ~ndims(y ) > 2
45
+ error(' %s : only audio data with [NFrames x NChannels] supported' , ...
46
+ upper(mfilename ));
47
+ end
48
+
49
+ %%
50
+ [NFrames , NChannels ] = size(y ); % Number of Frames and Channels
51
+ NSamples = NFrames * NChannels ; % total Samples
52
+ Nbits = 32 ; % 32 floating point
53
+ Nbyte = 4 ; % bytes per Samples
54
+
55
+ fileID = fopen(filename ,' w' , ' ieee-le' ); % open wav-file (little Endian)
56
+
57
+ fwrite(fileID , ' RIFF' , ' char*1' ); % RIFF header
58
+ % number of bytes following (chunk size)
59
+ Noverallbytes = 4 + 4 ; % for 'WAVE' and 'fmt '
60
+ Noverallbytes = Noverallbytes + 4 ; % for the number of bytes in 'fmt '
61
+ Noverallbytes = Noverallbytes + 16 ; % bytes in 'fmt '
62
+ Noverallbytes = Noverallbytes + 12 ; % for 'fact'
63
+ Noverallbytes = Noverallbytes + 8 + Nbyte * NSamples ; % 'data' chunk
64
+ fwrite(fileID , Noverallbytes , ' uint32' );
65
+
66
+ fwrite(fileID , ' WAVE' , ' char*1' ); % WAVE header
67
+
68
+ %% fmt chunk
69
+ fwrite(fileID , ' fmt ' , ' char*1' ); % mind the whitespace!
70
+ fwrite(fileID , 16 , ' uint32' ); % number of bytes following (chunk size)
71
+ fwrite(fileID , 3 , ' uint16' ); % data type (3 for float)
72
+ fwrite(fileID , NChannels , ' uint16' ); % number of channels
73
+ fwrite(fileID , fs , ' uint32' ); % samplerate
74
+ fwrite(fileID , fs * NChannels * Nbyte , ' uint32' ); % bytes per second
75
+ fwrite(fileID , NChannels * Nbyte , ' uint16' ); % frame size
76
+ fwrite(fileID , Nbits , ' uint16' ); % bits per sample
77
+
78
+ %% fact chunk (floating point only)
79
+ fwrite(fileID , ' fact' , ' char*1' );
80
+ fwrite(fileID , 4 , ' uint32' ); % number of bytes following (chunk size)
81
+ fwrite(fileID , NFrames , ' uint32' ); % number of frames
82
+
83
+ %% data chunk
84
+ fwrite(fileID , ' data' , ' char*1' );
85
+ fwrite(fileID , Nbyte * NSamples , ' uint32' ); % number of bytes following (chunk size)
86
+ y = y .' ; % prepare data for interlacing
87
+ fwrite(fileID , y(: ), ' float32' ); % interlaced data
88
+
89
+ %%
90
+ fclose(fileID );
91
+
92
+ end
0 commit comments