-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.cpp
359 lines (298 loc) · 9.69 KB
/
audio.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#define MINIAUDIO_IMPLEMENTATION
#include "audio.h"
#include <stdio.h>
#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
ma_context context;
ma_device_config deviceConfig;
ma_device device;
ma_decoder decoder;
ma_data_source_node dataSourceNode;
ma_node_graph_config nodeGraphConfig;
ma_node_graph nodeGraph;
// a Pretty Fast Fast Fourier Transform library
#include "libs/pffft/pffft.c"
#define RECENT_DATA_SIZE 1024
static int recent_data_index = 0;
static float *recent_data = NULL;
int recent_data_size() {
return RECENT_DATA_SIZE;
}
float *get_recent_data() {
return recent_data;
}
#define FFT_SIZE RECENT_DATA_SIZE
static float *fft_data = NULL;
static PFFFT_Setup *fft_setup;
int fft_size() {
return FFT_SIZE;
}
float *calc_fft()
{
pffft_transform_ordered(fft_setup, recent_data, fft_data, NULL, PFFFT_FORWARD);
// make the data easier to read
// via log(abs(x))
float *p = fft_data;
for (int i=0; i<FFT_SIZE; i++, p++) {
*p = log(abs(*p));
}
return fft_data;
}
void init_recent_data_and_fft()
{
recent_data = (float*)pffft_aligned_malloc(RECENT_DATA_SIZE * sizeof(float));
fft_data = (float*)pffft_aligned_malloc(FFT_SIZE * sizeof(float));
fft_setup = pffft_new_setup(FFT_SIZE, PFFFT_REAL);
}
static uint64_t __num_samples = 0;
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount);
bool setup_audio()
{
init_recent_data_and_fft();
if (ma_context_init(NULL, 0, NULL, &context) != MA_SUCCESS) {
printf("Failed to initialize audio context.\n");
return false;
}
ma_device_info* pPlaybackInfos;
ma_uint32 playbackCount;
ma_device_info* pCaptureInfos;
ma_uint32 captureCount;
if (ma_context_get_devices(&context, &pPlaybackInfos, &playbackCount, &pCaptureInfos, &captureCount) != MA_SUCCESS) {
printf("Failed to get audio device list.\n");
return false;
}
// Loop over each device info and do something with it. Here we just print the name with their index.
printf("Available Audio Devices:\n");
for (ma_uint32 iDevice = 0; iDevice < playbackCount; iDevice += 1) {
printf(" #%d: %s\n", iDevice, pPlaybackInfos[iDevice].name);
}
deviceConfig = ma_device_config_init(ma_device_type_playback);
deviceConfig.playback.format = ma_format_f32;
deviceConfig.playback.channels = 2;
deviceConfig.sampleRate = 48000;
deviceConfig.dataCallback = data_callback;
nodeGraphConfig = ma_node_graph_config_init(deviceConfig.playback.channels);
ma_result result = ma_node_graph_init(&nodeGraphConfig, NULL, &nodeGraph);
if (result != MA_SUCCESS) {
printf("Failed to initialize audio node graph.\n");
return false;
}
deviceConfig.pUserData = &nodeGraph;
if (ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS) {
printf("Failed to open playback device.\n");
return false;
}
char name[MA_MAX_DEVICE_NAME_LENGTH+1];
if (ma_device_get_name(&device, ma_device_type_playback, name, sizeof(name), NULL) != MA_SUCCESS) {
printf("Failed to get playback device name.\n");
return false;
}
printf("Selected Audio Device: %s\n", name);
return true;
}
void tear_down_audio()
{
ma_device_stop(&device);
ma_decoder_uninit(&decoder);
ma_device_uninit(&device);
ma_context_uninit(&context);
}
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
ma_uint64 framesRead;
ma_result result = ma_node_graph_read_pcm_frames(&nodeGraph, pOutput, frameCount, &framesRead);
if (result != MA_SUCCESS) {
return;
}
const int channels = 2;
float* src = (float*)pOutput;
ma_uint64 frames_to_copy = framesRead;
while (frames_to_copy > channels-1) {
float val = 0;
for (int c=0; c<channels; c++) {
val += *src++;
frames_to_copy--;
}
val /= channels;
recent_data[recent_data_index++] = val;
if (recent_data_index >= RECENT_DATA_SIZE) recent_data_index = 0;
}
// float* source = (float*)pOutput;
// int frame_size = sizeof(float);
// ma_uint64 frames_to_copy = framesRead;
// ma_uint64 frames_copied = 0;
// ma_uint64 max_frames = RECENT_DATA_SIZE;
//
// while (frames_to_copy > 0) {
// ma_uint64 frames_free = max_frames - recent_data_index;
// ma_uint64 chunk_size = MIN(frames_free, frames_to_copy);
// memcpy(&recent_data[recent_data_index], &source[frames_copied], chunk_size * frame_size);
// frames_to_copy -= chunk_size;
// recent_data_index += chunk_size;
// frames_copied += chunk_size;
// if (recent_data_index >= max_frames) recent_data_index = 0;
// }
(void)pInput;
}
bool load_audio_file(const char* path)
{
ma_result result = ma_decoder_uninit(&decoder);
if (result != MA_SUCCESS) {
printf("Warning: Un-init decoder failed?\n");
}
__num_samples = 0;
ma_node_detach_full(&dataSourceNode);
if (result != MA_SUCCESS) {
printf("Warning: Detatch failed?\n");
}
ma_data_source_uninit(&dataSourceNode);
if (result != MA_SUCCESS) {
printf("Warning: Un-init data source failed?\n");
}
ma_decoder_config decoderConfig = ma_decoder_config_init(deviceConfig.playback.format,
deviceConfig.playback.channels,
deviceConfig.sampleRate);
// ma_decoder_config decoderConfig = ma_decoder_config_init_default();
result = ma_decoder_init_file(path, &decoderConfig, &decoder);
if (result != MA_SUCCESS) {
printf("Could not load file: %s\n", path);
return false;
}
ma_format format;
ma_uint32 num_channels;
ma_uint32 sample_rate;
if (ma_decoder_get_data_format(&decoder, &format, &num_channels, &sample_rate, NULL, 0) != MA_SUCCESS) {
printf("Failed to determine audio file format.\n");
ma_decoder_uninit(&decoder);
return false;
}
if (format != deviceConfig.playback.format) {
printf("Audio file format mismatch %d != %d.\n", format, deviceConfig.playback.format);
}
if (num_channels != deviceConfig.playback.channels) {
printf("Audio file channels mismatch %d != %d.\n", num_channels, deviceConfig.playback.channels);
}
if (sample_rate != deviceConfig.sampleRate) {
printf("Audio file sample rate mismatch %d != %d.\n", sample_rate, deviceConfig.sampleRate);
}
if (ma_decoder_get_length_in_pcm_frames(&decoder, &__num_samples) != MA_SUCCESS) {
printf("Failed to determine audio file duration.\n");
ma_decoder_uninit(&decoder);
return false;
}
ma_data_source_node_config nodeConfig = ma_data_source_node_config_init(&decoder);
result = ma_data_source_node_init(&nodeGraph, &nodeConfig, NULL, &dataSourceNode);
if (result != MA_SUCCESS) {
printf("Failed to create data source node.\n");
ma_decoder_uninit(&decoder);
return false;
}
result = ma_node_attach_output_bus(&dataSourceNode, 0, ma_node_graph_get_endpoint(&nodeGraph), 0);
if (result != MA_SUCCESS) {
printf("Failed to attach node.\n");
ma_decoder_uninit(&decoder);
return false;
}
return true;
}
bool play_audio()
{
ma_result result = ma_device_start(&device);
if (result != MA_SUCCESS) {
printf("Failed to start playback device.\n");
tear_down_audio();
return false;
}
return true;
}
bool stop_audio()
{
if (ma_device_stop(&device) != MA_SUCCESS) {
printf("Failed to start playback device.\n");
tear_down_audio();
return false;
}
return true;
}
uint32_t sample_rate()
{
return deviceConfig.sampleRate;
}
int num_channels()
{
return decoder.outputChannels;
}
uint64_t num_samples()
{
return __num_samples;
}
const char *audio_format_str()
{
switch (deviceConfig.playback.format) {
case ma_format_f32:
return "f32";
case ma_format_u8:
return "u8";
case ma_format_s16:
return "s16";
case ma_format_s24:
return "s24";
case ma_format_s32:
return "s32";
default:
return "?";
}
}
const char *audio_state_str()
{
switch (ma_device_get_state(&device)) {
case ma_device_state_starting:
return "starting";
case ma_device_state_started:
return "started";
case ma_device_state_stopping:
return "stopping";
case ma_device_state_stopped:
return "stopped";
case ma_device_state_uninitialized:
return "uninitialized";
default:
return "?";
}
}
bool audio_looping()
{
return ma_data_source_node_is_looping(&dataSourceNode);
}
void audio_set_looping(bool loop)
{
ma_result result = ma_data_source_node_set_looping(&dataSourceNode, loop);
if (result != MA_SUCCESS) {
printf("Unable to set looping mode.\n");
}
}
uint64_t current_sample_position()
{
ma_uint64 cursor;
ma_result result = ma_decoder_get_cursor_in_pcm_frames(&decoder, &cursor);
if (result != MA_SUCCESS) {
return 0;
}
return cursor;
}
bool seek_audio(uint64_t targetFrame)
{
if (targetFrame >= __num_samples) targetFrame = __num_samples-1;
printf("seek %lld\n", targetFrame);
ma_result result = ma_decoder_seek_to_pcm_frame(&decoder, targetFrame);
if (result != MA_SUCCESS) {
return false;
}
return true;
}
void set_volume(float vol)
{
// ma_node_set_output_bus_volume(&dataSourceNode, 0, vol);
ma_node_set_output_bus_volume(ma_node_graph_get_endpoint(&nodeGraph), 0, vol);
}