Audio Blocks

benyboy

Member
Hi , I am trying send decoded audio packets into 2 x audio blocks for stereo audio.

I am sending the LEFT channel to transmit (block, 0); and the RIGHT channel data to transmit(block ,1 ); using the indexing in block.

I am suspicious this is correct although I do get noise coming out from left and right channels.

packetSwitch just alternates between LEFT audio channel packet and RIGHT audio channel packet.

When I have decoded the RIGHT channel audio data I try to copy both LEFT and RIGHT channel to the block using indexing.

Should I release(block); twice ?

Do I have the basic concept correct using indexing with transmit(block,0); for LEFT and transmit(block,1); for RIGHT audio channel ?

thanks,

Ben




Code:
int32_t AudioInputOpusDec::putData(uint8_t* compressedBuffer, int32_t bufferSize)
{
	

	if (packetSwitch == 0) {

	__disable_irq();
	memcpy(decoder_frame_buf_compressed_LEFT, compressedBuffer, bufferSize);
	decoder_compressed_frame_size = bufferSize;
	__enable_irq();

	 Serial.print(" copied LEFT compressed packets in = ");
	 Serial.println(packetSwitch);
	packetSwitch = 1;
	return inputPacketPhase;
}

     if (packetSwitch == 1) {

	 __disable_irq();
     memcpy(decoder_frame_buf_compressed_RIGHT, compressedBuffer, bufferSize);
     decoder_compressed_frame_size = bufferSize;
	 __enable_irq();

	  Serial.print(" copied RIGHT compressed packets in = ");
	  Serial.println(packetSwitch);
	 packetSwitch = 0;
	 return inputPacketPhase;
}
} // END


void AudioInputOpusDec::update(void)
{
	inputPacketPhase = 0;
	audio_block_t* block;

	block = allocate();

	  if (block) {

		  if (packetSwitch == 0) {


			  if (decoderInitialised)
			  {
				  decoder_decompressed_frame_size = opus_decode(m_opus_decoder_state, decoder_frame_buf_compressed_LEFT, decoder_compressed_frame_size, decoder_frame_buf_uncompressed_LEFT, CONFIG_AUDIO_FRAME_SIZE_SAMPLES, 0);
			  }

			  Serial.print("LEFT decompressed frame size =");
			  Serial.printf(" %d", decoder_decompressed_frame_size);
			  Serial.println();

			  // lets wait until we also have the RIGHT channel audio samples before we copy to block


		  }

			  if (packetSwitch == 1) {

				  if (decoderInitialised)
				  {
					  decoder_decompressed_frame_size = opus_decode(m_opus_decoder_state, decoder_frame_buf_compressed_RIGHT, decoder_compressed_frame_size, decoder_frame_buf_uncompressed_RIGHT, CONFIG_AUDIO_FRAME_SIZE_SAMPLES, 0);
				  }

				  Serial.print("RIGHT decompressed frame size =");
				  Serial.printf(" %d", decoder_decompressed_frame_size);
				  Serial.println();


				  // copy LEFT and RIGHT audio samples into teensy audio lib arrays.

				  __disable_irq();
				  memcpy(block->data, decoder_frame_buf_uncompressed_LEFT, decoder_decompressed_frame_size);
				  __enable_irq();
				  
				  transmit(block, 0);
				  release(block);

				  __disable_irq();
				  memcpy(block->data, decoder_frame_buf_uncompressed_RIGHT, decoder_decompressed_frame_size);
				  __enable_irq();
				
				  transmit(block, 1);
				  release(block);
			  }

		  

		} // END if block
	} // END void
 
2 blocks that point to 2 index's

ok, in case anyone else has the same question , I think the update code for 2 channels should be more like this.

I allocate 2 blocks but transmit the index 0 or 1 for left and right channel.

Code:
void AudioInputOpusDec::update(void)
{
    inputPacketPhase = 0;

    audio_block_t* blockL;
    audio_block_t* blockR;

    blockL= allocate();
    blockR= allocate();

        if (blockL) {

                if (decoderInitialised)
                {
                  decoder_decompressed_frame_size = opus_decode(m_opus_decoder_state, decoder_frame_buf_compressed_LEFT, decoder_compressed_frame_size, blockL->data, CONFIG_AUDIO_FRAME_SIZE_SAMPLES*sizeof(opus_int16), 0);
                }

             //   Serial.print("LEFT decompressed frame size =");
             //   Serial.printf(" %d", decoder_decompressed_frame_size);
             //   Serial.println();

                transmit(blockL, 0);
                release(blockL);
            
        } // END if (blockL ) 

        if (blockR) {
                  
                  if (decoderInitialised)
                  {
                    decoder_decompressed_frame_size = opus_decode(m_opus_decoder_state, decoder_frame_buf_compressed_RIGHT, decoder_compressed_frame_size, blockR->data, CONFIG_AUDIO_FRAME_SIZE_SAMPLES, 0);
                  }

                //   Serial.print("RIGHT decompressed frame size =");
                //  Serial.printf(" %d", decoder_decompressed_frame_size);
                //  Serial.println();

                  
                  transmit(blockR, 1);  
                  release(blockR);
                
        } // END if blockR
} // END void
 
Back
Top