stm32h747/stm32h747i-disco/CM7/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 (2)
22 #define PV_AUDIO_REC_RECORD_BUFFER_SIZE (512)
23 #define PV_AUDIO_REC_VOLUME_LEVEL (10)
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 
28 ALIGN_32BYTES (static uint16_t record_pdm_buffer[AUDIO_IN_PDM_BUFFER_SIZE]) __attribute__((section(".RAM_D3")));
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 
51  BSP_AUDIO_Init_t audio_in_init;
52  audio_in_init.Device = AUDIO_IN_DEVICE_DIGITAL_MIC;
55  audio_in_init.BitsPerSample = AUDIO_RESOLUTION_16B;
56  audio_in_init.Volume = PV_AUDIO_REC_VOLUME_LEVEL;
57 
58  if (BSP_AUDIO_IN_Init(1, &audio_in_init) != BSP_ERROR_NONE) {
60  }
61  return PV_STATUS_SUCCESS;
62 }
63 
67  }
68  pv_audio_rec.is_recording = true;
69  return PV_STATUS_SUCCESS;
70 }
71 
72 const int16_t *pv_audio_rec_get_new_buffer(void) {
73  if (read_index == -1) {
74  return NULL;
75  }
76 
77  if (last_read_index == read_index) {
78  return NULL;
79  }
80 
82 
84 }
85 
86 void BSP_AUDIO_IN_TransferComplete_CallBack(uint32_t Instance) {
87  if (Instance == 1U) {
91  BSP_AUDIO_IN_PDMToPCM(Instance,
96 
97  for (uint32_t i = 0; i < AUDIO_IN_PCM_BUFFER_SIZE / 2; i++) {
99  }
102  write_index = 1 - write_index;
103  buffer_index = 0;
104  }
105  }
106 }
107 
108 void BSP_AUDIO_IN_HalfTransfer_CallBack(uint32_t Instance) {
109  if (Instance == 1U) {
112  BSP_AUDIO_IN_PDMToPCM(Instance, (uint16_t*) &record_pdm_buffer[0],
116 
117  for (uint32_t i = 0; i < AUDIO_IN_PCM_BUFFER_SIZE / 2; i++) {
119  }
122  write_index = 1 - write_index;
123  buffer_index = 0;
124  }
125  }
126 }
127 
131 }
SCB_CleanDCache_by_Addr
__STATIC_FORCEINLINE void SCB_CleanDCache_by_Addr(uint32_t *addr, int32_t dsize)
D-Cache Clean by address.
Definition: imxrt1050/imxrt1050-evkb/CMSIS/core_cm7.h:2542
BSP_AUDIO_Init_t::SampleRate
uint32_t SampleRate
Definition: stm32h747i_discovery_audio.h:59
BSP_AUDIO_Init_t
Definition: stm32h747i_discovery_audio.h:56
ALIGN_32BYTES
ALIGN_32BYTES(static uint16_t record_pdm_buffer[AUDIO_IN_PDM_BUFFER_SIZE]) __attribute__((section(".RAM_D3")))
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
channel_number
uint32_t channel_number
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:38
ping_pong_buffer
static int16_t ping_pong_buffer[2][PV_AUDIO_REC_RECORD_BUFFER_SIZE]
Definition: psoc062s2/src/pv_audio_rec.c:49
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
PV_AUDIO_REC_VOLUME_LEVEL
#define PV_AUDIO_REC_VOLUME_LEVEL
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:23
AUDIO_IN_PDM_BUFFER_SIZE
#define AUDIO_IN_PDM_BUFFER_SIZE
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:26
BSP_AUDIO_Init_t::ChannelsNbr
uint32_t ChannelsNbr
Definition: stm32h747i_discovery_audio.h:61
BSP_AUDIO_IN_DeInit
void BSP_AUDIO_IN_DeInit(void)
Deinit the audio IN peripherals.
Definition: stm32469i_discovery_audio.c:1036
AUDIO_RESOLUTION_16B
#define AUDIO_RESOLUTION_16B
Definition: stm32h747i_discovery_audio.h:155
last_read_index
static int32_t last_read_index
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:32
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
BSP_ERROR_NONE
#define BSP_ERROR_NONE
Definition: stm32h747i_discovery_errno.h:30
is_recording
bool is_recording
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:42
PV_AUDIO_REC_AUDIO_FREQUENCY
#define PV_AUDIO_REC_AUDIO_FREQUENCY
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:20
write_index
static int32_t write_index
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:34
PV_AUDIO_REC_RECORD_BUFFER_SIZE
#define PV_AUDIO_REC_RECORD_BUFFER_SIZE
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:22
pv_audio_rec
struct @618 pv_audio_rec
BSP_AUDIO_IN_RecordPDM
int32_t BSP_AUDIO_IN_RecordPDM(uint32_t Instance, uint8_t *pBuf, uint32_t NbrOfBytes)
Start audio recording.
Definition: stm32h747i_discovery_audio.c:2082
PV_STATUS_SUCCESS
@ PV_STATUS_SUCCESS
Definition: porcupine/include/picovoice.h:34
pv_status_t
pv_status_t
Definition: porcupine/include/picovoice.h:33
pv_audio_rec_get_new_buffer
const int16_t * pv_audio_rec_get_new_buffer(void)
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:72
record_pcm_buffer
uint16_t * record_pcm_buffer
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:41
BSP_AUDIO_IN_HalfTransfer_CallBack
void BSP_AUDIO_IN_HalfTransfer_CallBack(uint32_t Instance)
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:108
pv_audio_rec_start
pv_status_t pv_audio_rec_start(void)
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:64
audio_frequency
uint32_t audio_frequency
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:39
record_buffer_size
uint32_t record_buffer_size
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:40
PV_AUDIO_REC_CHANNEL_NUMBER
#define PV_AUDIO_REC_CHANNEL_NUMBER
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:21
pv_audio_rec_deinit
void pv_audio_rec_deinit(void)
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:128
AUDIO_IN_PCM_BUFFER_SIZE
#define AUDIO_IN_PCM_BUFFER_SIZE
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:25
BSP_AUDIO_Init_t::Volume
uint32_t Volume
Definition: stm32h747i_discovery_audio.h:62
BSP_AUDIO_IN_TransferComplete_CallBack
void BSP_AUDIO_IN_TransferComplete_CallBack(uint32_t Instance)
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:86
BSP_AUDIO_Init_t::BitsPerSample
uint32_t BitsPerSample
Definition: stm32h747i_discovery_audio.h:60
record_pdm_buffer
static uint16_t record_pdm_buffer[AUDIO_IN_PDM_BUFFER_SIZE]
Definition: stm32f407/stm32f407g-disc1/Src/pv_audio_rec.c:28
SCB_InvalidateDCache_by_Addr
__STATIC_FORCEINLINE void SCB_InvalidateDCache_by_Addr(void *addr, int32_t dsize)
D-Cache Invalidate by address.
Definition: imxrt1050/imxrt1050-evkb/CMSIS/core_cm7.h:2512
read_index
static int32_t read_index
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:33
buffer_index
static int32_t buffer_index
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:35
AUDIO_IN_DEVICE_DIGITAL_MIC
#define AUDIO_IN_DEVICE_DIGITAL_MIC
Definition: stm32h747i_discovery_audio.h:297
BSP_AUDIO_Init_t::Device
uint32_t Device
Definition: stm32h747i_discovery_audio.h:58
__attribute__
__attribute__((section(".rev16_text"))) __STATIC_INLINE __ASM uint32_t __REV16(uint32_t value)
Reverse byte order (16 bit)
Definition: imxrt1050/imxrt1050-evkb/CMSIS/cmsis_armcc.h:492
pv_audio_rec_init
pv_status_t pv_audio_rec_init(void)
Definition: stm32h747/stm32h747i-disco/CM7/Src/pv_audio_rec.c:45
stm32h747i_discovery_audio.h
This file contains the common defines and functions prototypes for the stm32h747i_discovery_audio....


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