annotate data/fileio/CoreAudioFileReader.cpp @ 743:964a20b5a747 coreaudio_tests

Overhaul CoreAudioFileReader -- it now at least superficially appears to work
author Chris Cannam
date Mon, 02 Jul 2012 09:39:55 +0100
parents 029dd9e5cc29
children c8ab5f63360d
rev   line source
luisf@665 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
luisf@665 2
luisf@665 3 /*
luisf@665 4 Sonic Visualiser
luisf@665 5 An audio file viewer and annotation editor.
luisf@665 6 Centre for Digital Music, Queen Mary, University of London.
luisf@665 7 This file copyright 2006-2007 Chris Cannam and QMUL.
luisf@665 8
luisf@665 9 Based on QTAudioFile.cpp from SoundBite, copyright 2006
luisf@665 10 Chris Sutton and Mark Levy.
luisf@665 11
luisf@665 12 This program is free software; you can redistribute it and/or
luisf@665 13 modify it under the terms of the GNU General Public License as
luisf@665 14 published by the Free Software Foundation; either version 2 of the
luisf@665 15 License, or (at your option) any later version. See the file
luisf@665 16 COPYING included with this distribution for more information.
luisf@665 17 */
luisf@665 18
luisf@665 19 #ifdef HAVE_COREAUDIO
luisf@665 20
luisf@665 21 #include "CoreAudioFileReader.h"
luisf@665 22 #include "base/Profiler.h"
luisf@665 23 #include "base/ProgressReporter.h"
luisf@665 24 #include "system/System.h"
luisf@665 25
luisf@665 26 #include <QFileInfo>
luisf@665 27
luisf@665 28 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
luisf@665 29 #include <AudioToolbox/AudioToolbox.h>
Chris@743 30 #include <AudioToolbox/ExtendedAudioFile.h>
luisf@665 31 #else
luisf@665 32 #include "AudioToolbox.h"
luisf@665 33 #include "ExtendedAudioFile.h"
luisf@665 34 #endif
luisf@665 35
luisf@665 36 class CoreAudioFileReader::D
luisf@665 37 {
luisf@665 38 public:
Chris@743 39 D() : blockSize(1024) { }
luisf@665 40
Chris@743 41 ExtAudioFileRef file;
Chris@743 42 AudioBufferList buffer;
Chris@743 43 OSStatus err;
Chris@743 44 AudioStreamBasicDescription asbd;
Chris@743 45 int blockSize;
luisf@665 46 };
luisf@665 47
Chris@743 48 static QString
Chris@743 49 codestr(OSStatus err)
Chris@743 50 {
Chris@743 51 char text[5];
Chris@743 52 UInt32 uerr = err;
Chris@743 53 text[0] = (uerr >> 24) & 0xff;
Chris@743 54 text[1] = (uerr >> 16) & 0xff;
Chris@743 55 text[2] = (uerr >> 8) & 0xff;
Chris@743 56 text[3] = (uerr) & 0xff;
Chris@743 57 text[4] = '\0';
Chris@743 58 return QString("%1 (%2)").arg(err).arg(QString::fromAscii(text));
Chris@743 59 }
luisf@665 60
luisf@665 61 CoreAudioFileReader::CoreAudioFileReader(FileSource source,
Chris@743 62 DecodeMode decodeMode,
Chris@743 63 CacheMode mode,
Chris@743 64 size_t targetRate,
Chris@743 65 ProgressReporter *reporter) :
luisf@665 66 CodedAudioFileReader(mode, targetRate),
luisf@665 67 m_source(source),
luisf@665 68 m_path(source.getLocalFilename()),
luisf@665 69 m_d(new D),
luisf@665 70 m_reporter(reporter),
luisf@665 71 m_cancelled(false),
luisf@665 72 m_completion(0),
luisf@665 73 m_decodeThread(0)
luisf@665 74 {
Chris@743 75 m_channelCount = 0;
Chris@743 76 m_fileRate = 0;
luisf@665 77
Chris@743 78 Profiler profiler("CoreAudioFileReader::CoreAudioFileReader", true);
luisf@665 79
Chris@743 80 std::cerr << "CoreAudioFileReader: path is \"" << m_path.toStdString() << "\"" << std::endl;
luisf@665 81
Chris@743 82 QByteArray ba = m_path.toLocal8Bit();
luisf@665 83
Chris@743 84 CFURLRef url = CFURLCreateFromFileSystemRepresentation
Chris@743 85 (kCFAllocatorDefault,
Chris@743 86 (const UInt8 *)ba.data(),
Chris@743 87 (CFIndex)ba.length(),
Chris@743 88 false);
luisf@665 89
Chris@743 90 //!!! how do we find out if the file open fails because of DRM protection?
luisf@665 91
Chris@743 92 #if (MACOSX_DEPLOYMENT_TARGET <= 1040 && MAC_OS_X_VERSION_MIN_REQUIRED <= 1040)
Chris@743 93 FSRef fsref;
Chris@743 94 if (!CFURLGetFSRef(url, &fsref)) { // returns Boolean, not error code
Chris@743 95 m_error = "CoreAudioReadStream: Error looking up FS ref (file not found?)";
Chris@743 96 return;
Chris@743 97 }
Chris@743 98 m_d->err = ExtAudioFileOpen(&fsref, &m_d->file);
Chris@743 99 #else
Chris@743 100 m_d->err = ExtAudioFileOpenURL(url, &m_d->file);
Chris@743 101 #endif
luisf@665 102
Chris@743 103 CFRelease(url);
luisf@665 104
Chris@743 105 if (m_d->err) {
Chris@743 106 m_error = "CoreAudioReadStream: Error opening file: code " + codestr(m_d->err);
Chris@743 107 return;
Chris@743 108 }
Chris@743 109 if (!m_d->file) {
Chris@743 110 m_error = "CoreAudioReadStream: Failed to open file, but no error reported!";
Chris@743 111 return;
Chris@743 112 }
Chris@743 113
Chris@743 114 UInt32 propsize = sizeof(AudioStreamBasicDescription);
Chris@743 115 m_d->err = ExtAudioFileGetProperty
Chris@743 116 (m_d->file, kExtAudioFileProperty_FileDataFormat, &propsize, &m_d->asbd);
Chris@743 117
Chris@743 118 if (m_d->err) {
Chris@743 119 m_error = "CoreAudioReadStream: Error in getting basic description: code " + codestr(m_d->err);
Chris@743 120 return;
Chris@743 121 }
Chris@743 122
Chris@743 123 m_channelCount = m_d->asbd.mChannelsPerFrame;
Chris@743 124 m_sampleRate = m_d->asbd.mSampleRate;
luisf@665 125
Chris@743 126 std::cerr << "CoreAudioReadStream: " << m_channelCount << " channels, " << m_sampleRate << " Hz" << std::endl;
luisf@665 127
Chris@743 128 m_d->asbd.mSampleRate = getSampleRate();
luisf@665 129
Chris@743 130 m_d->asbd.mFormatID = kAudioFormatLinearPCM;
Chris@743 131 m_d->asbd.mFormatFlags =
Chris@743 132 kAudioFormatFlagIsFloat |
Chris@743 133 kAudioFormatFlagIsPacked |
Chris@743 134 kAudioFormatFlagsNativeEndian;
Chris@743 135 m_d->asbd.mBitsPerChannel = sizeof(float) * 8;
Chris@743 136 m_d->asbd.mBytesPerFrame = sizeof(float) * m_channelCount;
Chris@743 137 m_d->asbd.mBytesPerPacket = sizeof(float) * m_channelCount;
Chris@743 138 m_d->asbd.mFramesPerPacket = 1;
Chris@743 139 m_d->asbd.mReserved = 0;
Chris@743 140
Chris@743 141 m_d->err = ExtAudioFileSetProperty
Chris@743 142 (m_d->file, kExtAudioFileProperty_ClientDataFormat, propsize, &m_d->asbd);
Chris@743 143
Chris@743 144 if (m_d->err) {
Chris@743 145 m_error = "CoreAudioReadStream: Error in setting client format: code " + codestr(m_d->err);
Chris@743 146 return;
Chris@743 147 }
luisf@665 148
Chris@743 149 m_d->buffer.mNumberBuffers = 1;
Chris@743 150 m_d->buffer.mBuffers[0].mNumberChannels = m_channelCount;
Chris@743 151 m_d->buffer.mBuffers[0].mDataByteSize = sizeof(float) * m_channelCount * m_d->blockSize;
Chris@743 152 m_d->buffer.mBuffers[0].mData = new float[m_channelCount * m_d->blockSize];
luisf@665 153
Chris@743 154 initialiseDecodeCache();
luisf@665 155
Chris@743 156 if (m_reporter) {
Chris@743 157 connect(m_reporter, SIGNAL(cancelled()), this, SLOT(cancelled()));
Chris@743 158 m_reporter->setMessage
Chris@743 159 (tr("Decoding %1...").arg(QFileInfo(m_path).fileName()));
Chris@743 160 }
luisf@665 161
Chris@743 162 while (1) {
luisf@665 163
Chris@743 164 UInt32 framesRead = m_d->blockSize;
Chris@743 165 m_d->err = ExtAudioFileRead(m_d->file, &framesRead, &m_d->buffer);
luisf@665 166
Chris@743 167 if (m_d->err) {
Chris@743 168 m_error = QString("Error in CoreAudio decoding: code %1")
Chris@743 169 .arg(m_d->err);
Chris@743 170 break;
Chris@743 171 }
luisf@665 172
Chris@743 173 //!!! progress?
luisf@665 174
Chris@743 175 // std::cerr << "Read " << framesRead << " frames (block size " << m_d->blockSize << ")" << std::endl;
luisf@665 176
Chris@743 177 // buffers are interleaved unless specified otherwise
Chris@743 178 addSamplesToDecodeCache((float *)m_d->buffer.mBuffers[0].mData, framesRead);
luisf@665 179
Chris@743 180 if (framesRead < m_d->blockSize) break;
Chris@743 181 }
luisf@665 182
Chris@743 183 finishDecodeCache();
Chris@743 184 endSerialised();
luisf@665 185
Chris@743 186 m_completion = 100;
luisf@665 187
Chris@743 188 ExtAudioFileDispose(m_d->file);
luisf@665 189 }
luisf@665 190
luisf@665 191
luisf@665 192 CoreAudioFileReader::~CoreAudioFileReader()
luisf@665 193 {
Chris@743 194 std::cerr << "CoreAudioFileReader::~CoreAudioFileReader" << std::endl;
Chris@743 195 delete[] m_d->buffer.mBuffers[0].mData;
Chris@743 196 delete m_d;
luisf@665 197 }
luisf@665 198
luisf@665 199 void
luisf@665 200 CoreAudioFileReader::cancelled()
luisf@665 201 {
luisf@665 202 m_cancelled = true;
luisf@665 203 }
luisf@665 204
luisf@665 205 /*
luisf@665 206 void
luisf@665 207 CoreAudioFileReader::DecodeThread::run()
luisf@665 208 {
luisf@665 209 if (m_reader->m_cacheMode == CacheInTemporaryFile) {
luisf@665 210 m_reader->m_completion = 1;
luisf@665 211 m_reader->startSerialised("QuickTimeFileReader::Decode");
luisf@665 212 }
luisf@665 213
luisf@665 214 while (1) {
luisf@665 215
luisf@665 216 UInt32 framesRead = m_reader->m_d->blockSize;
luisf@665 217 UInt32 extractionFlags = 0;
luisf@665 218 m_reader->m_d->err = MovieAudioExtractionFillBuffer
luisf@665 219 (m_reader->m_d->extractionSessionRef, &framesRead,
luisf@665 220 &m_reader->m_d->buffer, &extractionFlags);
luisf@665 221 if (m_reader->m_d->err) {
luisf@665 222 m_reader->m_error = QString("Error in QuickTime decoding: code %1")
luisf@665 223 .arg(m_reader->m_d->err);
luisf@665 224 break;
luisf@665 225 }
luisf@665 226
luisf@665 227 // QuickTime buffers are interleaved unless specified otherwise
luisf@665 228 m_reader->addSamplesToDecodeCache(m_reader->m_d->data, framesRead);
luisf@665 229
luisf@665 230 if (framesRead < m_reader->m_d->blockSize) break;
luisf@665 231 }
luisf@665 232
luisf@665 233 m_reader->finishDecodeCache();
luisf@665 234
luisf@665 235 m_reader->m_d->err = MovieAudioExtractionEnd(m_reader->m_d->extractionSessionRef);
luisf@665 236 if (m_reader->m_d->err) {
luisf@665 237 m_reader->m_error = QString("Error ending QuickTime extraction session: code %1").arg(m_reader->m_d->err);
luisf@665 238 }
luisf@665 239
luisf@665 240 m_reader->m_completion = 100;
luisf@665 241 m_reader->endSerialised();
luisf@665 242 }
luisf@665 243 */
luisf@665 244
luisf@665 245
luisf@665 246 void
luisf@665 247 CoreAudioFileReader::getSupportedExtensions(std::set<QString> &extensions)
luisf@665 248 {
Chris@743 249 extensions.insert("aiff");
Chris@743 250 extensions.insert("aif");
Chris@743 251 extensions.insert("au");
Chris@743 252 extensions.insert("avi");
Chris@743 253 extensions.insert("m4a");
Chris@743 254 extensions.insert("m4b");
Chris@743 255 extensions.insert("m4p");
Chris@743 256 extensions.insert("m4v");
Chris@743 257 extensions.insert("mov");
Chris@743 258 extensions.insert("mp3");
Chris@743 259 extensions.insert("mp4");
Chris@743 260 extensions.insert("wav");
luisf@665 261 }
luisf@665 262
luisf@665 263 bool
luisf@665 264 CoreAudioFileReader::supportsExtension(QString extension)
luisf@665 265 {
Chris@743 266 std::set<QString> extensions;
Chris@743 267 getSupportedExtensions(extensions);
Chris@743 268 return (extensions.find(extension.toLower()) != extensions.end());
luisf@665 269 }
luisf@665 270
luisf@665 271 bool
luisf@665 272 CoreAudioFileReader::supportsContentType(QString type)
luisf@665 273 {
Chris@743 274 return (type == "audio/x-aiff" ||
Chris@743 275 type == "audio/x-wav" ||
Chris@743 276 type == "audio/mpeg" ||
Chris@743 277 type == "audio/basic" ||
Chris@743 278 type == "audio/x-aac" ||
Chris@743 279 type == "video/mp4" ||
Chris@743 280 type == "video/quicktime");
luisf@665 281 }
luisf@665 282
luisf@665 283 bool
luisf@665 284 CoreAudioFileReader::supports(FileSource &source)
luisf@665 285 {
Chris@743 286 return (supportsExtension(source.getExtension()) ||
Chris@743 287 supportsContentType(source.getContentType()));
luisf@665 288 }
luisf@665 289
luisf@665 290 #endif
luisf@665 291