comparison audio/AudioRecordTarget.cpp @ 482:01669adb0956 tony-2.0-integration

Merge through to branch for Tony 2.0
author Chris Cannam
date Thu, 20 Aug 2015 14:54:21 +0100
parents 1d4cb8befcfd
children 493f2af85497
comparison
equal deleted inserted replaced
448:b36042cb972a 482:01669adb0956
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 "AudioRecordTarget.h"
16
17 #include "base/ViewManagerBase.h"
18 #include "base/TempDirectory.h"
19
20 #include "data/model/WritableWaveFileModel.h"
21
22 #include <QDir>
23
24 AudioRecordTarget::AudioRecordTarget(ViewManagerBase *manager,
25 QString clientName) :
26 m_viewManager(manager),
27 m_clientName(clientName.toUtf8().data()),
28 m_recording(false),
29 m_recordSampleRate(44100),
30 m_model(0)
31 {
32 }
33
34 AudioRecordTarget::~AudioRecordTarget()
35 {
36 QMutexLocker locker(&m_mutex);
37 }
38
39 void
40 AudioRecordTarget::setSystemRecordBlockSize(int sz)
41 {
42 }
43
44 void
45 AudioRecordTarget::setSystemRecordSampleRate(int n)
46 {
47 m_recordSampleRate = n;
48 }
49
50 void
51 AudioRecordTarget::setSystemRecordLatency(int sz)
52 {
53 }
54
55 void
56 AudioRecordTarget::putSamples(int nframes, float **samples)
57 {
58 QMutexLocker locker(&m_mutex); //!!! bad here
59 if (!m_recording) return;
60 m_model->addSamples(samples, nframes);
61 }
62
63 void
64 AudioRecordTarget::setInputLevels(float peakLeft, float peakRight)
65 {
66 }
67
68 void
69 AudioRecordTarget::modelAboutToBeDeleted()
70 {
71 QMutexLocker locker(&m_mutex);
72 if (sender() == m_model) {
73 m_model = 0;
74 m_recording = false;
75 }
76 }
77
78 WritableWaveFileModel *
79 AudioRecordTarget::startRecording()
80 {
81 {
82 QMutexLocker locker(&m_mutex);
83 if (m_recording) {
84 cerr << "WARNING: AudioRecordTarget::startRecording: We are already recording" << endl;
85 return 0;
86 }
87
88 m_model = 0;
89
90 QDir parent(TempDirectory::getInstance()->getContainingPath());
91 QDir recordedDir;
92 QString subdirname = "recorded"; //!!! tr?
93 if (!parent.mkpath(subdirname)) {
94 cerr << "ERROR: AudioRecordTarget::startRecording: Failed to create recorded dir in \"" << parent.canonicalPath() << "\"" << endl;
95 return 0;
96 } else {
97 recordedDir = parent.filePath(subdirname);
98 }
99
100 QDateTime now = QDateTime::currentDateTime();
101
102 // Don't use QDateTime::toString(Qt::ISODate) as the ":" character
103 // isn't permitted in filenames on Windows
104 QString filename = QString("recorded-%1.wav")
105 .arg(now.toString("yyyyMMdd-HHmmss-zzz"));
106
107 m_audioFileName = recordedDir.filePath(filename);
108
109 m_model = new WritableWaveFileModel(m_recordSampleRate, 2, m_audioFileName);
110
111 if (!m_model->isOK()) {
112 cerr << "ERROR: AudioRecordTarget::startRecording: Recording failed"
113 << endl;
114 //!!! and throw?
115 delete m_model;
116 m_model = 0;
117 return 0;
118 }
119
120 m_recording = true;
121 }
122
123 emit recordStatusChanged(true);
124 return m_model;
125 }
126
127 void
128 AudioRecordTarget::stopRecording()
129 {
130 {
131 QMutexLocker locker(&m_mutex);
132 if (!m_recording) {
133 cerr << "WARNING: AudioRecordTarget::startRecording: Not recording" << endl;
134 return;
135 }
136
137 m_model->setCompletion(100);
138 m_model = 0;
139 m_recording = false;
140 }
141
142 emit recordStatusChanged(false);
143 }
144
145