LoaderSourceFile::LoaderSourceFile( SourceFile *source, Target *target ) : mSource( source ), mPacketOffset( 0 ) { AudioStreamBasicDescription sourceDescription; sourceDescription.mFormatID = source->mNativeFormatId; //kAudioFormatLinearPCM; sourceDescription.mFormatFlags = source->mNativeFormatFlags; //CalculateLPCMFlags( mSource->getBitsPerSample(), mSource->getBlockAlign() * 8, false, false, false ); sourceDescription.mSampleRate = source->getSampleRate(); sourceDescription.mBytesPerPacket = source->mBytesPerPacket; //( mSource->getBitsPerSample() * mSource->getChannelCount() ) / 8; sourceDescription.mFramesPerPacket = source->mFramesPerPacket; //1; sourceDescription.mBytesPerFrame = source->mBytesPerFrame; //( mSource->getBitsPerSample() * mSource->getChannelCount() ) / 8; sourceDescription.mChannelsPerFrame = source->getChannelCount(); sourceDescription.mBitsPerChannel = source->getBitsPerSample(); AudioStreamBasicDescription targetDescription; if( ! target->isPcm() ) { throw IoExceptionUnsupportedDataFormat(); } //right now this always converts to linear PCM --that's probably ok targetDescription.mFormatID = kAudioFormatLinearPCM; //target->mNativeFormatId; targetDescription.mFormatFlags = CalculateLPCMFlags( target->getBitsPerSample(), target->getBlockAlign() * 8, target->isFloat(), target->isBigEndian(), ( ! target->isInterleaved() ) ); //target->mNativeFormatFlags targetDescription.mSampleRate = target->getSampleRate(); targetDescription.mBytesPerPacket = ( mSource->getBitsPerSample() * mSource->getChannelCount() ) / 8; //target->mBytesPerPacket; targetDescription.mFramesPerPacket = 1; //target->mFramesPerPacket; targetDescription.mBytesPerFrame = ( mSource->getBlockAlign() ); //target->mBytesPerFrame; targetDescription.mChannelsPerFrame = target->getChannelCount(); targetDescription.mBitsPerChannel = target->getBitsPerSample(); mConverter = shared_ptr<CocoaCaConverter>( new CocoaCaConverter( this, &LoaderSourceFile::dataInputCallback, sourceDescription, targetDescription, mSource->mMaxPacketSize ) ); }
CocoaCaConverter::CocoaCaConverter( Loader * aLoader, LoaderFunction loaderFn, const AudioStreamBasicDescription& sourceDescription, const AudioStreamBasicDescription& targetDescription, uint32_t maxPacketSize ) : mLoader( aLoader ), mCurrentPacketDescriptions( NULL ), mLoaderFunction( loaderFn ), mMaxPacketSize( maxPacketSize ) { mConverterBuffer.mNumberBuffers = 0; mConverterBuffer.mBuffers = NULL; OSStatus err = noErr; err = AudioConverterNew( &sourceDescription, &targetDescription, &mConverter ); if( err ) { throw IoExceptionUnsupportedDataFormat(); /*switch(err) { case kAudioConverterErr_FormatNotSupported: std::cout << "kAudioConverterErr_FormatNotSupported" << std::endl; break; case kAudioConverterErr_OperationNotSupported: std::cout << "kAudioConverterErr_OperationNotSupported" << std::endl; break; case kAudioConverterErr_PropertyNotSupported: std::cout << "kAudioConverterErr_PropertyNotSupported" << std::endl; break; case kAudioConverterErr_InvalidInputSize: std::cout << "kAudioConverterErr_InvalidInputSize" << std::endl; break; case kAudioConverterErr_InvalidOutputSize: std::cout << "kAudioConverterErr_InvalidOutputSize" << std::endl; break; case kAudioConverterErr_UnspecifiedError: std::cout << "kAudioConverterErr_UnspecifiedError" << std::endl; break; case kAudioConverterErr_BadPropertySizeError: std::cout << "kAudioConverterErr_BadPropertySizeError" << std::endl; break; case kAudioConverterErr_RequiresPacketDescriptionsError: std::cout << "kAudioConverterErr_RequiresPacketDescriptionsError" << std::endl; break; case kAudioConverterErr_InputSampleRateOutOfRange: std::cout << "kAudioConverterErr_InputSampleRateOutOfRange" << std::endl; break; case kAudioConverterErr_OutputSampleRateOutOfRange: std::cout << "kAudioConverterErr_OutputSampleRateOutOfRange" << std::endl; break; }*/ } }