Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "audio_if.h"
Private variables
buffer_ctl
audio_state
Private function prototypes
AUDIO_Init()
AUDIO_Start()
AUDIO_Process()
GetData(void *, uint32_t, uint8_t *, uint32_t)
BSP_AUDIO_OUT_TransferComplete_CallBack()
BSP_AUDIO_OUT_HalfTransfer_CallBack()
BSP_AUDIO_OUT_Error_CallBack()
Files
loading...
CodeScopeSTM32 Libraries and SamplesI2S_AudioSrc/audio_if.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file I2S/I2S_Audio/Src/audio_if.c * @author MCD Application Team * @brief This file provides the Audio Out (playback) interface API ****************************************************************************** * @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_if.h" Includes /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private typedef -----------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ static AUDIO_BufferTypeDef buffer_ctl; static AUDIO_PLAYBACK_StateTypeDef audio_state; Private variables /* Private function prototypes -----------------------------------------------*/ static uint32_t GetData(void *pdata, uint32_t offset, uint8_t *pbuf, uint32_t NbrOfData); Private function prototypes /* Private functions ---------------------------------------------------------*/ /** * @brief Initializes Audio Interface. * @param None * @retval Audio error *//* ... */ AUDIO_ErrorTypeDef AUDIO_Init(void) { audio_state = AUDIO_STATE_IDLE; if(BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_AUTO, AUDIO_DEFAULT_VOLUME, I2S_AUDIOFREQ_8K) == 0) { audio_state = AUDIO_STATE_INIT; return AUDIO_ERROR_NONE; }if (BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_AUTO, AUDIO_DEFAULT_VOLUME, I2S_AUDIOFREQ_8K) == 0) { ... } return AUDIO_ERROR_IO; }{ ... } /** * @brief Starts Audio streaming. * @param None * @retval Audio error *//* ... */ AUDIO_ErrorTypeDef AUDIO_Start(void) { uint32_t bytesread; buffer_ctl.state = BUFFER_OFFSET_NONE; bytesread = GetData( (void *)AUDIO_FILE_ADDRESS, 0, &buffer_ctl.buff[0], AUDIO_BUFFER_SIZE); if(bytesread > 0) { BSP_AUDIO_OUT_Play((uint16_t*)&buffer_ctl.buff[0], AUDIO_BUFFER_SIZE); audio_state = AUDIO_STATE_PLAYING; buffer_ctl.fptr = bytesread; return AUDIO_ERROR_NONE; }if (bytesread > 0) { ... } return AUDIO_ERROR_IO; }{ ... } /** * @brief Manages Audio process. * @param None * @retval Audio error *//* ... */ AUDIO_ErrorTypeDef AUDIO_Process(void) { uint32_t bytesread; AUDIO_ErrorTypeDef error_state = AUDIO_ERROR_NONE; switch(audio_state) { case AUDIO_STATE_PLAYING: if(buffer_ctl.fptr >= AUDIO_FILE_SIZE) { /* Play audio sample again ... */ buffer_ctl.fptr = 0; error_state = AUDIO_ERROR_EOF; }if (buffer_ctl.fptr >= AUDIO_FILE_SIZE) { ... } /* 1st half buffer played; so fill it and continue playing from bottom*/ if(buffer_ctl.state == BUFFER_OFFSET_HALF) { bytesread = GetData((void *)AUDIO_FILE_ADDRESS, buffer_ctl.fptr, &buffer_ctl.buff[0], AUDIO_BUFFER_SIZE /2); if( bytesread >0) { buffer_ctl.state = BUFFER_OFFSET_NONE; buffer_ctl.fptr += bytesread; }if (bytesread >0) { ... } }if (buffer_ctl.state == BUFFER_OFFSET_HALF) { ... } /* 2nd half buffer played; so fill it and continue playing from top */ if(buffer_ctl.state == BUFFER_OFFSET_FULL) { bytesread = GetData((void *)AUDIO_FILE_ADDRESS, buffer_ctl.fptr, &buffer_ctl.buff[AUDIO_BUFFER_SIZE /2], AUDIO_BUFFER_SIZE /2); if( bytesread > 0) { buffer_ctl.state = BUFFER_OFFSET_NONE; buffer_ctl.fptr += bytesread; }if (bytesread > 0) { ... } }if (buffer_ctl.state == BUFFER_OFFSET_FULL) { ... } break; case AUDIO_STATE_PLAYING: default: error_state = AUDIO_ERROR_NOTREADY; break;default }switch (audio_state) { ... } return error_state; }{ ... } /** * @brief Gets Data from storage unit. * @param None * @retval None *//* ... */ static uint32_t GetData(void *pdata, uint32_t offset, uint8_t *pbuf, uint32_t NbrOfData) { uint8_t *lptr = pdata; uint32_t ReadDataNbr; ReadDataNbr = 0; while(((offset + ReadDataNbr) < AUDIO_FILE_SIZE) && (ReadDataNbr < NbrOfData)) { pbuf[ReadDataNbr]= lptr [offset + ReadDataNbr]; ReadDataNbr++; }while (((offset + ReadDataNbr) < AUDIO_FILE_SIZE) && (ReadDataNbr < NbrOfData)) { ... } return ReadDataNbr; }{ ... } /*------------------------------------------------------------------------------ Callbacks implementation: the callbacks API are defined __weak in the stm324xg_eval_audio.c file and their implementation should be done the user code if they are needed. Below some examples of callback implementations. ----------------------------------------------------------------------------*//* ... */ /** * @brief Calculates the remaining file size and new position of the pointer. * @param None * @retval None *//* ... */ void BSP_AUDIO_OUT_TransferComplete_CallBack(void) { if(audio_state == AUDIO_STATE_PLAYING) { /* Continue Playing from 1st half buffer */ BSP_AUDIO_OUT_ChangeBuffer((uint16_t*)&buffer_ctl.buff[0], AUDIO_BUFFER_SIZE /2); buffer_ctl.state = BUFFER_OFFSET_FULL; }if (audio_state == AUDIO_STATE_PLAYING) { ... } }{ ... } /** * @brief Manages the DMA Half Transfer complete interrupt. * @param None * @retval None *//* ... */ void BSP_AUDIO_OUT_HalfTransfer_CallBack(void) { if(audio_state == AUDIO_STATE_PLAYING) { /* Continue Playing from 2nd half buffer */ BSP_AUDIO_OUT_ChangeBuffer((uint16_t*)&buffer_ctl.buff[AUDIO_BUFFER_SIZE /2], AUDIO_BUFFER_SIZE /2); buffer_ctl.state = BUFFER_OFFSET_HALF; }if (audio_state == AUDIO_STATE_PLAYING) { ... } }{ ... } /** * @brief Manages the DMA FIFO error event. * @param None * @retval None *//* ... */ void BSP_AUDIO_OUT_Error_CallBack(void) { /* Display message on the LCD screen */ BSP_LCD_SetBackColor(LCD_COLOR_RED); BSP_LCD_DisplayStringAtLine(8, (uint8_t *)" DMA ERROR "); /* Stop the program with an infinite loop */ while (1) {}while (1) { ... } /* could also generate a system reset to recover from the error */ /* .... */ }{ ... }