comparison data/fileio/BQAFileReader.cpp @ 1583:c8fad3c14a2b bqaudiostream

Start wiring in BQAudioStream stuff
author Chris Cannam
date Thu, 06 Dec 2018 12:50:28 +0000
parents
children ff18abb88563
comparison
equal deleted inserted replaced
1578:07f23b90701a 1583:c8fad3c14a2b
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
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 #include "BQAFileReader.h"
16
17 #include <bqaudiostream/AudioReadStreamFactory.h>
18 #include <bqaudiostream/AudioReadStream.h>
19 #include <bqaudiostream/Exceptions.h>
20
21 #include "base/Profiler.h"
22 #include "base/ProgressReporter.h"
23
24 #include <QFileInfo>
25
26 using namespace std;
27
28 BQAFileReader::BQAFileReader(FileSource source,
29 DecodeMode decodeMode,
30 CacheMode mode,
31 sv_samplerate_t targetRate,
32 bool normalised,
33 ProgressReporter *reporter) :
34 CodedAudioFileReader(mode, targetRate, normalised),
35 m_source(source),
36 m_path(source.getLocalFilename()),
37 m_cancelled(false),
38 m_completion(0),
39 m_reporter(reporter),
40 m_decodeThread(0)
41 {
42 SVDEBUG << "BQAFileReader: local path: \"" << m_path
43 << "\", decode mode: " << decodeMode << " ("
44 << (decodeMode == DecodeAtOnce ? "DecodeAtOnce" : "DecodeThreaded")
45 << ")" << endl;
46
47 m_channelCount = 0;
48 m_fileRate = 0;
49
50 Profiler profiler("BQAFileReader::BQAFileReader");
51
52 try {
53 m_stream = breakfastquay::AudioReadStreamFactory::createReadStream
54 (m_path.toUtf8().data());
55 } catch (const std::exception &e) {
56 m_error = e.what();
57 m_stream = 0;
58 return;
59 }
60
61 m_channelCount = m_stream->getChannelCount();
62 m_fileRate = m_stream->getSampleRate();
63
64 initialiseDecodeCache();
65
66 if (decodeMode == DecodeAtOnce) {
67
68 if (m_reporter) {
69 connect(m_reporter, SIGNAL(cancelled()), this, SLOT(cancelled()));
70 m_reporter->setMessage
71 (tr("Decoding %1...").arg(QFileInfo(m_path).fileName()));
72 }
73
74 sv_frame_t blockSize = 65536;
75 floatvec_t block(blockSize * m_channelCount, 0.f);
76
77 while (true) {
78 try {
79 sv_frame_t retrieved =
80 m_stream->getInterleavedFrames(blockSize, block.data());
81
82 addSamplesToDecodeCache(block.data(), retrieved);
83
84 if (retrieved < blockSize) {
85 break;
86 }
87 } catch (const breakfastquay::InvalidFileFormat &f) {
88 m_error = f.what();
89 break;
90 }
91
92 if (m_cancelled) break;
93 }
94
95 if (isDecodeCacheInitialised()) finishDecodeCache();
96 endSerialised();
97
98 if (m_reporter) m_reporter->setProgress(100);
99
100 delete m_stream;
101 m_stream = 0;
102
103 } else {
104
105 if (m_reporter) m_reporter->setProgress(100);
106
107 m_decodeThread = new DecodeThread(this);
108 m_decodeThread->start();
109 }
110
111 //!!! todo metadata - maker, title, tags
112 }
113
114 BQAFileReader::~BQAFileReader()
115 {
116 if (m_decodeThread) {
117 m_cancelled = true;
118 m_decodeThread->wait();
119 delete m_decodeThread;
120 }
121
122 delete m_stream;
123 }
124
125 void
126 BQAFileReader::cancelled()
127 {
128 m_cancelled = true;
129 }
130
131 void
132 BQAFileReader::DecodeThread::run()
133 {
134 if (m_reader->m_cacheMode == CacheInTemporaryFile) {
135 m_reader->startSerialised("BQAFileReader::Decode");
136 }
137
138 sv_frame_t blockSize = 65536;
139 floatvec_t block(blockSize * m_reader->getChannelCount(), 0.f);
140
141 while (true) {
142 try {
143 sv_frame_t retrieved =
144 m_reader->m_stream->getInterleavedFrames
145 (blockSize, block.data());
146
147 m_reader->addSamplesToDecodeCache(block.data(), retrieved);
148
149 if (retrieved < blockSize) {
150 break;
151 }
152 } catch (const breakfastquay::InvalidFileFormat &f) {
153 m_reader->m_error = f.what();
154 break;
155 }
156
157 if (m_reader->m_cancelled) break;
158 }
159
160 if (m_reader->isDecodeCacheInitialised()) m_reader->finishDecodeCache();
161 m_reader->m_completion = 100;
162
163 m_reader->endSerialised();
164
165 delete m_reader->m_stream;
166 m_reader->m_stream = 0;
167 }
168
169 void
170 BQAFileReader::getSupportedExtensions(set<QString> &extensions)
171 {
172 vector<string> exts =
173 breakfastquay::AudioReadStreamFactory::getSupportedFileExtensions();
174 for (auto e: exts) {
175 extensions.insert(QString::fromUtf8(e.c_str()));
176 }
177 }
178
179 bool
180 BQAFileReader::supportsExtension(QString extension)
181 {
182 set<QString> extensions;
183 getSupportedExtensions(extensions);
184 return (extensions.find(extension.toLower()) != extensions.end());
185 }
186
187 bool
188 BQAFileReader::supportsContentType(QString type)
189 {
190 //!!! todo
191 return false;
192 }
193
194 bool
195 BQAFileReader::supports(FileSource &source)
196 {
197 return (supportsExtension(source.getExtension()) ||
198 supportsContentType(source.getContentType()));
199 }
200