This repository has been archived by the owner on May 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAudioGeneratorMP3.cpp
209 lines (170 loc) · 5.08 KB
/
AudioGeneratorMP3.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
/*
AudioGeneratorMP3
Wrap libmad MP3 library to play audio
Copyright (C) 2017 Earle F. Philhower, III
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AudioGeneratorMP3.h"
AudioGeneratorMP3::AudioGeneratorMP3()
{
running = false;
file = NULL;
output = NULL;
buffLen = 2048;
buff = NULL;
}
AudioGeneratorMP3::~AudioGeneratorMP3()
{
free(buff);
buff = NULL;
}
bool AudioGeneratorMP3::SetBufferSize(int sz)
{
if (running) return false;
buffLen = sz;
return true;
}
bool AudioGeneratorMP3::stop()
{
if (!running) return true;
running = false;
free(buff);
buff = NULL;
mad_synth_finish(&synth);
mad_frame_finish(&frame);
mad_stream_finish(&stream);
return file->close();
}
bool AudioGeneratorMP3::isRunning()
{
return running;
}
enum mad_flow AudioGeneratorMP3::ErrorToFlow()
{
char err[64];
strcpy_P(err, mad_stream_errorstr(&stream));
Serial.printf("Decoding error 0x%04x (%s) at byte offset %d\n", stream.error, err, (stream.this_frame - buff) + lastReadPos);
Serial.flush();
return MAD_FLOW_CONTINUE;
}
enum mad_flow AudioGeneratorMP3::Input()
{
int unused = 0;
if (stream.next_frame) {
unused = buffLen - (stream.next_frame - buff);
memmove(buff, stream.next_frame, unused);
}
if (unused == buffLen)
return MAD_FLOW_STOP;
lastReadPos = file->getPos() - unused;
int len = buffLen - unused;
len = file->read(buff + unused, len);
if (len == 0) return MAD_FLOW_STOP;
mad_stream_buffer(&stream, buff, len + unused);
return MAD_FLOW_CONTINUE;
}
bool AudioGeneratorMP3::DecodeNextFrame()
{
do {
if (!inInnerDecode) {
if (Input() == MAD_FLOW_STOP) return false;
}
while (1) {
inInnerDecode = true;
if (mad_frame_decode(&frame, &stream) == -1) {
if (!MAD_RECOVERABLE(stream.error)) break;
ErrorToFlow(); // Always returns CONTINUE
continue;
}
return true;
}
inInnerDecode = false;
} while (stream.error == MAD_ERROR_BUFLEN);
return false;
}
bool AudioGeneratorMP3::GetOneSample(int16_t sample[2])
{
if ( (samplePtr >= synth.pcm.length) && (nsCount >= 1152/32) ) {
if (!DecodeNextFrame()) return false;
samplePtr = 9999;
nsCount = 0;
}
if (synth.pcm.samplerate != lastRate) {
output->SetRate(synth.pcm.samplerate);
lastRate = synth.pcm.samplerate;
}
if (synth.pcm.channels != lastChannels) {
output->SetChannels(synth.pcm.channels);
lastChannels = synth.pcm.channels;
}
// If we're here, we have one decoded frame and sent 0 or more samples out
if (samplePtr < synth.pcm.length) {
sample[AudioOutput::LEFTCHANNEL ] = synth.pcm.samples[0][samplePtr];
sample[AudioOutput::RIGHTCHANNEL] = synth.pcm.samples[1][samplePtr];
samplePtr++;
} else {
samplePtr = 0;
switch ( mad_synth_frame_onens(&synth, &frame, nsCount++) ) {
case MAD_FLOW_STOP:
case MAD_FLOW_BREAK:
return false; // Either way we're done
default:
break; // Do nothing
}
// for IGNORE and CONTINUE, just play what we have now
sample[AudioOutput::LEFTCHANNEL ] = synth.pcm.samples[0][samplePtr];
sample[AudioOutput::RIGHTCHANNEL] = synth.pcm.samples[1][samplePtr];
samplePtr++;
}
return true;
}
bool AudioGeneratorMP3::loop()
{
static int16_t lastSample[2]= {0,0};
if (!running) return true; // Nothing to do here!
// First, try and push in the stored sample. If we can't, then punt and try later
if (!output->ConsumeSample(lastSample)) return true; // Can't send, but no error detected
// Try and stuff the buffer one sample at a time
do
{
if (!GetOneSample(lastSample)) {
running = false;
return false;
}
} while (running && output->ConsumeSample(lastSample));
return running;
}
bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output)
{
if (!source) return false;
file = source;
if (!output) return false;
this->output = output;
if (!file->isOpen()) return false; // Error
if (!output->begin()) return false;
// Where we are in generating one frame's data, set to invalid so we will run loop on first getsample()
samplePtr = 9999;
nsCount = 9999;
synth.pcm.length = 0;
lastRate = 0;
lastChannels = 0;
lastReadPos = 0;
inInnerDecode = false;
buff = reinterpret_cast<unsigned char *>(malloc(buffLen));
if (!buff) return false;
mad_stream_init(&stream);
mad_frame_init(&frame);
mad_synth_init(&synth);
mad_stream_options(&stream, 0); // TODO - add options suppoirt
running = true;
return true;
}