stm32 Speex源码示例
时间: 2023-06-10 21:08:35 浏览: 166
以下是使用STM32CubeIDE和Speex库的示例代码:
```c
#include "main.h"
#include "stm32f4xx_hal.h"
#include "speex/speex.h"
/* Speex */
#define FRAME_SIZE 160
#define ENCODED_FRAME_SIZE (FRAME_SIZE / 2 + 1)
#define BITRATE 16000
/* UART */
UART_HandleTypeDef huart2;
/* Speex variables */
SpeexBits bits;
void *enc_state;
void *dec_state;
int quality = 8;
int complexity = 3;
int vbr = 1;
int vad = 1;
int dtx = 0;
int denoise = 1;
int plc = 1;
int sample_rate = 16000;
int frames_per_packet = 1;
int packet_size = 0;
int16_t input_buffer[FRAME_SIZE];
uint8_t encoded_buffer[ENCODED_FRAME_SIZE];
/* Private function prototypes */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
int main(void)
{
/* MCU Configuration */
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
/* Speex initialization */
speex_bits_init(&bits);
enc_state = speex_encoder_init(&speex_nb_mode);
speex_encoder_ctl(enc_state, SPEEX_SET_QUALITY, &quality);
speex_encoder_ctl(enc_state, SPEEX_SET_COMPLEXITY, &complexity);
speex_encoder_ctl(enc_state, SPEEX_SET_VBR, &vbr);
speex_encoder_ctl(enc_state, SPEEX_SET_VAD, &vad);
speex_encoder_ctl(enc_state, SPEEX_SET_DTX, &dtx);
speex_encoder_ctl(enc_state, SPEEX_SET_SAMPLING_RATE, &sample_rate);
speex_encoder_ctl(enc_state, SPEEX_SET_PLC_TUNING, &plc);
speex_decoder_ctl(dec_state, SPEEX_SET_SAMPLING_RATE, &sample_rate);
dec_state = speex_decoder_init(&speex_nb_mode);
while (1)
{
/* Read audio data from ADC or microphone into input_buffer */
/* ... */
/* Encode audio data */
speex_bits_reset(&bits);
speex_encode_int(enc_state, input_buffer, &bits);
packet_size = speex_bits_write(&bits, (char *)encoded_buffer, ENCODED_FRAME_SIZE);
/* Transmit encoded audio data over UART */
HAL_UART_Transmit(&huart2, encoded_buffer, packet_size, HAL_MAX_DELAY);
/* Decode received audio data */
if (HAL_UART_Receive(&huart2, encoded_buffer, packet_size, HAL_MAX_DELAY) == HAL_OK)
{
speex_bits_reset(&bits);
speex_bits_read_from(&bits, (char *)encoded_buffer, packet_size);
speex_decode_int(dec_state, &bits, input_buffer);
}
}
}
/* System Clock Configuration */
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 50;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
{
Error_Handler();
}
}
/* USART2 Initialization Function */
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
/* GPIO Initialization Function */
static void MX_GPIO_Init(void)
{
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
}
/* Error handler */
void Error_Handler(void)
{
while (1)
{
}
}
```
在这个示例代码中,我们使用Speex库对音频数据进行编码和解码。首先,我们初始化Speex库并设置编码器和解码器的参数。然后,我们在一个无限循环中读取音频数据,对其进行编码,并通过UART传输编码后的数据。接收到的数据也通过UART传输,并解码成原始音频数据。
阅读全文