-
Notifications
You must be signed in to change notification settings - Fork 5
/
libwav.c
276 lines (240 loc) · 5.54 KB
/
libwav.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* Copyright 2016 - 2017 Marc Volker Dickmann */
#include <stdio.h>
#include <stdlib.h>
#include "libwav.h"
static enum wav_error
wav_content_read (wav_chunk *chunk, FILE *f)
{
const size_t items = chunk->chunk_size / sizeof (int);
return (fread (chunk->content.data, sizeof (int), items, f) == items ? WAV_OK : WAV_ERROR);
}
// WAV_HEADER
enum wav_error
wav_header_write (const wav_header *header, FILE *f)
{
if (f == NULL)
{
return WAV_FILE_NOT_OPENED;
}
else if (fwrite (header, sizeof (wav_header), 1, f) != 1)
{
return WAV_ERROR;
}
return WAV_OK;
}
enum wav_error
wav_header_read (wav_header *header, FILE *f)
{
if (f == NULL)
{
return WAV_FILE_NOT_OPENED;
}
else if (fread (header, sizeof (wav_header), 1, f) != 1)
{
return WAV_ERROR;
}
return WAV_OK;
}
void
wav_header_print (const wav_header *header)
{
printf ("Riff Type:\t%.4s\n", header->riff_type.str);
}
// WAV_FORMAT
enum wav_error
wav_format_write (const wav_format *format, FILE *f)
{
if (f == NULL)
{
return WAV_FILE_NOT_OPENED;
}
else if (fwrite (format, sizeof (wav_format), 1, f) != 1)
{
return WAV_ERROR;
}
return WAV_OK;
}
enum wav_error
wav_format_read (wav_format *format, FILE *f)
{
if (f == NULL)
{
return WAV_FILE_NOT_OPENED;
}
else if (fread (format, sizeof (wav_format), 1, f) != 1)
{
return WAV_ERROR;
}
return WAV_OK;
}
void
wav_format_print (const wav_format *format)
{
printf ("Format:\t\t%hu\n", format->format);
printf ("Channels:\t%hu\n", format->channels);
printf ("Samplerate:\t%u\n", format->samplerate);
printf ("Bytespersec:\t%u\n", format->bytespersec);
printf ("Blockalign:\t%hu\n", format->blockalign);
printf ("Bitwidth:\t%hu\n", format->bitwidth);
}
// WAV_CHUNK
void
wav_chunk_init (wav_chunk *chunk, const unsigned int id, const unsigned int size, const void *content)
{
chunk->chunk_id.hash = id;
chunk->chunk_size = size;
// Init the content based on the chunkid:
switch (chunk->chunk_id.hash)
{
case WAV_CHUNKID_RIFF:
chunk->content.header = *(wav_header*) content;
break;
case WAV_CHUNKID_FORMAT:
chunk->content.format = *(wav_format*) content;
break;
case WAV_CHUNKID_DATA:
chunk->content.data = (int*) content;
break;
default:
break;
}
}
enum wav_error
wav_chunk_write (const wav_chunk *chunk, FILE *f)
{
if (f == NULL)
{
return WAV_FILE_NOT_OPENED;
}
else if (fwrite (&chunk->chunk_id, sizeof (chunk->chunk_id), 1, f) != 1 ||
fwrite (&chunk->chunk_size, sizeof (chunk->chunk_size), 1, f) != 1)
{
return WAV_ERROR;
}
// Write the content based on the chunkid:
switch (chunk->chunk_id.hash)
{
case WAV_CHUNKID_RIFF:
return wav_header_write (&chunk->content.header, f);
case WAV_CHUNKID_FORMAT:
return wav_format_write (&chunk->content.format, f);
case WAV_CHUNKID_DATA:
fwrite (chunk->content.data, sizeof (int), chunk->chunk_size / sizeof (int), f);
return WAV_OK;
default:
return WAV_UNKNOWN_CHUNKID;
}
}
enum wav_error
wav_chunk_read (wav_chunk *chunk, FILE *f)
{
if (f == NULL)
{
return WAV_FILE_NOT_OPENED;
}
else if (fread (&chunk->chunk_id, sizeof (chunk->chunk_id), 1, f) != 1 ||
fread (&chunk->chunk_size, sizeof (chunk->chunk_size), 1, f) != 1)
{
return WAV_ERROR;
}
// Read the content based on the chunkid:
switch (chunk->chunk_id.hash)
{
case WAV_CHUNKID_RIFF:
return wav_header_read (&chunk->content.header, f);
case WAV_CHUNKID_FORMAT:
return wav_format_read (&chunk->content.format, f);
case WAV_CHUNKID_DATA:
chunk->content.data = malloc (chunk->chunk_size);
return wav_content_read (chunk, f);
default:
return WAV_UNKNOWN_CHUNKID;
}
}
void
wav_chunk_print (const wav_chunk *chunk)
{
printf ("Chunk ID:\t%.4s\n", chunk->chunk_id.str);
printf ("Chunk Size:\t%u\n", chunk->chunk_size);
switch (chunk->chunk_id.hash)
{
case WAV_CHUNKID_RIFF:
wav_header_print (&chunk->content.header);
break;
case WAV_CHUNKID_FORMAT:
wav_format_print (&chunk->content.format);
break;
default:
break;
}
}
// WAV_FILE
void
wav_free (wav_file *wavfile)
{
free (wavfile->data);
}
enum wav_error
wav_write (const wav_file *wavfile, const char *filename)
{
FILE *f = fopen (filename, "wb");
if (f == NULL)
{
return WAV_FILE_NOT_OPENED;
}
// Header
wav_chunk chunk;
wav_chunk_init (&chunk, WAV_CHUNKID_RIFF, 36 + (sizeof (int) * wavfile->datablocks), &wavfile->header);
wav_chunk_write (&chunk, f);
// Format
wav_chunk_init (&chunk, WAV_CHUNKID_FORMAT, 16, &wavfile->format);
wav_chunk_write (&chunk, f);
// Data
wav_chunk_init (&chunk, WAV_CHUNKID_DATA, sizeof (int) * wavfile->datablocks, wavfile->data);
wav_chunk_write (&chunk, f);
fclose (f);
return WAV_OK;
}
enum wav_error
wav_read (wav_file *wavfile, const char *filename)
{
FILE *f = fopen (filename, "rb");
if (f == NULL)
{
return WAV_FILE_NOT_OPENED;
}
// Check if its a valid file:
wav_chunk chunk;
if (wav_chunk_read (&chunk, f) != WAV_OK ||
chunk.chunk_id.hash != WAV_CHUNKID_RIFF)
{
fclose (f);
return WAV_INVALID_FILE;
}
wavfile->header = chunk.content.header;
wavfile->datablocks = 0;
wavfile->data = NULL;
// Read the chunks:
while (!feof (f))
{
wav_chunk_read (&chunk, f);
switch (chunk.chunk_id.hash)
{
case WAV_CHUNKID_FORMAT:
wavfile->format = chunk.content.format;
break;
case WAV_CHUNKID_DATA:
wavfile->datablocks = chunk.chunk_size / sizeof (int);
wavfile->data = chunk.content.data;
fclose (f);
return WAV_OK;
default:
// NOTE: Unknown chunk!
fseek (f, chunk.chunk_size, SEEK_CUR);
break;
}
}
// No DATA chunk found!
fclose (f);
return WAV_ERROR;
}