From 2ad6057f33f0142e932efcb270a908f55c7cd6f0 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Thu, 10 Aug 2023 13:56:47 +0100 Subject: [PATCH] macOS fix, mute control. --- src/i2s_audio.c | 1 + src/main.c | 42 ++++++++++++++++++++++++++++++++---------- src/tusb_config.h | 14 -------------- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/i2s_audio.c b/src/i2s_audio.c index 9160004..36d6625 100644 --- a/src/i2s_audio.c +++ b/src/i2s_audio.c @@ -4,6 +4,7 @@ #include "board_config.h" #include "pico/stdlib.h" #include "hardware/dma.h" +#include "board.h" #include "tusb.h" #include diff --git a/src/main.c b/src/main.c index e990229..e24862f 100644 --- a/src/main.c +++ b/src/main.c @@ -59,13 +59,8 @@ const uint8_t volume_ramp[] = { //--------------------------------------------------------------------+ // List of supported sample rates -#if defined(__RX__) - const uint32_t sample_rates[] = {44100, 48000}; -#else - const uint32_t sample_rates[] = {44100, 48000, 88200, 96000}; -#endif - -uint32_t current_sample_rate = 44100; +const uint32_t sample_rates[] = {48000, 88200, 96000}; +uint32_t current_sample_rate = 48000; #define N_SAMPLE_RATES TU_ARRAY_SIZE(sample_rates) @@ -304,6 +299,12 @@ static bool tud_audio_feature_unit_set_request(uint8_t rhport, audio_control_req TU_LOG1("Set channel %d Mute: %d\r\n", request->bChannelNumber, mute[request->bChannelNumber]); + if(mute[request->bChannelNumber]) { + system_led(255, 0, 0); + } else { + system_led(0, 0, 0); + } + return true; } else if (request->bControlSelector == AUDIO_FU_CTRL_VOLUME) @@ -312,6 +313,8 @@ static bool tud_audio_feature_unit_set_request(uint8_t rhport, audio_control_req volume[request->bChannelNumber] = ((audio_control_cur_2_t const *)buf)->bCur; + system_led(0, 0, volume[request->bChannelNumber] / 100); + TU_LOG1("Set channel %d volume: %d dB\r\n", request->bChannelNumber, volume[request->bChannelNumber] / 256); return true; @@ -427,13 +430,13 @@ void audio_task(void) if (board_millis() - start_ms >= volume_interval_ms) { int32_t volume_delta = get_volume_delta(); - if(volume_delta > 0) { + /*if(volume_delta > 0) { system_led(0, 255, 0); } else if (volume_delta < 0) { system_led(0, 0, 255); } else { system_led(0, 0, 0); - } + }*/ volume_delta *= volume_speed; @@ -445,6 +448,10 @@ void audio_task(void) system_volume += volume_delta; } + if(volume_delta != 0) { + system_led(0, system_volume, 0); + } + start_ms += volume_interval_ms; } @@ -456,8 +463,23 @@ void audio_task(void) { //led_b_state = !led_b_state; //gpio_put(LED_B, led_b_state); + + // "Hardware" volume is 0 - 100 in steps of 256, with a maximum value of 25600 - i2s_audio_give_buffer(spk_buf, (size_t)spk_data_size, current_resolution, volume_ramp[(uint8_t)system_volume]); + //unsigned int current_volume = (unsigned int)volume[0] * volume_ramp[(uint8_t)system_volume] / 25600; + + /*int current_volume = volume[0] / 100; + if (current_volume > 256) current_volume = 256; + if (current_volume < 0) current_volume = 0; + current_volume = volume_ramp[current_volume];*/ + + int current_volume = volume_ramp[system_volume]; + + if (mute[0]) { + current_volume = 0; + } + + i2s_audio_give_buffer(spk_buf, (size_t)spk_data_size, current_resolution, current_volume); spk_data_size = 0; } } diff --git a/src/tusb_config.h b/src/tusb_config.h index 18b4ae6..8442421 100644 --- a/src/tusb_config.h +++ b/src/tusb_config.h @@ -111,11 +111,7 @@ extern "C" { #define CFG_TUD_AUDIO_FUNC_1_N_FORMATS 2 // Audio format type I specifications -#if defined(__RX__) -#define CFG_TUD_AUDIO_FUNC_1_MAX_SAMPLE_RATE 48000 // 16bit/48kHz is the best quality for Renesas RX -#else #define CFG_TUD_AUDIO_FUNC_1_MAX_SAMPLE_RATE 48000 // 24bit/96kHz is the best quality for full-speed, high-speed is needed beyond this -#endif #define CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX 0 // was 1 #define CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX 2 @@ -125,20 +121,10 @@ extern "C" { #define CFG_TUD_AUDIO_FUNC_1_FORMAT_1_N_BYTES_PER_SAMPLE_RX 2 #define CFG_TUD_AUDIO_FUNC_1_FORMAT_1_RESOLUTION_RX 16 -#if defined(__RX__) -// 8bit in 8bit slots -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_N_BYTES_PER_SAMPLE_TX 1 -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_RESOLUTION_TX 8 -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_N_BYTES_PER_SAMPLE_RX 1 -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_RESOLUTION_RX 8 -#else -// 24bit in 32bit slots #define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_N_BYTES_PER_SAMPLE_TX 4 #define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_RESOLUTION_TX 24 #define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_N_BYTES_PER_SAMPLE_RX 4 #define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_RESOLUTION_RX 24 -#endif - // EP and buffer size - for isochronous EP´s, the buffer and EP size are equal (different sizes would not make sense) #define CFG_TUD_AUDIO_ENABLE_EP_IN 1