comparison audio/AudioRecordTarget.cpp @ 476:f4d1fa41b94b recording

Basic recording stuff
author Chris Cannam
date Tue, 18 Aug 2015 15:00:34 +0100
parents
children 411e019474e5
comparison
equal deleted inserted replaced
475:f93820d36cb0 476:f4d1fa41b94b
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/fileio/WavFileWriter.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_writer(0)
31 {
32 }
33
34 AudioRecordTarget::~AudioRecordTarget()
35 {
36 QMutexLocker locker(&m_mutex);
37 delete m_writer;
38 }
39
40 void
41 AudioRecordTarget::setSystemRecordBlockSize(int sz)
42 {
43 }
44
45 void
46 AudioRecordTarget::setSystemRecordSampleRate(int n)
47 {
48 m_recordSampleRate = n;
49 }
50
51 void
52 AudioRecordTarget::setSystemRecordLatency(int sz)
53 {
54 }
55
56 void
57 AudioRecordTarget::putSamples(int nframes, float **samples)
58 {
59 QMutexLocker locker(&m_mutex); //!!! bad here
60 if (!m_recording) return;
61 m_writer->writeSamples(samples, nframes);
62 }
63
64 void
65 AudioRecordTarget::setInputLevels(float peakLeft, float peakRight)
66 {
67 }
68
69 QString
70 AudioRecordTarget::startRecording()
71 {
72 QMutexLocker locker(&m_mutex);
73 if (m_recording) {
74 cerr << "WARNING: AudioRecordTarget::startRecording: We are already recording" << endl;
75 return "";
76 }
77
78 QDir parent(TempDirectory::getInstance()->getContainingPath());
79 QDir recordedDir;
80 QString subdirname = "recorded"; //!!! tr?
81 if (!parent.mkpath(subdirname)) {
82 cerr << "ERROR: AudioRecordTarget::startRecording: Failed to create recorded dir in \"" << parent.canonicalPath() << "\"" << endl;
83 return "";
84 } else {
85 recordedDir = parent.filePath(subdirname);
86 }
87
88 //!!! todo proper temp name as in TempDirectory
89
90 QString filename = "recorded.wav"; //!!!
91
92 m_audioFileName = recordedDir.filePath(filename);
93
94 m_writer = new WavFileWriter(m_audioFileName,
95 m_recordSampleRate,
96 2,
97 WavFileWriter::WriteToTarget);
98
99 if (!m_writer->isOK()) {
100 cerr << "ERROR: AudioRecordTarget::startRecording: Recording failed: "
101 << m_writer->getError() << endl;
102 //!!! and throw?
103 delete m_writer;
104 return "";
105 }
106
107 m_recording = true;
108
109 return m_audioFileName;
110 }
111
112 void
113 AudioRecordTarget::stopRecording()
114 {
115 QMutexLocker locker(&m_mutex);
116 if (!m_recording) {
117 cerr << "WARNING: AudioRecordTarget::startRecording: Not recording" << endl;
118 return;
119 }
120
121 m_writer->close();
122 delete m_writer;
123 m_recording = false;
124 }
125
126