Skip to content

Commit

Permalink
Attempt to reduce audio crackle when sending interrupts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Apr 9, 2024
1 parent 048d9c0 commit 459fa61
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
7 changes: 7 additions & 0 deletions src/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "hardware/structs/rosc.h"
#include "hardware/watchdog.h"
#include "hardware/sync.h"
#include "hardware/vreg.h"

using namespace encoder;
using namespace pimoroni;
Expand All @@ -20,6 +21,12 @@ absolute_time_t pressed_time;

extern "C" {
void system_init() {
// Apply a modest overvolt, default is 1.10v.
// this is required for a stable 250MHz on some RP2040s
vreg_set_voltage(VREG_VOLTAGE_1_20);
sleep_ms(10);
set_sys_clock_khz(250000, true);

// DCDC PSM control
// 0: PFM mode (best efficiency)
// 1: PWM mode (improved ripple)
Expand Down
63 changes: 33 additions & 30 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,35 @@ bool tud_audio_tx_done_pre_load_cb(uint8_t rhport, uint8_t itf, uint8_t ep_in, u
void audio_task(void)
{
static uint32_t start_ms = 0;
uint32_t volume_interval_ms = 10;
uint32_t volume_interval_ms = 50;

// When new data arrived, copy data from speaker buffer, to microphone buffer
// and send it over
// Only support speaker & headphone both have the same resolution
// If one is 16bit another is 24bit be care of LOUD noise !
if (spk_data_size)
{
//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

//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;
}

if (board_millis() - start_ms >= volume_interval_ms)
{
Expand All @@ -460,6 +488,7 @@ void audio_task(void)
};

tud_audio_int_write(&data);
tud_task();
}
/*if(volume_delta > 0) {
system_led(0, 255, 0);
Expand All @@ -471,6 +500,8 @@ void audio_task(void)

volume_delta *= volume_speed;

int old_system_volume = system_volume;

if(volume_delta + system_volume > 255) {
system_volume = 255;
} else if (volume_delta + system_volume < 0) {
Expand All @@ -487,7 +518,7 @@ void audio_task(void)
}
*/

if(volume_delta != 0) {
if(system_volume != old_system_volume) {
system_led(0, system_volume, 0);

volume[0] = system_volume * 100;
Expand All @@ -509,34 +540,6 @@ void audio_task(void)

start_ms += volume_interval_ms;
}

// When new data arrived, copy data from speaker buffer, to microphone buffer
// and send it over
// Only support speaker & headphone both have the same resolution
// If one is 16bit another is 24bit be care of LOUD noise !
if (spk_data_size)
{
//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

//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
2 changes: 1 addition & 1 deletion src/usb_descriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ enum
/* Output Terminal Descriptor(4.7.2.5) */\
TUD_AUDIO_DESC_OUTPUT_TERM(/*_termid*/ UAC2_ENTITY_SPK_OUTPUT_TERMINAL, /*_termtype*/ AUDIO_TERM_TYPE_OUT_GENERIC_SPEAKER, /*_assocTerm*/ 0x00, /*_srcid*/ UAC2_ENTITY_SPK_FEATURE_UNIT, /*_clkid*/ UAC2_ENTITY_CLOCK, /*_ctrl*/ 0x0000, /*_stridx*/ 0x00),\
/* Standard AC Interrupt Endpoint Descriptor(4.8.2.1) */\
TUD_AUDIO_DESC_STD_AC_INT_EP(/*_ep*/ _epint, /*_interval*/ 0x01), \
TUD_AUDIO_DESC_STD_AC_INT_EP(/*_ep*/ _epint, /*_interval*/ 0x010), \
/* Standard AS Interface Descriptor(4.9.1) */\
/* Interface 1, Alternate 0 - default alternate setting with 0 bandwidth */\
TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)(ITF_NUM_AUDIO_STREAMING_SPK), /*_altset*/ 0x00, /*_nEPs*/ 0x00, /*_stridx*/ 0x05),\
Expand Down

0 comments on commit 459fa61

Please sign in to comment.