annotate 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
rev   line source
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@477 30 m_model(0)
Chris@476 31 {
Chris@476 32 }
Chris@476 33
Chris@476 34 AudioRecordTarget::~AudioRecordTarget()
Chris@476 35 {
Chris@476 36 QMutexLocker locker(&m_mutex);
Chris@476 37 }
Chris@476 38
Chris@476 39 void
Chris@476 40 AudioRecordTarget::setSystemRecordBlockSize(int sz)
Chris@476 41 {
Chris@476 42 }
Chris@476 43
Chris@476 44 void
Chris@476 45 AudioRecordTarget::setSystemRecordSampleRate(int n)
Chris@476 46 {
Chris@476 47 m_recordSampleRate = n;
Chris@476 48 }
Chris@476 49
Chris@476 50 void
Chris@476 51 AudioRecordTarget::setSystemRecordLatency(int sz)
Chris@476 52 {
Chris@476 53 }
Chris@476 54
Chris@476 55 void
Chris@476 56 AudioRecordTarget::putSamples(int nframes, float **samples)
Chris@476 57 {
Chris@476 58 QMutexLocker locker(&m_mutex); //!!! bad here
Chris@476 59 if (!m_recording) return;
Chris@477 60 m_model->addSamples(samples, nframes);
Chris@476 61 }
Chris@476 62
Chris@476 63 void
Chris@476 64 AudioRecordTarget::setInputLevels(float peakLeft, float peakRight)
Chris@476 65 {
Chris@476 66 }
Chris@476 67
Chris@477 68 void
Chris@477 69 AudioRecordTarget::modelAboutToBeDeleted()
Chris@477 70 {
Chris@477 71 QMutexLocker locker(&m_mutex);
Chris@477 72 if (sender() == m_model) {
Chris@477 73 m_model = 0;
Chris@477 74 m_recording = false;
Chris@477 75 }
Chris@477 76 }
Chris@477 77
Chris@477 78 WritableWaveFileModel *
Chris@476 79 AudioRecordTarget::startRecording()
Chris@476 80 {
Chris@477 81 {
Chris@476 82 QMutexLocker locker(&m_mutex);
Chris@476 83 if (m_recording) {
Chris@476 84 cerr << "WARNING: AudioRecordTarget::startRecording: We are already recording" << endl;
Chris@477 85 return 0;
Chris@476 86 }
Chris@476 87
Chris@477 88 m_model = 0;
Chris@477 89
Chris@476 90 QDir parent(TempDirectory::getInstance()->getContainingPath());
Chris@476 91 QDir recordedDir;
Chris@476 92 QString subdirname = "recorded"; //!!! tr?
Chris@476 93 if (!parent.mkpath(subdirname)) {
Chris@476 94 cerr << "ERROR: AudioRecordTarget::startRecording: Failed to create recorded dir in \"" << parent.canonicalPath() << "\"" << endl;
Chris@477 95 return 0;
Chris@476 96 } else {
Chris@476 97 recordedDir = parent.filePath(subdirname);
Chris@476 98 }
Chris@476 99
Chris@480 100 QDateTime now = QDateTime::currentDateTime();
Chris@476 101
Chris@480 102 // Don't use QDateTime::toString(Qt::ISODate) as the ":" character
Chris@480 103 // isn't permitted in filenames on Windows
Chris@480 104 QString filename = QString("recorded-%1.wav")
Chris@480 105 .arg(now.toString("yyyyMMdd-HHmmss-zzz"));
Chris@476 106
Chris@476 107 m_audioFileName = recordedDir.filePath(filename);
Chris@476 108
Chris@477 109 m_model = new WritableWaveFileModel(m_recordSampleRate, 2, m_audioFileName);
Chris@477 110
Chris@477 111 if (!m_model->isOK()) {
Chris@477 112 cerr << "ERROR: AudioRecordTarget::startRecording: Recording failed"
Chris@477 113 << endl;
Chris@476 114 //!!! and throw?
Chris@477 115 delete m_model;
Chris@477 116 m_model = 0;
Chris@477 117 return 0;
Chris@476 118 }
Chris@476 119
Chris@476 120 m_recording = true;
Chris@477 121 }
Chris@477 122
Chris@477 123 emit recordStatusChanged(true);
Chris@477 124 return m_model;
Chris@476 125 }
Chris@476 126
Chris@476 127 void
Chris@476 128 AudioRecordTarget::stopRecording()
Chris@476 129 {
Chris@477 130 {
Chris@476 131 QMutexLocker locker(&m_mutex);
Chris@476 132 if (!m_recording) {
Chris@476 133 cerr << "WARNING: AudioRecordTarget::startRecording: Not recording" << endl;
Chris@476 134 return;
Chris@476 135 }
Chris@476 136
Chris@477 137 m_model->setCompletion(100);
Chris@477 138 m_model = 0;
Chris@476 139 m_recording = false;
Chris@477 140 }
Chris@477 141
Chris@477 142 emit recordStatusChanged(false);
Chris@476 143 }
Chris@476 144
Chris@476 145