[Libav-user] AVAudioEngine audio playing in slow-motion?

Isaksson Jörgen jogga at bitfield.se
Thu Jan 28 17:12:59 CET 2016

Hi all,

I'm trying to make a simple audio player using Libav and the new AVAudioEngine introduced in iOS 8 and OS X 10.10.

I have gotten to the point where it actually plays audio decoded by Libav, but the problem is that it's playing at half speed?

Surely I must have mixed up something in swr_convert or else where, but I just can't figure out how to fix the problem.

This is how I set up the AVAudioEngine...

_engine = [[AVAudioEngine alloc] init];
_player = [[AVAudioPlayerNode alloc] init];
[self.engine attachNode:self.player];
AVAudioChannelCount outputChannelCount = 2;
double outputSampleRate = 48000;
AVAudioFormat *audioOutputFormat = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:outputSampleRate channels:outputChannelCount];
AVAudioFormat is a wrapper around the good old AudioStreamBasicDescription which translates audioOutputFormat into this...

NSLog(@"audioOutputFormat: %@", audioOutputFormat.settings);

2016-01-28 13:37:18.416 AudioQueuePlayer[53198:7804348] audioOutputFormat: {
    AVFormatIDKey = 1819304813;
    AVLinearPCMBitDepthKey = 32;
    AVLinearPCMIsBigEndianKey = 0;
    AVLinearPCMIsFloatKey = 1;
    AVLinearPCMIsNonInterleaved = 1;
    AVNumberOfChannelsKey = 2;
    AVSampleRateKey = 48000;

I configure a PCM buffer to put some data into...

AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:audioOutputFormat frameCapacity:bufferCapacity];

Now I configure and init my SwrContext like this...

struct SwrContext *audioConvertContext;
audioConvertContext = swr_alloc_set_opts(NULL,
				av_get_default_channel_layout(outputChannelCount), // output 2 = stereo
                            	AV_SAMPLE_FMT_FLT, // output float
                            	outputSampleRate, // output sample rate
                         	codecContext->channel_layout, // input channel layout
                           	codecContext->sample_fmt, // input sample format
                              	codecContext->sample_rate, // input sample rate
Now on to the decoding part...

// decode the packet
bytesDecodec = avcodec_decode_audio4(codecContext, frame, &isFrameDecoded, &packet);
// calculate the size for our resampled buffer
swrBufferSize = av_samples_get_buffer_size(NULL, outputChannelCount, frame->nb_samples, AV_SAMPLE_FMT_FLT, 0);
// a pointer to the buffer
uint8_t *bufPtr = (uint8_t *)buffer.floatChannelData[0];
bufPtr += buffer.frameLength;
// resample audio
int numSamplesConverted = swr_convert(audioConvertContext,
					(const uint8_t **)frame->extended_data,
// advance the buffer and keep decoding until the buffer is full
buffer.frameLength += swrBufferSize;
When the buffer is full I play it like so...

[self.player scheduleBuffer:buffer completionHandler:nil];
[self.player play];
As mentioned above I can hear the sound perfectly, but at half speed/rate. In slow motion!

The weird thing is that if I double my outputSampleRate to 96000, the sound plays perfectly???

But I don't want the outputSampleRate to be 96000, I want it to be 48000.

I’m obviously not very experienced in this field.

Any of you sound wizards out there who can help me??? Please.

Best regards / Jörgen

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20160128/d840c50c/attachment.html>

More information about the Libav-user mailing list