svcore  1.9
DecodingWavFileReader.cpp
Go to the documentation of this file.
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 2007 QMUL.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "DecodingWavFileReader.h"
17 
18 #include "WavFileReader.h"
19 #include "base/Profiler.h"
20 #include "base/ProgressReporter.h"
21 
22 #include <QFileInfo>
23 
25  ResampleMode resampleMode,
26  CacheMode mode,
27  int targetRate,
28  bool normalised,
29  ProgressReporter *reporter) :
30  CodedAudioFileReader(mode, targetRate, normalised),
31  m_source(source),
32  m_path(source.getLocalFilename()),
33  m_cancelled(false),
34  m_processed(0),
35  m_completion(0),
36  m_original(0),
37  m_reporter(reporter),
38  m_decodeThread(0)
39 {
40  m_channelCount = 0;
41  m_fileRate = 0;
42 
43  SVDEBUG << "DecodingWavFileReader::DecodingWavFileReader(\""
44  << m_path << "\"): rate " << targetRate << endl;
45 
46  Profiler profiler("DecodingWavFileReader::DecodingWavFileReader", true);
47 
49  if (!m_original->isOK()) {
51  return;
52  }
53 
56 
58 
59  if (resampleMode == ResampleAtOnce) {
60 
61  if (m_reporter) {
62  connect(m_reporter, SIGNAL(cancelled()), this, SLOT(cancelled()));
64  (tr("Decoding %1...").arg(QFileInfo(m_path).fileName()));
65  }
66 
67  int blockSize = 16384;
68  int total = m_original->getFrameCount();
69 
70  SampleBlock block;
71 
72  for (int i = 0; i < total; i += blockSize) {
73 
74  int count = blockSize;
75  if (i + count > total) count = total - i;
76 
77  m_original->getInterleavedFrames(i, count, block);
78  addBlock(block);
79 
80  if (m_cancelled) break;
81  }
82 
84  endSerialised();
85 
87 
88  delete m_original;
89  m_original = 0;
90 
91  } else {
92 
94 
95  m_decodeThread = new DecodeThread(this);
97  }
98 }
99 
101 {
102  if (m_decodeThread) {
103  m_cancelled = true;
104  m_decodeThread->wait();
105  delete m_decodeThread;
106  }
107 
108  delete m_original;
109 }
110 
111 void
113 {
114  m_cancelled = true;
115 }
116 
117 void
119 {
121  m_reader->startSerialised("DecodingWavFileReader::Decode");
122  }
123 
124  int blockSize = 16384;
125  int total = m_reader->m_original->getFrameCount();
126 
127  SampleBlock block;
128 
129  for (int i = 0; i < total; i += blockSize) {
130 
131  int count = blockSize;
132  if (i + count > total) count = total - i;
133 
134  m_reader->m_original->getInterleavedFrames(i, count, block);
135  m_reader->addBlock(block);
136 
137  if (m_reader->m_cancelled) break;
138  }
139 
141  m_reader->m_completion = 100;
142 
144 
145  delete m_reader->m_original;
146  m_reader->m_original = 0;
147 }
148 
149 void
151 {
152  addSamplesToDecodeCache(frames);
153 
154  m_processed += frames.size();
155 
156  float ratio = float(m_sampleRate) / float(m_fileRate);
157 
158  int progress = lrint((float(m_processed) * ratio * 100) /
159  float(m_original->getFrameCount()));
160 
161  if (progress > 99) progress = 99;
163 
164  if (m_reporter) {
166  }
167 }
168 
169 void
170 DecodingWavFileReader::getSupportedExtensions(std::set<QString> &extensions)
171 {
173 }
174 
175 bool
177 {
178  return WavFileReader::supportsExtension(extension);
179 }
180 
181 bool
183 {
185 }
186 
187 bool
189 {
190  return WavFileReader::supports(source);
191 }
192 
193 
int getChannelCount() const
static bool supportsContentType(QString type)
std::vector< float > SampleBlock
Reader for audio files using libsndfile.
Definition: WavFileReader.h:36
static bool supports(FileSource &source)
void start()
Definition: Thread.cpp:34
static bool supportsExtension(QString ext)
bool isOK() const
DecodingWavFileReader(FileSource source, ResampleMode resampleMode, CacheMode cacheMode, int targetRate=0, bool normalised=false, ProgressReporter *reporter=0)
static bool supportsContentType(QString type)
void addSamplesToDecodeCache(float **samples, int nframes)
static bool supports(FileSource &source)
int getFrameCount() const
static void getSupportedExtensions(std::set< QString > &extensions)
FileSource is a class used to refer to the contents of a file that may be either local or at a remote...
Definition: FileSource.h:59
static void getSupportedExtensions(std::set< QString > &extensions)
void addBlock(const SampleBlock &frames)
virtual void setProgress(int percentage)=0
#define SVDEBUG
Definition: Debug.h:42
virtual void getInterleavedFrames(int start, int count, SampleBlock &frames) const
Must be safe to call from multiple threads with different arguments on the same object at the same ti...
bool isDecodeCacheInitialised() const
ProgressReporter * m_reporter
virtual QString getError() const
Definition: WavFileReader.h:43
virtual void setMessage(QString text)=0
int getSampleRate() const
void startSerialised(QString id)
static bool supportsExtension(QString ext)
Profile point instance class.
Definition: Profiler.h:86