1 /*
2 Copyright (c) 2009-2010 Tero Lindeman (kometbomb)
3
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation
6 files (the "Software"), to deal in the Software without
7 restriction, including without limitation the rights to use,
8 copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following
11 conditions:
12
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "wave.h"
27 #include "macros.h"
28 #include <stdlib.h>
29 #include <string.h>
30
31 typedef struct
32 {
33 char ckID[4];
34 Uint32 cksize;
35 } Chunk;
36
wave_load(FILE * f)37 Wave * wave_load(FILE *f)
38 {
39 struct {
40 Chunk c;
41 char WAVEID[4];
42 } __attribute__((__packed__)) RIFF;
43
44 if (fread(&RIFF, 1 , sizeof(RIFF), f) != sizeof(RIFF) || strncmp(RIFF.c.ckID, "RIFF", 4) != 0 || strncmp(RIFF.WAVEID, "WAVE", 4) != 0)
45 {
46 fatal("Not a RIFF wave file");
47 return NULL;
48 }
49
50 struct
51 {
52 Chunk c;
53 Uint16 wFormatTag;
54 Uint16 nChannels;
55 Uint32 nSamplesPerSec;
56 Uint32 nAvgBytesPerSec;
57 Uint16 nBlockAlign;
58 Uint16 wBitsPerSample;
59 /*----*/
60 Uint16 cbSize;
61 /*----*/
62 Uint16 wValidBitsPerSample;
63 Uint32 dwChannelMask;
64 char SubFormat[16];
65 } __attribute__((__packed__)) WAVE;
66
67 size_t beginning_of_WAVE = ftell(f);
68
69 if (fread(&WAVE, 1 , sizeof(WAVE), f) < 16 || strncmp(WAVE.c.ckID, "fmt ", 4) != 0)
70 {
71 fatal("No 'fmt ' chunk found");
72 return NULL;
73 }
74
75 if (WAVE.wFormatTag != WAVE_FORMAT_PCM/* && WAVE.wFormatTag != WAVE_FORMAT_FLOAT*/)
76 {
77 //fatal("Only PCM and float supported");
78 fatal("Only PCM supported");
79 return NULL;
80 }
81
82 fseek(f, beginning_of_WAVE + WAVE.c.cksize + 8, SEEK_SET);
83
84 Chunk peek = { "", 0 };
85
86 fread(&peek, 1, sizeof(peek) ,f);
87
88 if (strncmp(peek.ckID, "fact", 4) == 0)
89 {
90 fseek(f, sizeof(Uint32), SEEK_CUR);
91 }
92 else
93 {
94 fseek(f, beginning_of_WAVE + WAVE.c.cksize + 8, SEEK_SET);
95 }
96
97 fread(&peek, 1, sizeof(peek) ,f);
98
99 if (strncmp(peek.ckID, "data", 4) != 0)
100 {
101 fatal("No 'data' chunk found");
102 return NULL;
103 }
104
105 Wave *w = malloc(sizeof(*w));
106
107 w->format = WAVE.wFormatTag;
108 w->channels = WAVE.nChannels;
109 w->sample_rate = WAVE.nSamplesPerSec;
110 w->length = peek.cksize / (WAVE.wBitsPerSample / 8) / WAVE.nChannels;
111 w->bits_per_sample = WAVE.wBitsPerSample;
112
113 w->data = malloc(peek.cksize);
114
115 debug("Reading %d bytes (chn = %d, bits = %d)", peek.cksize, w->channels, w->bits_per_sample);
116
117 fread(w->data, 1, peek.cksize, f);
118
119 return w;
120 }
121
122
wave_destroy(Wave * wave)123 void wave_destroy(Wave *wave)
124 {
125 if (wave)
126 {
127 free(wave->data);
128 free(wave);
129 }
130 }
131