Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "main.h"
Private define
#define AUDIO_BUFFER_SIZE
Private variables
LEDsState
AudioPlayStart
WaveDataLength
AudioRemSize
Audio_Buffer
BufferOffset
Volume
FileRead
Directory
WavePlayBack(uint32_t)
WavePlayerPauseResume(uint32_t)
WavePlayerStop()
WavePlayerInit(uint32_t)
BSP_AUDIO_OUT_HalfTransfer_CallBack()
BSP_AUDIO_OUT_TransferComplete_CallBack()
BSP_AUDIO_OUT_Error_CallBack()
WavePlayerStart()
WavePlayer_CallBack()
Files
loading...
CodeScopeSTM32 Libraries and SamplesAudio_playback_and_recordSrc/waveplayer.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file Audio_playback_and_record/Src/waveplayer.c * @author MCD Application Team * @brief I2S Audio player program. ****************************************************************************** * @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 "main.h" /** @addtogroup STM32F4-Discovery_Audio_Player_Recorder * @{ *//* ... */ Includes /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ #define AUDIO_BUFFER_SIZE 4096 Private define /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* LED State (Toggle or OFF)*/ __IO uint32_t LEDsState; extern __IO uint32_t RepeatState, PauseResumeStatus, PressCount; /* Audio Play Start variable. Defined as external in main.c*//* ... */ __IO uint32_t AudioPlayStart = 0; /* Audio wave data length to be played */ static uint32_t WaveDataLength = 0; /* Audio wave remaining data length to be played */ static __IO uint32_t AudioRemSize = 0; /* Ping-Pong buffer used for audio play */ uint8_t Audio_Buffer[AUDIO_BUFFER_SIZE]; /* Position in the audio play buffer */ __IO BUFFER_StateTypeDef BufferOffset = BUFFER_OFFSET_NONE; /* Initial Volume level (from 0 (Mute) to 100 (Max)) */ static uint8_t Volume = 70; /* Variable used to indicate audio mode (play, record or stop). */ /* Defined in main.c */ extern __IO uint32_t CmdIndex; /* Variable used by FatFs*/ FIL FileRead; DIR Directory; /* Variable used to switch play from audio sample available on USB to recorded file. */ /* Defined in waverecorder.c */ extern uint32_t WaveRecStatus; /* Variable to indicate USB state (start/idle) */ /* Defined in main.c */ extern MSC_ApplicationTypeDef AppliState; Private variables /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ /** * @brief Plays Wave from a mass storage. * @param AudioFreq: Audio Sampling Frequency * @retval None *//* ... */ void WavePlayBack(uint32_t AudioFreq) { UINT bytesread = 0; /* Start playing */ AudioPlayStart = 1; RepeatState = REPEAT_ON; /* Initialize Wave player (Codec, DMA, I2C) */ if(WavePlayerInit(AudioFreq) != 0) { Error_Handler(); }if (WavePlayerInit(AudioFreq) != 0) { ... } /* Get Data from USB Flash Disk */ f_lseek(&FileRead, 0); f_read (&FileRead, &Audio_Buffer[0], AUDIO_BUFFER_SIZE, &bytesread); AudioRemSize = WaveDataLength - bytesread; /* Start playing Wave */ BSP_AUDIO_OUT_Play((uint16_t*)&Audio_Buffer[0], AUDIO_BUFFER_SIZE); LEDsState = LED6_TOGGLE; PauseResumeStatus = RESUME_STATUS; PressCount = 0; /* Check if the device is connected.*/ while((AudioRemSize != 0) && (AppliState != APPLICATION_IDLE)) { /* Test on the command: Playing */ if(CmdIndex == CMD_PLAY) { if(PauseResumeStatus == PAUSE_STATUS) { /* Stop Toggling LED2 to signal Pause */ LEDsState = STOP_TOGGLE; /* Pause playing Wave */ WavePlayerPauseResume(PauseResumeStatus); PauseResumeStatus = IDLE_STATUS; }if (PauseResumeStatus == PAUSE_STATUS) { ... } else if(PauseResumeStatus == RESUME_STATUS) { /* Toggling LED6 to signal Play */ LEDsState = LED6_TOGGLE; /* Resume playing Wave */ WavePlayerPauseResume(PauseResumeStatus); PauseResumeStatus = IDLE_STATUS; }else if (PauseResumeStatus == RESUME_STATUS) { ... } bytesread = 0; if(BufferOffset == BUFFER_OFFSET_HALF) { f_read(&FileRead, &Audio_Buffer[0], AUDIO_BUFFER_SIZE/2, (void *)&bytesread); BufferOffset = BUFFER_OFFSET_NONE; }if (BufferOffset == BUFFER_OFFSET_HALF) { ... } if(BufferOffset == BUFFER_OFFSET_FULL) { f_read(&FileRead, &Audio_Buffer[AUDIO_BUFFER_SIZE/2], AUDIO_BUFFER_SIZE/2, (void *)&bytesread); BufferOffset = BUFFER_OFFSET_NONE; }if (BufferOffset == BUFFER_OFFSET_FULL) { ... } if(AudioRemSize > (AUDIO_BUFFER_SIZE / 2)) { AudioRemSize -= bytesread; }if (AudioRemSize > (AUDIO_BUFFER_SIZE / 2)) { ... } else { AudioRemSize = 0; }else { ... } }if (CmdIndex == CMD_PLAY) { ... } else { /* Stop playing Wave */ WavePlayerStop(); f_close(&FileRead); AudioRemSize = 0; RepeatState = REPEAT_ON; break; }else { ... } }while ((AudioRemSize != 0) && (AppliState != APPLICATION_IDLE)) { ... } #ifdef PLAY_REPEAT_DISABLED RepeatState = REPEAT_OFF; /* Stop playing Wave */ WavePlayerStop(); f_close(&FileRead); /* Test on the command: Playing */ if(CmdIndex == CMD_PLAY) { LEDsState = LED4_TOGGLE; }if (CmdIndex == CMD_PLAY) { ... } /* ... */#else LEDsState = LEDS_OFF; RepeatState = REPEAT_ON; AudioPlayStart = 0; /* Stop playing Wave */ WavePlayerStop(); f_close(&FileRead);/* ... */ #endif /* PLAY_REPEAT_DISABLED */ }{ ... } /** * @brief Pauses or Resumes a played Wave. * @param state: Player state: Pause, Resume or Idle * @retval None *//* ... */ void WavePlayerPauseResume(uint32_t wState) { if(wState == PAUSE_STATUS) { BSP_AUDIO_OUT_Pause(); }if (wState == PAUSE_STATUS) { ... } else { BSP_AUDIO_OUT_Resume(); }else { ... } }{ ... } /** * @brief Stops playing Wave. * @param None * @retval None *//* ... */ void WavePlayerStop(void) { BSP_AUDIO_OUT_Stop(CODEC_PDWN_HW); }{ ... } /** * @brief Initializes the Wave player. * @param AudioFreq: Audio sampling frequency * @retval None *//* ... */ int WavePlayerInit(uint32_t AudioFreq) { /* MEMS Accelerometer configure to manage PAUSE, RESUME operations */ BSP_ACCELERO_Click_ITConfig(); /* Initialize the Audio codec and all related peripherals (I2S, I2C, IOExpander, IOs...) */ return(BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_AUTO, Volume, AudioFreq)); }{ ... } /*-------------------------------- Callbacks implementation: The callbacks prototypes are defined in the stm32f4_discovery_audio_codec.h file and their implementation should be done in the user code if they are needed. Below some examples of callback implementations. --------------------------------------------------------*//* ... */ /** * @brief Manages the DMA Half Transfer complete interrupt. * @param None * @retval None *//* ... */ void BSP_AUDIO_OUT_HalfTransfer_CallBack(void) { BufferOffset = BUFFER_OFFSET_HALF; }{ ... } /** * @brief Calculates the remaining file size and new position of the pointer. * @param None * @retval None *//* ... */ void BSP_AUDIO_OUT_TransferComplete_CallBack(void) { BufferOffset = BUFFER_OFFSET_FULL; BSP_AUDIO_OUT_ChangeBuffer((uint16_t*)&Audio_Buffer[0], AUDIO_BUFFER_SIZE /2); }{ ... } /** * @brief Manages the DMA FIFO error interrupt. * @param None * @retval None *//* ... */ void BSP_AUDIO_OUT_Error_CallBack(void) { /* Stop the program with an infinite loop */ while (1) {}while (1) { ... } /* Could also generate a system reset to recover from the error */ /* .... */ }{ ... } /** * @brief Starts Wave player. * @param None * @retval None *//* ... */ void WavePlayerStart(void) { UINT bytesread = 0; char path[] = "0:/"; char* wavefilename = NULL; WAVE_FormatTypeDef waveformat; /* Get the read out protection status */ if(f_opendir(&Directory, path) == FR_OK) { if(WaveRecStatus == 1) { wavefilename = REC_WAVE_NAME; }if (WaveRecStatus == 1) { ... } else { wavefilename = WAVE_NAME; }else { ... } /* Open the Wave file to be played */ if(f_open(&FileRead, wavefilename , FA_READ) != FR_OK) { BSP_LED_On(LED5); CmdIndex = CMD_RECORD; }if (f_open(&FileRead, wavefilename , FA_READ) != FR_OK) { ... } else { /* Read sizeof(WaveFormat) from the selected file */ f_read (&FileRead, &waveformat, sizeof(waveformat), &bytesread); /* Set WaveDataLenght to the Speech Wave length */ WaveDataLength = waveformat.FileSize; /* Play the Wave */ WavePlayBack(waveformat.SampleRate); }else { ... } }if (f_opendir(&Directory, path) == FR_OK) { ... } }{ ... } /** * @brief Resets the Wave player. * @param None * @retval None *//* ... */ void WavePlayer_CallBack(void) { if(AppliState != APPLICATION_IDLE) { /* Reset the Wave player variables */ RepeatState = REPEAT_ON; AudioPlayStart = 0; LEDsState = LEDS_OFF; PauseResumeStatus = RESUME_STATUS; WaveDataLength =0; PressCount = 0; /* Stop the Codec */ if(BSP_AUDIO_OUT_Stop(CODEC_PDWN_HW) != AUDIO_OK) { while(1){}; }if (BSP_AUDIO_OUT_Stop(CODEC_PDWN_HW) != AUDIO_OK) { ... } /* Turn OFF LED3, LED4 and LED6 */ BSP_LED_Off(LED3); BSP_LED_Off(LED4); BSP_LED_Off(LED6); }if (AppliState != APPLICATION_IDLE) { ... } }{ ... } /** * @} *//* ... */