stm32h735/stm32h735g-dk/Src/pv_audio_rec.c
Go to the documentation of this file.
1 /*
2  Copyright 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 
16 #include "pdm2pcm.h"
17 
18 #include "picovoice.h"
19 
20 #include "main.h"
21 
22 #define PV_AUDIO_REC_AUDIO_FREQUENCY (16000U)
23 #define PV_AUDIO_REC_CHANNEL_NUMBER (1)
24 #define PV_AUDIO_REC_RECORD_BUFFER_SIZE (512)
25 #define PV_AUDIO_REC_VOLUME_LEVEL (80)
26 
27 #define AUDIO_IN_PCM_BUFFER_SIZE (MIC_CAPTURE_IT_MS * 16)
28 
30 
31 static int32_t last_read_index = -1;
32 static int32_t read_index = 1;
33 static int32_t write_index = 0;
34 static int32_t buffer_index = 0;
35 
37 
41 
42 #define PV_AUDIO_DUMP_DURATION_SEC (3)
43 #define PV_AUDIO_REC_AUDIO_FREQUENCY (16000U)
44 #define PV_AUDIO_DUMP_BUFFER_SIZE ((uint32_t)(PV_AUDIO_DUMP_DURATION_SEC * PV_AUDIO_REC_AUDIO_FREQUENCY))
45 
46 struct {
47  uint32_t channel_number;
48  uint32_t audio_frequency;
50  uint16_t *record_pcm_buffer;
52 } pv_audio_rec;
53 
54 static void MX_SAI4_Init(void)
55 {
56 
82  hsai_BlockA4.SlotInit.SlotActive = 0x00000004;
83  if (HAL_SAI_Init(&hsai_BlockA4) != HAL_OK) {
85  }
86 
87 }
88 
89 static void MX_BDMA_Init(void)
90 {
94 
95 }
96 
97 static void MX_DMA_Init(void)
98 {
102 }
103 
104 static void MX_CRC_Init(void)
105 {
106 
107  hcrc.Instance = CRC;
113  if (HAL_CRC_Init(&hcrc) != HAL_OK) {
115  }
117 
118 }
119 
121 
122  MX_BDMA_Init();
123  MX_DMA_Init();
124  MX_SAI4_Init();
125  MX_CRC_Init();
126  MX_PDM2PCM_Init();
127 
128  return PV_STATUS_SUCCESS;
129 }
130 
133  pv_audio_rec.is_recording = true;
134  return PV_STATUS_SUCCESS;
135 }
136 
138  pv_audio_rec.is_recording = false;
139  return PV_STATUS_SUCCESS;
140 }
141 
142 const int16_t *pv_audio_rec_get_new_buffer(void) {
143  if (read_index == -1) {
144  return NULL;
145  }
146 
147  if (last_read_index == read_index) {
148  return NULL;
149  }
150 
152 
154 }
155 
157 
158  int32_t temp_buffer_size = PV_AUDIO_REC_RECORD_BUFFER_SIZE - buffer_index;
159  if (temp_buffer_size < AUDIO_IN_PCM_BUFFER_SIZE) {
160  for (uint32_t i = 0; i < temp_buffer_size; i++) {
162  }
164  write_index = 1 - write_index;
165  buffer_index = 0;
166  temp_buffer_size = AUDIO_IN_PCM_BUFFER_SIZE - temp_buffer_size;
167  } else {
168  temp_buffer_size = AUDIO_IN_PCM_BUFFER_SIZE;
169  }
170 
171  for (uint32_t i = 0; i < temp_buffer_size; i++) {
173  }
174 
177  write_index = 1 - write_index;
178  buffer_index = 0;
179  }
180 }
SAI_FS_STARTFRAME
#define SAI_FS_STARTFRAME
Definition: stm32f7xx_hal_sai.h:420
CRC_InitTypeDef::InputDataInversionMode
uint32_t InputDataInversionMode
Definition: stm32h7xx_hal_crc.h:87
SAI_InitTypeDef::MonoStereoMode
uint32_t MonoStereoMode
Definition: stm32f7xx_hal_sai.h:105
hsai_BlockA4
SAI_HandleTypeDef hsai_BlockA4
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:39
HAL_CRC_Init
HAL_StatusTypeDef HAL_CRC_Init(CRC_HandleTypeDef *hcrc)
SAI_FREE_PROTOCOL
#define SAI_FREE_PROTOCOL
Definition: stm32f7xx_hal_sai.h:350
SAI4_Block_A
#define SAI4_Block_A
Definition: stm32h735xx.h:2614
SAI_InitTypeDef::ClockStrobing
uint32_t ClockStrobing
Definition: stm32f7xx_hal_sai.h:126
NVIC_GetPriorityGrouping
#define NVIC_GetPriorityGrouping
Definition: imxrt1050/imxrt1050-evkb/CMSIS/core_cm7.h:1827
SAI_FIFOTHRESHOLD_1QF
#define SAI_FIFOTHRESHOLD_1QF
Definition: stm32f7xx_hal_sai.h:501
HAL_NVIC_EnableIRQ
void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)
SAI_InitTypeDef::Mckdiv
uint32_t Mckdiv
Definition: stm32f7xx_hal_sai.h:100
SAI_OUTPUTDRIVE_DISABLE
#define SAI_OUTPUTDRIVE_DISABLE
Definition: stm32f7xx_hal_sai.h:402
SAI_CLOCKSTROBING_FALLINGEDGE
#define SAI_CLOCKSTROBING_FALLINGEDGE
Definition: stm32f7xx_hal_sai.h:382
pdm2pcm.h
SAI_InitTypeDef::CompandingMode
uint32_t CompandingMode
Definition: stm32f7xx_hal_sai.h:108
SAI_FrameInitTypeDef::ActiveFrameLength
uint32_t ActiveFrameLength
Definition: stm32f7xx_hal_sai.h:146
__DMA_HandleTypeDef
DMA handle Structure definition.
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma.h:139
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
buffer_index
static int32_t buffer_index
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:34
BDMA_Channel0_IRQn
@ BDMA_Channel0_IRQn
Definition: stm32h735xx.h:170
__HAL_RCC_DMA1_CLK_ENABLE
#define __HAL_RCC_DMA1_CLK_ENABLE()
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc.h:414
SAI_InitTypeDef::AudioFrequency
uint32_t AudioFrequency
Definition: stm32f7xx_hal_sai.h:97
SAI_FrameInitTypeDef::FSDefinition
uint32_t FSDefinition
Definition: stm32f7xx_hal_sai.h:151
SAI_SLOTSIZE_DATASIZE
#define SAI_SLOTSIZE_DATASIZE
Definition: stm32f7xx_hal_sai.h:447
CRC_InitTypeDef::DefaultInitValueUse
uint8_t DefaultInitValueUse
Definition: stm32h7xx_hal_crc.h:67
MX_BDMA_Init
static void MX_BDMA_Init(void)
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:89
MX_DMA_Init
static void MX_DMA_Init(void)
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:97
DEFAULT_INIT_VALUE_ENABLE
#define DEFAULT_INIT_VALUE_ENABLE
Definition: stm32h7xx_hal_crc.h:159
AUDIO_IN_PCM_BUFFER_SIZE
#define AUDIO_IN_PCM_BUFFER_SIZE
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:27
DEFAULT_POLYNOMIAL_ENABLE
#define DEFAULT_POLYNOMIAL_ENABLE
Definition: stm32h7xx_hal_crc.h:150
__UART_HandleTypeDef
UART handle Structure definition.
Definition: stm32f4xx_hal_uart.h:141
CRC_HandleTypeDef::InputDataFormat
uint32_t InputDataFormat
Definition: stm32h7xx_hal_crc.h:113
SAI_InitTypeDef::Protocol
uint32_t Protocol
Definition: stm32f7xx_hal_sai.h:117
NVIC_EncodePriority
__STATIC_INLINE uint32_t NVIC_EncodePriority(uint32_t PriorityGroup, uint32_t PreemptPriority, uint32_t SubPriority)
Encode Priority.
Definition: imxrt1050/imxrt1050-evkb/CMSIS/core_cm7.h:2079
SAI_SlotInitTypeDef::SlotNumber
uint32_t SlotNumber
Definition: stm32f7xx_hal_sai.h:177
is_recording
bool is_recording
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:51
__SAI_HandleTypeDef::Instance
SAI_Block_TypeDef * Instance
Definition: stm32f7xx_hal_sai.h:193
SAI_InitTypeDef::FIFOThreshold
uint32_t FIFOThreshold
Definition: stm32f7xx_hal_sai.h:94
ENABLE
@ ENABLE
Definition: stm32f407/stm32f407g-disc1/Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h:194
SAI_MODEMASTER_RX
#define SAI_MODEMASTER_RX
Definition: stm32f7xx_hal_sai.h:339
CRC_OUTPUTDATA_INVERSION_DISABLE
#define CRC_OUTPUTDATA_INVERSION_DISABLE
Definition: stm32h7xx_hal_crc_ex.h:59
pv_audio_rec_stop
pv_status_t pv_audio_rec_stop(void)
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:137
HAL_OK
@ HAL_OK
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:42
SAI_FrameInitTypeDef::FSOffset
uint32_t FSOffset
Definition: stm32f7xx_hal_sai.h:157
SAI_InitTypeDef::Synchro
uint32_t Synchro
Definition: stm32f7xx_hal_sai.h:71
pv_audio_rec_init
pv_status_t pv_audio_rec_init(void)
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:120
PV_AUDIO_REC_RECORD_BUFFER_SIZE
#define PV_AUDIO_REC_RECORD_BUFFER_SIZE
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:24
pv_pcm_process
void pv_pcm_process(int16_t *record_pcm_buffer)
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:156
pv_audio_rec_get_new_buffer
const int16_t * pv_audio_rec_get_new_buffer(void)
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:142
SAI_DATASIZE_8
#define SAI_DATASIZE_8
Definition: stm32f7xx_hal_sai.h:360
__SAI_HandleTypeDef::Init
SAI_InitTypeDef Init
Definition: stm32f7xx_hal_sai.h:195
write_index
static int32_t write_index
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:33
SAI_PDM_CLOCK2_ENABLE
#define SAI_PDM_CLOCK2_ENABLE
Definition: stm32h735/stm32h735g-dk/Drivers/STM32H7xx_HAL_Driver/Inc/stm32h7xx_hal_sai.h:382
__SAI_HandleTypeDef
Definition: stm32f7xx_hal_sai.h:191
PV_STATUS_SUCCESS
@ PV_STATUS_SUCCESS
Definition: porcupine/include/picovoice.h:34
SAI_InitTypeDef::OutputDrive
uint32_t OutputDrive
Definition: stm32f7xx_hal_sai.h:80
SAI_NOCOMPANDING
#define SAI_NOCOMPANDING
Definition: stm32f7xx_hal_sai.h:512
read_index
static int32_t read_index
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:32
SAI_InitTypeDef::NoDivider
uint32_t NoDivider
Definition: stm32f7xx_hal_sai.h:85
SAI_AUDIO_FREQUENCY_MCKDIV
#define SAI_AUDIO_FREQUENCY_MCKDIV
Definition: stm32f7xx_hal_sai.h:330
pv_status_t
pv_status_t
Definition: porcupine/include/picovoice.h:33
MX_SAI4_Init
static void MX_SAI4_Init(void)
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:54
last_read_index
static int32_t last_read_index
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:31
ping_pong_buffer
static int16_t ping_pong_buffer[2][PV_AUDIO_REC_RECORD_BUFFER_SIZE]
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:29
SAI_MASTERDIVIDER_DISABLE
#define SAI_MASTERDIVIDER_DISABLE
Definition: stm32f7xx_hal_sai.h:412
CRC_INPUTDATA_INVERSION_NONE
#define CRC_INPUTDATA_INVERSION_NONE
Definition: stm32h7xx_hal_crc_ex.h:48
SAI_FS_FIRSTBIT
#define SAI_FS_FIRSTBIT
Definition: stm32f7xx_hal_sai.h:438
DMA1_Stream0_IRQn
@ DMA1_Stream0_IRQn
Definition: stm32f407xx.h:89
CRC_HandleTypeDef
CRC Handle Structure definition.
Definition: stm32h7xx_hal_crc.h:103
CRC
#define CRC
Definition: stm32f407xx.h:1112
SAI_InitTypeDef::DataSize
uint32_t DataSize
Definition: stm32f7xx_hal_sai.h:120
CRC_INPUTDATA_FORMAT_BYTES
#define CRC_INPUTDATA_FORMAT_BYTES
Definition: stm32h7xx_hal_crc.h:195
SAI_SlotInitTypeDef::SlotSize
uint32_t SlotSize
Definition: stm32f7xx_hal_sai.h:174
hcrc
CRC_HandleTypeDef hcrc
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:36
SAI_SlotInitTypeDef::SlotActive
uint32_t SlotActive
Definition: stm32f7xx_hal_sai.h:180
CRC_HandleTypeDef::Init
CRC_InitTypeDef Init
Definition: stm32h7xx_hal_crc.h:107
SAI_PdmInitTypeDef::Activation
FunctionalState Activation
Definition: stm32h735/stm32h735g-dk/Drivers/STM32H7xx_HAL_Driver/Inc/stm32h7xx_hal_sai.h:67
SAI_SlotInitTypeDef::FirstBitOffset
uint32_t FirstBitOffset
Definition: stm32f7xx_hal_sai.h:171
NVIC_SetPriority
#define NVIC_SetPriority
Definition: imxrt1050/imxrt1050-evkb/CMSIS/core_cm7.h:1835
SAI_FrameInitTypeDef::FSPolarity
uint32_t FSPolarity
Definition: stm32f7xx_hal_sai.h:154
main.h
pv_audio_rec
struct @607 pv_audio_rec
SAI_PdmInitTypeDef::ClockEnable
uint32_t ClockEnable
Definition: stm32h735/stm32h735g-dk/Drivers/STM32H7xx_HAL_Driver/Inc/stm32h7xx_hal_sai.h:70
MX_PDM2PCM_Init
void MX_PDM2PCM_Init(void)
Definition: pdm2pcm.c:36
pv_audio_rec_start
pv_status_t pv_audio_rec_start(void)
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:131
__HAL_CRC_DR_RESET
#define __HAL_CRC_DR_RESET(__HANDLE__)
Reset CRC Data Register.
Definition: stm32h7xx_hal_crc.h:231
NVIC_EnableIRQ
#define NVIC_EnableIRQ
Definition: imxrt1050/imxrt1050-evkb/CMSIS/core_cm7.h:1828
SAI_FIRSTBIT_LSB
#define SAI_FIRSTBIT_LSB
Definition: stm32f7xx_hal_sai.h:374
SAI_ASYNCHRONOUS
#define SAI_ASYNCHRONOUS
Definition: stm32f7xx_hal_sai.h:391
HAL_SAI_Init
HAL_StatusTypeDef HAL_SAI_Init(SAI_HandleTypeDef *hsai)
MX_CRC_Init
static void MX_CRC_Init(void)
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:104
record_pcm_buffer
uint16_t * record_pcm_buffer
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:50
HAL_NVIC_SetPriority
void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
audio_frequency
uint32_t audio_frequency
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:48
mic_capture_start
int mic_capture_start(void)
SAI_InitTypeDef::AudioMode
uint32_t AudioMode
Definition: stm32f7xx_hal_sai.h:68
CRC_HandleTypeDef::Instance
CRC_TypeDef * Instance
Definition: stm32h7xx_hal_crc.h:105
pv_error_handler
void pv_error_handler(void)
Definition: pv_imxrt1050.c:77
SAI_STEREOMODE
#define SAI_STEREOMODE
Definition: stm32f7xx_hal_sai.h:482
huart
UART_HandleTypeDef huart
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:38
CRC_InitTypeDef::DefaultPolynomialUse
uint8_t DefaultPolynomialUse
Definition: stm32h7xx_hal_crc.h:61
CRC_InitTypeDef::OutputDataInversionMode
uint32_t OutputDataInversionMode
Definition: stm32h7xx_hal_crc.h:94
SAI_FrameInitTypeDef::FrameLength
uint32_t FrameLength
Definition: stm32f7xx_hal_sai.h:140
record_buffer_size
uint32_t record_buffer_size
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:49
hdma_sai4_a
DMA_HandleTypeDef hdma_sai4_a
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:40
SAI_FS_ACTIVE_HIGH
#define SAI_FS_ACTIVE_HIGH
Definition: stm32f7xx_hal_sai.h:430
__SAI_HandleTypeDef::SlotInit
SAI_SlotInitTypeDef SlotInit
Definition: stm32f7xx_hal_sai.h:199
__SAI_HandleTypeDef::FrameInit
SAI_FrameInitTypeDef FrameInit
Definition: stm32f7xx_hal_sai.h:197
__HAL_RCC_BDMA_CLK_ENABLE
#define __HAL_RCC_BDMA_CLK_ENABLE()
Definition: stm32h735/stm32h735g-dk/Drivers/STM32H7xx_HAL_Driver/Inc/stm32h7xx_hal_rcc.h:1536
SAI_InitTypeDef::FirstBit
uint32_t FirstBit
Definition: stm32f7xx_hal_sai.h:123
channel_number
uint32_t channel_number
Definition: stm32h735/stm32h735g-dk/Src/pv_audio_rec.c:47
SAI_PdmInitTypeDef::MicPairsNbr
uint32_t MicPairsNbr
Definition: stm32h735/stm32h735g-dk/Drivers/STM32H7xx_HAL_Driver/Inc/stm32h7xx_hal_sai.h:68
SAI_InitTypeDef::PdmInit
SAI_PdmInitTypeDef PdmInit
Definition: stm32h735/stm32h735g-dk/Drivers/STM32H7xx_HAL_Driver/Inc/stm32h7xx_hal_sai.h:135


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