audio commit
This commit is contained in:
@@ -10,6 +10,7 @@ This example uses the SPI3 port tested on STM32F407VET "black" board. On other b
|
||||
I2S_HandleTypeDef hi2s3;
|
||||
DMA_HandleTypeDef hdma_spi3_tx;
|
||||
uint32_t dma_buffer[I2S_BUFFER_SIZE];
|
||||
//int16_t sine[WAV_SIZE] = {0};
|
||||
// sinus oszillator
|
||||
float osc_phi = 0;
|
||||
float osc_phi_inc = 440.0f / 44100.0f; // generating 440HZ
|
||||
@@ -37,6 +38,16 @@ void Error_Handler2(byte errorcode)
|
||||
}
|
||||
}
|
||||
|
||||
void generateSine(int32_t amplitude, int16_t *buffer, uint16_t length)
|
||||
{
|
||||
// Generate a sine wave signal with the provided amplitude and store it in
|
||||
// the provided buffer of size length.
|
||||
for (int i = 0; i < length; ++i)
|
||||
{
|
||||
buffer[i] = int16_t(float(amplitude) * sin(2.0 * PI * (1.0 / length) * i));
|
||||
}
|
||||
}
|
||||
|
||||
void FillBuffer(uint32_t *buffer, uint16_t len)
|
||||
{
|
||||
float a;
|
||||
@@ -62,11 +73,13 @@ void StartAudioBuffers(I2S_HandleTypeDef *hi2s)
|
||||
// start circular dma
|
||||
HAL_I2S_Transmit_DMA(hi2s, (uint16_t *)dma_buffer, I2S_BUFFER_SIZE << 1);
|
||||
}
|
||||
|
||||
extern "C" void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
|
||||
{
|
||||
// second half finished, filling it up again while first half is playing
|
||||
FillBuffer(&(dma_buffer[I2S_BUFFER_SIZE >> 1]), I2S_BUFFER_SIZE >> 1);
|
||||
}
|
||||
|
||||
extern "C" void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
|
||||
{
|
||||
// first half finished, filling it up again while second half is playing
|
||||
@@ -180,15 +193,10 @@ extern "C" void HAL_MspInit(void) // maybe useful, not included in this example
|
||||
/* SysTick_IRQn interrupt configuration */
|
||||
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
|
||||
}
|
||||
// void initAudio(void)
|
||||
// {
|
||||
|
||||
// }
|
||||
|
||||
// void handleAudio(void)
|
||||
// {
|
||||
|
||||
// }
|
||||
|
||||
|
||||
void initAudio()
|
||||
{
|
||||
@@ -197,15 +205,43 @@ void initAudio()
|
||||
MX_DMA_Init();
|
||||
MX_I2S3_Init();
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
pinMode(PIN_I2S_SDMODE, OUTPUT);
|
||||
digitalWrite(PIN_I2S_SDMODE, HIGH);
|
||||
digitalWrite(LED_BUILTIN, 0);
|
||||
StartAudioBuffers(&hi2s3);
|
||||
digitalWrite(LED_BUILTIN, 1);
|
||||
}
|
||||
|
||||
//#define PI 3.14159265358979323846
|
||||
#define TAU (2.0 * PI)
|
||||
|
||||
void handleAudio()
|
||||
{
|
||||
// just a dummy code in loop, audio out is generated by ISR
|
||||
digitalWrite(LED_BUILTIN, 1);
|
||||
delay(250);
|
||||
//HAL_I2S_Transmit(&hi2s3,&sine,WAV_SIZE,1000);
|
||||
HAL_StatusTypeDef res;
|
||||
int16_t signal[46876];
|
||||
int nsamples = sizeof(signal) / sizeof(signal[0]);
|
||||
|
||||
int i = 0;
|
||||
while(i < nsamples) {
|
||||
double t = ((double)i/2.0)/((double)nsamples);
|
||||
signal[i] = 32767*sin(100.0 * TAU * t); // left
|
||||
signal[i+1] = signal[i]; // right
|
||||
i += 2;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
res = HAL_I2S_Transmit(&hi2s3, (uint16_t*)signal, nsamples, HAL_MAX_DELAY);
|
||||
if(res != HAL_OK) {
|
||||
Serial.printf("I2S - ERROR, res = %d!\r\n", res);
|
||||
break;
|
||||
}
|
||||
}
|
||||
digitalWrite(LED_BUILTIN, 0);
|
||||
delay(250);
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user