Skip to content

Commit

Permalink
macOS fix, mute control.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Aug 10, 2023
1 parent be18dbb commit 2ad6057
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/i2s_audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "board_config.h"
#include "pico/stdlib.h"
#include "hardware/dma.h"
#include "board.h"
#include "tusb.h"
#include <math.h>

Expand Down
42 changes: 32 additions & 10 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}

Expand All @@ -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;
}
}
Expand Down
14 changes: 0 additions & 14 deletions src/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 2ad6057

Please sign in to comment.