Chris@476
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@476
|
2
|
Chris@476
|
3 /*
|
Chris@476
|
4 Sonic Visualiser
|
Chris@476
|
5 An audio file viewer and annotation editor.
|
Chris@476
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@476
|
7
|
Chris@476
|
8 This program is free software; you can redistribute it and/or
|
Chris@476
|
9 modify it under the terms of the GNU General Public License as
|
Chris@476
|
10 published by the Free Software Foundation; either version 2 of the
|
Chris@476
|
11 License, or (at your option) any later version. See the file
|
Chris@476
|
12 COPYING included with this distribution for more information.
|
Chris@476
|
13 */
|
Chris@476
|
14
|
Chris@476
|
15 #include "AudioRecordTarget.h"
|
Chris@476
|
16
|
Chris@476
|
17 #include "base/ViewManagerBase.h"
|
Chris@476
|
18 #include "base/TempDirectory.h"
|
Chris@476
|
19
|
Chris@477
|
20 #include "data/model/WritableWaveFileModel.h"
|
Chris@476
|
21
|
Chris@476
|
22 #include <QDir>
|
Chris@476
|
23
|
Chris@476
|
24 AudioRecordTarget::AudioRecordTarget(ViewManagerBase *manager,
|
Chris@476
|
25 QString clientName) :
|
Chris@476
|
26 m_viewManager(manager),
|
Chris@476
|
27 m_clientName(clientName.toUtf8().data()),
|
Chris@476
|
28 m_recording(false),
|
Chris@476
|
29 m_recordSampleRate(44100),
|
Chris@546
|
30 m_recordChannelCount(2),
|
Chris@485
|
31 m_frameCount(0),
|
Chris@477
|
32 m_model(0)
|
Chris@476
|
33 {
|
Chris@476
|
34 }
|
Chris@476
|
35
|
Chris@476
|
36 AudioRecordTarget::~AudioRecordTarget()
|
Chris@476
|
37 {
|
Chris@476
|
38 QMutexLocker locker(&m_mutex);
|
Chris@476
|
39 }
|
Chris@476
|
40
|
Chris@476
|
41 void
|
Chris@491
|
42 AudioRecordTarget::setSystemRecordBlockSize(int)
|
Chris@476
|
43 {
|
Chris@476
|
44 }
|
Chris@476
|
45
|
Chris@476
|
46 void
|
Chris@476
|
47 AudioRecordTarget::setSystemRecordSampleRate(int n)
|
Chris@476
|
48 {
|
Chris@476
|
49 m_recordSampleRate = n;
|
Chris@476
|
50 }
|
Chris@476
|
51
|
Chris@476
|
52 void
|
Chris@491
|
53 AudioRecordTarget::setSystemRecordLatency(int)
|
Chris@476
|
54 {
|
Chris@476
|
55 }
|
Chris@476
|
56
|
Chris@476
|
57 void
|
Chris@546
|
58 AudioRecordTarget::setSystemRecordChannelCount(int c)
|
Chris@546
|
59 {
|
Chris@546
|
60 m_recordChannelCount = c;
|
Chris@546
|
61 }
|
Chris@546
|
62
|
Chris@546
|
63 void
|
Chris@476
|
64 AudioRecordTarget::putSamples(int nframes, float **samples)
|
Chris@476
|
65 {
|
Chris@485
|
66 bool secChanged = false;
|
Chris@485
|
67 sv_frame_t frameToEmit = 0;
|
Chris@485
|
68
|
Chris@485
|
69 {
|
Chris@485
|
70 QMutexLocker locker(&m_mutex); //!!! bad here
|
Chris@485
|
71 if (!m_recording) return;
|
Chris@485
|
72
|
Chris@485
|
73 m_model->addSamples(samples, nframes);
|
Chris@485
|
74
|
Chris@485
|
75 sv_frame_t priorFrameCount = m_frameCount;
|
Chris@485
|
76 m_frameCount += nframes;
|
Chris@485
|
77
|
Chris@485
|
78 RealTime priorRT = RealTime::frame2RealTime
|
Chris@485
|
79 (priorFrameCount, m_recordSampleRate);
|
Chris@485
|
80 RealTime postRT = RealTime::frame2RealTime
|
Chris@485
|
81 (m_frameCount, m_recordSampleRate);
|
Chris@485
|
82
|
Chris@485
|
83 secChanged = (postRT.sec > priorRT.sec);
|
Chris@485
|
84 if (secChanged) frameToEmit = m_frameCount;
|
Chris@485
|
85 }
|
Chris@485
|
86
|
Chris@485
|
87 if (secChanged) {
|
Chris@485
|
88 emit recordDurationChanged(frameToEmit, m_recordSampleRate);
|
Chris@485
|
89 }
|
Chris@476
|
90 }
|
Chris@476
|
91
|
Chris@476
|
92 void
|
Chris@491
|
93 AudioRecordTarget::setInputLevels(float, float)
|
Chris@476
|
94 {
|
Chris@476
|
95 }
|
Chris@476
|
96
|
Chris@477
|
97 void
|
Chris@477
|
98 AudioRecordTarget::modelAboutToBeDeleted()
|
Chris@477
|
99 {
|
Chris@477
|
100 QMutexLocker locker(&m_mutex);
|
Chris@477
|
101 if (sender() == m_model) {
|
Chris@477
|
102 m_model = 0;
|
Chris@477
|
103 m_recording = false;
|
Chris@477
|
104 }
|
Chris@477
|
105 }
|
Chris@477
|
106
|
Chris@483
|
107 QString
|
Chris@508
|
108 AudioRecordTarget::getRecordContainerFolder()
|
Chris@508
|
109 {
|
Chris@508
|
110 QDir parent(TempDirectory::getInstance()->getContainingPath());
|
Chris@508
|
111 QString subdirname("recorded");
|
Chris@508
|
112
|
Chris@508
|
113 if (!parent.mkpath(subdirname)) {
|
Chris@508
|
114 cerr << "ERROR: AudioRecordTarget::getRecordContainerFolder: Failed to create recorded dir in \"" << parent.canonicalPath() << "\"" << endl;
|
Chris@508
|
115 return "";
|
Chris@508
|
116 } else {
|
Chris@508
|
117 return parent.filePath(subdirname);
|
Chris@508
|
118 }
|
Chris@508
|
119 }
|
Chris@508
|
120
|
Chris@508
|
121 QString
|
Chris@483
|
122 AudioRecordTarget::getRecordFolder()
|
Chris@483
|
123 {
|
Chris@508
|
124 QDir parent(getRecordContainerFolder());
|
Chris@508
|
125 QDateTime now = QDateTime::currentDateTime();
|
Chris@508
|
126 QString subdirname = QString("%1").arg(now.toString("yyyyMMdd"));
|
Chris@508
|
127
|
Chris@483
|
128 if (!parent.mkpath(subdirname)) {
|
Chris@483
|
129 cerr << "ERROR: AudioRecordTarget::getRecordFolder: Failed to create recorded dir in \"" << parent.canonicalPath() << "\"" << endl;
|
Chris@483
|
130 return "";
|
Chris@483
|
131 } else {
|
Chris@483
|
132 return parent.filePath(subdirname);
|
Chris@483
|
133 }
|
Chris@483
|
134 }
|
Chris@483
|
135
|
Chris@477
|
136 WritableWaveFileModel *
|
Chris@476
|
137 AudioRecordTarget::startRecording()
|
Chris@476
|
138 {
|
Chris@477
|
139 {
|
Chris@476
|
140 QMutexLocker locker(&m_mutex);
|
Chris@498
|
141
|
Chris@476
|
142 if (m_recording) {
|
Chris@476
|
143 cerr << "WARNING: AudioRecordTarget::startRecording: We are already recording" << endl;
|
Chris@477
|
144 return 0;
|
Chris@476
|
145 }
|
Chris@476
|
146
|
Chris@477
|
147 m_model = 0;
|
Chris@485
|
148 m_frameCount = 0;
|
Chris@477
|
149
|
Chris@483
|
150 QString folder = getRecordFolder();
|
Chris@483
|
151 if (folder == "") return 0;
|
Chris@483
|
152 QDir recordedDir(folder);
|
Chris@476
|
153
|
Chris@480
|
154 QDateTime now = QDateTime::currentDateTime();
|
Chris@476
|
155
|
Chris@480
|
156 // Don't use QDateTime::toString(Qt::ISODate) as the ":" character
|
Chris@480
|
157 // isn't permitted in filenames on Windows
|
Chris@480
|
158 QString filename = QString("recorded-%1.wav")
|
Chris@480
|
159 .arg(now.toString("yyyyMMdd-HHmmss-zzz"));
|
Chris@476
|
160
|
Chris@476
|
161 m_audioFileName = recordedDir.filePath(filename);
|
Chris@476
|
162
|
Chris@546
|
163 m_model = new WritableWaveFileModel(m_recordSampleRate,
|
Chris@546
|
164 m_recordChannelCount,
|
Chris@546
|
165 m_audioFileName);
|
Chris@477
|
166
|
Chris@477
|
167 if (!m_model->isOK()) {
|
Chris@477
|
168 cerr << "ERROR: AudioRecordTarget::startRecording: Recording failed"
|
Chris@477
|
169 << endl;
|
Chris@476
|
170 //!!! and throw?
|
Chris@477
|
171 delete m_model;
|
Chris@477
|
172 m_model = 0;
|
Chris@477
|
173 return 0;
|
Chris@476
|
174 }
|
Chris@476
|
175
|
Chris@476
|
176 m_recording = true;
|
Chris@477
|
177 }
|
Chris@477
|
178
|
Chris@477
|
179 emit recordStatusChanged(true);
|
Chris@477
|
180 return m_model;
|
Chris@476
|
181 }
|
Chris@476
|
182
|
Chris@476
|
183 void
|
Chris@476
|
184 AudioRecordTarget::stopRecording()
|
Chris@476
|
185 {
|
Chris@477
|
186 {
|
Chris@476
|
187 QMutexLocker locker(&m_mutex);
|
Chris@476
|
188 if (!m_recording) {
|
Chris@476
|
189 cerr << "WARNING: AudioRecordTarget::startRecording: Not recording" << endl;
|
Chris@476
|
190 return;
|
Chris@476
|
191 }
|
Chris@476
|
192
|
Chris@497
|
193 m_model->writeComplete();
|
Chris@477
|
194 m_model = 0;
|
Chris@476
|
195 m_recording = false;
|
Chris@477
|
196 }
|
Chris@477
|
197
|
Chris@477
|
198 emit recordStatusChanged(false);
|
Chris@497
|
199 emit recordCompleted();
|
Chris@476
|
200 }
|
Chris@476
|
201
|
Chris@476
|
202
|