- /* Select the audio stream endpoint */\r
- Endpoint_SelectEndpoint(AUDIO_STREAM_EPNUM);\r
- \r
- /* Check if the current endpoint can be read from (contains a packet) and that the next sample should be read */\r
- if (Endpoint_IsOUTReceived() && (TIFR0 & (1 << OCF0A)))\r
- {\r
- /* Clear the sample reload timer */\r
- TIFR0 |= (1 << OCF0A);\r
-\r
- /* Retrieve the signed 16-bit left and right audio samples */\r
- int16_t LeftSample_16Bit = (int16_t)Endpoint_Read_Word_LE();\r
- int16_t RightSample_16Bit = (int16_t)Endpoint_Read_Word_LE();\r
-\r
- /* Check to see if the bank is now empty */\r
- if (!(Endpoint_IsReadWriteAllowed()))\r
- {\r
- /* Acknowledge the packet, clear the bank ready for the next packet */\r
- Endpoint_ClearOUT();\r
- }\r
-\r
- /* Massage signed 16-bit left and right audio samples into signed 8-bit */\r
- int8_t LeftSample_8Bit = (LeftSample_16Bit >> 8);\r
- int8_t RightSample_8Bit = (RightSample_16Bit >> 8);\r
- \r
-#if defined(AUDIO_OUT_MONO)\r
- /* Mix the two channels together to produce a mono, 8-bit sample */\r
- int8_t MixedSample_8Bit = (((int16_t)LeftSample_8Bit + (int16_t)RightSample_8Bit) >> 1);\r
-\r
- /* Load the sample into the PWM timer channel */\r
- OCRxA = ((uint8_t)MixedSample_8Bit ^ (1 << 7));\r
-#elif defined(AUDIO_OUT_STEREO)\r
- /* Load the dual 8-bit samples into the PWM timer channels */\r
- OCRxA = ((uint8_t)LeftSample_8Bit ^ (1 << 7));\r
- OCRxB = ((uint8_t)RightSample_8Bit ^ (1 << 7));\r
-#elif defined(AUDIO_OUT_PORTC)\r
- /* Mix the two channels together to produce a mono, 8-bit sample */\r
- int8_t MixedSample_8Bit = (((int16_t)LeftSample_8Bit + (int16_t)RightSample_8Bit) >> 1);\r
-\r
- PORTC = MixedSample_8Bit;\r
-#else\r
- uint8_t LEDMask = LEDS_NO_LEDS;\r
-\r
- /* Make left channel positive (absolute) */\r
- if (LeftSample_8Bit < 0)\r
- LeftSample_8Bit = -LeftSample_8Bit;\r
-\r
- /* Make right channel positive (absolute) */\r
- if (RightSample_8Bit < 0)\r
- RightSample_8Bit = -RightSample_8Bit;\r
-\r
- /* Set first LED based on sample value */\r
- if (LeftSample_8Bit < ((128 / 8) * 1))\r
- LEDMask |= LEDS_LED2;\r
- else if (LeftSample_8Bit < ((128 / 8) * 3))\r
- LEDMask |= (LEDS_LED1 | LEDS_LED2);\r
- else\r
- LEDMask |= LEDS_LED1;\r
-\r
- /* Set second LED based on sample value */\r
- if (RightSample_8Bit < ((128 / 8) * 1))\r
- LEDMask |= LEDS_LED4;\r
- else if (RightSample_8Bit < ((128 / 8) * 3))\r
- LEDMask |= (LEDS_LED3 | LEDS_LED4);\r
- else\r
- LEDMask |= LEDS_LED3;\r
- \r
- LEDs_SetAllLEDs(LEDMask);\r
-#endif\r
- }\r