Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "audio_player_app.h"
Private variables
wav_file
AudioEvent
AudioThreadId
Private function prototypes
Private function prototypes
AUDIOPLAYER_Init(uint8_t)
AUDIOPLAYER_GetState()
AUDIOPLAYER_GetVolume()
AUDIOPLAYER_SetVolume(uint32_t)
AUDIOPLAYER_Play(uint32_t)
AUDIOPLAYER_Process()
AUDIOPLAYER_DeInit()
AUDIOPLAYER_Stop()
AUDIOPLAYER_Pause()
AUDIOPLAYER_Resume()
AUDIOPLAYER_SetPosition(uint32_t)
AUDIOPLAYER_Mute(uint8_t)
AUDIOPLAYER_GetFileInfo(char *, WAV_InfoTypedef *)
AUDIOPLAYER_SelectFile(char *)
AUDIOPLAYER_GetProgress()
AUDIO_TransferComplete_CallBack()
AUDIO_HalfTransfer_CallBack()
AUDIO_Error_CallBack()
Audio_Thread(const void *)
Files
loading...
CodeScopeSTM32 Libraries and SamplesSTemWinModules/audio_player/audio_player_app.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file audio_player_app.c * @author MCD Application Team * @brief Audio player application functions ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------*/ #include "audio_player_app.h" /** @addtogroup AUDIO_PLAYER_MODULE * @{ *//* ... */ /** @defgroup AUDIO_APPLICATION * @brief audio application routines * @{ *//* ... */ Includes /* External variables --------------------------------------------------------*/ /* Private typedef -----------------------------------------------------------*/ /* Private defines -----------------------------------------------------------*/ /* Private macros ------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ static FIL wav_file; static osMessageQId AudioEvent = 0; static osThreadId AudioThreadId = 0; Private variables /* Private function prototypes -----------------------------------------------*/ static void Audio_Thread(void const * argument); Private function prototypes /* Private function prototypes -----------------------------------------------*/ static void Audio_Thread(void const * argument); static void AUDIO_TransferComplete_CallBack(void); static void AUDIO_HalfTransfer_CallBack(void); static void AUDIO_Error_CallBack(void); Private function prototypes /* Private functions ---------------------------------------------------------*/ /** * @brief Initializes audio * @param None. * @retval Audio state. *//* ... */ AUDIOPLAYER_ErrorTypdef AUDIOPLAYER_Init(uint8_t volume) { /* Try to Init Audio interface in different config in case of failure */ BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_AUTO, volume, I2S_AUDIOFREQ_48K); /* Initialize internal audio structure */ haudio.out.state = AUDIOPLAYER_STOP; haudio.out.mute = MUTE_OFF; haudio.out.volume = volume; /* Register audio BSP drivers callbacks */ AUDIO_IF_RegisterCallbacks(AUDIO_TransferComplete_CallBack, AUDIO_HalfTransfer_CallBack, AUDIO_Error_CallBack); /* Create Audio Queue */ osMessageQDef(AUDIO_Queue, 1, uint16_t); AudioEvent = osMessageCreate (osMessageQ(AUDIO_Queue), NULL); /* Create Audio task */ osThreadDef(osAudio_Thread, Audio_Thread, osPriorityRealtime, 0, 512); AudioThreadId = osThreadCreate (osThread(osAudio_Thread), NULL); return AUDIOPLAYER_ERROR_NONE; }{ ... } /** * @brief Get audio state * @param None. * @retval Audio state. *//* ... */ AUDIOPLAYER_StateTypdef AUDIOPLAYER_GetState(void) { return haudio.out.state; }{ ... } /** * @brief Get audio volume * @param None. * @retval Audio volume. *//* ... */ uint32_t AUDIOPLAYER_GetVolume(void) { return haudio.out.volume; }{ ... } /** * @brief Set audio volume * @param Volume: Volume level to be set in percentage from 0% to 100% (0 for * Mute and 100 for Max volume level). * @retval Audio state. *//* ... */ AUDIOPLAYER_ErrorTypdef AUDIOPLAYER_SetVolume(uint32_t volume) { if(BSP_AUDIO_OUT_SetVolume(volume) == AUDIO_OK) { haudio.out.volume = volume; return AUDIOPLAYER_ERROR_NONE; }if (BSP_AUDIO_OUT_SetVolume(volume) == AUDIO_OK) { ... } else { return AUDIOPLAYER_ERROR_HW; }else { ... } }{ ... } /** * @brief Play audio stream * @param frequency: Audio frequency used to play the audio stream. * @retval Audio state. *//* ... */ AUDIOPLAYER_ErrorTypdef AUDIOPLAYER_Play(uint32_t frequency) { uint32_t numOfReadBytes; haudio.out.state = AUDIOPLAYER_PLAY; /* Fill whole buffer @ first time */ if(f_read(&wav_file, &haudio.buff[0], AUDIO_OUT_BUFFER_SIZE, (void *)&numOfReadBytes) == FR_OK) { if(numOfReadBytes != 0) { BSP_AUDIO_OUT_Pause(); BSP_AUDIO_OUT_SetFrequency(frequency); osThreadResume(AudioThreadId); BSP_AUDIO_OUT_Play((uint16_t*)&haudio.buff[0], AUDIO_OUT_BUFFER_SIZE); return AUDIOPLAYER_ERROR_NONE; }if (numOfReadBytes != 0) { ... } }if (f_read(&wav_file, &haudio.buff[0], AUDIO_OUT_BUFFER_SIZE, (void *)&numOfReadBytes) == FR_OK) { ... } return AUDIOPLAYER_ERROR_IO; }{ ... } /** * @brief Audio player process * @param None. * @retval Audio state. *//* ... */ AUDIOPLAYER_ErrorTypdef AUDIOPLAYER_Process(void) { switch(haudio.out.state) { case AUDIOPLAYER_START: haudio.out.state = AUDIOPLAYER_PLAY; break; case AUDIOPLAYER_START: case AUDIOPLAYER_EOF: AUDIOPLAYER_NotifyEndOfFile(); break; case AUDIOPLAYER_EOF: case AUDIOPLAYER_ERROR: AUDIOPLAYER_Stop(); break; case AUDIOPLAYER_ERROR: case AUDIOPLAYER_STOP: case AUDIOPLAYER_PLAY: default: break;default }switch (haudio.out.state) { ... } return AUDIOPLAYER_ERROR_NONE; }{ ... } /** * @brief Audio player DeInit * @param None. * @retval Audio state. *//* ... */ AUDIOPLAYER_ErrorTypdef AUDIOPLAYER_DeInit(void) { RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; haudio.out.state = AUDIOPLAYER_STOP; /* USB Clock recovering */ /* Select PLLI2S output as USB clock source */ PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_CK48; PeriphClkInitStruct.Clk48ClockSelection = RCC_CK48CLKSOURCE_PLLI2SQ; PeriphClkInitStruct.PLLI2S.PLLI2SM = 8; PeriphClkInitStruct.PLLI2S.PLLI2SN = 192; PeriphClkInitStruct.PLLI2S.PLLI2SQ = 4; PeriphClkInitStruct.PLLI2S.PLLI2SR = 4; if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { while(1); }if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { ... } BSP_AUDIO_OUT_Stop(CODEC_PDWN_HW); BSP_AUDIO_OUT_DeInit(); f_close(&wav_file); if(AudioEvent != 0) { vQueueDelete(AudioEvent); AudioEvent = 0; }if (AudioEvent != 0) { ... } if(AudioThreadId != 0) { osThreadTerminate(AudioThreadId); AudioThreadId = 0; }if (AudioThreadId != 0) { ... } return AUDIOPLAYER_ERROR_NONE; }{ ... } /** * @brief Stop audio stream. * @param None. * @retval Audio state. *//* ... */ AUDIOPLAYER_ErrorTypdef AUDIOPLAYER_Stop(void) { BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW); haudio.out.state = AUDIOPLAYER_STOP; f_close(&wav_file); if(AudioThreadId != 0) { osThreadSuspend(AudioThreadId); }if (AudioThreadId != 0) { ... } return AUDIOPLAYER_ERROR_NONE; }{ ... } /** * @brief Pause Audio stream * @param None. * @retval Audio state. *//* ... */ AUDIOPLAYER_ErrorTypdef AUDIOPLAYER_Pause(void) { if(AudioThreadId != 0) { osThreadSuspend(AudioThreadId); }if (AudioThreadId != 0) { ... } haudio.out.state = AUDIOPLAYER_PAUSE; BSP_AUDIO_OUT_Pause(); return AUDIOPLAYER_ERROR_NONE; }{ ... } /** * @brief Resume Audio stream * @param None. * @retval Audio state. *//* ... */ AUDIOPLAYER_ErrorTypdef AUDIOPLAYER_Resume(void) { if(AudioThreadId != 0) { osThreadResume(AudioThreadId); }if (AudioThreadId != 0) { ... } haudio.out.state = AUDIOPLAYER_PLAY; BSP_AUDIO_OUT_Resume(); return AUDIOPLAYER_ERROR_NONE; }{ ... } /** * @brief Sets audio stream position * @param position: stream position. * @retval Audio state. *//* ... */ AUDIOPLAYER_ErrorTypdef AUDIOPLAYER_SetPosition(uint32_t position) { long file_pos; file_pos = f_size(&wav_file) / AUDIO_OUT_BUFFER_SIZE / 100; file_pos *= (position * AUDIO_OUT_BUFFER_SIZE); AUDIOPLAYER_Pause(); f_lseek(&wav_file, file_pos); AUDIOPLAYER_Resume(); return AUDIOPLAYER_ERROR_NONE; }{ ... } /** * @brief Sets the volume at mute * @param state: could be MUTE_ON to mute sound or MUTE_OFF to unmute * the codec and restore previous volume level. * @retval Audio state. *//* ... */ AUDIOPLAYER_ErrorTypdef AUDIOPLAYER_Mute(uint8_t state) { BSP_AUDIO_OUT_SetMute(state); return AUDIOPLAYER_ERROR_NONE; }{ ... } /** * @brief Get the wav file information. * @param file: wav file. * @param info: pointer to wav file structure * @retval Audio state. *//* ... */ AUDIOPLAYER_ErrorTypdef AUDIOPLAYER_GetFileInfo(char* file, WAV_InfoTypedef* info) { uint32_t numOfReadBytes; AUDIOPLAYER_ErrorTypdef ret = AUDIOPLAYER_ERROR_IO; FIL fsfile; if( f_open(&fsfile, file, FA_OPEN_EXISTING | FA_READ) == FR_OK) { /* Fill the buffer to Send */ if(f_read(&fsfile, info, sizeof(WAV_InfoTypedef), (void *)&numOfReadBytes) == FR_OK) { if((info->ChunkID == 0x46464952) && (info->AudioFormat == 1)) { ret = AUDIOPLAYER_ERROR_NONE; }if ((info->ChunkID == 0x46464952) && (info->AudioFormat == 1)) { ... } }if (f_read(&fsfile, info, sizeof(WAV_InfoTypedef), (void *)&numOfReadBytes) == FR_OK) { ... } f_close(&fsfile); }if (f_open(&fsfile, file, FA_OPEN_EXISTING | FA_READ) == FR_OK) { ... } return ret; }{ ... } /** * @brief Select wav file. * @param file: wav file. * @retval Audio state. *//* ... */ AUDIOPLAYER_ErrorTypdef AUDIOPLAYER_SelectFile(char* file) { AUDIOPLAYER_ErrorTypdef ret = AUDIOPLAYER_ERROR_IO; if( f_open(&wav_file, file, FA_OPEN_EXISTING | FA_READ) == FR_OK) { f_lseek(&wav_file, sizeof(WAV_InfoTypedef)); ret = AUDIOPLAYER_ERROR_NONE; }if (f_open(&wav_file, file, FA_OPEN_EXISTING | FA_READ) == FR_OK) { ... } return ret; }{ ... } /** * @brief Get wav file progress. * @param None * @retval file progress. *//* ... */ uint32_t AUDIOPLAYER_GetProgress(void) { return (wav_file.fptr); }{ ... } /** * @brief Manages the DMA Transfer complete interrupt. * @param None * @retval None *//* ... */ static void AUDIO_TransferComplete_CallBack(void) { if(haudio.out.state == AUDIOPLAYER_PLAY) { BSP_AUDIO_OUT_ChangeBuffer((uint16_t*)&haudio.buff[0], AUDIO_OUT_BUFFER_SIZE /2); osMessagePut ( AudioEvent, PLAY_BUFFER_OFFSET_FULL, 0); }if (haudio.out.state == AUDIOPLAYER_PLAY) { ... } }{ ... } /** * @brief Manages the DMA Half Transfer complete interrupt. * @param None * @retval None *//* ... */ static void AUDIO_HalfTransfer_CallBack(void) { if(haudio.out.state == AUDIOPLAYER_PLAY) { BSP_AUDIO_OUT_ChangeBuffer((uint16_t*)&haudio.buff[AUDIO_OUT_BUFFER_SIZE /2], AUDIO_OUT_BUFFER_SIZE /2); osMessagePut ( AudioEvent, PLAY_BUFFER_OFFSET_HALF, 0); }if (haudio.out.state == AUDIOPLAYER_PLAY) { ... } }{ ... } /** * @brief Manages the DMA FIFO error interrupt. * @param None * @retval None *//* ... */ static void AUDIO_Error_CallBack(void) { haudio.out.state = AUDIOPLAYER_ERROR; }{ ... } /** * @brief Audio task * @param argument: pointer that is passed to the thread function as start argument. * @retval None *//* ... */ static void Audio_Thread(void const * argument) { uint32_t numOfReadBytes; osEvent event; for(;;) { event = osMessageGet(AudioEvent, 100 ); if( event.status == osEventMessage ) { if(haudio.out.state == AUDIOPLAYER_PLAY) { switch(event.value.v) { case PLAY_BUFFER_OFFSET_HALF: if(f_read(&wav_file, &haudio.buff[0], AUDIO_OUT_BUFFER_SIZE/2, (void *)&numOfReadBytes) == FR_OK) { if(numOfReadBytes == 0) { haudio.out.state = AUDIOPLAYER_EOF; }if (numOfReadBytes == 0) { ... } }if (f_read(&wav_file, &haudio.buff[0], AUDIO_OUT_BUFFER_SIZE/2, (void *)&numOfReadBytes) == FR_OK) { ... } else { haudio.out.state = AUDIOPLAYER_ERROR; }else { ... } break; case PLAY_BUFFER_OFFSET_HALF: case PLAY_BUFFER_OFFSET_FULL: if(f_read(&wav_file, &haudio.buff[AUDIO_OUT_BUFFER_SIZE/2], AUDIO_OUT_BUFFER_SIZE/2, (void *)&numOfReadBytes) == FR_OK) { if(numOfReadBytes == 0) { haudio.out.state = AUDIOPLAYER_EOF; }if (numOfReadBytes == 0) { ... } }if (f_read(&wav_file, &haudio.buff[AUDIO_OUT_BUFFER_SIZE/2], AUDIO_OUT_BUFFER_SIZE/2, (void *)&numOfReadBytes) == FR_OK) { ... } else { haudio.out.state = AUDIOPLAYER_ERROR; }else { ... } break; case PLAY_BUFFER_OFFSET_FULL: default: break;default }switch (event.value.v) { ... } }if (haudio.out.state == AUDIOPLAYER_PLAY) { ... } }if (event.status == osEventMessage) { ... } }for (;;) { ... } }{ ... } /** * @} *//* ... */ /** * @} *//* ... */