changeset 665:029dd9e5cc29 coreaudio_tests

tests for using coreaudio to read files in 64-bit mac osx
author luisf
date Thu, 18 Nov 2010 11:40:38 +0000
parents f19437971e17
children 4efa7429cd85
files data/fileio/AudioFileReaderFactory.cpp data/fileio/CodedAudioFileReader.cpp data/fileio/CoreAudioFileReader.cpp data/fileio/CoreAudioFileReader.h
diffstat 4 files changed, 500 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/data/fileio/AudioFileReaderFactory.cpp	Fri Mar 12 15:06:38 2010 +0000
+++ b/data/fileio/AudioFileReaderFactory.cpp	Thu Nov 18 11:40:38 2010 +0000
@@ -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;
 
-//    std::cerr << "AudioFileReaderFactory::createReader(\"" << source.getLocation().toStdString() << "\"): Requested rate: " << targetRate << std::endl;
+    std::cerr << "AudioFileReaderFactory::createReader(\"" << source.getLocation().toStdString() << "\"): Requested rate: " << targetRate << std::endl;
 
     if (!source.isOK() || !source.isAvailable()) {
         std::cerr << "AudioFileReaderFactory::createReader(\"" << source.getLocation().toStdString() << "\": Source unavailable" << std::endl;
@@ -170,6 +174,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
@@ -259,9 +283,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	Fri Mar 12 15:06:38 2010 +0000
+++ b/data/fileio/CodedAudioFileReader.cpp	Thu Nov 18 11:40:38 2010 +0000
@@ -40,7 +40,7 @@
     m_resampler(0),
     m_resampleBuffer(0)
 {
-//    std::cerr << "CodedAudioFileReader::CodedAudioFileReader: rate " << targetRate << std::endl;
+    std::cerr << "CodedAudioFileReader::CodedAudioFileReader: rate " << targetRate << std::endl;
 
     m_frameCount = 0;
     m_sampleRate = targetRate;
@@ -54,7 +54,7 @@
 
     if (m_cacheFileWritePtr) sf_close(m_cacheFileWritePtr);
 
-//    std::cerr << "CodedAudioFileReader::~CodedAudioFileReader: deleting cache file reader" << std::endl;
+    std::cerr << "CodedAudioFileReader::~CodedAudioFileReader: deleting cache file reader" << std::endl;
 
     delete m_cacheFileReader;
     delete[] m_cacheWriteBuffer;
@@ -72,7 +72,7 @@
 void
 CodedAudioFileReader::startSerialised(QString id)
 {
-//    std::cerr << "CodedAudioFileReader::startSerialised(" << id.toStdString() << ")" << std::endl;
+    std::cerr << "CodedAudioFileReader::startSerialised(" << id.toStdString() << ")" << std::endl;
 
     delete m_serialiser;
     m_serialiser = new Serialiser(id);
@@ -81,7 +81,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;
@@ -92,7 +92,7 @@
 {
     QMutexLocker locker(&m_cacheMutex);
 
-//    std::cerr << "CodedAudioFileReader::initialiseDecodeCache: file rate = " << m_fileRate << std::endl;
+    std::cerr << "CodedAudioFileReader::initialiseDecodeCache: file rate = " << m_fileRate << std::endl;
 
     if (m_fileRate == 0) {
         std::cerr << "CodedAudioFileReader::initialiseDecodeCache: ERROR: File sample rate unknown (bug in subclass implementation?)" << std::endl;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data/fileio/CoreAudioFileReader.cpp	Thu Nov 18 11:40:38 2010 +0000
@@ -0,0 +1,353 @@
+/* -*- 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-2007 Chris Cannam and QMUL.
+
+    Based on QTAudioFile.cpp from SoundBite, copyright 2006
+    Chris Sutton and Mark Levy.
+
+    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>
+
+
+// TODO: implement for windows
+#ifdef _WIN32
+#include <QTML.h>
+#include <Movies.h>
+#else
+
+
+#if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
+#include <AudioToolbox/AudioToolbox.h>
+#else
+#include "AudioToolbox.h"
+#include "ExtendedAudioFile.h"
+#endif
+
+#include "CAStreamBasicDescription.h"
+#include "CAXException.h"
+
+
+
+
+#endif
+
+class CoreAudioFileReader::D
+{
+public:
+  D() : data(0), blockSize(1024) { }
+
+  AudioBufferList              buffer;
+  float                        *data;
+  OSErr                        err;
+  AudioStreamBasicDescription  asbd;
+  // file reference
+
+  ExtAudioFileRef              *infile;
+  size_t                       blockSize;
+};
+
+
+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;
+
+  // TODO: check QT version
+
+  Handle dataRef;
+  OSType dataRefType;
+
+  //    CFStringRef URLString = CFStringCreateWithCString
+  //       (0, m_path.toLocal8Bit().data(), 0);
+
+  // what are these used for???
+  // ExtAudioFileRef *infile, outfile;
+
+  // Creates a new CFURL object for a file system entity using the native representation.
+  CFURLRef url = CFURLCreateFromFileSystemRepresentation
+      (kCFAllocatorDefault,
+          (const UInt8 *)m_path.toLocal8Bit().data(),
+          (CFIndex)m_path.length(),
+          false);
+
+  // first open the input file
+  m_d->err = ExtAudioFileOpenURL (url, m_d->infile);
+
+  if (m_d->err) {
+      m_error = QString("Error opening Audio File for CoreAudio decoder: code %1").arg(m_d->err);
+      return;
+  }
+  else {
+      std::cerr << "CoreAudio: opened URL" << std::endl;
+  }
+
+
+  // Get the audio data format
+  // the audio format gets stored in the &m_d->asbd
+  UInt32 thePropertySize = sizeof(m_d->asbd);
+
+
+  std::cerr << "CoreAudio: thePropertySize: " << thePropertySize << std::endl;
+
+  m_d->err = ExtAudioFileGetProperty(*m_d->infile, kAudioFilePropertyDataFormat, &thePropertySize, &m_d->asbd);
+
+  std::cerr << "CoreAudio: ExtAudioFileGetProperty res: " << &m_d->asbd << std::endl;
+
+  // CAStreamBasicDescription clientFormat = (inputFormat.mFormatID == kAudioFormatLinearPCM ? inputFormat : outputFormat);
+  // UInt32 size = sizeof(clientFormat);
+
+  // TODO: test input file's DRM rights
+
+  // are these already set?
+  m_channelCount = m_d->asbd.mChannelsPerFrame;
+  m_fileRate = m_d->asbd.mSampleRate;
+
+  std::cerr << "CoreAudio: Format ID: " << m_d->asbd.mFormatID << std::endl;
+  std::cerr << "CoreAudio: " << m_d->asbd.mChannelsPerFrame << " channels, " << m_fileRate << " kHz" << std::endl;
+
+
+
+  m_d->asbd.mFormatFlags =
+      kAudioFormatFlagIsFloat |
+      kAudioFormatFlagIsPacked |
+      kAudioFormatFlagsNativeEndian;
+  m_d->asbd.mBitsPerChannel = sizeof(float) * 8;
+  m_d->asbd.mBytesPerFrame = sizeof(float) * m_d->asbd.mChannelsPerFrame;
+  m_d->asbd.mBytesPerPacket = m_d->asbd.mBytesPerFrame;
+
+
+
+  /*
+
+  !!! what does this do exactly ?????
+
+  m_d->err = MovieAudioExtractionSetProperty
+      (m_d->extractionSessionRef,
+          kQTPropertyClass_MovieAudioExtraction_Audio,
+          kQTMovieAudioExtractionAudioPropertyID_AudioStreamBasicDescription,
+          sizeof(m_d->asbd),
+          &m_d->asbd);
+
+
+
+  if (m_d->err) {
+      m_error = QString("Error in QuickTime decoder property set: code %1").arg(m_d->err);
+      m_channelCount = 0;
+      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->data = new float[m_channelCount * m_d->blockSize];
+  m_d->buffer.mBuffers[0].mData = m_d->data;
+
+  initialiseDecodeCache();
+
+  // only decode at once for now
+  // if (decodeMode == DecodeAtOnce) {
+
+  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->infile, &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;
+
+      // QuickTime buffers are interleaved unless specified otherwise
+      addSamplesToDecodeCache(m_d->data, framesRead);
+
+      if (framesRead < m_d->blockSize) break;
+  }
+
+  finishDecodeCache();
+  endSerialised();
+
+  /*
+ TODO - close session
+ m_d->err = MovieAudioExtractionEnd(m_d->extractionSessionRef);
+  if (m_d->err) {
+      m_error = QString("Error ending QuickTime extraction session: code %1").arg(m_d->err);
+  }
+   */
+  m_completion = 100;
+
+
+
+  // } else {
+  //      if (m_reporter) m_reporter->setProgress(100);
+  //
+  //      if (m_channelCount > 0) {
+  //          m_decodeThread = new DecodeThread(this);
+  //          m_decodeThread->start();
+  //      }
+  //  }
+
+  std::cerr << "QuickTimeFileReader::QuickTimeFileReader: frame count is now " << getFrameCount() << ", error is \"\"" << m_error.toStdString() << "\"" << std::endl;
+}
+
+
+
+
+
+CoreAudioFileReader::~CoreAudioFileReader()
+{
+  std::cerr << "CoreAudioFileReader::~CoreAudioFileReader" << std::endl;
+
+  if (m_decodeThread) {
+      m_cancelled = true;
+      m_decodeThread->wait();
+      delete m_decodeThread;
+  }
+
+  // SetMovieActive(m_d->movie, FALSE);
+  // DisposeMovie(m_d->movie);
+
+  delete[] m_d->data;
+  delete m_d;
+}
+
+void
+CoreAudioFileReader::cancelled()
+{
+  m_cancelled = true;
+}
+
+/*
+void
+CoreAudioFileReader::DecodeThread::run()
+{
+  if (m_reader->m_cacheMode == CacheInTemporaryFile) {
+      m_reader->m_completion = 1;
+      m_reader->startSerialised("QuickTimeFileReader::Decode");
+  }
+
+  while (1) {
+
+      UInt32 framesRead = m_reader->m_d->blockSize;
+      UInt32 extractionFlags = 0;
+      m_reader->m_d->err = MovieAudioExtractionFillBuffer
+          (m_reader->m_d->extractionSessionRef, &framesRead,
+              &m_reader->m_d->buffer, &extractionFlags);
+      if (m_reader->m_d->err) {
+          m_reader->m_error = QString("Error in QuickTime decoding: code %1")
+                                    .arg(m_reader->m_d->err);
+          break;
+      }
+
+      // QuickTime buffers are interleaved unless specified otherwise
+      m_reader->addSamplesToDecodeCache(m_reader->m_d->data, framesRead);
+
+      if (framesRead < m_reader->m_d->blockSize) break;
+  }
+
+  m_reader->finishDecodeCache();
+
+  m_reader->m_d->err = MovieAudioExtractionEnd(m_reader->m_d->extractionSessionRef);
+  if (m_reader->m_d->err) {
+      m_reader->m_error = QString("Error ending QuickTime extraction session: code %1").arg(m_reader->m_d->err);
+  }
+
+  m_reader->m_completion = 100;
+  m_reader->endSerialised();
+} 
+*/
+
+
+void
+CoreAudioFileReader::getSupportedExtensions(std::set<QString> &extensions)
+{
+  extensions.insert("aiff");
+  extensions.insert("aif");
+  extensions.insert("au");
+  extensions.insert("avi");
+  extensions.insert("m4a");
+  extensions.insert("m4b");
+  extensions.insert("m4p");
+  extensions.insert("m4v");
+  extensions.insert("mov");
+  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" ||
+      type == "video/mp4" ||
+      type == "video/quicktime");
+}
+
+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	Thu Nov 18 11:40:38 2010 +0000
@@ -0,0 +1,95 @@
+/* -*- 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-2007 Chris Cannam and QMUL.
+
+    Based in part on QTAudioFile.h from SoundBite, copyright 2006
+    Chris Sutton and Mark Levy.
+    
+    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