Permalink
Browse files

Added updates to uac1 and uac2 device_audio_task.c by Nikolay from th…

…e audio-widget-nik branch to maintain better audio buffer synchronization to avoid distortion
  • Loading branch information...
1 parent 44154e3 commit c41166954a8114acf70a67132c49c2f6de68f81c @alexlee188 alexlee188 committed Jun 29, 2011
Showing with 157 additions and 112 deletions.
  1. +31 −12 src/uac1_device_audio_task.c
  2. +126 −100 src/uac2_device_audio_task.c
@@ -90,6 +90,7 @@
//_____ D E F I N I T I O N S ______________________________________________
+
#define FB_RATE_DELTA (1<<12)
//_____ D E C L A R A T I O N S ____________________________________________
@@ -98,10 +99,10 @@
//? why are these defined as statics?
static U32 index, spk_index;
-static U16 old_gap = SPK_BUFFER_SIZE;
+//static U16 old_gap = SPK_BUFFER_SIZE;
static U8 audio_buffer_out, spk_buffer_in; // the ID number of the buffer used for sending out to the USB
static volatile U32 *audio_buffer_ptr;
-static volatile U32 *spk_buffer_ptr;
+//static volatile U32 *spk_buffer_ptr;
static U8 ep_audio_in, ep_audio_out, ep_audio_out_fb;
@@ -116,7 +117,7 @@ void uac1_device_audio_task_init(U8 ep_in, U8 ep_out, U8 ep_out_fb)
audio_buffer_ptr = audio_buffer_0;
spk_index = 0;
spk_buffer_in = 0;
- spk_buffer_ptr = spk_buffer_0;
+// spk_buffer_ptr = spk_buffer_0;
mute = FALSE;
spk_mute = FALSE;
volume = 0x5000;
@@ -139,6 +140,7 @@ void uac1_device_audio_task_init(U8 ep_in, U8 ep_out, U8 ep_out_fb)
void uac1_device_audio_task(void *pvParameters)
{
+ Bool playerStarted = FALSE;
static U32 time=0;
static Bool startup=TRUE;
int i;
@@ -319,14 +321,17 @@ void uac1_device_audio_task(void *pvParameters)
if (Is_usb_full_speed_mode()) { // FB rate is 3 bytes in 10.14 format
- if ((gap < (SPK_BUFFER_SIZE/2)) && (gap < old_gap)) {
+ //if ((gap < (SPK_BUFFER_SIZE/2)) && (gap < old_gap)) {
+ if (gap < SPK_BUFFER_SIZE - 10 ) {
LED_Toggle(LED0);
FB_rate -= FB_RATE_DELTA;
- old_gap = gap;
- } else if ( (gap > (SPK_BUFFER_SIZE + (SPK_BUFFER_SIZE/2))) && (gap > old_gap)) {
+ //old_gap = gap;
+ } else
+ //if ( (gap > (SPK_BUFFER_SIZE + (SPK_BUFFER_SIZE/2))) && (gap > old_gap)) {
+ if ( gap > SPK_BUFFER_SIZE + 10 ) {
LED_Toggle(LED1);
FB_rate += FB_RATE_DELTA;
- old_gap = gap;
+ //old_gap = gap;
}
sample_LSB = FB_rate;
@@ -339,14 +344,17 @@ void uac1_device_audio_task(void *pvParameters)
// HS mode
// FB rate is 4 bytes in 12.14 format
- if ((gap < (SPK_BUFFER_SIZE/2)) && (gap < old_gap)){
+ //if ((gap < (SPK_BUFFER_SIZE/2)) && (gap < old_gap)){
+ if ( gap < SPK_BUFFER_SIZE - 10 ){
LED_Toggle(LED0);
FB_rate -= FB_RATE_DELTA;
- old_gap = gap;
- } else if ( (gap > (SPK_BUFFER_SIZE + (SPK_BUFFER_SIZE/2))) && (gap > old_gap)) {
+ //old_gap = gap;
+ } else
+ //if ( (gap > (SPK_BUFFER_SIZE + (SPK_BUFFER_SIZE/2))) && (gap > old_gap)) {
+ if ( gap > SPK_BUFFER_SIZE + 10 ) {
LED_Toggle(LED1);
FB_rate += FB_RATE_DELTA;
- old_gap = gap;
+ //old_gap = gap;
}
sample_LSB = FB_rate;
sample_SB = FB_rate >> 8;
@@ -365,6 +373,15 @@ void uac1_device_audio_task(void *pvParameters)
Usb_reset_endpoint_fifo_access(EP_AUDIO_OUT);
num_samples = Usb_byte_count(EP_AUDIO_OUT) / 6;
+ if(!playerStarted)
+ {
+ playerStarted = TRUE;
+ num_remaining = spk_pdca_channel->tcr;
+ if (spk_buffer_in != spk_buffer_out)
+ spk_buffer_in = 1 - spk_buffer_in;
+ spk_index = SPK_BUFFER_SIZE - num_remaining;
+ }
+
for (i = 0; i < num_samples; i++) {
if (spk_mute) {
sample_LSB = 0;
@@ -398,12 +415,14 @@ void uac1_device_audio_task(void *pvParameters)
if (spk_index >= SPK_BUFFER_SIZE){
spk_index = 0;
spk_buffer_in = 1 - spk_buffer_in;
- spk_buffer_ptr = spk_buffer_in ? spk_buffer_0 : spk_buffer_1;
+// spk_buffer_ptr = spk_buffer_in ? spk_buffer_0 : spk_buffer_1;
}
}
Usb_ack_out_received_free(EP_AUDIO_OUT);
} // end usb_out_received
} // end usb_alternate_setting_out == 1
+ else
+ playerStarted = FALSE;
//} // end startup else
} // end while vTask
Oops, something went wrong.

0 comments on commit c411669

Please sign in to comment.