1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-23 16:38:37 -05:00
sdrangel/plugins/channelrx/filesink/filesinksink.cpp

351 lines
11 KiB
C++
Raw Normal View History

2020-08-06 04:46:49 -04:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
// written by Christian Daniel //
// Copyright (C) 2015-2021 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
// Copyright (C) 2021, 2023 Jon Beniston, M7RCE <jon@beniston.com> //
// Copyright (C) 2021 Christoph Berg <myon@debian.org> //
2020-08-06 04:46:49 -04:00
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation as version 3 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <QDebug>
#include <QFileInfo>
2020-08-06 04:46:49 -04:00
#include "dsp/dspcommands.h"
#include "dsp/spectrumvis.h"
#include "filesinkmessages.h"
#include "filesinksink.h"
#include "dsp/filerecord.h"
#include "dsp/wavfilerecord.h"
2020-08-06 04:46:49 -04:00
FileSinkSink::FileSinkSink() :
2021-10-12 15:20:45 -04:00
m_sinkSampleRate(0),
2020-08-06 04:46:49 -04:00
m_nbCaptures(0),
m_preRecordBuffer(48000),
m_preRecordFill(0),
2020-11-13 23:51:19 -05:00
m_spectrumSink(nullptr),
m_msgQueueToGUI(nullptr),
2020-08-06 04:46:49 -04:00
m_recordEnabled(false),
m_record(false),
m_squelchOpen(false),
m_postSquelchCounter(0),
m_msCount(0),
m_byteCount(0),
m_bytesPerSample(sizeof(Sample))
{
m_fileSink = new FileRecord();
}
2020-08-06 04:46:49 -04:00
FileSinkSink::~FileSinkSink()
{}
void FileSinkSink::startRecording()
{
if (m_recordEnabled) // File is open for writing and valid
{
// set the length of pre record time
2021-10-12 15:20:45 -04:00
qint64 mSShift = m_sinkSampleRate == 0 ? 0 : (m_preRecordFill * 1000) / m_sinkSampleRate;
m_fileSink->setMsShift(-mSShift);
2020-08-06 04:46:49 -04:00
// notify capture start
if (!m_fileSink->startRecording())
{
// qWarning already output in startRecording, just need to send to GUI
if (m_msgQueueToGUI)
{
FileSinkMessages::MsgReportRecordFileError *msg
= FileSinkMessages::MsgReportRecordFileError::create(QString("Failed to open %1").arg(m_fileSink->getCurrentFileName()));
m_msgQueueToGUI->push(msg);
}
return;
}
2020-08-06 04:46:49 -04:00
m_record = true;
m_nbCaptures++;
if (m_msgQueueToGUI)
{
FileSinkMessages::MsgReportRecordFileName *msg1
= FileSinkMessages::MsgReportRecordFileName::create(m_fileSink->getCurrentFileName());
m_msgQueueToGUI->push(msg1);
FileSinkMessages::MsgReportRecording *msg2 = FileSinkMessages::MsgReportRecording::create(true);
m_msgQueueToGUI->push(msg2);
2020-08-06 04:46:49 -04:00
}
// copy pre record samples
SampleVector::iterator p1Begin, p1End, p2Begin, p2End;
m_preRecordBuffer.readBegin(m_preRecordFill, &p1Begin, &p1End, &p2Begin, &p2End);
if (p1Begin != p1End) {
m_fileSink->feed(p1Begin, p1End, false);
2020-08-06 04:46:49 -04:00
}
if (p2Begin != p2End) {
m_fileSink->feed(p2Begin, p2End, false);
2020-08-06 04:46:49 -04:00
}
m_byteCount += m_preRecordFill * m_bytesPerSample;
2020-08-06 04:46:49 -04:00
if (m_sinkSampleRate > 0) {
m_msCount += (m_preRecordFill * 1000) / m_sinkSampleRate;
}
}
}
void FileSinkSink::stopRecording()
{
if (m_record)
{
m_preRecordBuffer.reset();
if (!m_fileSink->stopRecording())
{
// qWarning already output stopRecording, just need to send to GUI
if (m_msgQueueToGUI)
{
FileSinkMessages::MsgReportRecordFileError *msg
= FileSinkMessages::MsgReportRecordFileError::create(QString("Error while writing to %1").arg(m_fileSink->getCurrentFileName()));
m_msgQueueToGUI->push(msg);
}
}
if (m_msgQueueToGUI)
{
FileSinkMessages::MsgReportRecording *msg = FileSinkMessages::MsgReportRecording::create(false);
m_msgQueueToGUI->push(msg);
}
2020-08-06 04:46:49 -04:00
m_record = false;
}
}
void FileSinkSink::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end)
{
SampleVector::const_iterator beginw = begin;
SampleVector::const_iterator endw = end;
if (m_decimator.getDecim() != 1)
{
for (SampleVector::const_iterator it = begin; it < end; ++it)
{
Complex c(it->real(), it->imag());
c *= m_nco.nextIQ();
Complex ci;
if (m_decimator.decimate(c, ci)) {
m_sampleBuffer.push_back(Sample(ci.real(), ci.imag()));
}
}
beginw = m_sampleBuffer.begin();
endw = m_sampleBuffer.end();
}
if (!m_record && (m_settings.m_preRecordTime != 0)) {
m_preRecordFill = m_preRecordBuffer.write(beginw, endw);
}
if (m_settings.m_squelchRecordingEnable)
{
int nbToWrite = endw - beginw;
if (m_squelchOpen)
{
m_fileSink->feed(beginw, endw, true);
2020-08-06 04:46:49 -04:00
}
else
{
if (nbToWrite < m_postSquelchCounter)
{
m_fileSink->feed(beginw, endw, true);
2020-08-06 04:46:49 -04:00
m_postSquelchCounter -= nbToWrite;
}
else
{
m_fileSink->feed(beginw, endw + m_postSquelchCounter, true);
2020-08-06 04:46:49 -04:00
nbToWrite = m_postSquelchCounter;
m_postSquelchCounter = 0;
stopRecording();
}
}
m_byteCount += nbToWrite * m_bytesPerSample;
2020-08-06 04:46:49 -04:00
if (m_sinkSampleRate > 0) {
m_msCount += (nbToWrite * 1000) / m_sinkSampleRate;
}
}
else if (m_record)
{
m_fileSink->feed(beginw, endw, true);
2020-08-06 04:46:49 -04:00
int nbSamples = endw - beginw;
m_byteCount += nbSamples * m_bytesPerSample;
2020-08-06 04:46:49 -04:00
if (m_sinkSampleRate > 0) {
m_msCount += (nbSamples * 1000) / m_sinkSampleRate;
}
}
if (m_spectrumSink) {
m_spectrumSink->feed(beginw, endw, false);
}
if (m_decimator.getDecim() != 1) {
m_sampleBuffer.clear();
}
}
void FileSinkSink::applyChannelSettings(
int channelSampleRate,
int sinkSampleRate,
int channelFrequencyOffset,
int64_t centerFrequency,
bool force)
{
qDebug() << "FileSinkSink::applyChannelSettings:"
<< " channelSampleRate: " << channelSampleRate
<< " sinkSampleRate: " << sinkSampleRate
<< " channelFrequencyOffset: " << channelFrequencyOffset
<< " centerFrequency: " << centerFrequency
<< " force: " << force;
if ((m_channelFrequencyOffset != channelFrequencyOffset) ||
(m_channelSampleRate != channelSampleRate) || force)
{
m_nco.setFreq(-channelFrequencyOffset, channelSampleRate);
}
if ((m_channelSampleRate != channelSampleRate)
|| (m_sinkSampleRate != sinkSampleRate) || force)
{
int decim = channelSampleRate / sinkSampleRate;
2021-01-19 06:36:56 -05:00
for (int i = 0; i < 7; i++) // find log2 between 0 and 6
2020-08-06 04:46:49 -04:00
{
if ((decim & 1) == 1)
2020-08-06 04:46:49 -04:00
{
qDebug() << "FileSinkSink::applyChannelSettings: log2decim: " << i;
m_decimator.setLog2Decim(i);
break;
}
decim >>= 1;
}
}
if ((m_centerFrequency != centerFrequency)
|| (m_channelFrequencyOffset != channelFrequencyOffset)
|| (m_sinkSampleRate != sinkSampleRate) || force)
{
DSPSignalNotification *notif = new DSPSignalNotification(sinkSampleRate, centerFrequency);
DSPSignalNotification *notifToSpectrum = new DSPSignalNotification(*notif);
m_fileSink->getInputMessageQueue()->push(notif);
2020-08-06 04:46:49 -04:00
m_spectrumSink->getInputMessageQueue()->push(notifToSpectrum);
if (m_msgQueueToGUI)
{
FileSinkMessages::MsgConfigureSpectrum *msg = FileSinkMessages::MsgConfigureSpectrum::create(
centerFrequency, sinkSampleRate);
m_msgQueueToGUI->push(msg);
}
}
if ((m_sinkSampleRate != sinkSampleRate) || force) {
m_preRecordBuffer.setSize(m_settings.m_preRecordTime * sinkSampleRate);
}
m_channelSampleRate = channelSampleRate;
m_channelFrequencyOffset = channelFrequencyOffset;
m_sinkSampleRate = sinkSampleRate;
m_centerFrequency = centerFrequency;
m_preRecordBuffer.reset();
}
void FileSinkSink::applySettings(const FileSinkSettings& settings, bool force)
{
qDebug() << "FileSinkSink::applySettings:"
<< "m_fileRecordName: " << settings.m_fileRecordName
<< "force: " << force;
QString fileRecordName = settings.m_fileRecordName;
2020-08-06 04:46:49 -04:00
if ((settings.m_fileRecordName != m_settings.m_fileRecordName) || force)
{
QFileInfo fileInfo(settings.m_fileRecordName);
QString extension = fileInfo.suffix();
if (extension.isEmpty()) {
fileRecordName.append(".sdriq");
} else if ((extension != "sdriq") && (extension != "wav")) {
fileRecordName.chop(extension.size());
fileRecordName.append("sdriq");
}
2020-08-06 04:46:49 -04:00
QString fileBase;
FileRecordInterface::RecordType recordType = FileRecordInterface::guessTypeFromFileName(fileRecordName, fileBase);
2020-08-06 04:46:49 -04:00
if ((recordType == FileRecordInterface::RecordTypeSdrIQ) || (recordType == FileRecordInterface::RecordTypeWav))
2020-08-06 04:46:49 -04:00
{
delete m_fileSink;
if (recordType == FileRecordInterface::RecordTypeSdrIQ) {
m_fileSink = new FileRecord(m_sinkSampleRate, m_centerFrequency);
} else {
m_fileSink = new WavFileRecord(m_sinkSampleRate, m_centerFrequency);
}
m_fileSink->setFileName(fileBase);
2020-08-06 04:46:49 -04:00
m_msCount = 0;
m_byteCount = 0;
m_bytesPerSample = m_fileSink->getBytesPerSample();
2020-08-06 04:46:49 -04:00
m_nbCaptures = 0;
m_recordEnabled = true;
}
else
{
m_recordEnabled = false;
}
}
if ((settings.m_preRecordTime != m_settings.m_squelchPostRecordTime) || force)
{
m_preRecordBuffer.setSize(settings.m_preRecordTime * m_sinkSampleRate);
if (settings.m_preRecordTime == 0) {
m_preRecordFill = 0;
}
}
m_settings = settings;
m_settings.m_fileRecordName = fileRecordName;
2020-08-06 04:46:49 -04:00
}
void FileSinkSink::squelchRecording(bool squelchOpen)
{
if (!m_recordEnabled || !m_settings.m_squelchRecordingEnable) {
return;
}
if (squelchOpen)
{
if (!m_record) {
2020-08-06 04:46:49 -04:00
startRecording();
}
}
else
{
m_postSquelchCounter = m_settings.m_squelchPostRecordTime * m_sinkSampleRate;
}
m_squelchOpen = squelchOpen;
2020-11-13 23:51:19 -05:00
}