summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/demac/wavwrite.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/demac/wavwrite.c')
-rw-r--r--lib/rbcodec/codecs/demac/wavwrite.c110
1 files changed, 110 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/demac/wavwrite.c b/lib/rbcodec/codecs/demac/wavwrite.c
new file mode 100644
index 0000000000..71d2b7bb97
--- /dev/null
+++ b/lib/rbcodec/codecs/demac/wavwrite.c
@@ -0,0 +1,110 @@
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 "inttypes.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#ifndef __WIN32__
37#define O_BINARY 0
38#endif
39
40static 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
54};
55
56int open_wav(struct ape_ctx_t* ape_ctx, char* filename)
57{
58 int fd;
59 int x;
60 int filesize;
61 int bytespersample;
62
63 fd=open(filename, O_CREAT|O_WRONLY|O_TRUNC|O_BINARY, 0644);
64 if (fd < 0)
65 return fd;
66
67 bytespersample=ape_ctx->bps/8;
68
69 filesize=ape_ctx->totalsamples*bytespersample*ape_ctx->channels+44;
70
71 // ChunkSize
72 x=filesize-8;
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;
77
78 // Number of channels
79 wav_header[22]=ape_ctx->channels;
80
81 // Samplerate
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;
86
87 // ByteRate
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;
93
94 // BlockAlign
95 wav_header[32]=(ape_ctx->bps/8)*ape_ctx->channels;
96
97 // Bits per sample
98 wav_header[34]=ape_ctx->bps;
99
100 // Subchunk2Size
101 x=filesize-44;
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;
106
107 write(fd,wav_header,sizeof(wav_header));
108
109 return fd;
110}