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
|
// This file is part of the 64k demo project.
// Experimental MP3 sample decoding via miniaudio (non-STRIP_ALL only).
// Compiles to nothing in stripped builds.
#include "mp3_sample.h"
#if !defined(STRIP_ALL)
// miniaudio.h is already compiled with MINIAUDIO_IMPLEMENTATION in audio.cc.
// Include here without it to get struct/function declarations only.
#include "miniaudio.h"
// Concrete definition (opaque to callers via header forward declaration).
struct Mp3Decoder {
ma_decoder dec;
};
Mp3Decoder* mp3_open(const uint8_t* data, size_t size) {
Mp3Decoder* d = new Mp3Decoder();
ma_decoder_config cfg = ma_decoder_config_init(ma_format_f32, 1, 32000);
if (ma_decoder_init_memory(data, size, &cfg, &d->dec) != MA_SUCCESS) {
delete d;
return nullptr;
}
return d;
}
int mp3_decode_range(Mp3Decoder* dec, int start_frame, int num_frames,
float* out) {
if (ma_decoder_seek_to_pcm_frame(&dec->dec, (ma_uint64)start_frame) !=
MA_SUCCESS) {
return 0;
}
ma_uint64 frames_read = 0;
ma_decoder_read_pcm_frames(&dec->dec, out, (ma_uint64)num_frames,
&frames_read);
return (int)frames_read;
}
int mp3_decode(Mp3Decoder* dec, int num_frames, float* out) {
ma_uint64 frames_read = 0;
ma_decoder_read_pcm_frames(&dec->dec, out, (ma_uint64)num_frames,
&frames_read);
return (int)frames_read;
}
void mp3_close(Mp3Decoder* dec) {
if (!dec)
return;
ma_decoder_uninit(&dec->dec);
delete dec;
}
#endif // !STRIP_ALL
|