-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathread_wav.cpp
More file actions
113 lines (97 loc) · 3.12 KB
/
Copy pathread_wav.cpp
File metadata and controls
113 lines (97 loc) · 3.12 KB
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
//
// openSAM: open source SAM emulator for X Plane
//
// Copyright (C) 2024, 2025, 2026 Holger Teutsch
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
// USA
//
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include "samjw.h"
#include "log_msg.h"
struct WAV_FORMAT {
short format;
short num_channels;
int sample_rate;
int byte_rate;
short block_align;
short bits_per_sample;
};
struct CHUNK_HEADER {
char id[4];
unsigned int size;
};
struct WAV_HEADER {
CHUNK_HEADER riffheader;
char wav[4];
CHUNK_HEADER fmtheader;
WAV_FORMAT fmt;
};
//
// A super simple wav file reader. I don't know and don't care whether it reads any other
// file than the ones supplied here.
//
void ReadWav(const std::string& fname, Sound& sound) {
sound = {};
FILE* f = fopen(fname.c_str(), "rb");
if (NULL == f) {
LogMsg("can't open wav %s", fname.c_str());
return;
}
WAV_HEADER header;
CHUNK_HEADER chunk;
if (1 != fread(&header, sizeof(header), 1, f)) {
LogMsg("can't read header");
fclose(f);
return;
}
if (memcmp(header.riffheader.id, "RIFF", 4) || memcmp(header.wav, "WAVE", 4) ||
memcmp(header.fmtheader.id, "fmt ", 4) || header.fmtheader.size < sizeof(WAV_FORMAT)) {
LogMsg("wav invalid header");
fclose(f);
return;
}
fseek(f, header.fmtheader.size - sizeof(WAV_FORMAT), SEEK_CUR);
// loop over chunks and find first data chunk
while (1 == fread(&chunk, sizeof(chunk), 1, f)) {
printf("chunk '%s' %d\n", chunk.id, chunk.size);
if (0 == memcmp(chunk.id, "data", 4)) {
void* data = malloc(chunk.size);
if (NULL == data) {
LogMsg("Can't malloc data for wav file");
fclose(f);
return;
}
if (1 != fread(data, chunk.size, 1, f)) {
LogMsg("error reading wav file");
fclose(f);
free(data);
return;
}
fclose(f);
sound.data = data;
sound.size = chunk.size;
sound.sample_rate = header.fmt.sample_rate;
sound.num_channels = header.fmt.num_channels;
return;
} else
fseek(f, chunk.size, SEEK_CUR); // skip over chunk
}
fclose(f);
LogMsg("can't find data chunk in %s", fname.c_str());
return;
}