From 802f818735d0e7fe79715ef7660d91b75d625fed Mon Sep 17 00:00:00 2001 From: Anthony Rabine Date: Wed, 26 Jul 2023 16:19:36 +0200 Subject: [PATCH] Working sound! Yeahhh --- .../raspberry-pico-w/pico_hal_wrapper.c | 75 ++--------- software/system/audio_player.c | 9 +- software/system/main.c | 127 +++++------------- software/system/ost_hal.h | 1 + software/system/qor.c | 1 - 5 files changed, 53 insertions(+), 160 deletions(-) diff --git a/software/platform/raspberry-pico-w/pico_hal_wrapper.c b/software/platform/raspberry-pico-w/pico_hal_wrapper.c index 78ab591..398ffa8 100644 --- a/software/platform/raspberry-pico-w/pico_hal_wrapper.c +++ b/software/platform/raspberry-pico-w/pico_hal_wrapper.c @@ -315,6 +315,14 @@ void ost_audio_play(const char *filename) i2s_start(&i2s); } +void ost_audio_stop() +{ + memset(i2s.out_ctrl_blocks[0], 0, STEREO_BUFFER_SIZE * sizeof(uint32_t)); + memset(i2s.out_ctrl_blocks[1], 0, STEREO_BUFFER_SIZE * sizeof(uint32_t)); + audio_stop(&audio_ctx); + i2s_stop(&i2s); +} + int ost_audio_process() { return audio_process(&audio_ctx); @@ -327,21 +335,15 @@ void ost_audio_register_callback(ost_audio_callback_t cb) AudioCallBack = cb; } -void ost_hal_audio_new_frame(const void *buff, int dma_trans_number) +void ost_hal_audio_new_frame(const void *buff, int size) { - if (dma_trans_number > STEREO_BUFFER_SIZE) + if (size > STEREO_BUFFER_SIZE) { // Problème return; } - memcpy(i2s.out_ctrl_blocks[i2s.buffer_index], buff, dma_trans_number * sizeof(uint32_t)); + memcpy(i2s.out_ctrl_blocks[i2s.buffer_index], buff, size * sizeof(uint32_t)); i2s.buffer_index = 1 - i2s.buffer_index; - - // - // dma_channel_transfer_from_buffer_now(shared_state.dma_channel, buff, dma_trans_number); - // dma_channel_start(shared_state.dma_channel); - - // dma_hw->ints0 = 1u << i2s.dma_ch_out_data; // clear the IRQ } void __isr __time_critical_func(audio_i2s_dma_irq_handler)() @@ -354,58 +356,3 @@ void __isr __time_critical_func(audio_i2s_dma_irq_handler)() AudioCallBack(); } } - -#if 0 // legacy audio test - -#define SAMPLE_RATE (44100) -#define DMA_BUF_LEN (32) -#define I2S_NUM (0) -#define WAVE_FREQ_HZ (235.0f) -#define TWOPI (6.28318531f) -#define PHASE_INC (TWOPI * WAVE_FREQ_HZ / SAMPLE_RATE) - -// Accumulated phase -static float p = 0.0f; - -// Output buffer (2ch interleaved) -static uint32_t out_buf[DMA_BUF_LEN * 2]; - -uint32_t audio_count = 0; - -#include - -// Fill the output buffer and write to I2S DMA -static void write_buffer() -{ - // out_buf[0] = 0x70010001; - // out_buf[1] = 0xAAAA0001; - - dma_channel_transfer_from_buffer_now(shared_state.dma_channel, out_buf, 2); - dma_channel_start(shared_state.dma_channel); - - // You could put a taskYIELD() here to ensure other tasks always have a chance to run. - // taskYIELD(); -} - -void hal_audio_test() -{ - audio_i2s_set_enabled(true); - audio_count = 0; - write_buffer(); -} - -// irq handler for DMA -void __isr __time_critical_func(audio_i2s_dma_irq_handler)() -{ - uint dma_channel = shared_state.dma_channel; - if (dma_irqn_get_channel_status(PICO_AUDIO_I2S_DMA_IRQ, dma_channel)) - { - dma_irqn_acknowledge_channel(PICO_AUDIO_I2S_DMA_IRQ, dma_channel); - } - - write_buffer(); - // busy_wait_ms(1); - // audio_process(&audio_ctx); -} - -#endif diff --git a/software/system/audio_player.c b/software/system/audio_player.c index b9efeab..b77e096 100644 --- a/software/system/audio_player.c +++ b/software/system/audio_player.c @@ -11,9 +11,8 @@ #include "serializers.h" // Audio Double Buffer for DMA transfer -int32_t audio_buf[SIZE_OF_SAMPLES]; +int32_t audio_buf[SIZE_OF_SAMPLES * 2]; // x2 because we store L+R -// int16_t audio_buf16[2][SIZE_OF_SAMPLES]; // Audio Buffer for File Read uint8_t raw_buf[SIZE_OF_SAMPLES * 2 * 2]; // x2 for 16-bit, and x2 for L+R @@ -203,8 +202,8 @@ static int get_audio_buf(audio_ctx_t *ctx, int32_t *buf_32b) // Avec le AUDIO PICO de waveshare, on entend un truc - buf_32b[i * 2] = ((int32_t)((int16_t)leu16_get(&raw_buf[i * 4]))); - buf_32b[i * 2 + 1] = ((int32_t)((int16_t)leu16_get(&raw_buf[i * 4 + 2]))); + buf_32b[i * 2] = ((int32_t)((int16_t)leu16_get(&raw_buf[i * 4]))) << 16; + buf_32b[i * 2 + 1] = ((int32_t)((int16_t)leu16_get(&raw_buf[i * 4 + 2]))) << 16; // buf_32b[i * 2] = 1; // buf_32b[i * 2 + 1] = 4; @@ -226,7 +225,7 @@ static int get_audio_buf(audio_ctx_t *ctx, int32_t *buf_32b) */ // ctx->audio_info.lvl_l = get_level(lvl_l / (number / 4)); // ctx->audio_info.lvl_r = get_level(lvl_r / (number / 4)); - ctx->transfer_size = (number / 4); // 32 bytes tranfers + ctx->transfer_size = (number / 2); // 32 bytes tranfers return _next_is_end; } diff --git a/software/system/main.c b/software/system/main.c index f8314d2..260affd 100644 --- a/software/system/main.c +++ b/software/system/main.c @@ -49,20 +49,6 @@ int main(void) #include "sdcard.h" -const uint16_t tones[3][8] = - { - {0xff, 131, 147, 165, 175, 196, 220, 247}, - {0xff, 262, 294, 330, 349, 392, 440, 494}, - {0xff, 524, 988, 660, 698, 784, 880, 988}, -}; - -const uint8_t Happy_birthday[] = - { - 6, 1, 3, 5, 1, 1, 3, 1, 2, 5, 1, 2, 1, 2, 2, 6, 1, 1, 5, 1, 1, 6, 1, 4, 3, 1, 1, - 5, 1, 1, 6, 1, 1, 5, 1, 2, 3, 1, 2, 1, 1, 1, 6, 0, 1, 5, 1, 1, 3, 1, 1, 2, 1, 4, - 2, 1, 3, 3, 1, 1, 5, 1, 2, 5, 1, 1, 6, 1, 1, 3, 1, 2, 2, 1, 2, 1, 1, 4, 5, 1, 4, - 3, 1, 1, 2, 1, 1, 1, 1, 1, 6, 0, 1, 1, 1, 1, 5, 0, 8, 0}; - #include #include #include @@ -73,53 +59,6 @@ void ost_hal_panic() { } -extern void qor_sleep(); - -static qor_mbox_t b; - -typedef struct -{ - uint8_t ev; -} ost_event_t; - -ost_event_t ev_queue[10]; - -qor_tcb_t tcb1; -qor_tcb_t tcb2; - -void UserTask_1(void *args) -{ - // InstrumentTriggerPE11_Init(); - // uint32_t count = 0; - - qor_mbox_init(&b, (void **)&ev_queue, 10); - while (1) - { - - ost_hal_gpio_set(OST_GPIO_DEBUG_LED, 1); - - // qor_sleep(); - ost_event_t *e = NULL; - qor_mbox_wait(&b, (void **)&e, 3); - - for (int i = 0; i < 65500; i++) - { - for (int j = 0; j < 100; j++) - ; - } - - // ost_system_delay_ms(500); - ost_hal_gpio_set(OST_GPIO_DEBUG_LED, 0); - // ost_system_delay_ms(500); - - for (int i = 0; i < 65500; i++) - { - for (int j = 0; j < 100; j++) - ; - } - } -} - // =========================================================================================================== // SD CARD TASK // =========================================================================================================== @@ -128,20 +67,42 @@ static uint32_t AudioStack[4096]; static qor_mbox_t AudioMailBox; -static ost_event_t wake_up; - typedef struct { uint8_t ev; } ost_audio_event_t; +static ost_audio_event_t wake_up; + ost_audio_event_t audio_queue[10]; +static int dbg_state = 0; + // End of DMA transfer callback static void audio_callback(void) { - qor_mbox_notify(&b, (void **)&wake_up, QOR_MBOX_OPTION_SEND_BACK); - gpio_xor_mask(1 << 1); + dbg_state = 1 - dbg_state; + gpio_put(1, dbg_state); + qor_mbox_notify(&AudioMailBox, (void **)&wake_up, QOR_MBOX_OPTION_SEND_BACK); +} +#include +clock_t clock() +{ + return (clock_t)time_us_64() / 1000; +} + +void show_duration(uint32_t millisecondes) +{ + uint32_t minutes, secondes, reste; + + // Calcul des minutes, secondes et millisecondes + minutes = millisecondes / (60 * 1000); + reste = millisecondes % (60 * 1000); + secondes = reste / 1000; + reste = reste % 1000; + + // Affichage du temps + debug_printf("Temps : %d minutes, %d secondes, %d millisecondes\r\n", minutes, secondes, reste); } void AudioTask(void *args) @@ -163,38 +124,17 @@ void AudioTask(void *args) while (1) { - -// Benchmark code -#if 0 - if (onetime) - { - onetime = false; - - ost_audio_play("out2.wav"); - - int isPlaying = 0; - int count = 0; - do - { - gpio_put(1, 1); - isPlaying = ost_audio_process(); - gpio_put(1, 0); - count++; - - } while (isPlaying); - debug_printf("Packets: %d\r\n", count); - } -#endif - + debug_printf("\r\n-------------------------------------------------------\r\nPlaying: out2.wav\r\n"); + clock_t startTime = clock(); ost_audio_play("out2.wav"); - ost_event_t *e = NULL; + ost_audio_event_t *e = NULL; int isPlaying = 0; int count = 0; do { - uint32_t res = qor_mbox_wait(&AudioMailBox, (void **)&e, 30); // On devrait recevoir un message toutes les 3ms (durée d'envoi d'un buffer I2S) + uint32_t res = qor_mbox_wait(&AudioMailBox, (void **)&e, 300); // On devrait recevoir un message toutes les 3ms (durée d'envoi d'un buffer I2S) if (res == QOR_MBOX_OK) { @@ -205,6 +145,13 @@ void AudioTask(void *args) } while (isPlaying); + ost_audio_stop(); + clock_t endTime = clock(); + uint32_t executionTime = endTime - startTime; + + debug_printf("\r\nPackets: %d\r\n", count); + show_duration(executionTime); + for (;;) { ost_hal_gpio_set(OST_GPIO_DEBUG_LED, 0); diff --git a/software/system/ost_hal.h b/software/system/ost_hal.h index a46d596..8b90ab4 100644 --- a/software/system/ost_hal.h +++ b/software/system/ost_hal.h @@ -48,6 +48,7 @@ extern "C" void ost_system_delay_ms(uint32_t delay); void ost_audio_play(const char *filename); + void ost_audio_stop(); int ost_audio_process(); typedef void (*ost_audio_callback_t)(void); void ost_audio_register_callback(ost_audio_callback_t cb); diff --git a/software/system/qor.c b/software/system/qor.c index 580dd35..a58e547 100644 --- a/software/system/qor.c +++ b/software/system/qor.c @@ -382,7 +382,6 @@ void qor_mbox_init(qor_mbox_t *mbox, void **msgBuffer, uint32_t maxCount) mbox->msgBuffer = msgBuffer; mbox->maxCount = maxCount; mbox->read = 0; - mbox->read = 0; mbox->head = NULL; mbox->count = 0; }