-
Notifications
You must be signed in to change notification settings - Fork 0
/
audioplay.c
413 lines (352 loc) · 9.67 KB
/
audioplay.c
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Smart Toybox Audio playback
//
// Copyright(C) 2016. Nebojsa Sumrak and Jelena (Petra) Markovic
//
// 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 2 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, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301 USA.
// Hardware & driverlib library includes
#include "rom.h"
#include "rom_map.h"
#include "utils.h"
#include "hw_ints.h"
// Demo app includes
#include "network.h"
#include "common.h"
#include "debug.h"
#include "files.h"
#include "audioplay.h"
#include "circ_buff.h"
#include "audiohw.h"
#include "wp/wavpack.h"
#include "STBreader.h"
#define MAX_MUSIC_SIZE 63
struct audio_fileinput {
int infile;
unsigned offset;
int filelen;
char music[MAX_MUSIC_SIZE];
uchar inmusic;
uchar pause;
uchar repeat[3];
WavpackContext wpc;
read_stream rsf;
};
static struct audio_fileinput fileleft;
static struct audio_fileinput fileright;
inline int32_t read_bytes(struct audio_fileinput *fi, void *buff, int32_t bcount)
{
int32_t fleft = fi->filelen - fi->offset;
if(fleft<=0) {
file_close(fi->infile);
fi->infile = -1;
return -1;
}
if(fleft<bcount) bcount = fleft;
if (fi->infile == -1) return 0;
int32_t read = file_read(fi->infile, fi->offset, buff, bcount);
if (read < bcount) {
file_close(fi->infile);
fi->infile = -1;
} else {
fi->offset += read;
// UART_PRINT("read = %d new offset = %d\n\r", read, fi->offset);
}
return read;
}
static int32_t read_bytes_left(void *buff, int32_t bcount)
{
return read_bytes(&fileleft, buff, bcount);
}
static int32_t read_bytes_right(void *buff, int32_t bcount)
{
return read_bytes(&fileright, buff, bcount);
}
void audioplay_init()
{
CLEAR(fileleft);
CLEAR(fileright);
fileleft.infile = fileright.infile = -1;
fileleft.rsf = read_bytes_left;
fileright.rsf = read_bytes_right;
}
inline void do_rewind(char cto, struct audio_fileinput *fi, int type)
{
if(fi->music[fi->inmusic+1] && (++fi->repeat[type]) == fi->music[fi->inmusic+1]-'0') {
fi->inmusic += 2;
fi->repeat[type] = 0;
return;
}
while(fi->inmusic && fi->music[--fi->inmusic] != cto) ;
fi->inmusic++;
}
static int play_next_file(struct audio_fileinput *fi)
{
while(1)
switch(fi->music[fi->inmusic]) {
case 0:
fi->music[(fi->inmusic = 0)] = 0;
return -1;
// rewind
case '{':
case '[':
case '(':
fi->inmusic++;
continue;
case '}':
do_rewind('{',fi,0);
continue;
case ']':
do_rewind('[',fi,1);
continue;
case ')':
do_rewind('(',fi,2);
continue;
// pause
case ',':
fi->pause = 2;
fi->inmusic++;
return 0;
case ';':
fi->pause = 4;
fi->inmusic++;
return 0;
case '.':
fi->pause = 8;
fi->inmusic++;
return 0;
case '\'':
fi->pause = 16;
fi->inmusic++;
return 0;
case '"':
fi->pause = 31;
fi->inmusic++;
return 0;
default:
fi->infile = file_open(fi->music[fi->inmusic++], &fi->filelen);
if (fi->infile == -1) return 1;
fi->offset = 0;
if (WavpackOpenFileInput(&fi->wpc, fi->rsf)) return 2;
return 0;
}
}
// channels - 1 (left), 2 (right) or 3 (both)
void audioplay_fadeout(unsigned channels)
{
ASSERT(channels>0 && channels<4);
int filled = circbuff_get_filled();
if(filled < (HALF_WORD_SIZE * CB_TRANSFER_SZ)*2) {
UART_PRINT("fadeout: buff filled %d\n\r",filled);
return;
}
int toprocess = (filled - (HALF_WORD_SIZE * CB_TRANSFER_SZ)) >> 2; // get number of samples of left/right channel (minus i2s current transfer)
UART_PRINT("num samples to fade out: %d\n\r",toprocess);
int inpass = toprocess / 98;
int now = inpass;
int curval = 99;
int buffer = CB_TRANSFER_SZ / 2; // read i2s chunks (size is long)
int boffset = (HALF_WORD_SIZE * CB_TRANSFER_SZ); // set to not disturb i2s
unsigned char *buff = circbuff_get_read_ptr_offset(boffset);
for(; toprocess; toprocess--)
{
short el;
if(channels & 1) {
el = ((signed)buff[1] << 8) + buff[0];
el = (((int)el) * curval) / 100;
*buff++ = el;
*buff++ = el >> 8;
} else buff += 2;
if(channels & 2) {
el = ((signed)buff[1] << 8) + buff[0];
el = (((int)el) * curval) / 100;
*buff++ = el;
*buff++ = el >> 8;
} else buff += 2;
if(!(--now)) {
now = inpass;
if(curval) curval--;
}
if(!(--buffer)) {
buffer = CB_TRANSFER_SZ / 2;
buff = circbuff_get_read_ptr_offset((boffset += CB_TRANSFER_SZ * 2));
}
}
}
void audioplay_process()
{
while(circbuff_get_filled() <= PACKET_SIZE * (PLAY_SIZE-2)) {
// unsigned long now = time_get();
audioplay_getbuffer(circbuff_get_write_ptr());
circbuff_update_write_ptr(PACKET_SIZE);
// unsigned long elapse = time_get()-now;
// if(elapse>10) UART_PRINT("el %d\n\r", elapse);
}
}
inline void audioplay_dostop(struct audio_fileinput *fi)
{
if (fi->infile != -1) {
file_close(fi->infile);
fi->infile = -1;
}
if(fi->music[fi->inmusic]) audioplay_fadeout(fi==&fileleft ? 1 : 2);
fi->pause = 0;
CLEAR(fi->repeat);
fi->music[(fi->inmusic = 0)] = 0;
}
void audioplay_stop_all()
{
audioplay_fadeout(3);
fileleft.music[(fileleft.inmusic = 0)] = 0;
fileright.music[(fileright.inmusic = 0)] = 0;
audioplay_dostop(&fileleft);
audioplay_dostop(&fileright);
}
void audioplay_stop(int channel)
{
audioplay_dostop(channel ? &fileright : &fileleft);
}
int audioplay_playfile(int channel, const char *music)
{
struct audio_fileinput *fi = channel ? &fileright : &fileleft;
audioplay_dostop(fi);
strcpy(fi->music, music);
return play_next_file(fi);
}
int audioplay_appendmusic(int channel, unsigned char type, int num)
{
struct audio_fileinput *fi = channel ? &fileright : &fileleft;
int len = strlen(fi->music);
if(!len) return audioplay_playmusic(channel, type, num);
char c;
if((c=fi->music[len-1])=='}' || c==']' || c==')')
fi->music[len++] = '1'; // break infinite loop
stb_get_music(fi->music+len, type, num);
return 0;
}
int audioplay_playmusic(int channel, unsigned char type, int num)
{
struct audio_fileinput *fi = channel ? &fileright : &fileleft;
audioplay_dostop(fi);
stb_get_music(fi->music, type, num);
return play_next_file(fi);
}
// Reformat samples from longs in processor's native endian mode to
// little-endian data with 2-pass channel muxing.
static void convert2stereo(int32_t *input, uchar *output, int scount)
{
for (; scount; scount--)
{
int32_t temp = *(input++);
*(output++) = (uchar)temp;
*(output++) = (uchar)(temp >> 8);
output += 2; // skip channel, will be filled in next pass (or is already filled)
}
}
#ifdef AUDIO_SOFTWARE_MONO
// Reformat samples from longs in processor's native endian mode to
// little-endian data muxing with with 2-pass channel muxing.
static void mix2mono(int32_t *input1, int32_t *input2, uchar *output, int scount)
{
for (; scount; scount--)
{
int32_t a = *(input1++);
int32_t b = *(input2++);//((signed)*((unsigned short*)output++)) | (*((signed short *)output++) << 8);
int32_t m = a+b;
#if (AUDIO_SOFTWARE_MONO == 1)
a += 32768;
b += 32768;
m = ((a < 32768) && (b < 32768)) ? ((a * b) >> 15) : (((a + b) << 1) - ((a * b) >> 15) - 65536);
if (m == 65536) m = 32767;
else m -= 32768;
#else
if(m>32767) m = 32767;
else if(m<-32768) m = -32768;
#endif
*(output++) = 0;
*(output++) = 0;
*(output++) = (uchar)m;
*(output++) = (uchar)(m >> 8); // put mix into left channel
}
}
#else
static void fill_pause2stereo(uchar *output, int scount)
{
for (; scount; scount--)
{
*(output++) = *(output++) = 0;
output += 2; // skip channel, will be filled in next pass (or is already filled)
}
}
#endif
static int filecheck(struct audio_fileinput *fi)
{
if(fi->infile != -1) return 1;
if(play_next_file(fi)) return 0;
if(fi->pause) {
fi->pause--;
return 0;
}
return 1;
}
void audioplay_getbuffer(uchar *buffer) // sample count = 256*16bit*stereo = 1024 bytes
{
uint32_t samples_unpacked=0;
#ifdef AUDIO_SOFTWARE_MONO
static int32_t temp_buffer1[256], temp_buffer2[256];
#else
static int32_t temp_buffer[256];
#define temp_buffer1 temp_buffer
#define temp_buffer2 temp_buffer
#endif
leftagain:
if(fileleft.pause) {
#ifdef AUDIO_SOFTWARE_MONO
memset(temp_buffer2, 0, 1024);
#else
fill_pause2stereo(buffer, 256);
#endif
fileleft.pause--;
} else {
if (filecheck(&fileleft)) {
samples_unpacked += WavpackUnpackSamples(&fileleft.wpc, temp_buffer1+samples_unpacked, 256-samples_unpacked);
if (!samples_unpacked && fileleft.infile != -1) goto leftagain;
if (samples_unpacked < 256) goto leftagain;
} else
memset(temp_buffer1 + samples_unpacked, 0, (256 - samples_unpacked) << 2);
convert2stereo(temp_buffer1, buffer, 256);
}
samples_unpacked = 0;
rightagain:
if(fileright.pause) {
#ifdef AUDIO_SOFTWARE_MONO
memset(temp_buffer2, 0, 1024);
mix2mono(temp_buffer1, temp_buffer2, buffer, 256);
#else
fill_pause2stereo(buffer+2, 256);
#endif
fileright.pause--;
} else {
if (filecheck(&fileright)) {
samples_unpacked += WavpackUnpackSamples(&fileright.wpc, temp_buffer2+samples_unpacked, 256-samples_unpacked);
if (!samples_unpacked && fileright.infile != -1) goto rightagain;
if (samples_unpacked < 256) goto rightagain;
} else
memset(temp_buffer2 + samples_unpacked, 0, (256 - samples_unpacked) << 2);
#ifdef AUDIO_SOFTWARE_MONO
mix2mono(temp_buffer1, temp_buffer2, buffer, 256);
#else
convert2stereo(temp_buffer2, buffer+2, 256);
#endif
}
}