summaryrefslogtreecommitdiff
path: root/apps/codecs/demac/wavwrite.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/demac/wavwrite.c')
-rw-r--r--apps/codecs/demac/wavwrite.c107
1 files changed, 107 insertions, 0 deletions
diff --git a/apps/codecs/demac/wavwrite.c b/apps/codecs/demac/wavwrite.c
new file mode 100644
index 0000000000..5fb82e148a
--- /dev/null
+++ b/apps/codecs/demac/wavwrite.c
@@ -0,0 +1,107 @@
1/*
2
3demac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#include <stdio.h>
26#include <inttypes.h>
27#include <stdlib.h>
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <string.h>
33
34#include "parser.h"
35
36
37static unsigned char wav_header[44]={
38 'R','I','F','F',// 0 - ChunkID
39 0,0,0,0, // 4 - ChunkSize (filesize-8)
40 'W','A','V','E',// 8 - Format
41 'f','m','t',' ',// 12 - SubChunkID
42 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM
43 1,0, // 20 - AudioFormat (1=Uncompressed)
44 2,0, // 22 - NumChannels
45 0,0,0,0, // 24 - SampleRate in Hz
46 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
47 4,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
48 16,0, // 34 - BitsPerSample
49 'd','a','t','a',// 36 - Subchunk2ID
50 0,0,0,0 // 40 - Subchunk2Size
51};
52
53int open_wav(struct ape_ctx_t* ape_ctx, char* filename)
54{
55 int fd;
56 int x;
57 int filesize;
58 int bytespersample;
59
60 fd=creat(filename,0644);
61 if (fd < 0)
62 return fd;
63
64 bytespersample=ape_ctx->bps/8;
65
66 filesize=ape_ctx->totalsamples*bytespersample*ape_ctx->channels+44;
67
68 // ChunkSize
69 x=filesize-8;
70 wav_header[4]=(x&0xff);
71 wav_header[5]=(x&0xff00)>>8;
72 wav_header[6]=(x&0xff0000)>>16;
73 wav_header[7]=(x&0xff000000)>>24;
74
75 // Number of channels
76 wav_header[22]=ape_ctx->channels;
77
78 // Samplerate
79 wav_header[24]=ape_ctx->samplerate&0xff;
80 wav_header[25]=(ape_ctx->samplerate&0xff00)>>8;
81 wav_header[26]=(ape_ctx->samplerate&0xff0000)>>16;
82 wav_header[27]=(ape_ctx->samplerate&0xff000000)>>24;
83
84 // ByteRate
85 x=ape_ctx->samplerate*(ape_ctx->bps/8)*ape_ctx->channels;
86 wav_header[28]=(x&0xff);
87 wav_header[29]=(x&0xff00)>>8;
88 wav_header[30]=(x&0xff0000)>>16;
89 wav_header[31]=(x&0xff000000)>>24;
90
91 // BlockAlign
92 wav_header[32]=(ape_ctx->bps/8)*ape_ctx->channels;
93
94 // Bits per sample
95 wav_header[34]=ape_ctx->bps;
96
97 // Subchunk2Size
98 x=filesize-44;
99 wav_header[40]=(x&0xff);
100 wav_header[41]=(x&0xff00)>>8;
101 wav_header[42]=(x&0xff0000)>>16;
102 wav_header[43]=(x&0xff000000)>>24;
103
104 write(fd,wav_header,sizeof(wav_header));
105
106 return fd;
107}