-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmp3decoder.cpp
More file actions
195 lines (160 loc) · 4.58 KB
/
mp3decoder.cpp
File metadata and controls
195 lines (160 loc) · 4.58 KB
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
// Mp3Decoder.cpp: implementation of the CMp3Decoder class.
//
//////////////////////////////////////////////////////////////////////
#include "mp3decoder.h"
#ifdef __cplusplus
extern "C" {
#endif
#include <mad.h> //mpeg library
#ifdef __cplusplus
}
#endif
#ifdef _WIN32
#include <process.h>
#endif
#include <math.h>
#include <assert.h>
#include "liboddcast.h"
#include "log.h"
#define READSIZE 2000 // JCBUG i forget if we need a READSIZE, and if so, why this size
extern FILE *outToEncoder;
///////////////////////////////////////////////////
// Private methods
void mp3dec_init(MP3_DECODE_ST* pdecode)
{
assert(pdecode);
pdecode->m_mp3BufPos = 0;
mad_stream_init(&pdecode->stream);
mad_frame_init(&pdecode->frame);
mad_synth_init(&pdecode->synth);
}
long mp3dec_feed_stream(MP3_DECODE_ST* pdecode, char *pdata, long size)
{
assert(pdecode);
LogMessage(LOG_DEBUG, "feed_stream: size=%d, mp3pos=%d", size, pdecode->m_mp3BufPos);
if (pdecode->m_mp3BufPos+size > MP3BUFSIZE)
{
assert(0);
return ERR_MP3DECODER_BUFFER_FULL;
}
memcpy(&pdecode->m_pmp3Buf[pdecode->m_mp3BufPos], pdata, size);
pdecode->m_mp3BufPos += size;
return ERR_OK;
}
long mp3dec_get_stream_size(MP3_DECODE_ST* pdecode)
{
return pdecode->m_mp3BufPos;
}
static signed int scale(mad_fixed_t sample)
{
// round
sample += (1L << (MAD_F_FRACBITS - 16));
// clip
if (sample >= MAD_F_ONE)
sample = MAD_F_ONE - 1;
else if (sample < -MAD_F_ONE)
sample = -MAD_F_ONE;
// quantize
return sample >> (MAD_F_FRACBITS + 1 - 16);
}
int handle_mp3_output(MP3_DECODE_ST *pdecode)
{
unsigned int nchannels, nsamples;
float samples[8196];
struct mad_pcm *pcm;
mad_fixed_t const *left_ch, *right_ch;
static short lastSample = 0;
static signed int sample;
pcm = &pdecode->synth.pcm;
nchannels = pcm->channels;
nsamples = pcm->length;
left_ch = pcm->samples[0];
right_ch = pcm->samples[1];
long in_samplerate = pdecode->sampleRate;
long out_samplerate = getCurrentSamplerate();
long in_nch = pdecode->nch;
long out_nch = getCurrentChannels();
// Send it to liboddcast (cross fingers)
LogMessage(LOG_DEBUG, "Read %d/%d/%d - Sending %d/%d/%d", pdecode->bitRate/1000, pdecode->sampleRate, pdecode->nch, getCurrentBitrate(), getCurrentSamplerate(), getCurrentChannels());
int samplecount = 0;
for (int i=0;i<nsamples;i++) {
signed int sample;
sample = scale(*left_ch++);
samples[samplecount] = sample/32767.f;
// clipping
if (samples[samplecount] > 1.0) {
samples[samplecount] = 1.0;
}
if (samples[samplecount] < -1.0) {
samples[samplecount] = -1.0;
}
samplecount++;
if (nchannels == 2)
{
sample = scale(*right_ch++);
samples[samplecount] = sample/32767.f;
// clipping
if (samples[samplecount] > 1.0) {
samples[samplecount] = 1.0;
}
if (samples[samplecount] < -1.0) {
samples[samplecount] = -1.0;
}
samplecount++;
}
else {
//samples[samplecount] = 0.0;
//samplecount++;
}
}
LogMessage(LOG_DEBUG, "handle_output - nsamples = %d, nchannels = %d, in_samplerate = %d, in_nch = %d", nsamples, nchannels, in_samplerate, in_nch);
return handle_output((float *)&samples, nsamples, nchannels, in_samplerate);
}
int mp3dec_decode(MP3_DECODE_ST* pdecode)
{
struct mad_stream* pstream = &pdecode->stream;
struct mad_frame* pframe = &pdecode->frame;
struct mad_synth* psynth = &pdecode->synth;
int ret = 1;
mad_stream_buffer(pstream, (const unsigned char*)pdecode->m_pmp3Buf, pdecode->m_mp3BufPos);
while(1)
{
long ret = mad_frame_decode(pframe, pstream);
if (pstream->error != MAD_ERROR_NONE) {
if (pstream->error == MAD_ERROR_BUFLEN ||
!MAD_RECOVERABLE(pstream->error))
break;
//LogMessage(LOG_ERROR, "pstream Error: 0x%x", pstream->error);
//pstream->error = MAD_ERROR_NONE;
}
if (pstream->error == MAD_ERROR_NONE) {
mad_synth_frame(psynth, pframe);
pdecode->sampleRate = pframe->header.samplerate;
pdecode->bitRate = pframe->header.bitrate;
switch (pframe->header.mode) {
case MAD_MODE_SINGLE_CHANNEL:
pdecode->nch = 1;
break;
case MAD_MODE_DUAL_CHANNEL:
case MAD_MODE_JOINT_STEREO:
case MAD_MODE_STEREO:
pdecode->nch = 2;
break;
}
ret = handle_mp3_output(pdecode);
}
}
pstream->error = MAD_ERROR_NONE;
if (!pstream->next_frame)
assert(0);
long bufferleft = (long)&(pdecode->m_pmp3Buf[pdecode->m_mp3BufPos]) -
(long)pstream->next_frame;
assert(bufferleft > 0);
// move the mp3buf to the begining
memmove(pdecode->m_pmp3Buf,
&pdecode->m_pmp3Buf[pdecode->m_mp3BufPos-bufferleft],
bufferleft);
pdecode->m_mp3BufPos = bufferleft;
LogMessage(LOG_DEBUG, "---------");
return ret;
}