2015-08-02 19:04:20 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2015 Edouard Griffiths, F4EXB //
|
|
|
|
// //
|
|
|
|
// 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 //
|
|
|
|
// //
|
|
|
|
// 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 <string.h>
|
|
|
|
#include <errno.h>
|
2015-08-09 04:33:04 -04:00
|
|
|
#include <QDebug>
|
2015-08-02 19:04:20 -04:00
|
|
|
|
|
|
|
#include "util/simpleserializer.h"
|
2015-08-19 16:12:08 -04:00
|
|
|
#include "dsp/dspcommands.h"
|
2015-09-27 06:50:38 -04:00
|
|
|
#include "dsp/dspengine.h"
|
2015-08-02 19:04:20 -04:00
|
|
|
#include "filesourcegui.h"
|
|
|
|
#include "filesourceinput.h"
|
2016-10-02 04:30:58 -04:00
|
|
|
|
|
|
|
#include <dsp/filerecord.h>
|
|
|
|
|
2015-08-02 19:04:20 -04:00
|
|
|
#include "filesourcethread.h"
|
|
|
|
|
|
|
|
MESSAGE_CLASS_DEFINITION(FileSourceInput::MsgConfigureFileSource, Message)
|
2015-08-05 18:10:46 -04:00
|
|
|
MESSAGE_CLASS_DEFINITION(FileSourceInput::MsgConfigureFileSourceName, Message)
|
2015-08-05 19:14:44 -04:00
|
|
|
MESSAGE_CLASS_DEFINITION(FileSourceInput::MsgConfigureFileSourceWork, Message)
|
2016-02-25 08:07:39 -05:00
|
|
|
MESSAGE_CLASS_DEFINITION(FileSourceInput::MsgConfigureFileSourceSeek, Message)
|
2015-08-05 19:43:35 -04:00
|
|
|
MESSAGE_CLASS_DEFINITION(FileSourceInput::MsgConfigureFileSourceStreamTiming, Message)
|
2015-08-04 21:25:06 -04:00
|
|
|
MESSAGE_CLASS_DEFINITION(FileSourceInput::MsgReportFileSourceAcquisition, Message)
|
|
|
|
MESSAGE_CLASS_DEFINITION(FileSourceInput::MsgReportFileSourceStreamData, Message)
|
2015-08-05 19:14:44 -04:00
|
|
|
MESSAGE_CLASS_DEFINITION(FileSourceInput::MsgReportFileSourceStreamTiming, Message)
|
2015-08-02 19:04:20 -04:00
|
|
|
|
|
|
|
FileSourceInput::Settings::Settings() :
|
|
|
|
m_fileName("./test.sdriq")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileSourceInput::Settings::resetToDefaults()
|
|
|
|
{
|
|
|
|
m_fileName = "./test.sdriq";
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray FileSourceInput::Settings::serialize() const
|
|
|
|
{
|
|
|
|
SimpleSerializer s(1);
|
|
|
|
s.writeString(1, m_fileName);
|
|
|
|
return s.final();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSourceInput::Settings::deserialize(const QByteArray& data)
|
|
|
|
{
|
|
|
|
SimpleDeserializer d(data);
|
|
|
|
|
|
|
|
if(!d.isValid()) {
|
|
|
|
resetToDefaults();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(d.getVersion() == 1) {
|
|
|
|
int intval;
|
|
|
|
d.readString(1, &m_fileName, "./test.sdriq");
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
resetToDefaults();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-17 02:29:34 -04:00
|
|
|
FileSourceInput::FileSourceInput(const QTimer& masterTimer) :
|
2015-08-02 19:04:20 -04:00
|
|
|
m_settings(),
|
|
|
|
m_fileSourceThread(NULL),
|
|
|
|
m_deviceDescription(),
|
2015-08-04 21:25:06 -04:00
|
|
|
m_fileName("..."),
|
2015-08-02 19:04:20 -04:00
|
|
|
m_sampleRate(0),
|
|
|
|
m_centerFrequency(0),
|
2016-02-24 20:30:50 -05:00
|
|
|
m_recordLength(0),
|
2015-08-02 19:04:20 -04:00
|
|
|
m_startingTimeStamp(0),
|
|
|
|
m_masterTimer(masterTimer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSourceInput::~FileSourceInput()
|
|
|
|
{
|
2015-08-17 02:29:34 -04:00
|
|
|
stop();
|
2015-08-02 19:04:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileSourceInput::openFileStream()
|
|
|
|
{
|
2015-08-04 21:25:06 -04:00
|
|
|
//stopInput();
|
|
|
|
|
2015-08-03 22:17:24 -04:00
|
|
|
if (m_ifstream.is_open()) {
|
|
|
|
m_ifstream.close();
|
|
|
|
}
|
|
|
|
|
2016-02-24 20:30:50 -05:00
|
|
|
m_ifstream.open(m_fileName.toStdString().c_str(), std::ios::binary | std::ios::ate);
|
|
|
|
quint64 fileSize = m_ifstream.tellg();
|
|
|
|
m_ifstream.seekg(0,std::ios_base::beg);
|
2016-10-02 04:30:58 -04:00
|
|
|
FileRecord::Header header;
|
|
|
|
FileRecord::readHeader(m_ifstream, header);
|
2015-08-02 19:04:20 -04:00
|
|
|
|
|
|
|
m_sampleRate = header.sampleRate;
|
|
|
|
m_centerFrequency = header.centerFrequency;
|
|
|
|
m_startingTimeStamp = header.startTimeStamp;
|
2015-08-04 21:25:06 -04:00
|
|
|
|
2016-10-02 04:30:58 -04:00
|
|
|
if (fileSize > sizeof(FileRecord::Header)) {
|
|
|
|
m_recordLength = (fileSize - sizeof(FileRecord::Header)) / (4 * m_sampleRate);
|
2016-02-24 20:30:50 -05:00
|
|
|
} else {
|
|
|
|
m_recordLength = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "FileSourceInput::openFileStream: " << m_fileName.toStdString().c_str()
|
|
|
|
<< " fileSize: " << fileSize << "bytes"
|
|
|
|
<< " length: " << m_recordLength << " seconds";
|
|
|
|
|
|
|
|
MsgReportFileSourceStreamData *report = MsgReportFileSourceStreamData::create(m_sampleRate,
|
|
|
|
m_centerFrequency,
|
|
|
|
m_startingTimeStamp,
|
|
|
|
m_recordLength); // file stream data
|
2015-08-26 03:25:02 -04:00
|
|
|
getOutputMessageQueueToGUI()->push(report);
|
2015-08-02 19:04:20 -04:00
|
|
|
}
|
|
|
|
|
2016-02-25 08:07:39 -05:00
|
|
|
void FileSourceInput::seekFileStream(int seekPercentage)
|
|
|
|
{
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
|
|
|
|
|
|
|
if ((m_ifstream.is_open()) && !m_fileSourceThread->isRunning())
|
|
|
|
{
|
|
|
|
int seekPoint = ((m_recordLength * seekPercentage) / 100) * m_sampleRate;
|
|
|
|
m_fileSourceThread->setSamplesCount(seekPoint);
|
|
|
|
seekPoint *= 4; // + sizeof(FileSink::Header)
|
|
|
|
m_ifstream.clear();
|
2016-10-08 04:02:36 -04:00
|
|
|
m_ifstream.seekg(seekPoint + sizeof(FileRecord::Header), std::ios::beg);
|
2016-02-25 08:07:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 21:40:45 -04:00
|
|
|
bool FileSourceInput::start()
|
2015-08-02 19:04:20 -04:00
|
|
|
{
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
2016-10-08 00:00:54 -04:00
|
|
|
qDebug() << "FileSourceInput::start";
|
2015-08-04 21:25:06 -04:00
|
|
|
|
|
|
|
if (m_ifstream.tellg() != 0) {
|
|
|
|
m_ifstream.clear();
|
2016-10-08 04:02:36 -04:00
|
|
|
m_ifstream.seekg(sizeof(FileRecord::Header), std::ios::beg);
|
2015-08-04 21:25:06 -04:00
|
|
|
}
|
2015-08-02 19:04:20 -04:00
|
|
|
|
2016-10-08 00:00:54 -04:00
|
|
|
if(!m_sampleFifo.setSize(m_sampleRate * 4)) {
|
2015-08-04 17:45:32 -04:00
|
|
|
qCritical("Could not allocate SampleFifo");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-04 21:25:06 -04:00
|
|
|
//openFileStream();
|
2015-08-02 19:04:20 -04:00
|
|
|
|
|
|
|
if((m_fileSourceThread = new FileSourceThread(&m_ifstream, &m_sampleFifo)) == NULL) {
|
|
|
|
qFatal("out of memory");
|
2015-08-17 02:29:34 -04:00
|
|
|
stop();
|
|
|
|
return false;
|
2015-08-02 19:04:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
m_fileSourceThread->setSamplerate(m_sampleRate);
|
|
|
|
m_fileSourceThread->connectTimer(m_masterTimer);
|
|
|
|
m_fileSourceThread->startWork();
|
2015-08-10 16:15:16 -04:00
|
|
|
m_deviceDescription = "FileSource";
|
2015-08-02 19:04:20 -04:00
|
|
|
|
|
|
|
mutexLocker.unlock();
|
2015-08-04 21:25:06 -04:00
|
|
|
//applySettings(m_generalSettings, m_settings, true);
|
2015-08-10 16:15:16 -04:00
|
|
|
qDebug("FileSourceInput::startInput: started");
|
2015-08-02 19:04:20 -04:00
|
|
|
|
2015-08-17 02:29:34 -04:00
|
|
|
MsgReportFileSourceAcquisition *report = MsgReportFileSourceAcquisition::create(true); // acquisition on
|
2015-08-26 03:25:02 -04:00
|
|
|
getOutputMessageQueueToGUI()->push(report);
|
2015-08-02 19:04:20 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-17 02:29:34 -04:00
|
|
|
void FileSourceInput::stop()
|
2015-08-02 19:04:20 -04:00
|
|
|
{
|
2015-08-17 02:29:34 -04:00
|
|
|
qDebug() << "FileSourceInput::stop";
|
2015-08-02 19:04:20 -04:00
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
|
|
|
|
2015-08-17 02:29:34 -04:00
|
|
|
if(m_fileSourceThread != 0)
|
|
|
|
{
|
2015-08-02 19:04:20 -04:00
|
|
|
m_fileSourceThread->stopWork();
|
|
|
|
delete m_fileSourceThread;
|
2015-08-17 02:29:34 -04:00
|
|
|
m_fileSourceThread = 0;
|
2015-08-02 19:04:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
m_deviceDescription.clear();
|
2015-08-03 22:17:24 -04:00
|
|
|
|
2015-08-17 02:29:34 -04:00
|
|
|
MsgReportFileSourceAcquisition *report = MsgReportFileSourceAcquisition::create(false); // acquisition off
|
2015-08-26 03:25:02 -04:00
|
|
|
getOutputMessageQueueToGUI()->push(report);
|
2015-08-02 19:04:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString& FileSourceInput::getDeviceDescription() const
|
|
|
|
{
|
|
|
|
return m_deviceDescription;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FileSourceInput::getSampleRate() const
|
|
|
|
{
|
|
|
|
return m_sampleRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 FileSourceInput::getCenterFrequency() const
|
|
|
|
{
|
|
|
|
return m_centerFrequency;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::time_t FileSourceInput::getStartingTimeStamp() const
|
|
|
|
{
|
|
|
|
return m_startingTimeStamp;
|
|
|
|
}
|
|
|
|
|
2015-08-17 02:29:34 -04:00
|
|
|
bool FileSourceInput::handleMessage(const Message& message)
|
2015-08-02 19:04:20 -04:00
|
|
|
{
|
2015-08-05 19:14:44 -04:00
|
|
|
if (MsgConfigureFileSourceName::match(message))
|
|
|
|
{
|
2015-08-17 02:29:34 -04:00
|
|
|
MsgConfigureFileSourceName& conf = (MsgConfigureFileSourceName&) message;
|
|
|
|
m_fileName = conf.getFileName();
|
2015-08-04 21:25:06 -04:00
|
|
|
openFileStream();
|
2015-08-02 19:04:20 -04:00
|
|
|
return true;
|
2015-08-05 19:14:44 -04:00
|
|
|
}
|
|
|
|
else if (MsgConfigureFileSourceWork::match(message))
|
|
|
|
{
|
2015-08-17 02:29:34 -04:00
|
|
|
MsgConfigureFileSourceWork& conf = (MsgConfigureFileSourceWork&) message;
|
|
|
|
bool working = conf.isWorking();
|
|
|
|
|
2015-08-05 19:14:44 -04:00
|
|
|
if (m_fileSourceThread != 0)
|
|
|
|
{
|
2015-08-17 02:29:34 -04:00
|
|
|
if (working)
|
|
|
|
{
|
2015-08-05 19:14:44 -04:00
|
|
|
m_fileSourceThread->startWork();
|
2016-02-25 08:07:39 -05:00
|
|
|
/*
|
2015-08-30 07:04:26 -04:00
|
|
|
MsgReportFileSourceStreamTiming *report =
|
|
|
|
MsgReportFileSourceStreamTiming::create(m_fileSourceThread->getSamplesCount());
|
2016-02-25 08:07:39 -05:00
|
|
|
getOutputMessageQueueToGUI()->push(report);*/
|
2015-08-17 02:29:34 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-05 19:14:44 -04:00
|
|
|
m_fileSourceThread->stopWork();
|
|
|
|
}
|
|
|
|
}
|
2015-08-17 02:29:34 -04:00
|
|
|
|
2015-08-05 19:14:44 -04:00
|
|
|
return true;
|
|
|
|
}
|
2016-02-25 08:07:39 -05:00
|
|
|
else if (MsgConfigureFileSourceSeek::match(message))
|
|
|
|
{
|
|
|
|
MsgConfigureFileSourceSeek& conf = (MsgConfigureFileSourceSeek&) message;
|
|
|
|
int seekPercentage = conf.getPercentage();
|
|
|
|
seekFileStream(seekPercentage);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-05 19:43:35 -04:00
|
|
|
else if (MsgConfigureFileSourceStreamTiming::match(message))
|
|
|
|
{
|
2015-08-17 02:29:34 -04:00
|
|
|
MsgReportFileSourceStreamTiming *report;
|
|
|
|
|
|
|
|
if (m_fileSourceThread != 0)
|
|
|
|
{
|
|
|
|
report = MsgReportFileSourceStreamTiming::create(m_fileSourceThread->getSamplesCount());
|
2015-08-30 07:04:26 -04:00
|
|
|
getOutputMessageQueueToGUI()->push(report);
|
2015-08-17 02:29:34 -04:00
|
|
|
}
|
|
|
|
|
2015-08-30 07:04:26 -04:00
|
|
|
return true;
|
2015-08-05 19:43:35 -04:00
|
|
|
}
|
2015-08-05 19:14:44 -04:00
|
|
|
else
|
|
|
|
{
|
2015-08-02 19:04:20 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|