Hello everyone!
I am learning sound programming on Linux. In my app I am using ffmpeg to decode the audio, and then ALSA to pass it on to speakers. So far everything is fine - the packets are retrieved, stored in a queue. After that, the packets are read from the queue, decoded and the result passed to ALSA. Now my problem is this - the program plays the audio as fast as it can, and not at the speed it supposed to. I have read somewhere that I need some buffer or something, but I have not found any information about it. I would really appreciate if someone could point me int the right direction. Thanks! Here is the playback loop -

while (1)
{
   packet_queue_get(&audioq, &packet, 0);
   long lAudioBytesRemaining= packet.size;
   uint8_t  * pAudioRawData=packet.data;
   int bytesDecoded = -1; 
		
				
   int iNumAudioBytes = AVCODEC_MAX_AUDIO_FRAME_SIZE;
   long lSize = lAudioBytesRemaining  + FF_INPUT_BUFFER_PADDING_SIZE;
   uint8_t  * pIn = new uint8_t[lSize];
   memset(pIn, 0, lSize);
   memcpy(pIn, pAudioRawData, lAudioBytesRemaining);

   //This function decodes the audio, stores the result in buffer
   bytesDecoded = avcodec_decode_audio2(aCodecCtx, buffer,
                               &iNumAudioBytes, 
                               pIn, lAudioBytesRemaining);
			
   delete pIn;
			
   if(bytesDecoded <= 0)
   {
         cerr << "* Decoding Error!" << endl;
         break;
    }
    else
    {
         //This function takes the buffer and passes its content to sound card
         alsaPlay((void*)buffer, bytesDecoded);
    }

}

ok... lets put it another way -
how can I make a 1D array of decoded samples play at normal speed, and not as fast as possible? Anybody?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.