Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / codecs / demac / wavwrite.c
blob71d2b7bb975b94a6715d48bf9dd00aef60fa0b22
1 /*
3 demac - A Monkey's Audio decoder
5 $Id$
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
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>
34 #include "parser.h"
36 #ifndef __WIN32__
37 #define O_BINARY 0
38 #endif
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)
58 int fd;
59 int x;
60 int filesize;
61 int bytespersample;
63 fd=open(filename, O_CREAT|O_WRONLY|O_TRUNC|O_BINARY, 0644);
64 if (fd < 0)
65 return fd;
67 bytespersample=ape_ctx->bps/8;
69 filesize=ape_ctx->totalsamples*bytespersample*ape_ctx->channels+44;
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;
78 // Number of channels
79 wav_header[22]=ape_ctx->channels;
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;
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;
94 // BlockAlign
95 wav_header[32]=(ape_ctx->bps/8)*ape_ctx->channels;
97 // Bits per sample
98 wav_header[34]=ape_ctx->bps;
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;
107 write(fd,wav_header,sizeof(wav_header));
109 return fd;