stm32f411/stm32f411e-disco/Src/pv_audio_rec.c
Go to the documentation of this file.
1 /*
2  Copyright 2020-2021 Picovoice Inc.
3 
4  You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
5  file accompanying this source.
6 
7  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8  an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  specific language governing permissions and limitations under the License.
10 */
11 
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 
17 
18 #include "picovoice.h"
19 
20 #define PV_AUDIO_REC_AUDIO_FREQUENCY (16000U)
21 #define PV_AUDIO_REC_CHANNEL_NUMBER (1)
22 #define PV_AUDIO_REC_RECORD_BUFFER_SIZE (512)
23 #define PV_AUDIO_REC_VOLUME_LEVEL (100)
24 
25 #define AUDIO_IN_PCM_BUFFER_SIZE ((uint32_t)(PV_AUDIO_REC_AUDIO_FREQUENCY / 1000 * PV_AUDIO_REC_CHANNEL_NUMBER))
26 #define AUDIO_IN_PDM_BUFFER_SIZE ((uint32_t)(128 * PV_AUDIO_REC_AUDIO_FREQUENCY / 16000 * PV_AUDIO_REC_CHANNEL_NUMBER))
27 
31 
32 static int32_t last_read_index = -1;
33 static int32_t read_index = 1;
34 static int32_t write_index = 0;
35 static int32_t buffer_index = 0;
36 
37 struct {
38  uint32_t channel_number;
39  uint32_t audio_frequency;
41  uint16_t *record_pcm_buffer;
43 } pv_audio_rec;
44 
48  pv_audio_rec.record_buffer_size = PV_AUDIO_REC_RECORD_BUFFER_SIZE;
49  pv_audio_rec.record_pcm_buffer = record_pcm_buffer;
50 
53  }
56  }
57  return PV_STATUS_SUCCESS;
58 }
59 
63  }
64  pv_audio_rec.is_recording = true;
65  return PV_STATUS_SUCCESS;
66 }
67 
68 const int16_t *pv_audio_rec_get_new_buffer(void) {
69  if (read_index == -1) {
70  return NULL;
71  }
72 
73  if (last_read_index == read_index) {
74  return NULL;
75  }
76 
78 
80 }
81 
85  for (uint32_t i = 0; i < AUDIO_IN_PCM_BUFFER_SIZE; i++) {
87  }
91  buffer_index = 0;
92  }
93 }
94 
98  for (uint32_t i = 0; i < AUDIO_IN_PCM_BUFFER_SIZE; i++) {
100  }
103  write_index = 1 - write_index;
104  buffer_index = 0;
105  }
106 }
107 
110 }
BSP_AUDIO_IN_SetVolume
uint8_t BSP_AUDIO_IN_SetVolume(uint8_t Volume)
Controls the audio in volume level.
Definition: stm32f4_discovery_audio.c:799
buffer_index
static int32_t buffer_index
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:35
BSP_AUDIO_IN_PDMToPCM
uint8_t BSP_AUDIO_IN_PDMToPCM(uint16_t *PDMBuf, uint16_t *PCMBuf)
Converts audio format from PDM to PCM.
Definition: stm32f4_discovery_audio.c:814
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
BSP_AUDIO_IN_Stop
uint8_t BSP_AUDIO_IN_Stop(void)
Stops audio recording.
Definition: stm32f4_discovery_audio.c:754
PV_STATUS_INVALID_STATE
@ PV_STATUS_INVALID_STATE
Definition: porcupine/include/picovoice.h:40
BSP_AUDIO_IN_TransferComplete_CallBack
void BSP_AUDIO_IN_TransferComplete_CallBack(void)
User callback when record buffer is filled.
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:82
PV_AUDIO_REC_CHANNEL_NUMBER
#define PV_AUDIO_REC_CHANNEL_NUMBER
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:21
BSP_AUDIO_IN_Init
uint8_t BSP_AUDIO_IN_Init(uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr)
Initializes wave recording.
Definition: stm32f4_discovery_audio.c:705
record_pdm_buffer
static uint16_t record_pdm_buffer[AUDIO_IN_PDM_BUFFER_SIZE]
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:28
channel_number
uint32_t channel_number
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:38
record_buffer_size
uint32_t record_buffer_size
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:40
is_recording
bool is_recording
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:42
audio_frequency
uint32_t audio_frequency
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:39
stm32f411e_discovery_audio.h
This file contains the common defines and functions prototypes for stm32f411e_discovery_audio....
ping_pong_buffer
static int16_t ping_pong_buffer[2][PV_AUDIO_REC_RECORD_BUFFER_SIZE]
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:30
PV_STATUS_SUCCESS
@ PV_STATUS_SUCCESS
Definition: porcupine/include/picovoice.h:34
read_index
static int32_t read_index
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:33
pv_audio_rec_get_new_buffer
const int16_t * pv_audio_rec_get_new_buffer(void)
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:68
record_pcm_buffer
static uint16_t record_pcm_buffer[2 *AUDIO_IN_PCM_BUFFER_SIZE]
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:29
write_index
static int32_t write_index
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:34
pv_status_t
pv_status_t
Definition: porcupine/include/picovoice.h:33
pv_audio_rec_init
pv_status_t pv_audio_rec_init(void)
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:45
pv_audio_rec
struct @481 pv_audio_rec
PV_AUDIO_REC_RECORD_BUFFER_SIZE
#define PV_AUDIO_REC_RECORD_BUFFER_SIZE
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:22
BSP_AUDIO_IN_Record
uint8_t BSP_AUDIO_IN_Record(uint16_t *pbuf, uint32_t size)
Starts audio recording.
Definition: stm32f4_discovery_audio.c:737
PV_AUDIO_REC_VOLUME_LEVEL
#define PV_AUDIO_REC_VOLUME_LEVEL
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:23
AUDIO_IN_PCM_BUFFER_SIZE
#define AUDIO_IN_PCM_BUFFER_SIZE
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:25
PV_AUDIO_REC_AUDIO_FREQUENCY
#define PV_AUDIO_REC_AUDIO_FREQUENCY
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:20
pv_audio_rec_deinit
void pv_audio_rec_deinit(void)
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:108
DEFAULT_AUDIO_IN_BIT_RESOLUTION
#define DEFAULT_AUDIO_IN_BIT_RESOLUTION
Definition: stm32f4_discovery_audio.h:155
AUDIO_OK
#define AUDIO_OK
Definition: stm32f4_discovery_audio.h:149
BSP_AUDIO_IN_HalfTransfer_CallBack
void BSP_AUDIO_IN_HalfTransfer_CallBack(void)
Manages the DMA Half Transfer complete event.
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:95
last_read_index
static int32_t last_read_index
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:32
pv_audio_rec_start
pv_status_t pv_audio_rec_start(void)
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:60
AUDIO_IN_PDM_BUFFER_SIZE
#define AUDIO_IN_PDM_BUFFER_SIZE
Definition: stm32f411/stm32f411e-disco/Src/pv_audio_rec.c:26


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:50