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
|
#ifndef UTILITY_H
#define UTILITY_H
#ifdef _MSC_VER
#pragma warning(disable : 4786)
#endif
#include <map>
#include <string>
#include <utility>
#include <stdlib.h>
#include "audiere.h"
#include "types.h"
#if defined(_MSC_VER) && _MSC_VER <= 1200 && !defined(_STLPORT_VERSION)
// std::min and std::max are broken in VC++ 6, so define our own.
// Unfortunately, this means we must include utility.h to use
// std::min and std::max
namespace std {
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
template<typename T>
inline T min(T a, T b) {
return (a < b ? a : b);
}
template<typename T>
inline T max(T a, T b) {
return (a > b ? a : b);
}
}
#else
#include <algorithm>
#endif
namespace audiere {
template<typename T>
T clamp(T min, T x, T max) {
return std::max(std::min(x, max), min);
}
class ParameterList {
public:
ParameterList(const char* parameters);
std::string getValue(const std::string& key, const std::string& defValue) const;
bool getBoolean(const std::string& key, bool def) const;
int getInt(const std::string& key, int def) const;
private:
std::map<std::string, std::string> m_values;
};
int strcmp_case(const char* a, const char* b);
inline int GetFrameSize(SampleSource* source) {
int channel_count, sample_rate;
SampleFormat sample_format;
source->getFormat(channel_count, sample_rate, sample_format);
return GetSampleSize(sample_format) * channel_count;
}
inline int GetFrameSize(const SampleSourcePtr& source) {
return GetFrameSize(source.get());
}
inline u16 read16_le(const u8* b) {
return b[0] + (b[1] << 8);
}
inline u16 read16_be(const u8* b) {
return (b[0] << 8) + b[1];
}
inline u32 read32_le(const u8* b) {
return read16_le(b) + (read16_le(b + 2) << 16);
}
inline u32 read32_be(const u8* b) {
return (read16_be(b) << 16) + read16_be(b + 2);
}
/// Converts an 80-bit IEEE 754 floating point number to a u32.
inline u32 readLD_be(const u8* b) {
u32 mantissa = read32_be(b + 2);
u8 exp = 30 - b[1];
u32 last = 0;
while (exp--) {
last = mantissa;
mantissa >>= 1;
}
if (last & 0x1) {
mantissa++;
}
return mantissa;
}
inline int GetFileLength(File* file) {
int pos = file->tell();
file->seek(0, File::END);
int length = file->tell();
file->seek(pos, File::BEGIN);
return length;
}
inline SampleSource* OpenBufferStream(
void* samples, int sample_count,
int channel_count, int sample_rate, SampleFormat sample_format)
{
return CreateSampleBuffer(
samples, sample_count,
channel_count, sample_rate, sample_format)->openStream();
}
class QueueBuffer {
public:
QueueBuffer() {
m_capacity = 256;
m_size = 0;
m_buffer = (u8*)malloc(m_capacity);
}
~QueueBuffer() {
m_buffer = (u8*)realloc(m_buffer, 0);
}
int getSize() {
return m_size;
}
void write(const void* buffer, int size) {
bool need_realloc = false;
while (size + m_size > m_capacity) {
m_capacity *= 2;
need_realloc = true;
}
if (need_realloc) {
m_buffer = (u8*)realloc(m_buffer, m_capacity);
}
memcpy(m_buffer + m_size, buffer, size);
m_size += size;
}
int read(void* buffer, int size) {
int to_read = std::min(size, m_size);
memcpy(buffer, m_buffer, to_read);
memmove(m_buffer, m_buffer + to_read, m_size - to_read);
m_size -= to_read;
return to_read;
}
void clear() {
m_size = 0;
}
private:
u8* m_buffer;
int m_capacity;
int m_size;
// private and unimplemented to prevent their use
QueueBuffer(const QueueBuffer&);
QueueBuffer& operator=(const QueueBuffer&);
};
class SizedBuffer {
public:
SizedBuffer() {
m_capacity = 256;
m_buffer = malloc(m_capacity);
}
~SizedBuffer() {
m_buffer = realloc(m_buffer, 0);
}
void ensureSize(int size) {
bool need_realloc = false;
while (m_capacity < size) {
m_capacity *= 2;
need_realloc = true;
}
if (need_realloc) {
m_buffer = realloc(m_buffer, m_capacity);
}
}
void* get() {
return m_buffer;
}
private:
void* m_buffer;
int m_capacity;
// private and unimplemented to prevent their use
SizedBuffer(const SizedBuffer&);
SizedBuffer& operator=(const SizedBuffer&);
};
}
#endif
|