annotate data/fileio/CoreAudioFileReader.cpp @ 1026:01a90c025eaa coreaudio-metadata

Pending
author Chris Cannam
date Mon, 09 Feb 2015 10:27:21 +0000
parents d03b3d956358
children
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.
Chris@748 7 This file copyright 2006-2012 Chris Cannam and QMUL.
luisf@665 8
luisf@665 9 This program is free software; you can redistribute it and/or
luisf@665 10 modify it under the terms of the GNU General Public License as
luisf@665 11 published by the Free Software Foundation; either version 2 of the
luisf@665 12 License, or (at your option) any later version. See the file
luisf@665 13 COPYING included with this distribution for more information.
luisf@665 14 */
luisf@665 15
luisf@665 16 #ifdef HAVE_COREAUDIO
luisf@665 17
luisf@665 18 #include "CoreAudioFileReader.h"
luisf@665 19 #include "base/Profiler.h"
luisf@665 20 #include "base/ProgressReporter.h"
luisf@665 21 #include "system/System.h"
luisf@665 22
luisf@665 23 #include <QFileInfo>
luisf@665 24
luisf@665 25 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
luisf@665 26 #include <AudioToolbox/AudioToolbox.h>
Chris@743 27 #include <AudioToolbox/ExtendedAudioFile.h>
luisf@665 28 #else
luisf@665 29 #include "AudioToolbox.h"
luisf@665 30 #include "ExtendedAudioFile.h"
luisf@665 31 #endif
luisf@665 32
luisf@665 33 class CoreAudioFileReader::D
luisf@665 34 {
luisf@665 35 public:
Chris@819 36 D() : blockSize(1024), valid(false) { }
luisf@665 37
Chris@743 38 ExtAudioFileRef file;
Chris@743 39 AudioBufferList buffer;
Chris@743 40 OSStatus err;
Chris@743 41 AudioStreamBasicDescription asbd;
Chris@743 42 int blockSize;
Chris@819 43 bool valid;
luisf@665 44 };
luisf@665 45
Chris@743 46 static QString
Chris@743 47 codestr(OSStatus err)
Chris@743 48 {
Chris@743 49 char text[5];
Chris@743 50 UInt32 uerr = err;
Chris@743 51 text[0] = (uerr >> 24) & 0xff;
Chris@743 52 text[1] = (uerr >> 16) & 0xff;
Chris@743 53 text[2] = (uerr >> 8) & 0xff;
Chris@743 54 text[3] = (uerr) & 0xff;
Chris@743 55 text[4] = '\0';
Chris@794 56 return QString("%1 (%2)").arg(err).arg(QString::fromLocal8Bit(text));
Chris@743 57 }
luisf@665 58
luisf@665 59 CoreAudioFileReader::CoreAudioFileReader(FileSource source,
Chris@743 60 DecodeMode decodeMode,
Chris@743 61 CacheMode mode,
Chris@929 62 int targetRate,
Chris@920 63 bool normalised,
Chris@743 64 ProgressReporter *reporter) :
Chris@920 65 CodedAudioFileReader(mode, targetRate, normalised),
luisf@665 66 m_source(source),
luisf@665 67 m_path(source.getLocalFilename()),
luisf@665 68 m_d(new D),
luisf@665 69 m_reporter(reporter),
luisf@665 70 m_cancelled(false),
luisf@665 71 m_completion(0),
luisf@665 72 m_decodeThread(0)
luisf@665 73 {
Chris@743 74 m_channelCount = 0;
Chris@743 75 m_fileRate = 0;
luisf@665 76
Chris@760 77 m_d->buffer.mBuffers[0].mData = 0;
Chris@760 78
Chris@743 79 Profiler profiler("CoreAudioFileReader::CoreAudioFileReader", true);
luisf@665 80
Chris@844 81 SVDEBUG << "CoreAudioFileReader: path is \"" << m_path << "\"" << endl;
luisf@665 82
Chris@743 83 QByteArray ba = m_path.toLocal8Bit();
luisf@665 84
Chris@743 85 CFURLRef url = CFURLCreateFromFileSystemRepresentation
Chris@743 86 (kCFAllocatorDefault,
Chris@743 87 (const UInt8 *)ba.data(),
Chris@743 88 (CFIndex)ba.length(),
Chris@743 89 false);
luisf@665 90
Chris@743 91 m_d->err = ExtAudioFileOpenURL(url, &m_d->file);
luisf@665 92
Chris@743 93 CFRelease(url);
luisf@665 94
Chris@743 95 if (m_d->err) {
Chris@743 96 m_error = "CoreAudioReadStream: Error opening file: code " + codestr(m_d->err);
Chris@743 97 return;
Chris@743 98 }
Chris@743 99 if (!m_d->file) {
Chris@743 100 m_error = "CoreAudioReadStream: Failed to open file, but no error reported!";
Chris@743 101 return;
Chris@743 102 }
Chris@743 103
Chris@743 104 UInt32 propsize = sizeof(AudioStreamBasicDescription);
Chris@743 105 m_d->err = ExtAudioFileGetProperty
Chris@743 106 (m_d->file, kExtAudioFileProperty_FileDataFormat, &propsize, &m_d->asbd);
Chris@743 107
Chris@743 108 if (m_d->err) {
Chris@743 109 m_error = "CoreAudioReadStream: Error in getting basic description: code " + codestr(m_d->err);
Chris@819 110 ExtAudioFileDispose(m_d->file);
Chris@743 111 return;
Chris@743 112 }
Chris@743 113
Chris@743 114 m_channelCount = m_d->asbd.mChannelsPerFrame;
Chris@755 115 m_fileRate = m_d->asbd.mSampleRate;
luisf@665 116
Chris@843 117 cerr << "CoreAudioReadStream: " << m_channelCount << " channels, " << m_fileRate << " Hz" << endl;
luisf@665 118
Chris@743 119 m_d->asbd.mFormatID = kAudioFormatLinearPCM;
Chris@743 120 m_d->asbd.mFormatFlags =
Chris@743 121 kAudioFormatFlagIsFloat |
Chris@743 122 kAudioFormatFlagIsPacked |
Chris@743 123 kAudioFormatFlagsNativeEndian;
Chris@743 124 m_d->asbd.mBitsPerChannel = sizeof(float) * 8;
Chris@743 125 m_d->asbd.mBytesPerFrame = sizeof(float) * m_channelCount;
Chris@743 126 m_d->asbd.mBytesPerPacket = sizeof(float) * m_channelCount;
Chris@743 127 m_d->asbd.mFramesPerPacket = 1;
Chris@743 128 m_d->asbd.mReserved = 0;
Chris@743 129
Chris@743 130 m_d->err = ExtAudioFileSetProperty
Chris@743 131 (m_d->file, kExtAudioFileProperty_ClientDataFormat, propsize, &m_d->asbd);
Chris@743 132
Chris@743 133 if (m_d->err) {
Chris@743 134 m_error = "CoreAudioReadStream: Error in setting client format: code " + codestr(m_d->err);
Chris@819 135 ExtAudioFileDispose(m_d->file);
Chris@743 136 return;
Chris@743 137 }
luisf@665 138
Chris@743 139 m_d->buffer.mNumberBuffers = 1;
Chris@743 140 m_d->buffer.mBuffers[0].mNumberChannels = m_channelCount;
Chris@743 141 m_d->buffer.mBuffers[0].mDataByteSize = sizeof(float) * m_channelCount * m_d->blockSize;
Chris@743 142 m_d->buffer.mBuffers[0].mData = new float[m_channelCount * m_d->blockSize];
luisf@665 143
Chris@819 144 m_d->valid = true;
Chris@819 145
Chris@1026 146 readMetadata();
Chris@743 147 initialiseDecodeCache();
luisf@665 148
Chris@743 149 if (m_reporter) {
Chris@743 150 connect(m_reporter, SIGNAL(cancelled()), this, SLOT(cancelled()));
Chris@743 151 m_reporter->setMessage
Chris@743 152 (tr("Decoding %1...").arg(QFileInfo(m_path).fileName()));
Chris@743 153 }
luisf@665 154
Chris@743 155 while (1) {
luisf@665 156
Chris@743 157 UInt32 framesRead = m_d->blockSize;
Chris@743 158 m_d->err = ExtAudioFileRead(m_d->file, &framesRead, &m_d->buffer);
luisf@665 159
Chris@743 160 if (m_d->err) {
Chris@743 161 m_error = QString("Error in CoreAudio decoding: code %1")
Chris@743 162 .arg(m_d->err);
Chris@743 163 break;
Chris@743 164 }
luisf@665 165
Chris@743 166 //!!! progress?
luisf@665 167
Chris@843 168 // cerr << "Read " << framesRead << " frames (block size " << m_d->blockSize << ")" << endl;
luisf@665 169
Chris@743 170 // buffers are interleaved unless specified otherwise
Chris@743 171 addSamplesToDecodeCache((float *)m_d->buffer.mBuffers[0].mData, framesRead);
luisf@665 172
Chris@743 173 if (framesRead < m_d->blockSize) break;
Chris@743 174 }
luisf@665 175
Chris@743 176 finishDecodeCache();
Chris@743 177 endSerialised();
luisf@665 178
Chris@743 179 m_completion = 100;
luisf@665 180 }
luisf@665 181
luisf@665 182 CoreAudioFileReader::~CoreAudioFileReader()
luisf@665 183 {
Chris@843 184 cerr << "CoreAudioFileReader::~CoreAudioFileReader" << endl;
Chris@819 185
Chris@819 186 if (m_d->valid) {
Chris@819 187 ExtAudioFileDispose(m_d->file);
Chris@819 188 delete[] m_d->buffer.mBuffers[0].mData;
Chris@819 189 }
Chris@819 190
Chris@743 191 delete m_d;
luisf@665 192 }
luisf@665 193
luisf@665 194 void
Chris@1026 195 CoreAudioFileReader::readMetadata()
Chris@1026 196 {
Chris@1026 197 AudioFileID audiofile;
Chris@1026 198 UInt32 propsize = sizeof(AudioFileID);
Chris@1026 199
Chris@1026 200 // err is local, failures here are only warnings
Chris@1026 201 OSStatus err = noErr;
Chris@1026 202
Chris@1026 203 err = ExtAudioFileGetProperty
Chris@1026 204 (m_d->file, kExtAudioFileProperty_AudioFile, &propsize, &audiofile);
Chris@1026 205
Chris@1026 206 if (err) {
Chris@1026 207 cerr << "WARNING: CoreAudioReadStream: Error in getting underlying audio file: code " << codestr(err) << endl;
Chris@1026 208 return;
Chris@1026 209 }
Chris@1026 210
Chris@1026 211 propsize = 0;
Chris@1026 212 char *data = 0;
Chris@1026 213 err = AudioFileGetPropertyInfo
Chris@1026 214 (audiofile, kAudioFilePropertyID3Tag, &propsize, 0);
Chris@1026 215 if (err) {
Chris@1026 216 cerr << "WARNING: CoreAudioReadStream: Failed to get id3 tag size: code " << codestr(err) << endl;
Chris@1026 217 return;
Chris@1026 218 }
Chris@1026 219
Chris@1026 220 data = new char[propsize];
Chris@1026 221 err = AudioFileGetProperty
Chris@1026 222 (audiofile, kAudioFilePropertyID3Tag, &propsize, data);
Chris@1026 223 if (err) {
Chris@1026 224 cerr << "WARNING: CoreAudioReadStream: Failed to read id3 tag data: code " << codestr(err) << endl;
Chris@1026 225 return;
Chris@1026 226 }
Chris@1026 227
Chris@1026 228
Chris@1026 229
Chris@1026 230 delete[] data;
Chris@1026 231 }
Chris@1026 232
Chris@1026 233 void
luisf@665 234 CoreAudioFileReader::cancelled()
luisf@665 235 {
luisf@665 236 m_cancelled = true;
luisf@665 237 }
luisf@665 238
luisf@665 239 void
luisf@665 240 CoreAudioFileReader::getSupportedExtensions(std::set<QString> &extensions)
luisf@665 241 {
Chris@743 242 extensions.insert("aiff");
Chris@743 243 extensions.insert("aif");
Chris@743 244 extensions.insert("au");
Chris@743 245 extensions.insert("m4a");
Chris@743 246 extensions.insert("m4b");
Chris@743 247 extensions.insert("m4p");
Chris@743 248 extensions.insert("mp3");
Chris@743 249 extensions.insert("mp4");
Chris@743 250 extensions.insert("wav");
luisf@665 251 }
luisf@665 252
luisf@665 253 bool
luisf@665 254 CoreAudioFileReader::supportsExtension(QString extension)
luisf@665 255 {
Chris@743 256 std::set<QString> extensions;
Chris@743 257 getSupportedExtensions(extensions);
Chris@743 258 return (extensions.find(extension.toLower()) != extensions.end());
luisf@665 259 }
luisf@665 260
luisf@665 261 bool
luisf@665 262 CoreAudioFileReader::supportsContentType(QString type)
luisf@665 263 {
Chris@743 264 return (type == "audio/x-aiff" ||
Chris@743 265 type == "audio/x-wav" ||
Chris@743 266 type == "audio/mpeg" ||
Chris@743 267 type == "audio/basic" ||
Chris@749 268 type == "audio/x-aac");
luisf@665 269 }
luisf@665 270
luisf@665 271 bool
luisf@665 272 CoreAudioFileReader::supports(FileSource &source)
luisf@665 273 {
Chris@743 274 return (supportsExtension(source.getExtension()) ||
Chris@743 275 supportsContentType(source.getContentType()));
luisf@665 276 }
luisf@665 277
luisf@665 278 #endif
luisf@665 279