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
|
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "audio.h"
#include "audio_internal.h"
#define DR_MP3_IMPLEMENTATION
#define DR_MP3_FLOAT_OUTPUT
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wpedantic"
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wfloat-equal"
#pragma GCC diagnostic ignored "-Wcast-qual"
#pragma GCC diagnostic ignored "-Wcast-align"
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#pragma GCC diagnostic ignored "-Wshadow"
#pragma GCC diagnostic ignored "-Wformat"
#endif
#include "dr_mp3.h"
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
#define MP3_SCRATCH_FRAMES 4096U
typedef struct {
drmp3 decoder;
float scratch[MP3_SCRATCH_FRAMES * 2U];
} Mp3Source;
static bool mp3_read_frames(AudioSource *source, float *stereo, size_t requested,
size_t *frames_read, char *message, size_t message_size)
{
Mp3Source *mp3 = source->impl;
uint64_t remaining = source->total_frames - source->frames_read;
size_t produced = 0U;
*frames_read = 0U;
if ((uint64_t)requested > remaining) {
requested = (size_t)remaining;
}
while (produced < requested) {
size_t want = requested - produced;
drmp3_uint64 got;
size_t frame;
if (want > MP3_SCRATCH_FRAMES) {
want = MP3_SCRATCH_FRAMES;
}
got = drmp3_read_pcm_frames_f32(&mp3->decoder, (drmp3_uint64)want,
mp3->scratch);
if (got == 0U) {
return set_message(message, message_size,
"MP3 stream ended after %" PRIu64 " of %" PRIu64
" frames", source->frames_read + produced,
source->total_frames);
}
for (frame = 0U; frame < (size_t)got; ++frame) {
if (source->channels == 2U) {
stereo[(produced + frame) * 2U] = mp3->scratch[frame * 2U];
stereo[(produced + frame) * 2U + 1U] = mp3->scratch[frame * 2U + 1U];
} else {
stereo[(produced + frame) * 2U] = mp3->scratch[frame];
stereo[(produced + frame) * 2U + 1U] = mp3->scratch[frame];
}
}
produced += (size_t)got;
if ((size_t)got < want) {
return set_message(message, message_size,
"MP3 stream ended after %" PRIu64 " of %" PRIu64
" frames", source->frames_read + produced,
source->total_frames);
}
}
source->frames_read += produced;
*frames_read = produced;
return true;
}
static void mp3_close(AudioSource *source)
{
Mp3Source *mp3 = source->impl;
if (mp3 != NULL) {
drmp3_uninit(&mp3->decoder);
free(mp3);
source->impl = NULL;
}
}
static bool mp3_configure(AudioSource *source, Mp3Source *mp3, const char *path,
char *message, size_t message_size)
{
uint32_t sample_rate = mp3->decoder.sampleRate;
uint16_t channels = (uint16_t)mp3->decoder.channels;
uint64_t total = 0U;
if (channels != 1U && channels != 2U) {
return set_message(message, message_size,
"MP3 must contain one or two channels (found %u)",
(unsigned)channels);
}
if (sample_rate < 8000U || sample_rate > 768000U) {
return set_message(message, message_size,
"unsupported MP3 sample rate: %" PRIu32, sample_rate);
}
for (;;) {
drmp3_uint64 got = drmp3_read_pcm_frames_f32(&mp3->decoder,
MP3_SCRATCH_FRAMES,
mp3->scratch);
if (got == 0U) {
break;
}
if (!checked_add_u64(total, (uint64_t)got, &total)) {
return set_message(message, message_size, "MP3 '%s' is too long", path);
}
}
if (total == 0U) {
return set_message(message, message_size,
"MP3 '%s' contains no audio frames", path);
}
if (mp3->decoder.totalPCMFrameCount != DRMP3_UINT64_MAX) {
drmp3_uint64 declared = drmp3_get_pcm_frame_count(&mp3->decoder);
if (declared != 0U && (uint64_t)declared != total) {
fprintf(stderr,
"warning: MP3 '%s' declares %" PRIu64 " frames but only %" PRIu64
" could be decoded; the file may be truncated\n",
path, (uint64_t)declared, total);
}
}
if (!drmp3_seek_to_pcm_frame(&mp3->decoder, 0)) {
return set_message(message, message_size, "cannot rewind MP3 '%s'", path);
}
source->sample_rate = sample_rate;
source->channels = channels;
source->total_frames = total;
source->frames_read = 0U;
return true;
}
static const AudioSourceOps MP3_OPS = {
mp3_read_frames,
mp3_close
};
bool mp3_source_open(AudioSource *source, const char *path, char *message,
size_t message_size)
{
Mp3Source *mp3 = calloc(1U, sizeof(*mp3));
if (mp3 == NULL) {
return set_message(message, message_size,
"cannot allocate the MP3 decoder: %s", strerror(errno));
}
if (!drmp3_init_file(&mp3->decoder, path, NULL)) {
free(mp3);
return set_message(message, message_size,
"cannot decode MP3 '%s': invalid or unsupported stream",
path);
}
if (!mp3_configure(source, mp3, path, message, message_size)) {
drmp3_uninit(&mp3->decoder);
free(mp3);
return false;
}
source->format_name = "MP3";
source->ops = &MP3_OPS;
source->impl = mp3;
return true;
}
|