Skip to content

Commit 347aa43

Browse files
committed
resource: cleanup
1 parent deceb26 commit 347aa43

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

src/resource/sound_wav.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@ namespace wav
2121
// See: https://www.mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
2222
struct WavHeader
2323
{
24-
// Header chunk.
25-
char riff[4]; // Must contain 'RIFF'.
26-
s32 file_size; // Total file size minus 8.
27-
char wave[4]; // Must contain 'WAVE'.
24+
char fourcc[4]; // Must contain 'RIFF'.
25+
s32 file_size; // Total file size minus 8.
26+
char wave_fourcc[4]; // Must contain 'WAVE'.
2827
};
2928

3029
struct WavFormat
3130
{
32-
char fmt[4]; // Must contain 'fmt '.
33-
s32 fmt_chunk_size; // Size of format chunk minus 8.
34-
s16 tag; // 1 = PCM integer, 3 = float.
31+
char fourcc[4]; // Must contain 'fmt '.
32+
s32 size; // Size of this chunk minus 8.
33+
s16 tag; // See WAVE_FORMAT_*
3534
s16 channels; // Number of channels.
3635
s32 sample_rate; // Sample rate in Hz.
3736
s32 byte_per_sec; // Number of bytes to read per second (sample_rate * byte_per_block).
@@ -41,7 +40,7 @@ namespace wav
4140

4241
struct WavFormatExt
4342
{
44-
s16 ext_size; // Size of extension (0 or 22).
43+
s16 size; // Size of extended data (0 or 22).
4544
};
4645

4746
struct WavFormatExt22
@@ -53,8 +52,8 @@ namespace wav
5352

5453
struct WavData
5554
{
56-
char data[4]; // Must contain 'data'.
57-
s32 data_size; // Samples size in bytes.
55+
char fourcc[4]; // Must contain 'data'.
56+
s32 size; // Samples size in bytes.
5857
// Data.
5958
};
6059

@@ -70,8 +69,8 @@ namespace wav
7069
const WavHeader *wav = (WavHeader *)array::begin(buf);
7170

7271
// Validate header chunk.
73-
RETURN_IF_FALSE(WAV, strncmp(wav->riff, "RIFF", 4) == 0, opts, "Bad header chunk");
74-
RETURN_IF_FALSE(WAV, strncmp(wav->wave, "WAVE", 4) == 0, opts, "Bad header chunk");
72+
RETURN_IF_FALSE(WAV, strncmp(wav->fourcc, "RIFF", 4) == 0, opts, "Bad header chunk");
73+
RETURN_IF_FALSE(WAV, strncmp(wav->wave_fourcc, "WAVE", 4) == 0, opts, "Bad header chunk");
7574
RETURN_IF_FALSE(WAV, (s32)array::size(buf) == wav->file_size + 8, opts, "Truncated source");
7675

7776
const WavFormat *fmt = (WavFormat *)&wav[1];
@@ -80,22 +79,22 @@ namespace wav
8079
const WavData *data = NULL;
8180

8281
// Validate format chunk.
83-
RETURN_IF_FALSE(WAV, strncmp(fmt->fmt, "fmt ", 4) == 0, opts, "Bad data format chunk");
82+
RETURN_IF_FALSE(WAV, strncmp(fmt->fourcc, "fmt ", 4) == 0, opts, "Bad data format chunk");
8483
RETURN_IF_FALSE(WAV, fmt->tag == WAVE_FORMAT_PCM, opts, "Unsupported data format");
8584

86-
if (fmt->fmt_chunk_size == 40 || fmt->fmt_chunk_size == 18) {
85+
if (fmt->size == 40 || fmt->size == 18) {
8786
fmt_ext = (WavFormatExt *)&fmt[1];
8887

89-
if (fmt->fmt_chunk_size == 40) {
90-
RETURN_IF_FALSE(WAV, fmt_ext->ext_size == 22, opts, "Bad extended data format size");
88+
if (fmt->size == 40) {
89+
RETURN_IF_FALSE(WAV, fmt_ext->size == 22, opts, "Bad extended data format size");
9190
fmt_ext22 = (WavFormatExt22 *)&fmt_ext[1];
9291
data = (WavData *)&fmt_ext22[1];
9392
} else {
94-
RETURN_IF_FALSE(WAV, fmt_ext->ext_size == 0, opts, "Bad extended data format size");
93+
RETURN_IF_FALSE(WAV, fmt_ext->size == 0, opts, "Bad extended data format size");
9594
data = (WavData *)&fmt_ext[1];
9695
}
9796
} else {
98-
RETURN_IF_FALSE(WAV, fmt->fmt_chunk_size == 16, opts, "Bad data format size");
97+
RETURN_IF_FALSE(WAV, fmt->size == 16, opts, "Bad data format size");
9998
// Some encoders write extended format data even though they report the format chunk to
10099
// be 16-bytes long (i.e. standard, non-extended length). Scan the buffer for the 'data'
101100
// chunk.
@@ -126,14 +125,14 @@ namespace wav
126125
);
127126

128127
// Validate data chunk.
129-
RETURN_IF_FALSE(WAV, strncmp(data->data, "data", 4) == 0, opts, "Bad data chunk");
130-
RETURN_IF_FALSE(WAV, data->data_size <= s32(array::size(buf) - sizeof(*wav)), opts, "Bad data chunk size");
128+
RETURN_IF_FALSE(WAV, strncmp(data->fourcc, "data", 4) == 0, opts, "Bad data chunk");
129+
RETURN_IF_FALSE(WAV, data->size <= s32(array::size(buf) - sizeof(*wav)), opts, "Bad data chunk size");
131130

132131
// Convert to intermediate 32-bit float.
133132
if (fmt->bit_depth == 8) {
134133
const f32 scale = 255.0f;
135134
const u8 *pcm = (u8 *)&data[1];
136-
for (s32 i = 0; i < data->data_size; i += fmt->byte_per_block) {
135+
for (s32 i = 0; i < data->size; i += fmt->byte_per_block) {
137136
for (s16 c = 0; c < fmt->channels; ++c) {
138137
const f32 conv = *pcm++ / scale * 2.0f - 1.0f;
139138
array::push_back(s._samples, clamp(conv, -1.0f, 1.0f));
@@ -142,7 +141,7 @@ namespace wav
142141
} else if (fmt->bit_depth == 16) {
143142
const f32 scale = 32768.0f;
144143
const s16 *pcm = (s16 *)&data[1];
145-
for (s32 i = 0; i < data->data_size; i += fmt->byte_per_block) {
144+
for (s32 i = 0; i < data->size; i += fmt->byte_per_block) {
146145
for (s16 c = 0; c < fmt->channels; ++c) {
147146
const f32 conv = *pcm++ / scale;
148147
array::push_back(s._samples, clamp(conv, -1.0f, 1.0f));

0 commit comments

Comments
 (0)