3 demac - A Monkey's Audio decoder
7 Copyright (C) Dave Chapman 2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
28 #include <sys/types.h>
40 static unsigned char wav_header
[44]={
41 'R','I','F','F',// 0 - ChunkID
42 0,0,0,0, // 4 - ChunkSize (filesize-8)
43 'W','A','V','E',// 8 - Format
44 'f','m','t',' ',// 12 - SubChunkID
45 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM
46 1,0, // 20 - AudioFormat (1=Uncompressed)
47 2,0, // 22 - NumChannels
48 0,0,0,0, // 24 - SampleRate in Hz
49 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
50 4,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
51 16,0, // 34 - BitsPerSample
52 'd','a','t','a',// 36 - Subchunk2ID
53 0,0,0,0 // 40 - Subchunk2Size
56 int open_wav(struct ape_ctx_t
* ape_ctx
, char* filename
)
63 fd
=open(filename
, O_CREAT
|O_WRONLY
|O_TRUNC
|O_BINARY
, 0644);
67 bytespersample
=ape_ctx
->bps
/8;
69 filesize
=ape_ctx
->totalsamples
*bytespersample
*ape_ctx
->channels
+44;
73 wav_header
[4]=(x
&0xff);
74 wav_header
[5]=(x
&0xff00)>>8;
75 wav_header
[6]=(x
&0xff0000)>>16;
76 wav_header
[7]=(x
&0xff000000)>>24;
79 wav_header
[22]=ape_ctx
->channels
;
82 wav_header
[24]=ape_ctx
->samplerate
&0xff;
83 wav_header
[25]=(ape_ctx
->samplerate
&0xff00)>>8;
84 wav_header
[26]=(ape_ctx
->samplerate
&0xff0000)>>16;
85 wav_header
[27]=(ape_ctx
->samplerate
&0xff000000)>>24;
88 x
=ape_ctx
->samplerate
*(ape_ctx
->bps
/8)*ape_ctx
->channels
;
89 wav_header
[28]=(x
&0xff);
90 wav_header
[29]=(x
&0xff00)>>8;
91 wav_header
[30]=(x
&0xff0000)>>16;
92 wav_header
[31]=(x
&0xff000000)>>24;
95 wav_header
[32]=(ape_ctx
->bps
/8)*ape_ctx
->channels
;
98 wav_header
[34]=ape_ctx
->bps
;
102 wav_header
[40]=(x
&0xff);
103 wav_header
[41]=(x
&0xff00)>>8;
104 wav_header
[42]=(x
&0xff0000)>>16;
105 wav_header
[43]=(x
&0xff000000)>>24;
107 write(fd
,wav_header
,sizeof(wav_header
));