forked from aroyer-qc/MP3-STM32F746G-DISCO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
1654 lines (1397 loc) · 52.4 KB
/
player.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
******************************************************************************
* @file player.c
* @author MCD Application Team
* @brief This file provides the Audio Out (playback) interface API
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2019 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "player.h"
/* Private define ------------------------------------------------------------*/
/* LCD RK043FN48H : (X*Y):480*272 pixels */
#define TOUCH_NEXT_XMIN 325
#define TOUCH_NEXT_XMAX 365
#define TOUCH_NEXT_YMIN 212
#define TOUCH_NEXT_YMAX 252
#define TOUCH_PREVIOUS_XMIN 250
#define TOUCH_PREVIOUS_XMAX 290
#define TOUCH_PREVIOUS_YMIN 212
#define TOUCH_PREVIOUS_YMAX 252
#define TOUCH_STOP_XMIN 170
#define TOUCH_STOP_XMAX 210
#define TOUCH_STOP_YMIN 212
#define TOUCH_STOP_YMAX 252
#define TOUCH_PAUSE_XMIN 100
#define TOUCH_PAUSE_XMAX 135
#define TOUCH_PAUSE_YMIN 212
#define TOUCH_PAUSE_YMAX 252
#define TOUCH_PAUSE_XSPACE 20
#define TOUCH_VOL_MINUS_XMIN 20
#define TOUCH_VOL_MINUS_XMAX 71
#define TOUCH_VOL_MINUS_YMIN 212
#define TOUCH_VOL_MINUS_YMAX 252
#define TOUCH_VOL_XOFFSET 4
#define TOUCH_VOL_LINE 14
#define TOUCH_VOL_PLUS_XMIN 402
#define TOUCH_VOL_PLUS_XMAX 453
#define TOUCH_VOL_PLUS_YMIN 212
#define TOUCH_VOL_PLUS_YMAX 252
#define TOUCH_EQ1_PLUS_XMIN 330
#define TOUCH_EQ1_PLUS_XMAX 359
#define TOUCH_EQ1_PLUS_YMIN 50
#define TOUCH_EQ1_PLUS_YMAX 100
#define TOUCH_EQ1_MINUS_XMIN 330
#define TOUCH_EQ1_MINUS_XMAX 359
#define TOUCH_EQ1_MINUS_YMIN 110
#define TOUCH_EQ1_MINUS_YMAX 160
#define TOUCH_EQ2_PLUS_XMIN 360
#define TOUCH_EQ2_PLUS_XMAX 389
#define TOUCH_EQ2_PLUS_YMIN 50
#define TOUCH_EQ2_PLUS_YMAX 100
#define TOUCH_EQ2_MINUS_XMIN 360
#define TOUCH_EQ2_MINUS_XMAX 389
#define TOUCH_EQ2_MINUS_YMIN 110
#define TOUCH_EQ2_MINUS_YMAX 160
#define TOUCH_EQ3_PLUS_XMIN 390
#define TOUCH_EQ3_PLUS_XMAX 419
#define TOUCH_EQ3_PLUS_YMIN 50
#define TOUCH_EQ3_PLUS_YMAX 100
#define TOUCH_EQ3_MINUS_XMIN 390
#define TOUCH_EQ3_MINUS_XMAX 419
#define TOUCH_EQ3_MINUS_YMIN 110
#define TOUCH_EQ3_MINUS_YMAX 160
#define TOUCH_EQ4_PLUS_XMIN 420
#define TOUCH_EQ4_PLUS_XMAX 449
#define TOUCH_EQ4_PLUS_YMIN 50
#define TOUCH_EQ4_PLUS_YMAX 100
#define TOUCH_EQ4_MINUS_XMIN 420
#define TOUCH_EQ4_MINUS_XMAX 449
#define TOUCH_EQ4_MINUS_YMIN 110
#define TOUCH_EQ4_MINUS_YMAX 160
#define TOUCH_EQ5_PLUS_XMIN 450
#define TOUCH_EQ5_PLUS_XMAX 479
#define TOUCH_EQ5_PLUS_YMIN 50
#define TOUCH_EQ5_PLUS_YMAX 100
#define TOUCH_EQ5_MINUS_XMIN 450
#define TOUCH_EQ5_MINUS_XMAX 479
#define TOUCH_EQ5_MINUS_YMIN 110
#define TOUCH_EQ5_MINUS_YMAX 160
#define TOUCH_EQ_XOFFSET 4
#define TOUCH_EQ_MINUS_LINE 8
#define TOUCH_EQ_PLUS_LINE 4
#define PLAYER_CLEAR_XWIDTH 250
#define PLAYER_COUNT_TEXT_XMIN 263
#define PLAYER_COUNT_TEXT_YLINE 5
#define MSG_ERR_MOD_YLINE 15
/*SRC module selected*/
#define SELECT_SRC_NONE 0
#define SELECT_SRC_236 1
#define SELECT_SRC_441 2
/* MemPool size */
#define MEMPOOLBUFSZE (3*MAX_OUT_PACKET_SZE)
/* Private macro -------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/*Audio state variable*/
AUDIO_PLAYBACK_CfgTypeDef AudioCfgChange;
/* MemPool: Audio handler variables */
AUDIO_HANDLE_t hAudio; /*instance*/
AUDIO_HANDLE_t *hptr; /*pointer on instance*/
/* data number read */
__IO uint32_t NumberOfData = 0;
uint32_t OutDecPacketSizeBytes = MAX_OUT_PACKET_SZE; /* in bytes */
/* Audio Decoder structure instance when present */
Decoder_TypeDef sDecoderStruct;
uint32_t decsize; /*returned output decoder size in bytes*/
uint32_t DecSampNbBytes = 2; /* 16b: nb bytes per sample */
uint32_t DecNumChannels = 2; /* 2: stereo channels*/
/* In buffer size for MemPool read */
int16_t AudioBufferSize; /* SRC input buffer size in bytes*/
int32_t sizeAvailable; /* MemPool available size in bytes*/
/* Time play information */
uint8_t time[30];
uint8_t time_tot[30];
int EQ[5] = {16,7,5,10,14}; /* default for the equalizer
/*Audio module parameters memory allocation*/
void *pSrc236PersistentMem;
void *pSrc441PersistentMem;
void *pSrcScratchMem;
/*--Debug IT check--*/
uint32_t status_IT1=0; /*loop 1 LowPriority*/
uint32_t counter_IT1stpIT2=0; /*loop 2 HighPriority*/
/* Input Audio File*/
FIL AudioFile;
static uint8_t *FilePath; /*Audio file name */
static char FileExtension =' ';
static int16_t FilePos = 0;
/*Generic Audio Info*/
static Audio_InfoTypeDef AudioFormatData;
static __IO uint32_t uwVolumeRef = 30; /*80*/
static uint32_t PauseStatus = 0;
/* Graphic Player icons*/
static Point PlayPoints[] = {{TOUCH_STOP_XMIN, TOUCH_STOP_YMIN},
{TOUCH_STOP_XMAX, (TOUCH_STOP_YMIN+TOUCH_STOP_YMAX)/2},
{TOUCH_STOP_XMIN, TOUCH_STOP_YMAX}};
static Point NextPoints[] = {{TOUCH_NEXT_XMIN, TOUCH_NEXT_YMIN},
{TOUCH_NEXT_XMAX, (TOUCH_NEXT_YMIN+TOUCH_NEXT_YMAX)/2},
{TOUCH_NEXT_XMIN, TOUCH_NEXT_YMAX}};
static Point PreviousPoints[] = {{TOUCH_PREVIOUS_XMIN, (TOUCH_PREVIOUS_YMIN+TOUCH_PREVIOUS_YMAX)/2},
{TOUCH_PREVIOUS_XMAX, TOUCH_PREVIOUS_YMIN},
{TOUCH_PREVIOUS_XMAX, TOUCH_PREVIOUS_YMAX}};
/* Buffer used to store the audio file header */
static uint8_t tHeaderTmp[MAX_AUDIO_HEADER_SIZE];
/* MemPool Write: In Buffer to read audio file */
static int8_t Buffer1[MAX_OUT_PACKET_SZE];
/* MemPool Read: Out Buffer for Audio module processing */
static uint8_t WavToSrcTmpBuffer[SRC236_FRAME_SIZE_MAX*2*2]; /* SRC_FRAME_SIZE (240>147) * 2 (bytes x sample) * 2 (stereo) */
/*Double BUFFER for Output Audio stream*/
static AUDIO_OUT_BufferTypeDef BufferCtl; /* to the codec */
/* SRC: Sample Rate Converter variables */
static char SRC236_ratio[8][3]={" 2 "," 3 "," 6 ","1_2","1_3","1_6","3_2","2_3"};
static uint8_t SrcTypeSelected; /* 236, 441, None */
static uint8_t SrcIterations;
static uint32_t AudioReadSize; /* nr of bytes to retrieve from USB*/
/*BUFFERs for Audio Module interface*/
static buffer_t InputBuffer;
static buffer_t *pInputBuffer = &InputBuffer;
static buffer_t OutputBuffer;
static buffer_t *pOutputBuffer = &OutputBuffer;
/* Private function prototypes -----------------------------------------------*/
static AUDIO_ErrorTypeDef PlayerGetFileInfo(uint16_t file_idx, Audio_InfoTypeDef *info);
/* init functions */
static uint8_t PlayerStreamInit(uint32_t AudioFreq); /* config SAI output Audio stream*/
static void SWI_EXTI0_Config(void); /* Loop1: EXT1 SWI used to fill MemPool in first decoder loop*/
/* internal DMA IT output Callback processing*/
static AUDIO_ErrorTypeDef AudioOutCallBackProcess(uint16_t offset); /* Loop2: DMA IT callback processing in output SRC loop*/
/* Internal Audio player functions*/
static void PlayerRecorderDisplayButtons(void); /* display player buttons*/
static void AcquireTouchButtons(void); /* acquire player buttons*/
static void PlayerDynamicCfg(void); /* set dynamic parameters*/
static void PlayerDisplaySettings(void); /* display specific audio module settings*/
static void PlayerRecorderDisplayEQ(void);
static AUDIO_ErrorTypeDef MemPoolToSrc(uint16_t offset); /* read MemPool and SRC audio processing*/
/* reset audio player buffers*/
static uint32_t ResetBuffers(void);
static uint32_t PLAYER_Close(void);
static void UpdateTimeInformation(void); /* update the audio file time information*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Initializes Audio Interface.
* @param None
* @retval Audio error
*/
AUDIO_ErrorTypeDef PLAYER_Init(void)
{
uint32_t src_scratch_mem_size;
/* Allocat mem for SRC236 and SRC411 */
pSrc236PersistentMem = malloc(src236_persistent_mem_size); /* 0x1EC: 492 */
pSrc441PersistentMem = malloc(src441_persistent_mem_size); /* 0x0E8: 232 */
if ((pSrc236PersistentMem == NULL) || (pSrc441PersistentMem == NULL))
{
free (pSrc236PersistentMem);
pSrc236PersistentMem = NULL;
free (pSrc441PersistentMem);
pSrc441PersistentMem = NULL;
while(1);
}
if (src236_scratch_mem_size > src441_scratch_mem_size)
{
src_scratch_mem_size = src236_scratch_mem_size; /* 0x784:1924 */
}
else
{
src_scratch_mem_size = src441_scratch_mem_size; /* 0xC9C: 3228*/
}
pSrcScratchMem = malloc(src_scratch_mem_size);
if (pSrcScratchMem == NULL)
{
free (pSrcScratchMem);
pSrcScratchMem = NULL;
while(1);
}
/* MemPool: RAM dynamic memory allocation,
Allocate SRAM memory MemPool structure */
hAudio.pMemPool = (FWK_MEMPOOL_t*) malloc(sizeof(FWK_MEMPOOL_t)); /* 0x10: 16 */
if (hAudio.pMemPool == NULL)
{
free(hAudio.pMemPool);
hAudio.pMemPool = NULL;
while(1);
}
/* Allocate SRAM memory for MemPool Buffer: at least 3 times Max output audio decoder packet */
hAudio.pMemPool->pBuff = (uint8_t*) malloc(MEMPOOLBUFSZE); /* 0x9000: 36864 */
if(hAudio.pMemPool->pBuff == NULL)
{
free(hAudio.pMemPool->pBuff);
hAudio.pMemPool->pBuff = NULL;
while(1);
}
FWK_MEMPOOL_Init(hAudio.pMemPool, MEMPOOLBUFSZE);
/* pointer on audio handler structure*/
hptr = (AUDIO_HANDLE_t*)&hAudio;
/* Configure EXTI in SWI mode: to fill MemPool with Buffer1 from Input Audio file */
SWI_EXTI0_Config();
/* Configure the audio peripherals: as output SAI (I2S mode) audio stream 48kHz to headphones*/
if(BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_AUTO, uwVolumeRef, I2S_AUDIOFREQ_48K) == 0)
{
return AUDIO_ERROR_NONE;
}
else
{
return AUDIO_ERROR_IO;
}
}
/**
* @brief Configures EXTI Line0 in interrupt SWI mode
* used to fill MemPool in first loop with decoded data
* @param None
* @retval None
*/
static void SWI_EXTI0_Config(void)
{
/* Loop1: Unmask EXT line0 IT */
EXTI->IMR = EXTI_IMR_MR0;
/* Enable and set EXTI Line0 Interrupt Preempt-priority: Loop 1 has to be in lower priority (compared to DMA IT loop2 or LCD IT) */
HAL_NVIC_SetPriority(EXTI0_IRQn, 8, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
}
/**
* @brief Starts Audio streaming.
* @param idx: File index
* @retval Audio error
*/
AUDIO_ErrorTypeDef PLAYER_Start(uint8_t idx)
{
AUDIO_ErrorTypeDef error = AUDIO_ERROR_NONE;
AUDIO_ErrorTypeDef error_tmp;
src236_static_param_t src236_static_param;
src441_static_param_t src441_static_param;
uint32_t src_input_frame_size;
uint32_t src_error;
/* Enables and resets CRC-32 from STM32 HW */
__HAL_RCC_CRC_CLK_ENABLE();
CRC->CR = CRC_CR_RESET;
/* Reinit and force close file*/
//PLAYER_Close();
if(AUDIO_GetFilObjectNumber() > idx)
{
BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
BSP_LCD_FillRect(0, LINE(3), BSP_LCD_GetXSize() , LINE(1));
/*-- Select decoder type and Get info from header --*/
error_tmp=PlayerGetFileInfo(idx, &AudioFormatData);
if(error_tmp != AUDIO_ERROR_NONE)
{
return AUDIO_ERROR_CODEC;
}
/* Input stereo audio only */
if(AudioFormatData.NbrChannels != 2)
{
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAtLine(MSG_ERR_MOD_YLINE, (uint8_t*) "Error: only stereo audio input is supported.");
BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
f_close(&AudioFile);
AudioState = AUDIO_STATE_STOP;
return (AUDIO_ERROR_INVALID_VALUE);
}
/*Input frame size to read from MemPool before SRC processing to get 480 samples output*/
switch(AudioFormatData.SampleRate)
{
case 8000:
src236_static_param.src_mode = SRC236_RATIO_6;
SrcTypeSelected = SELECT_SRC_236;
SrcIterations = 1;
src_input_frame_size = AUDIO_OUT_BUFFER_SIZE/(8*SrcIterations*6);
break;
case 16000:
src236_static_param.src_mode = SRC236_RATIO_3;
SrcTypeSelected = SELECT_SRC_236;
SrcIterations = 1;
src_input_frame_size = AUDIO_OUT_BUFFER_SIZE/(8*SrcIterations*3);
break;
case 24000:
src236_static_param.src_mode = SRC236_RATIO_2;
SrcTypeSelected = SELECT_SRC_236;
SrcIterations = 1;
src_input_frame_size = AUDIO_OUT_BUFFER_SIZE/(8*SrcIterations*2);
break;
case 32000:
src236_static_param.src_mode = SRC236_RATIO_3_2;
SrcTypeSelected = SELECT_SRC_236;
SrcIterations = 2; /* frame size smaller but processing repeated 2 times */
src_input_frame_size = AUDIO_OUT_BUFFER_SIZE/(8*SrcIterations*3/2);
break;
case 44100:
/* src441_static_param does not have params to be configured */
SrcTypeSelected = SELECT_SRC_441;
SrcIterations = 3; /* frame size smaller but processing repeated 3 times */
src_input_frame_size = (AUDIO_OUT_BUFFER_SIZE/480)*441/(8*SrcIterations);
break;
case 48000:
SrcTypeSelected = SELECT_SRC_NONE;
SrcIterations = 2; /* frame size smaller but processing repeated 2 times considering SRC236 input req.*/
src_input_frame_size = AUDIO_OUT_BUFFER_SIZE/(8*SrcIterations); /* half buff, stereo, byte x sample */
break;
case 96000:
src236_static_param.src_mode = SRC236_RATIO_1_2;
SrcTypeSelected = SELECT_SRC_236;
SrcIterations = 4; /* frame size smaller but processing repeated 4 times */
src_input_frame_size = AUDIO_OUT_BUFFER_SIZE/(8*SrcIterations*1/2);
break;
default:
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAtLine(MSG_ERR_MOD_YLINE, (uint8_t*) "Error: This sample frequency is not supported.");
BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
f_close(&AudioFile);
AudioState = AUDIO_STATE_STOP;
return (AUDIO_ERROR_INVALID_VALUE);
}
AudioBufferSize = src_input_frame_size*4; /* stereo & byte x sample */
AudioReadSize = AudioBufferSize; /* Buffer size in number of bytes used to read MemPool */
if (SrcTypeSelected == SELECT_SRC_236)
{
/* SRC236 effect reset */
src_error = src236_reset(pSrc236PersistentMem, pSrcScratchMem);
if (src_error != SRC236_ERROR_NONE)
{
return (AUDIO_ERROR_SRC);
}
/* SRC236 effect static parameters setting */
src_error = src236_setParam(&src236_static_param, pSrc236PersistentMem);
if (src_error != SRC236_ERROR_NONE)
{
return (AUDIO_ERROR_SRC);
}
}
if (SrcTypeSelected == SELECT_SRC_441)
{
/* SRC236 effect reset */
src_error = src441_reset(pSrc441PersistentMem, pSrcScratchMem);
if (src_error != SRC441_ERROR_NONE)
{
return (AUDIO_ERROR_SRC);
}
/* SRC236 effect static parameters setting */
src_error = src441_setParam(&src441_static_param, pSrc441PersistentMem);
if (src_error != SRC441_ERROR_NONE)
{
return (AUDIO_ERROR_SRC);
}
}
/* Buffers used for audio module interface after MemPool */
InputBuffer.data_ptr = &WavToSrcTmpBuffer;
InputBuffer.nb_bytes_per_Sample = AudioFormatData.BitPerSample/8; /* 8 bits in 0ne byte */
InputBuffer.nb_channels = AudioFormatData.NbrChannels;
InputBuffer.mode = INTERLEAVED;
InputBuffer.buffer_size = src_input_frame_size; /* in number of samples */
OutputBuffer.nb_bytes_per_Sample = AudioFormatData.BitPerSample/8; /* 8 bits in 0ne byte */
OutputBuffer.nb_channels = AudioFormatData.NbrChannels;
OutputBuffer.mode = INTERLEAVED;
OutputBuffer.buffer_size = AUDIO_OUT_BUFFER_SIZE/8; /* half buff of stereo samples in bytes */
/* Adjust the Audio frequency codec is always 48Khz for this application*/
if (PlayerStreamInit(I2S_AUDIOFREQ_48K) != 0)
{
return AUDIO_ERROR_CODEC;
}
/* Final Output double buffer structure */
BufferCtl.state = BUFFER_OFFSET_NONE;
/*eqv read file pointer on output */
BufferCtl.fptr = 0;
/* WAV format */
if (FileExtension =='V') /*bypass header: only for Audio files WAV format*/
{
/* Get Data from USB Flash Disk and Remove Wave format header */
f_lseek(&AudioFile, 44);
}
/*** Get Data from USB Flash Disk ***/
/*++ Loop1 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/* -- Write MemPool and Fill Buffer1 from Audio file at start
(same code as callback processing of EXT0 line SWI) */
UsbToMemPool();
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/* -- Loop2: Process MemPool and Fill complete double output buffer at first
time */
error = MemPoolToSrc(0);
if ( error == AUDIO_ERROR_NONE)
{
error = MemPoolToSrc(AUDIO_OUT_BUFFER_SIZE/2);
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/* Loop1 (again if new available space) */
UsbToMemPool();
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/* Play complete double output buffer at first time */
if ( error == AUDIO_ERROR_NONE)
{
AudioState = AUDIO_STATE_PLAY;
PlayerRecorderDisplayButtons();
BSP_LCD_DisplayStringAt(PLAYER_COUNT_TEXT_XMIN, LINE(PLAYER_COUNT_TEXT_YLINE+1), (uint8_t *)"[PLAY ]", LEFT_MODE);
if(BufferCtl.fptr != 0)
{ /*DMA stream from output double buffer to codec in Circular mode launch*/
BSP_AUDIO_OUT_Play((uint16_t*)&BufferCtl.buff[0], AUDIO_OUT_BUFFER_SIZE);
// Reset previous EQ setting
BSP_AUDIO_OUT_AdjustEQ(0, EQ[0]);
BSP_AUDIO_OUT_AdjustEQ(1, EQ[1]);
BSP_AUDIO_OUT_AdjustEQ(2, EQ[2]);
BSP_AUDIO_OUT_AdjustEQ(3, EQ[3]);
BSP_AUDIO_OUT_AdjustEQ(4, EQ[4]);
BSP_AUDIO_OUT_SetMute(AUDIO_MUTE_ON);
for(int i = 0; i <uwVolumeRef; i++)
{
BSP_AUDIO_OUT_SetVolume(i);
}
return AUDIO_ERROR_NONE;
}
}
}
return AUDIO_ERROR_IO;
}
/**
* @brief Manages Loop1 Audio process.
* Write MemPool and Fill Buffer1 from Audio file
* @param None
* @retval Audio error
*/
AUDIO_ErrorTypeDef UsbToMemPool(void)
{
/*++ Loop1 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/* -- Write MemPool and Fill Buffer1 from Audio file at start (same code as callback processing of EXT0 line SWI) */
FWK_MEMPOOL_GetAvailableSize(hptr->pMemPool, &sizeAvailable);
while (sizeAvailable >= OutDecPacketSizeBytes)
{
/* Audio Decoder between Audio File (MST) and Buffer1 then MemPool writing */
if (sDecoderStruct.Decoder_DecodeData != NULL) {
DecSampNbBytes=AudioFormatData.BitPerSample/8; /* 2 bytes*/
DecNumChannels= AudioFormatData.NbrChannels; /* stereo*/
/*OutDecPacketSizeBytes: frame packet to decode (in bytes)*/
decsize = sDecoderStruct.Decoder_DecodeData((__IO int16_t*)(&Buffer1[0]), (OutDecPacketSizeBytes/(DecNumChannels*DecSampNbBytes)), NULL);
}
if(decsize != 0)
{
/*write Buffer1 to MemPool*/
FWK_MEMPOOL_Write(hptr->pMemPool, (uint8_t*)Buffer1, decsize); /* input buffer1 in bytes */
}
else /* End of file detected*/
{
/*PLAYER_Stop();
break; */
}
/*update available size in MemPool*/
FWK_MEMPOOL_GetAvailableSize(hptr->pMemPool, &sizeAvailable);
}
return AUDIO_ERROR_NONE;
}
/**
* @brief Manages Audio process.
* @param None
* @retval Audio error
*/
AUDIO_ErrorTypeDef PLAYER_Process(void)
{
AUDIO_ErrorTypeDef audio_error = AUDIO_ERROR_NONE;
uint8_t str[10];
if((AudioState != AUDIO_STATE_PLAY) && (AudioState != AUDIO_STATE_START))
{
BSP_AUDIO_OUT_SetMute(AUDIO_MUTE_OFF);
}
switch(AudioState)
{
/*Audio player state machine */
case AUDIO_STATE_PLAY:
if(AudioFile.fptr >= AudioFormatData.FileSize)
{
BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW);
AudioState = AUDIO_STATE_NEXT;
}
/*--------------------------------------------------------------------*/
/*-- Loop1: Compute sizeAvailable in MemPool and activate SWI for MemPool writing --*/
/* Check if pool state is not full */
if (FWK_MEMPOOL_GetState(hptr->pMemPool) != FWK_MEMPOOL_STATUS_FULL)
{
/* Check if pool state is not empty */
if (FWK_MEMPOOL_GetState(hptr->pMemPool) == FWK_MEMPOOL_STATUS_EMPTY)
{
sizeAvailable = hptr->pMemPool->buffSze; /* Empty case*/
}
else
{
FWK_MEMPOOL_GetAvailableSize(hptr->pMemPool, &sizeAvailable); /* Other */
}
}
else
{
sizeAvailable = 0; /* Full case */
}
/* MemPool writing from Buffer1 filled with audio file (Lower priority) */
/* Callback: Loop while there is enough room in Pool_1 for decoding */
if(sizeAvailable >= OutDecPacketSizeBytes)
{
/* activate SW interrupt on EXT0 line */
EXTI->SWIER|= 0x00000001u; /* EXTI_SWIER_SWIER0_Msk */
}
/*--------------------------------------------------------------------*/
/*Update file elapsed time*/
*time=0; /*elapsed time*/
*time_tot=0; /*duration*/
UpdateTimeInformation();
BSP_LCD_SetTextColor(LCD_COLOR_CYAN);
BSP_LCD_DisplayStringAt(PLAYER_COUNT_TEXT_XMIN, LINE(PLAYER_COUNT_TEXT_YLINE), time, LEFT_MODE);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
/* Update audio state machine according to touch acquisition */
AcquireTouchButtons();
PlayerDynamicCfg();
break;
case AUDIO_STATE_START:
/*reset audio player buffers*/
ResetBuffers();
audio_error = PLAYER_Start(FilePos);
if (audio_error != AUDIO_ERROR_NONE)
{
return audio_error;
}
AudioState = AUDIO_STATE_PLAY;
break;
case AUDIO_STATE_STOP:
if(PauseStatus==1)
{
/* Display blue cyan pause rectangles */
BSP_LCD_SetTextColor(LCD_COLOR_CYAN);
BSP_LCD_FillRect(TOUCH_PAUSE_XMIN, TOUCH_PAUSE_YMIN , 15, TOUCH_PAUSE_YMAX - TOUCH_PAUSE_YMIN);
BSP_LCD_FillRect(TOUCH_PAUSE_XMIN + TOUCH_PAUSE_XSPACE, TOUCH_PAUSE_YMIN, 15, TOUCH_PAUSE_YMAX - TOUCH_PAUSE_YMIN);
PauseStatus=0;
}
BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
BSP_LCD_FillRect(TOUCH_STOP_XMIN, TOUCH_STOP_YMIN , /* Delete Stop rectangle */
TOUCH_STOP_XMAX - TOUCH_STOP_XMIN,
TOUCH_STOP_YMAX - TOUCH_STOP_YMIN);
BSP_LCD_SetTextColor(LCD_COLOR_CYAN);
BSP_LCD_FillPolygon(PlayPoints, 3); /* Play icon */
BSP_LCD_SetTextColor(LCD_COLOR_CYAN);
sprintf((char *)str, "[00:00]");
BSP_LCD_DisplayStringAt(PLAYER_COUNT_TEXT_XMIN, LINE(PLAYER_COUNT_TEXT_YLINE), str, LEFT_MODE);
sprintf((char *)str, "[STOP ]");
BSP_LCD_DisplayStringAt(PLAYER_COUNT_TEXT_XMIN, LINE(PLAYER_COUNT_TEXT_YLINE+1), str, LEFT_MODE);
BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW);
PLAYER_Close();
AudioState = AUDIO_STATE_WAIT;
break;
case AUDIO_STATE_NEXT:
if(++FilePos >= AUDIO_GetFilObjectNumber())
{
FilePos = 0;
}
BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW);
PLAYER_Close();
BSP_LCD_DisplayStringAtLine(15, (uint8_t *)" ");
AudioState = AUDIO_STATE_START;
break;
case AUDIO_STATE_PREVIOUS:
if(--FilePos < 0)
{
FilePos = AUDIO_GetFilObjectNumber() - 1;
}
BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW);
PLAYER_Close();
BSP_LCD_DisplayStringAtLine(15, (uint8_t *)" ");
AudioState = AUDIO_STATE_START;
break;
case AUDIO_STATE_PAUSE:
PauseStatus=1;
BSP_LCD_SetTextColor(LCD_COLOR_CYAN);
BSP_LCD_DisplayStringAt(PLAYER_COUNT_TEXT_XMIN, LINE(PLAYER_COUNT_TEXT_YLINE+1), (uint8_t *)"[PAUSE]", LEFT_MODE);
BSP_LCD_SetTextColor(LCD_COLOR_RED); /* Display red pause rectangles */
BSP_LCD_FillRect(TOUCH_PAUSE_XMIN, TOUCH_PAUSE_YMIN , 15, TOUCH_PAUSE_YMAX - TOUCH_PAUSE_YMIN);
BSP_LCD_FillRect(TOUCH_PAUSE_XMIN + TOUCH_PAUSE_XSPACE, TOUCH_PAUSE_YMIN, 15, TOUCH_PAUSE_YMAX - TOUCH_PAUSE_YMIN);
BSP_AUDIO_OUT_Pause();
AudioState = AUDIO_STATE_WAIT;
break;
case AUDIO_STATE_RESUME:
PauseStatus=0;
BSP_LCD_SetTextColor(LCD_COLOR_CYAN);
BSP_LCD_DisplayStringAt(PLAYER_COUNT_TEXT_XMIN, LINE(PLAYER_COUNT_TEXT_YLINE+1), (uint8_t *)"[PLAY ]", LEFT_MODE);
/* Display blue cyan pause rectangles */
BSP_LCD_FillRect(TOUCH_PAUSE_XMIN, TOUCH_PAUSE_YMIN , 15, TOUCH_PAUSE_YMAX - TOUCH_PAUSE_YMIN);
BSP_LCD_FillRect(TOUCH_PAUSE_XMIN + TOUCH_PAUSE_XSPACE, TOUCH_PAUSE_YMIN, 15, TOUCH_PAUSE_YMAX - TOUCH_PAUSE_YMIN);
BSP_AUDIO_OUT_Resume();
AudioState = AUDIO_STATE_PLAY;
break;
case AUDIO_STATE_INIT:
PlayerRecorderDisplayButtons();
AudioState = AUDIO_STATE_STOP;
case AUDIO_STATE_WAIT:
case AUDIO_STATE_IDLE:
default:
/* Update audio state machine according to touch acquisition */
AcquireTouchButtons();
PlayerDynamicCfg();
break;
}
return audio_error;
}
/**
* @brief Reset All buffers.
* @param None
* @retval None
*/
static uint32_t ResetBuffers(void)
{
uint32_t i =0;
/*reset MemPool buffer*/
/* FWK_MEMPOOL_DeInit(hAudio.pMemPool); */
FWK_MEMPOOL_Init(hAudio.pMemPool,MEMPOOLBUFSZE);
/*reset header buffer*/
for (i = 0; i < MAX_AUDIO_HEADER_SIZE; i++)
{
tHeaderTmp[i] = 0;
}
/*reset output double buffer*/
for (i = 0; i < AUDIO_OUT_BUFFER_SIZE; i++)
{
BufferCtl.buff[i] = 0;
}
return 0;
}
/**
* @brief Stops Audio streaming.
* @param None
* @retval Audio error
*/
AUDIO_ErrorTypeDef PLAYER_Stop(void)
{
for(int i = uwVolumeRef; i >= 0; i--)
{
BSP_AUDIO_OUT_SetVolume(i);
}
AudioState = AUDIO_STATE_STOP;
/*reset files reading list*/
FilePos = 0;
/*Stop the output audio stream*/
BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW);
/* Close the current file */
f_close(&AudioFile);
return AUDIO_ERROR_NONE;
}
/**
* @brief Close Audio decoder instance.
* @param None
* @retval None
* @note: call before Player_Stop then exit.
*/
static uint32_t PLAYER_Close(void)
{
/* Close the decoder instance */
if (sDecoderStruct.DecoderDeInit != NULL)
{
sDecoderStruct.DecoderDeInit();
}
/* Empty the decoder structure */
CODERS_SelectDecoder(&sDecoderStruct, ' ');
/* Close the current file */
f_close(&AudioFile);
return AUDIO_ERROR_NONE;
}
/**
* @brief This function Manages the DMA Transfer interrupts callback processing.
* @param uint16_t offset
* @retval audio_error
*/
static AUDIO_ErrorTypeDef AudioOutCallBackProcess(uint16_t offset)
{
AUDIO_ErrorTypeDef audio_error = AUDIO_ERROR_NONE;
audio_error = MemPoolToSrc(offset);
if (audio_error != AUDIO_ERROR_NONE)
{
return audio_error;
}
BufferCtl.state = BUFFER_OFFSET_NONE;
return audio_error;
}
/**
* @brief Manages the DMA Complete Transfer interrupt
* Calculates the remaining file size and new position of the pointer.
* @param None
* @retval None
*/
void BSP_AUDIO_OUT_TransferComplete_CallBack(void)
{
/* Debug */
if(status_IT1==1) counter_IT1stpIT2++;
if(AudioState == AUDIO_STATE_PLAY)
{
BufferCtl.state = BUFFER_OFFSET_FULL;
AudioOutCallBackProcess(AUDIO_OUT_BUFFER_SIZE/2);
}
}
/**
* @brief Manages the DMA Half Transfer interrupt.
* @param None
* @retval None
*/
void BSP_AUDIO_OUT_HalfTransfer_CallBack(void)
{
/* Debug */
if(status_IT1==1) counter_IT1stpIT2++;
if(AudioState == AUDIO_STATE_PLAY)
{
BufferCtl.state = BUFFER_OFFSET_HALF;
AudioOutCallBackProcess(0);
}
}
/**
* @brief SWI EXTI0 line detection callback.
* Loop1: processing to fill MemPool with Buffer1 after USB key file read
* @retval None
*/
void SWI_EXTI0_Callback(void)
{
/* Debug */
status_IT1=1;
/* Loop1 processing*/
UsbToMemPool();
/* Debug */
status_IT1=0;
}
/*******************************************************************************
Static Functions
*******************************************************************************/
/**
* @brief Gets the file info from header.
* @param file_idx: File index
* @param info: Pointer to Audio file (MP3, WAV)
* @retval Audio error
*/
static AUDIO_ErrorTypeDef PlayerGetFileInfo(uint16_t file_idx, Audio_InfoTypeDef *info)
{
uint32_t bytesread;
uint8_t *header;
uint8_t error_tmp=0;
/*header pointer allocation*/
header=tHeaderTmp;
uint8_t str[FILEMGR_FILE_NAME_SIZE + 20];
/* Open Audio File */
if(f_open(&AudioFile, (char *)FileList.file[file_idx].name, FA_OPEN_EXISTING | FA_READ) != FR_OK)
{
return AUDIO_ERROR_IO;
}
/* Select Audio Decoder with Abs Layer */
FilePath = FileList.file[file_idx].name; /*EXPLE_FILE_NAME */
/* Get the extension of audio file */
FileExtension = FilePath[strlen((char *)FilePath) - 1];
/* ++ Read Header File information ++ */
if(f_read(&AudioFile, header, MAX_AUDIO_HEADER_SIZE, (void *)&bytesread) != FR_OK)
{
f_close(&AudioFile);
return AUDIO_ERROR_IO;
}
/* Inititalize the decoder Abs layer structure instance with matching decoder decoder or Null pointers */
if(CODERS_SelectDecoder(&sDecoderStruct, FileExtension) != 0)
{
return AUDIO_ERROR_CODEC;
}
/* Use the most suitable packet size */
OutDecPacketSizeBytes = sDecoderStruct.PacketSize;
/* Initialization of the decoder */
if (sDecoderStruct.DecoderInit != NULL)
{
error_tmp=sDecoderStruct.DecoderInit((uint8_t*)header,Dec_ReadDataCallback, PlayerSetPosition);
if (error_tmp == 0)
{
/* ++ Fill the "info" structure with audio format parameters after decoding++ */
/* Extract the current sampling rate */
if (sDecoderStruct.Decoder_GetSamplingRate != NULL)
{
info->SampleRate = sDecoderStruct.Decoder_GetSamplingRate();
}
info->FileSize = AudioFile.fsize; /*in bytes*/
/* --MP3 --*/
if (FileExtension=='3')
{
info->NbrChannels = sDecoderStruct.Decoder_GetNbChannels(); /*stereo== 2*/
info->FileFormat ='3'; /*MP3*/
info->AudioFormat ='P'; /*PCM*/
info->BitPerSample =16;
/*After decoding: ((NumberOfFrames * mp3_Info.nSamplesPerFrame))/ NumberOfBytes;*/
info->ByteRate =sDecoderStruct.Decoder_GetBitRateKbps()*1024/8;
}
/*--WAVE--*/
else if ((FileExtension=='v')||(FileExtension=='V'))
{
info->NbrChannels = sDecoderStruct.Decoder_GetNbChannels();