1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-25 17:28:50 -05:00

Refactor common code into WavFileRecord class

This commit is contained in:
Jon Beniston 2021-05-21 21:05:23 +01:00
parent b036dbfd7d
commit 631b9c256f
4 changed files with 69 additions and 72 deletions

View File

@ -27,7 +27,6 @@
#endif
#include <QDebug>
#include <QRegExp>
#include "dsp/dspcommands.h"
#include "dsp/devicesamplesink.h"
@ -175,7 +174,7 @@ void FileSourceSource::openFileStream(const QString& fileName)
quint64 fileSize = m_ifstream.tellg();
m_samplesCount = 0;
if (m_settings.m_fileName.endsWith(".wav"))
if (m_fileName.endsWith(".wav"))
{
WavFileRecord::Header header;
m_ifstream.seekg(0, std::ios_base::beg);
@ -185,44 +184,16 @@ void FileSourceSource::openFileStream(const QString& fileName)
{
// Some WAV files written by SDR tools have auxi header
m_centerFrequency = header.m_auxi.m_centerFreq;
m_startingTimeStamp = QDateTime(QDate(
header.m_auxi.m_startTime.m_year,
header.m_auxi.m_startTime.m_month,
header.m_auxi.m_startTime.m_day
), QTime(
header.m_auxi.m_startTime.m_hour,
header.m_auxi.m_startTime.m_minute,
header.m_auxi.m_startTime.m_second,
header.m_auxi.m_startTime.m_milliseconds
)).toMSecsSinceEpoch() / 1000;
m_startingTimeStamp = header.getStartTime().toMSecsSinceEpoch() / 1000;
}
else
{
// Attempt to extract time and frequency from filename
QRegExp dateTimeRE("([12][0-9][0-9][0-9]).?([01][0-9]).?([0-3][0-9]).?([0-2][0-9]).?([0-5][0-9]).?([0-5][0-9])");
if (dateTimeRE.indexIn(m_settings.m_fileName) != -1)
{
m_startingTimeStamp = QDateTime(QDate(
dateTimeRE.capturedTexts()[1].toInt(),
dateTimeRE.capturedTexts()[2].toInt(),
dateTimeRE.capturedTexts()[3].toInt()
), QTime(
dateTimeRE.capturedTexts()[4].toInt(),
dateTimeRE.capturedTexts()[5].toInt(),
dateTimeRE.capturedTexts()[6].toInt()
)).toMSecsSinceEpoch() / 1000;
}
// Attempt to extract centre frequency from filename
QRegExp freqkRE("(([0-9]+)kHz)");
QRegExp freqRE("(([0-9]+)Hz)");
if (freqkRE.indexIn(m_settings.m_fileName))
{
m_centerFrequency = freqkRE.capturedTexts()[2].toLongLong() * 1000LL;
}
else if (freqRE.indexIn(m_settings.m_fileName))
{
m_centerFrequency = freqRE.capturedTexts()[2].toLongLong();
// Attempt to extract start time and frequency from filename
QDateTime startTime;
if (WavFileRecord::getStartTime(m_fileName, startTime)) {
m_startingTimeStamp = startTime.toMSecsSinceEpoch() / 1000;
}
WavFileRecord::getCenterFrequency(m_fileName, m_centerFrequency);
}
m_sampleSize = header.m_bitsPerSample;

View File

@ -21,7 +21,6 @@
#include <QDebug>
#include <QNetworkReply>
#include <QBuffer>
#include <QRegExp>
#include "SWGDeviceSettings.h"
#include "SWGFileInputSettings.h"
@ -111,44 +110,16 @@ void FileInput::openFileStream()
{
// Some WAV files written by SDR tools have auxi header
m_centerFrequency = header.m_auxi.m_centerFreq;
m_startingTimeStamp = QDateTime(QDate(
header.m_auxi.m_startTime.m_year,
header.m_auxi.m_startTime.m_month,
header.m_auxi.m_startTime.m_day
), QTime(
header.m_auxi.m_startTime.m_hour,
header.m_auxi.m_startTime.m_minute,
header.m_auxi.m_startTime.m_second,
header.m_auxi.m_startTime.m_milliseconds
)).toMSecsSinceEpoch() / 1000;
m_startingTimeStamp = header.getStartTime().toMSecsSinceEpoch() / 1000;
}
else
{
// Attempt to extract time and frequency from filename
QRegExp dateTimeRE("([12][0-9][0-9][0-9]).?([01][0-9]).?([0-3][0-9]).?([0-2][0-9]).?([0-5][0-9]).?([0-5][0-9])");
if (dateTimeRE.indexIn(m_settings.m_fileName) != -1)
{
m_startingTimeStamp = QDateTime(QDate(
dateTimeRE.capturedTexts()[1].toInt(),
dateTimeRE.capturedTexts()[2].toInt(),
dateTimeRE.capturedTexts()[3].toInt()
), QTime(
dateTimeRE.capturedTexts()[4].toInt(),
dateTimeRE.capturedTexts()[5].toInt(),
dateTimeRE.capturedTexts()[6].toInt()
)).toMSecsSinceEpoch() / 1000;
}
// Attempt to extract centre frequency from filename
QRegExp freqkRE("(([0-9]+)kHz)");
QRegExp freqRE("(([0-9]+)Hz)");
if (freqkRE.indexIn(m_settings.m_fileName))
{
m_centerFrequency = freqkRE.capturedTexts()[2].toLongLong() * 1000LL;
}
else if (freqRE.indexIn(m_settings.m_fileName))
{
m_centerFrequency = freqRE.capturedTexts()[2].toLongLong();
// Attempt to extract start time and frequency from filename
QDateTime startTime;
if (WavFileRecord::getStartTime(m_settings.m_fileName, startTime)) {
m_startingTimeStamp = startTime.toMSecsSinceEpoch() / 1000;
}
WavFileRecord::getCenterFrequency(m_settings.m_fileName, m_centerFrequency);
}
m_sampleSize = header.m_bitsPerSample;

View File

@ -21,6 +21,7 @@
#include <QDebug>
#include <QDateTime>
#include <QRegExp>
#include "dsp/dspcommands.h"
#include "util/simpleserializer.h"
@ -340,3 +341,51 @@ void WavFileRecord::writeHeader(std::ofstream& sampleFile, Header& header)
{
sampleFile.write((const char *) &header, sizeof(Header));
}
bool WavFileRecord::getCenterFrequency(QString fileName, quint64& centerFrequency)
{
// Attempt to extract center frequency from filename
QRegExp freqkRE("(([0-9]+)kHz)");
QRegExp freqRE("(([0-9]+)Hz)");
if (freqkRE.indexIn(fileName))
{
centerFrequency = freqkRE.capturedTexts()[2].toLongLong() * 1000LL;
return true;
}
else if (freqRE.indexIn(fileName))
{
centerFrequency = freqRE.capturedTexts()[2].toLongLong();
return true;
}
return false;
}
bool WavFileRecord::getStartTime(QString fileName, QDateTime& startTime)
{
// Attempt to extract start time from filename
QRegExp dateTimeRE("([12][0-9][0-9][0-9]).?([01][0-9]).?([0-3][0-9]).?([0-2][0-9]).?([0-5][0-9]).?([0-5][0-9])");
if (dateTimeRE.indexIn(fileName) != -1)
{
startTime = QDateTime(QDate(
dateTimeRE.capturedTexts()[1].toInt(),
dateTimeRE.capturedTexts()[2].toInt(),
dateTimeRE.capturedTexts()[3].toInt()),
QTime(
dateTimeRE.capturedTexts()[4].toInt(),
dateTimeRE.capturedTexts()[5].toInt(),
dateTimeRE.capturedTexts()[6].toInt()));
return true;
}
return false;
}
QDateTime WavFileRecord::Header::getStartTime() const
{
return QDateTime(QDate(m_auxi.m_startTime.m_year,
m_auxi.m_startTime.m_month,
m_auxi.m_startTime.m_day),
QTime(m_auxi.m_startTime.m_hour,
m_auxi.m_startTime.m_minute,
m_auxi.m_startTime.m_second,
m_auxi.m_startTime.m_milliseconds));
}

View File

@ -66,7 +66,7 @@ public:
quint32 m_unused5;
char m_nextFilename[96];
};
struct Header
struct SDRBASE_API Header
{
Chunk m_riffHeader;
char m_type[4]; // "WAVE"
@ -80,6 +80,8 @@ public:
Chunk m_auxiHeader;
Auxi m_auxi;
Chunk m_dataHeader;
QDateTime getStartTime() const;
};
#pragma pack(pop)
@ -107,6 +109,10 @@ public:
static bool readHeader(std::ifstream& samplefile, Header& header);
static void writeHeader(std::ofstream& samplefile, Header& header);
// These functions guess from the filename, not contents
static bool getCenterFrequency(QString fileName, quint64& centerFrequency);
static bool getStartTime(QString fileName, QDateTime& startTime);
private:
QString m_fileBase;
quint32 m_sampleRate;