luisf@665: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ luisf@665: luisf@665: /* luisf@665: Sonic Visualiser luisf@665: An audio file viewer and annotation editor. luisf@665: Centre for Digital Music, Queen Mary, University of London. Chris@748: This file copyright 2006-2012 Chris Cannam and QMUL. luisf@665: luisf@665: This program is free software; you can redistribute it and/or luisf@665: modify it under the terms of the GNU General Public License as luisf@665: published by the Free Software Foundation; either version 2 of the luisf@665: License, or (at your option) any later version. See the file luisf@665: COPYING included with this distribution for more information. luisf@665: */ luisf@665: luisf@665: #ifdef HAVE_COREAUDIO luisf@665: luisf@665: #include "CoreAudioFileReader.h" luisf@665: #include "base/Profiler.h" luisf@665: #include "base/ProgressReporter.h" luisf@665: #include "system/System.h" luisf@665: luisf@665: #include luisf@665: luisf@665: #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__) luisf@665: #include Chris@743: #include luisf@665: #else luisf@665: #include "AudioToolbox.h" luisf@665: #include "ExtendedAudioFile.h" luisf@665: #endif luisf@665: luisf@665: class CoreAudioFileReader::D luisf@665: { luisf@665: public: Chris@819: D() : blockSize(1024), valid(false) { } luisf@665: Chris@743: ExtAudioFileRef file; Chris@743: AudioBufferList buffer; Chris@743: OSStatus err; Chris@743: AudioStreamBasicDescription asbd; Chris@743: int blockSize; Chris@819: bool valid; luisf@665: }; luisf@665: Chris@743: static QString Chris@743: codestr(OSStatus err) Chris@743: { Chris@743: char text[5]; Chris@743: UInt32 uerr = err; Chris@743: text[0] = (uerr >> 24) & 0xff; Chris@743: text[1] = (uerr >> 16) & 0xff; Chris@743: text[2] = (uerr >> 8) & 0xff; Chris@743: text[3] = (uerr) & 0xff; Chris@743: text[4] = '\0'; Chris@794: return QString("%1 (%2)").arg(err).arg(QString::fromLocal8Bit(text)); Chris@743: } luisf@665: luisf@665: CoreAudioFileReader::CoreAudioFileReader(FileSource source, Chris@743: DecodeMode decodeMode, Chris@743: CacheMode mode, Chris@1040: sv_samplerate_t targetRate, Chris@920: bool normalised, Chris@743: ProgressReporter *reporter) : Chris@920: CodedAudioFileReader(mode, targetRate, normalised), luisf@665: m_source(source), luisf@665: m_path(source.getLocalFilename()), luisf@665: m_d(new D), luisf@665: m_reporter(reporter), luisf@665: m_cancelled(false), luisf@665: m_completion(0), luisf@665: m_decodeThread(0) luisf@665: { Chris@743: m_channelCount = 0; Chris@743: m_fileRate = 0; luisf@665: Chris@760: m_d->buffer.mBuffers[0].mData = 0; Chris@760: Chris@743: Profiler profiler("CoreAudioFileReader::CoreAudioFileReader", true); luisf@665: Chris@844: SVDEBUG << "CoreAudioFileReader: path is \"" << m_path << "\"" << endl; luisf@665: Chris@743: QByteArray ba = m_path.toLocal8Bit(); luisf@665: Chris@743: CFURLRef url = CFURLCreateFromFileSystemRepresentation Chris@743: (kCFAllocatorDefault, Chris@743: (const UInt8 *)ba.data(), Chris@743: (CFIndex)ba.length(), Chris@743: false); luisf@665: Chris@743: //!!! how do we find out if the file open fails because of DRM protection? luisf@665: matthiasm@773: //#if (MACOSX_DEPLOYMENT_TARGET <= 1040 && MAC_OS_X_VERSION_MIN_REQUIRED <= 1040) matthiasm@773: // FSRef fsref; matthiasm@773: // if (!CFURLGetFSRef(url, &fsref)) { // returns Boolean, not error code matthiasm@773: // m_error = "CoreAudioReadStream: Error looking up FS ref (file not found?)"; matthiasm@773: // return; matthiasm@773: // } matthiasm@773: // m_d->err = ExtAudioFileOpen(&fsref, &m_d->file); matthiasm@773: //#else Chris@743: m_d->err = ExtAudioFileOpenURL(url, &m_d->file); matthiasm@773: //#endif luisf@665: Chris@743: CFRelease(url); luisf@665: Chris@743: if (m_d->err) { Chris@743: m_error = "CoreAudioReadStream: Error opening file: code " + codestr(m_d->err); Chris@743: return; Chris@743: } Chris@743: if (!m_d->file) { Chris@743: m_error = "CoreAudioReadStream: Failed to open file, but no error reported!"; Chris@743: return; Chris@743: } Chris@743: Chris@743: UInt32 propsize = sizeof(AudioStreamBasicDescription); Chris@743: m_d->err = ExtAudioFileGetProperty Chris@743: (m_d->file, kExtAudioFileProperty_FileDataFormat, &propsize, &m_d->asbd); Chris@743: Chris@743: if (m_d->err) { Chris@743: m_error = "CoreAudioReadStream: Error in getting basic description: code " + codestr(m_d->err); Chris@819: ExtAudioFileDispose(m_d->file); Chris@743: return; Chris@743: } Chris@743: Chris@743: m_channelCount = m_d->asbd.mChannelsPerFrame; Chris@755: m_fileRate = m_d->asbd.mSampleRate; luisf@665: Chris@843: cerr << "CoreAudioReadStream: " << m_channelCount << " channels, " << m_fileRate << " Hz" << endl; luisf@665: Chris@743: m_d->asbd.mFormatID = kAudioFormatLinearPCM; Chris@743: m_d->asbd.mFormatFlags = Chris@743: kAudioFormatFlagIsFloat | Chris@743: kAudioFormatFlagIsPacked | Chris@743: kAudioFormatFlagsNativeEndian; Chris@743: m_d->asbd.mBitsPerChannel = sizeof(float) * 8; Chris@743: m_d->asbd.mBytesPerFrame = sizeof(float) * m_channelCount; Chris@743: m_d->asbd.mBytesPerPacket = sizeof(float) * m_channelCount; Chris@743: m_d->asbd.mFramesPerPacket = 1; Chris@743: m_d->asbd.mReserved = 0; Chris@743: Chris@743: m_d->err = ExtAudioFileSetProperty Chris@743: (m_d->file, kExtAudioFileProperty_ClientDataFormat, propsize, &m_d->asbd); Chris@743: Chris@743: if (m_d->err) { Chris@743: m_error = "CoreAudioReadStream: Error in setting client format: code " + codestr(m_d->err); Chris@819: ExtAudioFileDispose(m_d->file); Chris@743: return; Chris@743: } luisf@665: Chris@743: m_d->buffer.mNumberBuffers = 1; Chris@743: m_d->buffer.mBuffers[0].mNumberChannels = m_channelCount; Chris@743: m_d->buffer.mBuffers[0].mDataByteSize = sizeof(float) * m_channelCount * m_d->blockSize; Chris@743: m_d->buffer.mBuffers[0].mData = new float[m_channelCount * m_d->blockSize]; luisf@665: Chris@819: m_d->valid = true; Chris@819: Chris@743: initialiseDecodeCache(); luisf@665: Chris@743: if (m_reporter) { Chris@743: connect(m_reporter, SIGNAL(cancelled()), this, SLOT(cancelled())); Chris@743: m_reporter->setMessage Chris@743: (tr("Decoding %1...").arg(QFileInfo(m_path).fileName())); Chris@743: } luisf@665: Chris@743: while (1) { luisf@665: Chris@743: UInt32 framesRead = m_d->blockSize; Chris@743: m_d->err = ExtAudioFileRead(m_d->file, &framesRead, &m_d->buffer); luisf@665: Chris@743: if (m_d->err) { Chris@743: m_error = QString("Error in CoreAudio decoding: code %1") Chris@743: .arg(m_d->err); Chris@743: break; Chris@743: } luisf@665: Chris@743: //!!! progress? luisf@665: Chris@843: // cerr << "Read " << framesRead << " frames (block size " << m_d->blockSize << ")" << endl; luisf@665: Chris@743: // buffers are interleaved unless specified otherwise Chris@743: addSamplesToDecodeCache((float *)m_d->buffer.mBuffers[0].mData, framesRead); luisf@665: Chris@743: if (framesRead < m_d->blockSize) break; Chris@743: } luisf@665: Chris@743: finishDecodeCache(); Chris@743: endSerialised(); luisf@665: Chris@743: m_completion = 100; luisf@665: } luisf@665: luisf@665: luisf@665: CoreAudioFileReader::~CoreAudioFileReader() luisf@665: { Chris@843: cerr << "CoreAudioFileReader::~CoreAudioFileReader" << endl; Chris@819: Chris@819: if (m_d->valid) { Chris@819: ExtAudioFileDispose(m_d->file); Chris@819: delete[] m_d->buffer.mBuffers[0].mData; Chris@819: } Chris@819: Chris@743: delete m_d; luisf@665: } luisf@665: luisf@665: void luisf@665: CoreAudioFileReader::cancelled() luisf@665: { luisf@665: m_cancelled = true; luisf@665: } luisf@665: luisf@665: void luisf@665: CoreAudioFileReader::getSupportedExtensions(std::set &extensions) luisf@665: { Chris@743: extensions.insert("aiff"); Chris@743: extensions.insert("aif"); Chris@743: extensions.insert("au"); Chris@743: extensions.insert("m4a"); Chris@743: extensions.insert("m4b"); Chris@743: extensions.insert("m4p"); Chris@743: extensions.insert("mp3"); Chris@743: extensions.insert("mp4"); Chris@743: extensions.insert("wav"); luisf@665: } luisf@665: luisf@665: bool luisf@665: CoreAudioFileReader::supportsExtension(QString extension) luisf@665: { Chris@743: std::set extensions; Chris@743: getSupportedExtensions(extensions); Chris@743: return (extensions.find(extension.toLower()) != extensions.end()); luisf@665: } luisf@665: luisf@665: bool luisf@665: CoreAudioFileReader::supportsContentType(QString type) luisf@665: { Chris@743: return (type == "audio/x-aiff" || Chris@743: type == "audio/x-wav" || Chris@743: type == "audio/mpeg" || Chris@743: type == "audio/basic" || Chris@749: type == "audio/x-aac"); luisf@665: } luisf@665: luisf@665: bool luisf@665: CoreAudioFileReader::supports(FileSource &source) luisf@665: { Chris@743: return (supportsExtension(source.getExtension()) || Chris@743: supportsContentType(source.getContentType())); luisf@665: } luisf@665: luisf@665: #endif luisf@665: