1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
22
23
26
27
32
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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
195
201
202
208
209
210
211
212
213
219
225
234
243
244
248
249
250
251
254
255
256
257
258
263
264
265
266
267
268
269
270
271
276
277
283
284
285
290
291
292
293
294
295
296
297
298
303
304
310
311
312
315
316
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
341
342
343
344
350
351
352
358
365
371
377
383
387
391
398
404
408
412
418
419
420
421
422
423
424
425
426
427
428
429
435
436
437
444
445
446
447
448
449
450
451
452
453
454
/* ... */
#include "waverecorder.h"
Includes
uint8_t pHeaderBuff[44];
Private define
static AUDIO_IN_BufferTypeDef BufferCtl;
extern WAVE_FormatTypeDef WaveFormat;
Private variables
static uint32_t WavProcess_EncInit(uint32_t Freq, uint8_t* pHeader);
static uint32_t WavProcess_HeaderInit(uint8_t* pHeader, WAVE_FormatTypeDef* pWaveFormatStruct);
static uint32_t WavProcess_HeaderUpdate(uint8_t* pHeader, WAVE_FormatTypeDef* pWaveFormatStruct);
Private function prototypes
/* ... */
/* ... */
AUDIO_ErrorTypeDef AUDIO_REC_Start(void)
{
uint32_t byteswritten = 0;
uint8_t str[FILEMGR_FILE_NAME_SIZE + 20];
if(f_open(&WavFile, REC_WAVE_NAME, FA_CREATE_ALWAYS | FA_WRITE) == FR_OK)
{
WavProcess_EncInit(DEFAULT_AUDIO_IN_FREQ, pHeaderBuff);
if(f_write(&WavFile, pHeaderBuff, 44, (void*)&byteswritten) == FR_OK)
{
AudioState = AUDIO_STATE_RECORD;
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
sprintf((char *)str, "Recording file: %s",
(char *)REC_WAVE_NAME);
BSP_LCD_ClearStringLine(4);
BSP_LCD_DisplayStringAtLine(4, str);
BSP_LCD_SetTextColor(LCD_COLOR_CYAN);
sprintf((char *)str, "Sample rate : %d Hz", (int)DEFAULT_AUDIO_IN_FREQ);
BSP_LCD_ClearStringLine(6);
BSP_LCD_DisplayStringAtLine(6, str);
sprintf((char *)str, "Channels number : %d", (int)DEFAULT_AUDIO_IN_CHANNEL_NBR);
BSP_LCD_ClearStringLine(7);
BSP_LCD_DisplayStringAtLine(7, str);
sprintf((char *)str, "File Size :");
BSP_LCD_ClearStringLine(8);
BSP_LCD_DisplayStringAtLine(8, str);
BSP_LCD_DisplayStringAt(250, LINE(14), (uint8_t *)" [RECORD]", LEFT_MODE);
{
if(byteswritten != 0)
{
BSP_AUDIO_IN_Init(DEFAULT_AUDIO_IN_FREQ, DEFAULT_AUDIO_IN_BIT_RESOLUTION, DEFAULT_AUDIO_IN_CHANNEL_NBR);
BSP_AUDIO_IN_Record((uint16_t*)&BufferCtl.pdm_buff[0], AUDIO_IN_PDM_BUFFER_SIZE);
BufferCtl.fptr = byteswritten;
BufferCtl.pcm_ptr = 0;
BufferCtl.offset = 0;
BufferCtl.wr_state = BUFFER_EMPTY;
return AUDIO_ERROR_NONE;
}if (byteswritten != 0) { ... }
...}
}if (f_write(&WavFile, pHeaderBuff, 44, (void*)&byteswritten) == FR_OK) { ... }
}if (f_open(&WavFile, REC_WAVE_NAME, FA_CREATE_ALWAYS | FA_WRITE) == FR_OK) { ... }
return AUDIO_ERROR_IO;
}{ ... }
/* ... */
AUDIO_ErrorTypeDef AUDIO_REC_Process(void)
{
uint32_t byteswritten = 0;
AUDIO_ErrorTypeDef audio_error = AUDIO_ERROR_NONE;
uint32_t elapsed_time;
static uint32_t prev_elapsed_time = 0xFFFFFFFF;
uint8_t str[16];
switch(AudioState)
{
case AUDIO_STATE_RECORD:
if(BufferCtl.fptr >= REC_SAMPLE_LENGTH)
{
AudioState = AUDIO_STATE_STOP;
break;
}if (BufferCtl.fptr >= REC_SAMPLE_LENGTH) { ... }
if(BufferCtl.wr_state == BUFFER_FULL)
{
if(f_write(&WavFile, (uint8_t*)(BufferCtl.pcm_buff + BufferCtl.offset),
AUDIO_IN_PCM_BUFFER_SIZE,
(void*)&byteswritten) != FR_OK)
{
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAtLine(16, (uint8_t *)"RECORD FAIL");
return AUDIO_ERROR_IO;
}if (f_write(&WavFile, (uint8_t*)(BufferCtl.pcm_buff + BufferCtl.offset), AUDIO_IN_PCM_BUFFER_SIZE, (void*)&byteswritten) != FR_OK) { ... }
BufferCtl.fptr += byteswritten;
BufferCtl.wr_state = BUFFER_EMPTY;
}if (BufferCtl.wr_state == BUFFER_FULL) { ... }
elapsed_time = BufferCtl.fptr / (DEFAULT_AUDIO_IN_FREQ * DEFAULT_AUDIO_IN_CHANNEL_NBR * 2);
if(prev_elapsed_time != elapsed_time)
{
prev_elapsed_time = elapsed_time;
sprintf((char *)str, "[%02d:%02d]", (int)(elapsed_time /60), (int)(elapsed_time%60));
BSP_LCD_SetTextColor(LCD_COLOR_CYAN);
BSP_LCD_DisplayStringAt(263, LINE(8), str, LEFT_MODE);
sprintf((char *)str, "%4d KB", (int)((int32_t)BufferCtl.fptr/1024));
BSP_LCD_DisplayStringAt(83, LINE(8), str, LEFT_MODE);
}if (prev_elapsed_time != elapsed_time) { ... }
break;
case AUDIO_STATE_RECORD:
case AUDIO_STATE_STOP:
BSP_AUDIO_IN_Stop();
if(f_lseek(&WavFile, 0) == FR_OK)
{
WavProcess_HeaderUpdate(pHeaderBuff, &WaveFormat);
if(f_write(&WavFile, pHeaderBuff, sizeof(WAVE_FormatTypeDef), (void*)&byteswritten) == FR_OK)
{
audio_error = AUDIO_ERROR_EOF;
}if (f_write(&WavFile, pHeaderBuff, sizeof(WAVE_FormatTypeDef), (void*)&byteswritten) == FR_OK) { ... }
else
{
audio_error = AUDIO_ERROR_IO;
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAtLine(16, (uint8_t *)"RECORD FAIL");
}else { ... }
}if (f_lseek(&WavFile, 0) == FR_OK) { ... }
else
{
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAtLine(16, (uint8_t *)"RECORD FAIL");
audio_error = AUDIO_ERROR_IO;
}else { ... }
AudioState = AUDIO_STATE_IDLE;
f_close(&WavFile);
break;
case AUDIO_STATE_STOP:
case AUDIO_STATE_PAUSE:
BSP_LCD_DisplayStringAt(250, LINE(14), (uint8_t *)" [PAUSE] ", LEFT_MODE);
BSP_AUDIO_IN_Pause();
AudioState = AUDIO_STATE_WAIT;
break;
case AUDIO_STATE_PAUSE:
case AUDIO_STATE_RESUME:
BSP_LCD_DisplayStringAt(250, LINE(14), (uint8_t *)" [RECORD] ", LEFT_MODE);
BSP_AUDIO_IN_Resume();
AudioState = AUDIO_STATE_RECORD;
break;
case AUDIO_STATE_RESUME:
case AUDIO_STATE_VOLUME_UP:
if(AudioInVolume <= 90)
{
AudioInVolume += 10;
}if (AudioInVolume <= 90) { ... }
BSP_AUDIO_IN_SetVolume(AudioInVolume);
AudioState = AUDIO_STATE_RECORD;
break;
case AUDIO_STATE_VOLUME_UP:
case AUDIO_STATE_VOLUME_DOWN:
if(AudioInVolume >= 10)
{
AudioInVolume -= 10;
}if (AudioInVolume >= 10) { ... }
BSP_AUDIO_IN_SetVolume(AudioInVolume);
AudioState = AUDIO_STATE_RECORD;
break;
case AUDIO_STATE_VOLUME_DOWN:
case AUDIO_STATE_NEXT:
case AUDIO_STATE_PREVIOUS:
AudioState = AUDIO_STATE_RECORD;
break;
case AUDIO_STATE_PREVIOUS:
case AUDIO_STATE_WAIT:
case AUDIO_STATE_IDLE:
case AUDIO_STATE_INIT:
default:
break;default
}switch (AudioState) { ... }
return audio_error;
}{ ... }
/* ... */
void BSP_AUDIO_IN_TransferComplete_CallBack(void)
{
BSP_AUDIO_IN_PDMToPCM((uint16_t*)&BufferCtl.pdm_buff[AUDIO_IN_PDM_BUFFER_SIZE/2],
&BufferCtl.pcm_buff[BufferCtl.pcm_ptr]);
BufferCtl.pcm_ptr+= AUDIO_IN_PDM_BUFFER_SIZE/4/2;
if(BufferCtl.pcm_ptr == AUDIO_IN_PCM_BUFFER_SIZE/2)
{
BufferCtl.wr_state = BUFFER_FULL;
BufferCtl.offset = 0;
}if (BufferCtl.pcm_ptr == AUDIO_IN_PCM_BUFFER_SIZE/2) { ... }
if(BufferCtl.pcm_ptr >= AUDIO_IN_PCM_BUFFER_SIZE)
{
BufferCtl.wr_state = BUFFER_FULL;
BufferCtl.offset = AUDIO_IN_PCM_BUFFER_SIZE/2;
BufferCtl.pcm_ptr = 0;
}if (BufferCtl.pcm_ptr >= AUDIO_IN_PCM_BUFFER_SIZE) { ... }
}{ ... }
/* ... */
void BSP_AUDIO_IN_HalfTransfer_CallBack(void)
{
BSP_AUDIO_IN_PDMToPCM((uint16_t*)&BufferCtl.pdm_buff[0],
&BufferCtl.pcm_buff[BufferCtl.pcm_ptr]);
BufferCtl.pcm_ptr+= AUDIO_IN_PDM_BUFFER_SIZE/4/2;
if(BufferCtl.pcm_ptr == AUDIO_IN_PCM_BUFFER_SIZE/2)
{
BufferCtl.wr_state = BUFFER_FULL;
BufferCtl.offset = 0;
}if (BufferCtl.pcm_ptr == AUDIO_IN_PCM_BUFFER_SIZE/2) { ... }
if(BufferCtl.pcm_ptr >= AUDIO_IN_PCM_BUFFER_SIZE)
{
BufferCtl.wr_state = BUFFER_FULL;
BufferCtl.offset = AUDIO_IN_PCM_BUFFER_SIZE/2;
BufferCtl.pcm_ptr = 0;
}if (BufferCtl.pcm_ptr >= AUDIO_IN_PCM_BUFFER_SIZE) { ... }
}{ ... }
/* ... */
/* ... */
static uint32_t WavProcess_EncInit(uint32_t Freq, uint8_t *pHeader)
{
WaveFormat.SampleRate = Freq;
WaveFormat.NbrChannels = 2;
WaveFormat.BitPerSample = 16;
WaveFormat.FileSize = 0x001D4C00;
WaveFormat.SubChunk1Size = 44;
WaveFormat.ByteRate = (WaveFormat.SampleRate * \
(WaveFormat.BitPerSample/8) * \
WaveFormat.NbrChannels);
WaveFormat.BlockAlign = WaveFormat.NbrChannels * \
(WaveFormat.BitPerSample/8);
if(WavProcess_HeaderInit(pHeader, &WaveFormat))
{
return 1;
}if (WavProcess_HeaderInit(pHeader, &WaveFormat)) { ... }
return 0;
}{ ... }
/* ... */
static uint32_t WavProcess_HeaderInit(uint8_t* pHeader, WAVE_FormatTypeDef* pWaveFormatStruct)
{
pHeader[0] = 'R';
pHeader[1] = 'I';
pHeader[2] = 'F';
pHeader[3] = 'F';
Write chunkID, must be 'RIFF'
/* ... */
pHeader[4] = 0x00;
pHeader[5] = 0x4C;
pHeader[6] = 0x1D;
pHeader[7] = 0x00;Write the file length
pHeader[8] = 'W';
pHeader[9] = 'A';
pHeader[10] = 'V';
pHeader[11] = 'E';
Write the file format, must be 'WAVE'
pHeader[12] = 'f';
pHeader[13] = 'm';
pHeader[14] = 't';
pHeader[15] = ' ';
Write the format chunk, must be'fmt '
pHeader[16] = 0x10;
pHeader[17] = 0x00;
pHeader[18] = 0x00;
pHeader[19] = 0x00;
Write the length of the 'fmt' data, must be 0x10
pHeader[20] = 0x01;
pHeader[21] = 0x00;
Write the audio format, must be 0x01 (PCM)
pHeader[22] = pWaveFormatStruct->NbrChannels;
pHeader[23] = 0x00;
Write the number of channels, ie. 0x01 (Mono)
pHeader[24] = (uint8_t)((pWaveFormatStruct->SampleRate & 0xFF));
pHeader[25] = (uint8_t)((pWaveFormatStruct->SampleRate >> 8) & 0xFF);
pHeader[26] = (uint8_t)((pWaveFormatStruct->SampleRate >> 16) & 0xFF);
pHeader[27] = (uint8_t)((pWaveFormatStruct->SampleRate >> 24) & 0xFF);
Write the Sample Rate in Hz
pHeader[28] = (uint8_t)((pWaveFormatStruct->ByteRate & 0xFF));
pHeader[29] = (uint8_t)((pWaveFormatStruct->ByteRate >> 8) & 0xFF);
pHeader[30] = (uint8_t)((pWaveFormatStruct->ByteRate >> 16) & 0xFF);
pHeader[31] = (uint8_t)((pWaveFormatStruct->ByteRate >> 24) & 0xFF);
Write the Byte Rate
pHeader[32] = pWaveFormatStruct->BlockAlign;
pHeader[33] = 0x00;
Write the block alignment
pHeader[34] = pWaveFormatStruct->BitPerSample;
pHeader[35] = 0x00;
Write the number of bits per sample
pHeader[36] = 'd';
pHeader[37] = 'a';
pHeader[38] = 't';
pHeader[39] = 'a';
Write the Data chunk, must be 'data'
pHeader[40] = 0x00;
pHeader[41] = 0x4C;
pHeader[42] = 0x1D;
pHeader[43] = 0x00;
return 0;
}{ ... }
/* ... */
static uint32_t WavProcess_HeaderUpdate(uint8_t* pHeader, WAVE_FormatTypeDef* pWaveFormatStruct)
{
/* ... */
pHeader[4] = (uint8_t)(BufferCtl.fptr);
pHeader[5] = (uint8_t)(BufferCtl.fptr >> 8);
pHeader[6] = (uint8_t)(BufferCtl.fptr >> 16);
pHeader[7] = (uint8_t)(BufferCtl.fptr >> 24);Write the file length
BufferCtl.fptr -=44;
pHeader[40] = (uint8_t)(BufferCtl.fptr);
pHeader[41] = (uint8_t)(BufferCtl.fptr >> 8);
pHeader[42] = (uint8_t)(BufferCtl.fptr >> 16);
pHeader[43] = (uint8_t)(BufferCtl.fptr >> 24);
return 0;Write the number of sample data
}{ ... }