-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwavedump.c
186 lines (166 loc) · 5.06 KB
/
wavedump.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
/*
* Audio Overload SDK
*
* Wave dumping
*
* Author: Nmlgc
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "ao.h"
#include "utils.h"
#include "wavedump.h"
typedef struct {
uint32 FOURCC;
uint32 Size;
} RIFFCHUNK;
// This is pretty much identical to the WAVEFORMAT+PCMWAVEFORMAT structures
// from <windows.h>, which we can't use because they're stupidly declared in
// such a way as to come with 2 padding bytes before and after
// PCMWAVEFORMAT.wBitsPerSample.
typedef struct {
uint16 wFormatTag; // format type
uint16 nChannels; // number of channels (i.e. mono, stereo, etc.)
uint32 nSamplesPerSec; // sample rate
uint32 nAvgBytesPerSec; // for buffer estimation
uint16 nBlockAlign; // block size of data
uint16 wBitsPerSample;
} WAVEFORMAT_PCM;
// flags for wFormatTag field of WAVEFORMAT
#define WAVE_FORMAT_PCM 1
typedef struct {
RIFFCHUNK cRIFF;
uint32 WAVE;
RIFFCHUNK cfmt;
WAVEFORMAT_PCM Format;
RIFFCHUNK cdata;
} WAVEHEADER;
typedef struct {
RIFFCHUNK ccue;
uint32 points; // number of cue points
} CUEHEADER;
typedef struct {
uint32 dwName; // unique identification value
uint32 dwPosition; // play order position
uint32 fccChunk; // RIFF ID of corresponding data chunk
uint32 dwChunkStart; // offset from [fccChunk] to the LIST chunk, or 0 if no such chunk is present
uint32 dwBlockStart; // offset from [fccChunk] to a (unspecified) "block" containing the sample
uint32 dwSampleOffset; // offset from [dwBlockStart] to the sample
} CUEPOINT;
static void wavedump_LIST_adtl_labl_write(
wavedump_t *wave, uint32 point_id, const char *label
)
{
RIFFCHUNK cLIST;
const uint32 adtl = LE32(*(uint32*)"adtl");
RIFFCHUNK clabl;
size_t label_len;
assert(wave);
assert(label);
label_len = strlen(label) + 1;
clabl.FOURCC = LE32(*(uint32*)"labl");
cLIST.FOURCC = LE32(*(uint32*)"LIST");
clabl.Size = sizeof(point_id) + label_len;
cLIST.Size = sizeof(adtl) + sizeof(clabl) + clabl.Size;
fwrite(&cLIST, sizeof(cLIST), 1, wave->file);
fwrite(&adtl, sizeof(adtl), 1, wave->file);
fwrite(&clabl, sizeof(clabl), 1, wave->file);
fwrite(&point_id, sizeof(point_id), 1, wave->file);
fwrite(label, label_len, 1, wave->file);
}
static void wavedump_header_fill(
WAVEHEADER *h, uint32 data_size, uint32 file_size,
uint32 sample_rate, uint16 bits_per_sample, uint16 channels
)
{
h->cRIFF.FOURCC = LE32(*(uint32*)"RIFF");
h->cRIFF.Size = LE32(file_size - sizeof(RIFFCHUNK));
h->WAVE = LE32(*(uint32*)"WAVE");
h->cfmt.FOURCC = LE32(*(uint32*)"fmt ");
h->cfmt.Size = LE32(sizeof(h->Format));
h->Format.wFormatTag = LE16(WAVE_FORMAT_PCM);
h->Format.nChannels = LE16(channels);
h->Format.nSamplesPerSec = LE16(sample_rate);
h->Format.nBlockAlign = LE16((channels * bits_per_sample) / 8);
h->Format.nAvgBytesPerSec = LE32(
h->Format.nSamplesPerSec * h->Format.nBlockAlign
);
h->Format.wBitsPerSample = LE16(bits_per_sample);
h->cdata.FOURCC = LE32(*(uint32*)"data");
h->cdata.Size = LE32(data_size);
}
ao_bool wavedump_open(wavedump_t *wave, const char *fn)
{
uint32 temp = 0;
assert(wave);
wave->data_size = 0;
wave->loop_sample = 0;
wave->file = fopen_derivative(fn, ".wav");
if(!wave->file) {
return false;
}
// Jump over header, we write that one later
fwrite(&temp, 1, sizeof(WAVEHEADER), wave->file);
return true;
}
void wavedump_loop_set(wavedump_t *wave, uint32 loop_sample)
{
assert(wave);
assert(wave->file);
wave->loop_sample = loop_sample;
}
void wavedump_append(wavedump_t *wave, uint32 len, void *buf)
{
assert(wave);
if(wave->file) {
wave->data_size += len;
// XXX: Doesn't the data need to be swapped on big-endian platforms?
// That would mean that we need to know the target wave format on
// opening time.
fwrite(buf, len, 1, wave->file);
}
}
void wavedump_finish(
wavedump_t *wave, uint32 sample_rate, uint16 bits_per_sample, uint16 channels
)
{
assert(wave);
if(wave->file) {
WAVEHEADER h;
// RIFF chunks have to be word-aligned, so we have to pad out the
// data chunk if the number of samples happens to be odd.
if(wave->data_size & 1) {
uint8 pad = 0;
// fwrite() rather than wavedump_append(), as the chunk size
// obviously doesn't include the padding.
fwrite(&pad, sizeof(pad), 1, wave->file);
}
if(wave->loop_sample) {
// Write the "cue " chunk, as well as an additional
// LIST-adtl-labl chunk for newer GoldWave versions
CUEHEADER cue;
CUEPOINT point;
cue.ccue.FOURCC = LE32(*(uint32*)"cue ");
cue.ccue.Size = LE32(4 + 1 * sizeof(CUEPOINT));
cue.points = LE32(1);
point.dwName = LE32(0);
point.dwChunkStart = LE32(0);
point.dwBlockStart = LE32(0);
point.dwPosition = LE32(wave->loop_sample);
point.dwSampleOffset = LE32(wave->loop_sample);
point.fccChunk = LE32(*(uint32*)"data");
fwrite(&cue, sizeof(cue), 1, wave->file);
fwrite(&point, sizeof(point), 1, wave->file);
wavedump_LIST_adtl_labl_write(wave, 0, "Loop point");
}
wavedump_header_fill(
&h, wave->data_size, ftell(wave->file),
sample_rate, bits_per_sample, channels
);
fseek(wave->file, 0, SEEK_SET);
fwrite(&h, sizeof(h), 1, wave->file);
fclose(wave->file);
wave->file = NULL;
}
}