-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmp3.c
More file actions
296 lines (274 loc) · 6.01 KB
/
mp3.c
File metadata and controls
296 lines (274 loc) · 6.01 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
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
/*
* mp3.c
*
* Copyright 2014 Fernando Rodriguez (support@fernansoft.com).
* All rights reserved
*
*/
#include <stdlib.h>
#include "mp3.h"
#include "mpeg_2.h"
#include "mpeg_2_synth.h"
/*
// machine states
*/
#define MP3_STATE_IDLE 0
#define MP3_STATE_STARTING 1
#define MP3_STATE_WAITING_FOR_DATA 2
#define MP3_STATE_DECODING 3
#define MP3_STATE_OUTPUT_READY 4
#define MP3_STATE_FAILED 5
#define MP3_STATE_FINISHED 6
#if defined(__XC16__) || defined(__C30__)
static unsigned char state __attribute__((__near__, section(".mp3")));
static unsigned char mp3_nch __attribute__((__near__, section(".mp3")));
/*static char decode_header __attribute__((section(".mp3_sm")));*/
#define WEAK /*__attribute__((__weak__))*/
#else
static unsigned char state;
static unsigned char mp3_nch;
/*static char decode_header;*/
#define WEAK
#endif
/*
// initialize decoder
*/
void mp3_sm_init(unsigned int nch)
{
/*decode_header = 0;*/
state = MP3_STATE_IDLE;
mp3_nch = nch;
}
/*
// start the decoder
*/
void mp3_sm_start(void)
{
/*
// get the state machine going
*/
state = MP3_STATE_STARTING;
}
#if 0 && (defined(__XC16__) || defined(__C30__))
unsigned int WEAK mp3_sm_input(void)
{
return MP3_RSP_BREAK;
}
#else
extern unsigned int WEAK mp3_sm_input(void);
#endif
#if 0
unsigned int WEAK mp3_sm_header(mp3_header_t const * header)
{
return MP3_RSP_CONTINUE;
}
#endif
#if 0 && (defined(__XC16__) || defined(__C30__))
unsigned int WEAK mp3_sm_output(mp3_header_t const * header, mp3_frac pcm_samples[2][1152])
{
return MP3_RSP_CONTINUE;
}
#else
extern unsigned int WEAK mp3_sm_output(mp3_header_t const * header, mp3_frac pcm_samples[2][1152]);
#endif
#if 0 && (defined(__XC16__) || defined(__C30__))
void WEAK mp3_sm_finished(int result)
{
return;
}
#else
void WEAK mp3_sm_finished(int result);
#endif
#if 0 && (defined(__XC16__) || defined(__C30__))
unsigned int WEAK mp3_sm_error(const unsigned int error)
{
return MP3_RSP_CONTINUE;
}
#else
extern unsigned int WEAK mp3_sm_error(const unsigned int error);
#endif
/*
// decoder state machine
*/
void mp3_sm_tasks(void)
{
switch (state)
{
case MP3_STATE_IDLE:
/*
// nothing to do
*/
return;
case MP3_STATE_STARTING:
/*
// initialize decoder
*/
mp3_init();
mp3_synth_init();
/*
// advance state machine
*/
state = MP3_STATE_WAITING_FOR_DATA;
/* no break */
case MP3_STATE_WAITING_FOR_DATA:
/*
// call input handler
*/
switch (mp3_sm_input())
{
case MP3_RSP_STOP:
state = MP3_STATE_FINISHED;
return;
case MP3_RSP_BREAK:
state = MP3_STATE_FAILED;
return;
case MP3_RSP_NOT_READY:
case MP3_RSP_IGNORE: return;
case MP3_RSP_CONTINUE: break;
}
/*
// advance state machine
*/
state = MP3_STATE_DECODING;
/* no break */
case MP3_STATE_DECODING:
/*
// if header decoding is enabled then
// decode the header
*/
#if 0
{
/*
// decode header
*/
if (mp3_decode_header() != MP3_SUCCESS)
{
/*
// if more data is needed request it on
// next step
*/
if (mp3_get_last_error() == MP3_ERROR_EOF)
{
state = MP3_STATE_WAITING_FOR_DATA;
return;
}
/*
// invoke error handler
*/
switch (mp3_sm_error(mp3_get_last_error()))
{
case MP3_RSP_STOP:
state = MP3_STATE_FINISHED;
return;
case MP3_RSP_BREAK:
state = MP3_STATE_FAILED;
return;
case MP3_RSP_IGNORE:
case MP3_RSP_CONTINUE:
default: return;
}
}
/*
// invoke decoded header handler
*/
switch (mp3_sm_header(&mp3_header))
{
case MP3_RSP_STOP:
state = MP3_STATE_FINISHED;
return;
case MP3_RSP_BREAK:
state = MP3_STATE_FAILED;
return;
case MP3_RSP_IGNORE: return;
case MP3_RSP_CONTINUE: break;
}
}
#endif
/*
// decode the next frame
*/
if (mp3_decode(mp3_nch) != MP3_SUCCESS)
{
/*
// if more data is needed request it on
// next step
*/
if (mp3_get_last_error() == MP3_ERROR_EOF)
{
state = MP3_STATE_WAITING_FOR_DATA;
return;
}
/*
// if there was a decoding error invoke error
// handler
*/
switch (mp3_sm_error(mp3_get_last_error()))
{
case MP3_RSP_STOP:
state = MP3_STATE_FINISHED;
return;
case MP3_RSP_BREAK:
state = MP3_STATE_FAILED;
return;
case MP3_RSP_IGNORE: break;
case MP3_RSP_CONTINUE:
default: return;
}
}
/*
// synth pcm audio
*/
mp3_synth(mp3_header.no_of_channels, mp3_header.no_of_sb_samples);
/*
// advance state machine
*/
state = MP3_STATE_OUTPUT_READY;
/* no break */
case MP3_STATE_OUTPUT_READY:
/*
// invoke output handler
*/
switch (mp3_sm_output(&mp3_header, mp3_out->pcm_samples))
{
case MP3_RSP_STOP:
state = MP3_STATE_FINISHED;
return;
case MP3_RSP_BREAK:
state = MP3_STATE_FAILED;
return;
case MP3_RSP_IGNORE:
return;
case MP3_RSP_CONTINUE:
default:
state = MP3_STATE_DECODING;
return;
}
/* no break */
case MP3_STATE_FAILED:
/*
// set the result to an error code
*/
mp3_sm_finished(-1);
/*
// set state machine to idle
*/
state = MP3_STATE_IDLE;
/*
// return
*/
return;
case MP3_STATE_FINISHED:
/*
// finished decoding
*/
mp3_sm_finished(0);
/*
// set state machine to idle
*/
state = MP3_STATE_IDLE;
/*
// return
*/
return;
}
}