comparison data/fileio/ResamplingWavFileReader.cpp @ 297:c022976d18e8

* Merge from sv-match-alignment branch (excluding alignment-specific document). - add aggregate wave model (not yet complete enough to be added as a true model in a layer, but there's potential) - add play solo mode - add alignment model -- unused in plain SV - fix two plugin leaks - add m3u playlist support (opens all files at once, potentially hazardous) - fix retrieval of pre-encoded URLs - add ability to resample audio files on import, so as to match rates with other files previously loaded; add preference for same - add preliminary support in transform code for range and rate of transform input - reorganise preferences dialog, move dark-background option to preferences, add option for temporary directory location
author Chris Cannam
date Fri, 28 Sep 2007 13:56:38 +0000
parents
children 3a6725f285d6
comparison
equal deleted inserted replaced
296:2b6c99b607f1 297:c022976d18e8
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 "ResamplingWavFileReader.h"
17
18 #include "WavFileReader.h"
19 #include "base/Profiler.h"
20
21 #include <QProgressDialog>
22 #include <QFileInfo>
23 #include <QApplication>
24
25 ResamplingWavFileReader::ResamplingWavFileReader(QString path,
26 ResampleMode resampleMode,
27 CacheMode mode,
28 size_t targetRate) :
29 CodedAudioFileReader(mode, targetRate),
30 m_path(path),
31 m_cancelled(false),
32 m_processed(0),
33 m_completion(0),
34 m_original(0),
35 m_progress(0),
36 m_decodeThread(0)
37 {
38 m_channelCount = 0;
39 m_fileRate = 0;
40
41 std::cerr << "ResamplingWavFileReader::ResamplingWavFileReader(\""
42 << path.toStdString() << "\"): rate " << targetRate << std::endl;
43
44 Profiler profiler("ResamplingWavFileReader::ResamplingWavFileReader", true);
45
46 m_original = new WavFileReader(path);
47 if (!m_original->isOK()) {
48 m_error = m_original->getError();
49 return;
50 }
51
52 m_channelCount = m_original->getChannelCount();
53 m_fileRate = m_original->getSampleRate();
54
55 initialiseDecodeCache();
56
57 if (resampleMode == ResampleAtOnce) {
58
59 m_progress = new QProgressDialog
60 (QObject::tr("Resampling %1...").arg(QFileInfo(path).fileName()),
61 QObject::tr("Stop"), 0, 100);
62 m_progress->hide();
63
64 size_t blockSize = 16384;
65 size_t total = m_original->getFrameCount();
66
67 SampleBlock block;
68
69 for (size_t i = 0; i < total; i += blockSize) {
70
71 size_t count = blockSize;
72 if (i + count > total) count = total - i;
73
74 m_original->getInterleavedFrames(i, count, block);
75 addBlock(block);
76
77 if (m_cancelled) break;
78 }
79
80 if (isDecodeCacheInitialised()) finishDecodeCache();
81
82 delete m_original;
83 m_original = 0;
84
85 delete m_progress;
86 m_progress = 0;
87
88 } else {
89
90 m_decodeThread = new DecodeThread(this);
91 m_decodeThread->start();
92 }
93 }
94
95 ResamplingWavFileReader::~ResamplingWavFileReader()
96 {
97 if (m_decodeThread) {
98 m_cancelled = true;
99 m_decodeThread->wait();
100 delete m_decodeThread;
101 }
102
103 delete m_original;
104 }
105
106 void
107 ResamplingWavFileReader::DecodeThread::run()
108 {
109 if (m_reader->m_cacheMode == CacheInTemporaryFile) {
110 m_reader->startSerialised("ResamplingWavFileReader::Decode");
111 }
112
113 size_t blockSize = 16384;
114 size_t total = m_reader->m_original->getFrameCount();
115
116 SampleBlock block;
117
118 for (size_t i = 0; i < total; i += blockSize) {
119
120 size_t count = blockSize;
121 if (i + count > total) count = total - i;
122
123 m_reader->m_original->getInterleavedFrames(i, count, block);
124 m_reader->addBlock(block);
125
126 if (m_reader->m_cancelled) break;
127 }
128
129 if (m_reader->isDecodeCacheInitialised()) m_reader->finishDecodeCache();
130 m_reader->m_completion = 100;
131
132 m_reader->endSerialised();
133
134 delete m_reader->m_original;
135 m_reader->m_original = 0;
136 }
137
138 void
139 ResamplingWavFileReader::addBlock(const SampleBlock &frames)
140 {
141 addSamplesToDecodeCache(frames);
142
143 m_processed += frames.size();
144
145 int progress = lrint((float(m_processed) * 100) /
146 float(m_original->getFrameCount()));
147
148 if (progress > 99) progress = 99;
149 m_completion = progress;
150
151 if (m_progress) {
152 if (progress > m_progress->value()) {
153 m_progress->setValue(progress);
154 m_progress->show();
155 m_progress->raise();
156 qApp->processEvents();
157 if (m_progress->wasCanceled()) {
158 m_cancelled = true;
159 }
160 }
161 }
162 }
163
164 void
165 ResamplingWavFileReader::getSupportedExtensions(std::set<QString> &extensions)
166 {
167 WavFileReader::getSupportedExtensions(extensions);
168 }
169
170