comparison data/fileio/CoreAudioFileReader.cpp @ 747:523e97d0f6eb

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