/
wav.cpp
167 lines (147 loc) · 3.72 KB
/
wav.cpp
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "wav.hpp"
#include <SDL.h>
#include "logger.hpp"
#include "sdl_logger.hpp"
#include <iostream>
namespace {
const char RIFF[4] = {'R', 'I', 'F', 'F'};
const char WAVE[4] = {'W', 'A', 'V', 'E'};
struct Header
{
char riff[4];
uint32_t len;
char wave[4];
};
struct Chunk
{
uint32_t id;
uint32_t len;
};
struct Fmt
{
uint16_t format_id;
uint16_t num_channels;
uint32_t sample_rate;
uint32_t bytes_per_second;
uint16_t block_size;
uint16_t bits_per_sample;
};
} // unnamed namespace
Wav::Wav()
: data(NULL)
, len(0)
, pos(0)
{
}
Wav::~Wav()
{
free(data);
}
bool Wav::LoadFile(const SP &filename)
{
SDL_RWops *f = SDL_RWFromFile(filename.c_str(), "rb");
if (f == NULL) {
SDL_ERROR("SDL_RWFromFile failed");
return false;
}
bool ret = ParseData(f);
SDL_RWclose(f);
return ret;
}
bool Wav::ParseData(const void *data, size_t size)
{
SDL_RWops *f = SDL_RWFromConstMem(data, size);
if (f == NULL) {
SDL_ERROR("SDL_RWFromConstMem failed");
return false;
}
bool ret = ParseData(f);
SDL_RWclose(f);
return false;
}
bool Wav::ParseData(SDL_RWops *f)
{
Header header;
if (SDL_RWread(f, &header, sizeof header, 1) != 1) {
//LOG_ERROR("WAV header not found in %s", filename.c_str());
return false;
}
if (memcmp(header.riff, RIFF, 4) != 0 || memcmp(header.wave, WAVE, 4) != 0) {
//LOG_ERROR("%s is not WAV format", filename.c_str());
return false;
}
ssize_t rest = header.len - sizeof WAVE;
while (rest > 0) {
Chunk chunk;
if (SDL_RWread(f, &chunk, sizeof chunk, 1) != 1) {
//LOG_ERROR("Chunk header not found in %s", filename.c_str());
return false;
}
rest -= sizeof chunk;
switch (chunk.id) {
case 0x20746d66: // "fmt "
{
Fmt fmt;
if (SDL_RWread(f, &fmt, sizeof fmt, 1) != 1) {
//LOG_ERROR("Fmt chunk not found in %s", filename.c_str());
return false;
}
#if 0
LOG_INFO("Fmt chunk:");
LOG_INFO(" format_id: 0x%04x", fmt.format_id);
LOG_INFO(" num_channels: %u", fmt.num_channels);
LOG_INFO(" sample_rate: %u", fmt.sample_rate);
LOG_INFO(" bytes_per_second: %u", fmt.bytes_per_second);
LOG_INFO(" block_size: %u", fmt.block_size);
LOG_INFO(" bits_per_sample: %u", fmt.bits_per_sample);
#endif
if (fmt.format_id != 1) {
//LOG_ERROR("%s is unsupported id %d, but supported Raw PCM format only", filename.c_str(), fmt.format_id);
return false;
}
if (chunk.len > sizeof fmt) {
uint16_t ex_len;
if (SDL_RWread(f, &ex_len, sizeof ex_len, 1) != 1) {
//LOG_ERROR("Extra part not found in %s", filename.c_str());
return false;
}
if (ex_len > 0) {
SDL_RWseek(f, ex_len, RW_SEEK_CUR);
}
}
}
break;
case 0x61746164: // "data"
len = chunk.len;
data = malloc(len);
SDL_RWread(f, data, len, 1);
break;
default:
SDL_RWseek(f, chunk.len, RW_SEEK_CUR);
break;
}
rest -= chunk.len;
if (chunk.len & 1) {
SDL_RWseek(f, 1, RW_SEEK_CUR);
--rest;
}
}
return true;
}
size_t Wav::ReadSamples(void *buf, size_t samples)
{
//std::cout << "samples=" << samples << ", pos=" << pos << std::endl;
size_t rest = len / sizeof (int16_t) - pos; // 残りサンプル数
size_t n = std::min(rest, samples); // 読み出そうとするサンプル数
const uint8_t *src_addr = reinterpret_cast<uint8_t *>(data) + pos * sizeof (int16_t); // 読み出すデータの先頭アドレス
const size_t src_len = n * sizeof (int16_t); // 読みだすデータの長さ(バイト単位)
memcpy(buf, src_addr, src_len);
pos += n;
if (n < samples) {
memset(reinterpret_cast<uint8_t *>(buf) + src_len * sizeof (int16_t), 0, (samples - n) * sizeof (int16_t));
}
return n;
}