derekli66 / Learning-Core-Audio-Swift-SampleCode

Swift sample code for the book, Learning Core Audio. The original sample code was written in C/Objective-C but I tried to make it in Swift version.
MIT License
156 stars 27 forks source link

CH06_AudioConverter Error: AudioConverterFillComplexBuffer OSStatus -50 #3

Closed little-dandan closed 4 years ago

little-dandan commented 4 years ago

use AudioConverterFillComplexBuffer OSStatus: -50 in your demo。

derekli66 commented 4 years ago

Hi little-dandan,

I checked the OSStatus, -50. It's an audio parameter error.
See on OSStatus.com.

  1. Would you like to share your testing conditions when you run this demo code? I am also curious about when this error raises.

  2. Do you mean to check the OSStatus, -50 and log the error in the code ?

little-dandan commented 4 years ago

Thank you for your answer: as i run your demo, the acquired audio file can not play. its duration is 00:00, you can try it (CH06_AudioConverter). My Xcode version is: Version 11.3.1(11C504) Thank you

derekli66 commented 4 years ago

The issue came from using the latest API AudioFileReadPacketData. In the original sample code which was written in C language, it used the old API AudioFileReadPackets which is fine to use zero value of outNumBytes. But, int the new API AudioFileReadPacketData , it is no longer using outNumBytes and uses ioNumBytes instead. That means we need to specified the correct size of out buffer in bytes.

New Implementation like below

var outByteCount: UInt32 = ioDataPacketCount.pointee * UInt32(audioConverterSettings.inputFilePacketMaxSize)
audioConverterSettings.sourceBuffer = calloc(1, Int(outByteCount))
var result = AudioFileReadPacketData(audioConverterSettings.inputFile!,
                                         true,
                                         &outByteCount,
                                         audioConverterSettings.inputFilePacketDescriptions,
                                         Int64(audioConverterSettings.inputFilePacketIndex),
                                         ioDataPacketCount,
                                         audioConverterSettings.sourceBuffer)

Please check the commit f5b4591c44a54c726bc53690e20e3e669f62a46c

Thanks for @little-dandan to help correct this error.

little-dandan commented 4 years ago

Thank you: But there are new problems. output file duration is very short. You can open the compressed audio file, the sound content will play very fast, completely can not hear the content.