3 Copyright (C) 2005 and later, Cockos Incorporated
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
26 This file provides a simple class for writing basic 16 or 24 bit PCM WAV files.
36 #include "pcmfmtcvt.h"
37 #include "wdlstring.h"
38 #include "win32_utf8.h"
43 // appending doesnt check sample types
52 WaveWriter(const char *filename
, int bps
, int nch
, int srate
, int allow_append
=1)
58 Open(filename
,bps
,nch
,srate
,allow_append
);
62 int Open(const char *filename
, int bps
, int nch
, int srate
, int allow_append
=1)
68 m_fp
=fopen(filename
,"r+b");
71 fseek(m_fp
,0,SEEK_END
);
76 fwrite(buf
,1,44-pos
,m_fp
);
82 m_fp
=fopen(filename
,"wb");
86 fwrite(tbuf
,1,44,m_fp
); // room for header
99 int bytelen
=ftell(m_fp
)-44;
100 fseek(m_fp
,0,SEEK_SET
);
103 fwrite("RIFF",1,4,m_fp
);
104 int riff_size
=bytelen
+44-8;
106 for (x
= 0; x
< 32; x
+= 8)
108 unsigned char c
=(riff_size
>>x
)&255;
111 fwrite("WAVEfmt \x10\0\0\0",1,12,m_fp
);
112 fwrite("\1\0",1,2,m_fp
); // PCM
114 for (x
= 0; x
< 16; x
+= 8) // nch
116 char c
=(m_nch
>>x
)&255;
119 for (x
= 0; x
< 32; x
+= 8) // srate
121 char c
=(m_srate
>>x
)&255;
124 for (x
= 0; x
< 32; x
+= 8) // bytes_per_sec
126 char c
=((m_nch
* (m_bps
/8) * m_srate
)>>x
)&255;
129 int blockalign
=m_nch
* (m_bps
/8);
130 for (x
= 0; x
< 16; x
+= 8) // block alignment
132 char c
=(blockalign
>>x
)&255;
135 for (x
= 0; x
< 16; x
+= 8) // bits/sample
137 char c
=((m_bps
&~7)>>x
)&255;
140 fwrite("data",1,4,m_fp
);
141 for (x
= 0; x
< 32; x
+= 8) // size
143 char c
=((bytelen
)>>x
)&255;
152 const char *GetFileName() { return m_fn
.Get(); }
154 int Status() { return !!m_fp
; }
158 if (m_fp
) return ftell(m_fp
)-44;
162 void WriteRaw(void *buf
, int len
)
164 if (m_fp
) fwrite(buf
,1,len
,m_fp
);
167 void WriteFloats(float *samples
, int nsamples
)
176 float_TO_INT16(a
,*samples
);
177 unsigned char c
=a
&0xff;
184 else if (m_bps
== 24)
189 float_to_i24(samples
,a
);
196 void WriteDoubles(double *samples
, int nsamples
)
205 double_TO_INT16(a
,*samples
);
206 unsigned char c
=a
&0xff;
213 else if (m_bps
== 24)
218 double_to_i24(samples
,a
);
225 void WriteFloatsNI(float **samples
, int offs
, int nsamples
, int nchsrc
=0)
229 if (nchsrc
< 1) nchsrc
=m_nch
;
231 float *tmpptrs
[2]={samples
[0]+offs
,m_nch
>1?(nchsrc
>1?samples
[1]+offs
:samples
[0]+offs
):NULL
};
238 for (ch
= 0; ch
< m_nch
; ch
++)
241 float_TO_INT16(a
,tmpptrs
[ch
][0]);
242 unsigned char c
=a
&0xff;
250 else if (m_bps
== 24)
255 for (ch
= 0; ch
< m_nch
; ch
++)
258 float_to_i24(tmpptrs
[ch
],a
);
266 void WriteDoublesNI(double **samples
, int offs
, int nsamples
, int nchsrc
=0)
270 if (nchsrc
< 1) nchsrc
=m_nch
;
272 double *tmpptrs
[2]={samples
[0]+offs
,m_nch
>1?(nchsrc
>1?samples
[1]+offs
:samples
[0]+offs
):NULL
};
279 for (ch
= 0; ch
< m_nch
; ch
++)
282 double_TO_INT16(a
,tmpptrs
[ch
][0]);
283 unsigned char c
=a
&0xff;
291 else if (m_bps
== 24)
296 for (ch
= 0; ch
< m_nch
; ch
++)
299 double_to_i24(tmpptrs
[ch
],a
);
308 int get_nch() { return m_nch
; }
309 int get_srate() { return m_srate
; }
310 int get_bps() { return m_bps
; }
315 int m_bps
,m_nch
,m_srate
;