Mercurial > hg > svcore
changeset 751:8b79ad680ee7
Merge
author | Chris Cannam |
---|---|
date | Wed, 26 Sep 2012 21:28:14 +0100 |
parents | 5bfc4930588d (current diff) 0f8a6dda8ac1 (diff) |
children | c9fc7728ab0d |
files | |
diffstat | 9 files changed, 395 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgtags Wed Sep 26 21:27:10 2012 +0100 +++ b/.hgtags Wed Sep 26 21:28:14 2012 +0100 @@ -28,3 +28,5 @@ 579b2da21e7ad4e661e865ba7104c286fac3d510 sv_v1.9 579b2da21e7ad4e661e865ba7104c286fac3d510 sv_v1.9 9a0272c2d596b144ab145466cd8752c196a2016d sv_v1.9 +aca6e61eaea35a9c3f865a68521820b17cce7d3d sv_v2.0 +383f0b9458d2910c0ad70759e1f44da39ef1b196 sonic-annotator-0.7
--- a/config.pri.in Wed Sep 26 21:27:10 2012 +0100 +++ b/config.pri.in Wed Sep 26 21:28:14 2012 +0100 @@ -1,4 +1,3 @@ - CONFIG += @QMAKE_CONFIG@ DEFINES += @HAVES@ @@ -12,8 +11,8 @@ linux*:LIBS += -lasound -macx*:DEFINES += HAVE_QUICKTIME -macx*:LIBS += -framework QuickTime -framework CoreAudio -framework CoreMidi -framework AudioUnit -framework AudioToolbox -framework CoreFoundation -framework CoreServices +macx*:DEFINES += HAVE_COREAUDIO +macx*:LIBS += -framework CoreAudio -framework CoreMidi -framework AudioUnit -framework AudioToolbox -framework CoreFoundation -framework CoreServices LIBS += @LIBS@
--- a/configure Wed Sep 26 21:27:10 2012 +0100 +++ b/configure Wed Sep 26 21:28:14 2012 +0100 @@ -624,7 +624,6 @@ ac_subst_vars='LTLIBOBJS LIBOBJS -CODENAME QMAKE_CONFIG HAVES CXXFLAGS_MINIMAL @@ -7032,10 +7031,9 @@ - - ac_config_files="$ac_config_files config.pri" + cat >confcache <<\_ACEOF # This file is a shell script that caches the results of configure # tests run on this system so they can be shared between configure
--- a/configure.ac Wed Sep 26 21:27:10 2012 +0100 +++ b/configure.ac Wed Sep 26 21:28:14 2012 +0100 @@ -111,9 +111,9 @@ AC_SUBST(QMAKE_CONFIG) -AC_SUBST(CODENAME) +AC_CONFIG_FILES([config.pri]) -AC_OUTPUT([config.pri]) +AC_OUTPUT if ! $QMAKE -r; then AC_MSG_ERROR([qmake failed: Command was "$QMAKE -r"])
--- a/data/fileio/AudioFileReaderFactory.cpp Wed Sep 26 21:27:10 2012 +0100 +++ b/data/fileio/AudioFileReaderFactory.cpp Wed Sep 26 21:28:14 2012 +0100 @@ -20,6 +20,7 @@ #include "OggVorbisFileReader.h" #include "MP3FileReader.h" #include "QuickTimeFileReader.h" +#include "CoreAudioFileReader.h" #include <QString> #include <QFileInfo> @@ -42,6 +43,9 @@ #ifdef HAVE_QUICKTIME QuickTimeFileReader::getSupportedExtensions(extensions); #endif +#ifdef HAVE_COREAUDIO + CoreAudioFileReader::getSupportedExtensions(extensions); +#endif QString rv; for (std::set<QString>::const_iterator i = extensions.begin(); @@ -73,7 +77,7 @@ { QString err; -// SVDEBUG << "AudioFileReaderFactory::createReader(\"" << source.getLocation() << "\"): Requested rate: " << targetRate << endl; + SVDEBUG << "AudioFileReaderFactory::createReader(\"" << source.getLocation() << "\"): Requested rate: " << targetRate << endl; if (!source.isOK()) { std::cerr << "AudioFileReaderFactory::createReader(\"" << source.getLocation() << "\": Failed to retrieve source (transmission error?): " << source.getErrorString() << std::endl; @@ -175,6 +179,26 @@ } #endif +#ifdef HAVE_COREAUDIO + if (!reader) { + if (CoreAudioFileReader::supports(source)) { + reader = new CoreAudioFileReader + (source, + threading ? + CoreAudioFileReader::DecodeThreaded : + CoreAudioFileReader::DecodeAtOnce, + CoreAudioFileReader::CacheInTemporaryFile, + targetRate, + reporter); + if (!reader->isOK()) { + delete reader; + reader = 0; + } + } + } +#endif + + // If none of the readers claimed to support this file extension, // perhaps the extension is missing or misleading. Try again, // ignoring it. We have to be confident that the reader won't @@ -264,9 +288,30 @@ } #endif +#ifdef HAVE_COREAUDIO + + std::cerr << "AudioFileReaderFactory: HAVE_COREAUDIO" << std::endl; + + if (!reader) { + reader = new CoreAudioFileReader + (source, + threading ? + CoreAudioFileReader::DecodeThreaded : + CoreAudioFileReader::DecodeAtOnce, + CoreAudioFileReader::CacheInTemporaryFile, + targetRate, + reporter); + + if (!reader->isOK()) { + delete reader; + reader = 0; + } + } +#endif + if (reader) { if (reader->isOK()) { -// std::cerr << "AudioFileReaderFactory: Reader is OK" << std::endl; + std::cerr << "AudioFileReaderFactory: Reader is OK" << std::endl; return reader; } std::cerr << "AudioFileReaderFactory: Preferred reader for "
--- a/data/fileio/CodedAudioFileReader.cpp Wed Sep 26 21:27:10 2012 +0100 +++ b/data/fileio/CodedAudioFileReader.cpp Wed Sep 26 21:28:14 2012 +0100 @@ -41,7 +41,7 @@ m_resampler(0), m_resampleBuffer(0) { -// SVDEBUG << "CodedAudioFileReader::CodedAudioFileReader: rate " << targetRate << endl; + SVDEBUG << "CodedAudioFileReader::CodedAudioFileReader: rate " << targetRate << endl; m_frameCount = 0; m_sampleRate = targetRate; @@ -55,7 +55,7 @@ if (m_cacheFileWritePtr) sf_close(m_cacheFileWritePtr); -// SVDEBUG << "CodedAudioFileReader::~CodedAudioFileReader: deleting cache file reader" << endl; + SVDEBUG << "CodedAudioFileReader::~CodedAudioFileReader: deleting cache file reader" << endl; delete m_cacheFileReader; delete[] m_cacheWriteBuffer; @@ -73,7 +73,7 @@ void CodedAudioFileReader::startSerialised(QString id) { -// SVDEBUG << "CodedAudioFileReader::startSerialised(" << id << ")" << endl; + SVDEBUG << "CodedAudioFileReader::startSerialised(" << id << ")" << endl; delete m_serialiser; m_serialiser = new Serialiser(id); @@ -82,7 +82,7 @@ void CodedAudioFileReader::endSerialised() { -// std::cerr << "CodedAudioFileReader(" << this << ")::endSerialised: id = " << (m_serialiser ? m_serialiser->getId().toStdString() : "(none)") << std::endl; + std::cerr << "CodedAudioFileReader(" << this << ")::endSerialised: id = " << (m_serialiser ? m_serialiser->getId().toStdString() : "(none)") << std::endl; delete m_serialiser; m_serialiser = 0; @@ -93,7 +93,7 @@ { QMutexLocker locker(&m_cacheMutex); -// SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: file rate = " << m_fileRate << endl; + SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: file rate = " << m_fileRate << endl; if (m_fileRate == 0) { SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: ERROR: File sample rate unknown (bug in subclass implementation?)" << endl;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/fileio/CoreAudioFileReader.cpp Wed Sep 26 21:28:14 2012 +0100 @@ -0,0 +1,242 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2006-2012 Chris Cannam and QMUL. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. + */ + +#ifdef HAVE_COREAUDIO + +#include "CoreAudioFileReader.h" +#include "base/Profiler.h" +#include "base/ProgressReporter.h" +#include "system/System.h" + +#include <QFileInfo> + +#if !defined(__COREAUDIO_USE_FLAT_INCLUDES__) +#include <AudioToolbox/AudioToolbox.h> +#include <AudioToolbox/ExtendedAudioFile.h> +#else +#include "AudioToolbox.h" +#include "ExtendedAudioFile.h" +#endif + +class CoreAudioFileReader::D +{ +public: + D() : blockSize(1024) { } + + ExtAudioFileRef file; + AudioBufferList buffer; + OSStatus err; + AudioStreamBasicDescription asbd; + int blockSize; +}; + +static QString +codestr(OSStatus err) +{ + char text[5]; + UInt32 uerr = err; + text[0] = (uerr >> 24) & 0xff; + text[1] = (uerr >> 16) & 0xff; + text[2] = (uerr >> 8) & 0xff; + text[3] = (uerr) & 0xff; + text[4] = '\0'; + return QString("%1 (%2)").arg(err).arg(QString::fromAscii(text)); +} + +CoreAudioFileReader::CoreAudioFileReader(FileSource source, + DecodeMode decodeMode, + CacheMode mode, + size_t targetRate, + ProgressReporter *reporter) : + CodedAudioFileReader(mode, targetRate), + m_source(source), + m_path(source.getLocalFilename()), + m_d(new D), + m_reporter(reporter), + m_cancelled(false), + m_completion(0), + m_decodeThread(0) +{ + m_channelCount = 0; + m_fileRate = 0; + + Profiler profiler("CoreAudioFileReader::CoreAudioFileReader", true); + + std::cerr << "CoreAudioFileReader: path is \"" << m_path.toStdString() << "\"" << std::endl; + + QByteArray ba = m_path.toLocal8Bit(); + + CFURLRef url = CFURLCreateFromFileSystemRepresentation + (kCFAllocatorDefault, + (const UInt8 *)ba.data(), + (CFIndex)ba.length(), + false); + + //!!! how do we find out if the file open fails because of DRM protection? + +#if (MACOSX_DEPLOYMENT_TARGET <= 1040 && MAC_OS_X_VERSION_MIN_REQUIRED <= 1040) + FSRef fsref; + if (!CFURLGetFSRef(url, &fsref)) { // returns Boolean, not error code + m_error = "CoreAudioReadStream: Error looking up FS ref (file not found?)"; + return; + } + m_d->err = ExtAudioFileOpen(&fsref, &m_d->file); +#else + m_d->err = ExtAudioFileOpenURL(url, &m_d->file); +#endif + + CFRelease(url); + + if (m_d->err) { + m_error = "CoreAudioReadStream: Error opening file: code " + codestr(m_d->err); + return; + } + if (!m_d->file) { + m_error = "CoreAudioReadStream: Failed to open file, but no error reported!"; + return; + } + + UInt32 propsize = sizeof(AudioStreamBasicDescription); + m_d->err = ExtAudioFileGetProperty + (m_d->file, kExtAudioFileProperty_FileDataFormat, &propsize, &m_d->asbd); + + if (m_d->err) { + m_error = "CoreAudioReadStream: Error in getting basic description: code " + codestr(m_d->err); + return; + } + + m_channelCount = m_d->asbd.mChannelsPerFrame; + m_sampleRate = m_d->asbd.mSampleRate; + + std::cerr << "CoreAudioReadStream: " << m_channelCount << " channels, " << m_sampleRate << " Hz" << std::endl; + + m_d->asbd.mSampleRate = getSampleRate(); + + m_d->asbd.mFormatID = kAudioFormatLinearPCM; + m_d->asbd.mFormatFlags = + kAudioFormatFlagIsFloat | + kAudioFormatFlagIsPacked | + kAudioFormatFlagsNativeEndian; + m_d->asbd.mBitsPerChannel = sizeof(float) * 8; + m_d->asbd.mBytesPerFrame = sizeof(float) * m_channelCount; + m_d->asbd.mBytesPerPacket = sizeof(float) * m_channelCount; + m_d->asbd.mFramesPerPacket = 1; + m_d->asbd.mReserved = 0; + + m_d->err = ExtAudioFileSetProperty + (m_d->file, kExtAudioFileProperty_ClientDataFormat, propsize, &m_d->asbd); + + if (m_d->err) { + m_error = "CoreAudioReadStream: Error in setting client format: code " + codestr(m_d->err); + return; + } + + m_d->buffer.mNumberBuffers = 1; + m_d->buffer.mBuffers[0].mNumberChannels = m_channelCount; + m_d->buffer.mBuffers[0].mDataByteSize = sizeof(float) * m_channelCount * m_d->blockSize; + m_d->buffer.mBuffers[0].mData = new float[m_channelCount * m_d->blockSize]; + + initialiseDecodeCache(); + + if (m_reporter) { + connect(m_reporter, SIGNAL(cancelled()), this, SLOT(cancelled())); + m_reporter->setMessage + (tr("Decoding %1...").arg(QFileInfo(m_path).fileName())); + } + + while (1) { + + UInt32 framesRead = m_d->blockSize; + m_d->err = ExtAudioFileRead(m_d->file, &framesRead, &m_d->buffer); + + if (m_d->err) { + m_error = QString("Error in CoreAudio decoding: code %1") + .arg(m_d->err); + break; + } + + //!!! progress? + + // std::cerr << "Read " << framesRead << " frames (block size " << m_d->blockSize << ")" << std::endl; + + // buffers are interleaved unless specified otherwise + addSamplesToDecodeCache((float *)m_d->buffer.mBuffers[0].mData, framesRead); + + if (framesRead < m_d->blockSize) break; + } + + finishDecodeCache(); + endSerialised(); + + m_completion = 100; + + ExtAudioFileDispose(m_d->file); +} + + +CoreAudioFileReader::~CoreAudioFileReader() +{ + std::cerr << "CoreAudioFileReader::~CoreAudioFileReader" << std::endl; + delete[] m_d->buffer.mBuffers[0].mData; + delete m_d; +} + +void +CoreAudioFileReader::cancelled() +{ + m_cancelled = true; +} + +void +CoreAudioFileReader::getSupportedExtensions(std::set<QString> &extensions) +{ + extensions.insert("aiff"); + extensions.insert("aif"); + extensions.insert("au"); + extensions.insert("m4a"); + extensions.insert("m4b"); + extensions.insert("m4p"); + extensions.insert("mp3"); + extensions.insert("mp4"); + extensions.insert("wav"); +} + +bool +CoreAudioFileReader::supportsExtension(QString extension) +{ + std::set<QString> extensions; + getSupportedExtensions(extensions); + return (extensions.find(extension.toLower()) != extensions.end()); +} + +bool +CoreAudioFileReader::supportsContentType(QString type) +{ + return (type == "audio/x-aiff" || + type == "audio/x-wav" || + type == "audio/mpeg" || + type == "audio/basic" || + type == "audio/x-aac"); +} + +bool +CoreAudioFileReader::supports(FileSource &source) +{ + return (supportsExtension(source.getExtension()) || + supportsContentType(source.getContentType())); +} + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/fileio/CoreAudioFileReader.h Wed Sep 26 21:28:14 2012 +0100 @@ -0,0 +1,92 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2006-2012 Chris Cannam and QMUL. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#ifndef _COREAUDIO_FILE_READER_H_ +#define _COREAUDIO_FILE_READER_H_ + +#ifdef HAVE_COREAUDIO + +#include "CodedAudioFileReader.h" + +#include "base/Thread.h" + +#include <set> + +class ProgressReporter; + +class CoreAudioFileReader : public CodedAudioFileReader +{ + Q_OBJECT + +public: + enum DecodeMode { + DecodeAtOnce, // decode the file on construction, with progress + DecodeThreaded // decode in a background thread after construction + }; + + CoreAudioFileReader(FileSource source, + DecodeMode decodeMode, + CacheMode cacheMode, + size_t targetRate = 0, + ProgressReporter *reporter = 0); + virtual ~CoreAudioFileReader(); + + virtual QString getError() const { return m_error; } + virtual QString getLocation() const { return m_source.getLocation(); } + virtual QString getTitle() const { return m_title; } + + static void getSupportedExtensions(std::set<QString> &extensions); + static bool supportsExtension(QString ext); + static bool supportsContentType(QString type); + static bool supports(FileSource &source); + + virtual int getDecodeCompletion() const { return m_completion; } + + virtual bool isUpdating() const { + return m_decodeThread && m_decodeThread->isRunning(); + } + +public slots: + void cancelled(); + +protected: + FileSource m_source; + QString m_path; + QString m_error; + QString m_title; + + class D; + D *m_d; + + ProgressReporter *m_reporter; + bool m_cancelled; + int m_completion; + + class DecodeThread : public Thread + { + public: + // DecodeThread(QuickTimeFileReader *reader) : m_reader(reader) { } + virtual void run(); + + protected: + // QuickTimeFileReader *m_reader; + }; + + DecodeThread *m_decodeThread; +}; + +#endif + +#endif
--- a/svcore.pro Wed Sep 26 21:27:10 2012 +0100 +++ b/svcore.pro Wed Sep 26 21:28:14 2012 +0100 @@ -120,6 +120,7 @@ data/fileio/OggVorbisFileReader.h \ data/fileio/PlaylistFileReader.h \ data/fileio/QuickTimeFileReader.h \ + data/fileio/CoreAudioFileReader.h \ data/fileio/ResamplingWavFileReader.h \ data/fileio/WavFileReader.h \ data/fileio/WavFileWriter.h \ @@ -179,6 +180,7 @@ data/fileio/OggVorbisFileReader.cpp \ data/fileio/PlaylistFileReader.cpp \ data/fileio/QuickTimeFileReader.cpp \ + data/fileio/CoreAudioFileReader.cpp \ data/fileio/ResamplingWavFileReader.cpp \ data/fileio/WavFileReader.cpp \ data/fileio/WavFileWriter.cpp \