<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi all,<br class=""><br class="">I'm trying to make a simple audio player using Libav and the new AVAudioEngine introduced in iOS 8 and OS X 10.10.<br class=""><br class="">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?<br class=""><br class="">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.<br class=""><br class="">This is how I set up the AVAudioEngine...<br class=""><br class=""><font color="#703daa" face="Menlo" class=""><span style="font-size: 11px;" class="">_engine = [[AVAudioEngine alloc] init];<br class="">_player = [[AVAudioPlayerNode alloc] init];<br class="">[self.engine attachNode:self.player];<br class="">    <br class="">AVAudioChannelCount outputChannelCount = 2;<br class="">double outputSampleRate = 48000;<br class="">    <br class="">AVAudioFormat *audioOutputFormat = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:outputSampleRate channels:outputChannelCount];<br class=""></span></font>    <br class="">AVAudioFormat is a wrapper around the good old AudioStreamBasicDescription which translates audioOutputFormat into this...<br class=""><br class=""><font color="#703daa" face="Menlo" class=""><span style="font-size: 11px;" class="">NSLog(@"audioOutputFormat: %@", audioOutputFormat.settings);<br class=""><br class=""></span></font><div class=""><font color="#703daa" face="Menlo" class=""><span style="font-size: 11px;" class="">2016-01-28 13:37:18.416 AudioQueuePlayer[53198:7804348] audioOutputFormat: {<br class="">    AVFormatIDKey = 1819304813;<br class="">    AVLinearPCMBitDepthKey = 32;<br class="">    AVLinearPCMIsBigEndianKey = 0;<br class="">    AVLinearPCMIsFloatKey = 1;<br class="">    AVLinearPCMIsNonInterleaved = 1;<br class="">    AVNumberOfChannelsKey = 2;<br class="">    AVSampleRateKey = 48000;<br class="">}</span></font><br class=""><br class="">I configure a PCM buffer to put some data into...<br class=""><br class=""></div><div class=""><font color="#703daa" face="Menlo" class=""><span style="font-size: 11px;" class="">AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:audioOutputFormat frameCapacity:bufferCapacity];<br class=""></span></font><br class="">Now I configure and init my SwrContext like this...<br class=""><br class=""><font color="#703daa" face="Menlo" class=""><span style="font-size: 11px;" class="">struct SwrContext *audioConvertContext;<br class="">audioConvertContext = swr_alloc_set_opts(NULL,<br class=""><span class="Apple-tab-span" style="white-space:pre">                          </span>av_get_default_channel_layout(outputChannelCount), // output 2 = stereo<br class="">                            <span class="Apple-tab-span" style="white-space:pre">      </span>AV_SAMPLE_FMT_FLT, // output float<br class="">                            <span class="Apple-tab-span" style="white-space:pre">   </span>outputSampleRate, // output sample rate<br class="">                         <span class="Apple-tab-span" style="white-space:pre">      </span>codecContext->channel_layout, // input channel layout<br class="">                           <span class="Apple-tab-span" style="white-space:pre">      </span>codecContext->sample_fmt, // input sample format<br class="">                              <span class="Apple-tab-span" style="white-space:pre">   </span>codecContext->sample_rate, // input sample rate<br class="">                            <span class="Apple-tab-span" style="white-space:pre">   </span>0,<br class="">                              <span class="Apple-tab-span" style="white-space:pre">    </span>NULL);<br class=""></span></font>                                             <br class="">Now on to the decoding part...<br class=""><br class=""><font color="#703daa" face="Menlo" class=""><span style="font-size: 11px;" class="">// decode the packet<br class="">bytesDecodec = avcodec_decode_audio4(codecContext, frame, &isFrameDecoded, &packet);<br class="">    <br class="">// calculate the size for our resampled buffer<br class="">swrBufferSize = av_samples_get_buffer_size(NULL, outputChannelCount, frame->nb_samples, AV_SAMPLE_FMT_FLT, 0);<br class="">                    <br class="">// a pointer to the buffer<br class="">uint8_t *bufPtr = (uint8_t *)buffer.floatChannelData[0];<br class="">bufPtr += buffer.frameLength;<br class=""><span class="Apple-tab-span" style="white-space:pre">       </span><br class="">// resample audio<br class="">int numSamplesConverted = swr_convert(audioConvertContext,<br class=""><span class="Apple-tab-span" style="white-space:pre">                                  </span>&bufPtr,<br class=""><span class="Apple-tab-span" style="white-space:pre">                                   </span>frame->nb_samples,<br class=""><span class="Apple-tab-span" style="white-space:pre">                                  </span>(const uint8_t **)frame->extended_data,<br class=""><span class="Apple-tab-span" style="white-space:pre">                                     </span>frame->nb_samples);<br class=""><span class="Apple-tab-span" style="white-space:pre">                                                                         </span>  <br class="">// advance the buffer and keep decoding until the buffer is full<br class="">buffer.frameLength += swrBufferSize;</span></font><br class=""><span class="Apple-tab-span" style="white-space:pre">   </span><br class="">When the buffer is full I play it like so...<br class=""><br class=""><font color="#703daa" face="Menlo" class=""><span style="font-size: 11px;" class="">[self.player scheduleBuffer:buffer completionHandler:nil];<br class="">[self.player play];</span></font><br class="">    <br class="">As mentioned above I can hear the sound perfectly, but at half speed/rate. In slow motion!<br class=""><br class="">The weird thing is that if I double my outputSampleRate to 96000, the sound plays perfectly???<br class=""><br class="">But I don't want the outputSampleRate to be 96000, I want it to be 48000.</div><div class=""><br class=""></div><div class="">I’m obviously not very experienced in this field.<br class=""><br class="">Any of you sound wizards out there who can help me??? Please.<br class=""><br class="">Best regards / Jörgen</div><div class=""><br class=""></div></body></html>