mirror of
https://github.com/cjcliffe/CubicSDR.git
synced 2025-09-03 13:47:53 -04:00
Sequence recording on format change; user label as filename when available
This commit is contained in:
parent
8f31fd9f5b
commit
326a993a29
@ -19,9 +19,27 @@ void AudioFile::setOutputFileName(std::string filename) {
|
|||||||
std::string AudioFile::getOutputFileName() {
|
std::string AudioFile::getOutputFileName() {
|
||||||
std::string recPath = wxGetApp().getConfig()->getRecordingPath();
|
std::string recPath = wxGetApp().getConfig()->getRecordingPath();
|
||||||
|
|
||||||
// TODO: Handle invalid chars, etc..
|
// Strip any invalid characters from the name
|
||||||
|
std::string stripChars("<>:\"/\\|?*");
|
||||||
std::string filenameBaseSafe = filenameBase;
|
std::string filenameBaseSafe = filenameBase;
|
||||||
|
|
||||||
return recPath + filePathSeparator + filenameBaseSafe + getSuffix() + "." + getExtension();
|
for (size_t i = 0, iMax = filenameBaseSafe.length(); i < iMax; i++) {
|
||||||
|
if (stripChars.find(filenameBaseSafe[i]) != std::string::npos) {
|
||||||
|
filenameBaseSafe.replace(i,1,"_");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create output file name
|
||||||
|
std::string outputFileName = recPath + filePathSeparator + filenameBaseSafe + "." + getExtension();
|
||||||
|
|
||||||
|
int idx = 0;
|
||||||
|
|
||||||
|
// If the file exists; then find the next non-existing file in sequence.
|
||||||
|
while (FILE *file = fopen(outputFileName.c_str(), "r")) {
|
||||||
|
fclose(file);
|
||||||
|
outputFileName = recPath + filePathSeparator + filenameBaseSafe + "-" + std::to_string(++idx) + "." + getExtension();
|
||||||
|
}
|
||||||
|
|
||||||
|
return outputFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,6 @@ public:
|
|||||||
|
|
||||||
virtual void setOutputFileName(std::string filename);
|
virtual void setOutputFileName(std::string filename);
|
||||||
virtual std::string getExtension() = 0;
|
virtual std::string getExtension() = 0;
|
||||||
virtual std::string getSuffix() = 0;
|
|
||||||
virtual std::string getOutputFileName();
|
virtual std::string getOutputFileName();
|
||||||
|
|
||||||
virtual bool writeToFile(AudioThreadInputPtr input) = 0;
|
virtual bool writeToFile(AudioThreadInputPtr input) = 0;
|
||||||
|
@ -58,18 +58,11 @@ std::string AudioFileWAV::getExtension()
|
|||||||
return "wav";
|
return "wav";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string AudioFileWAV::getSuffix()
|
|
||||||
{
|
|
||||||
return suffix;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool AudioFileWAV::writeToFile(AudioThreadInputPtr input)
|
bool AudioFileWAV::writeToFile(AudioThreadInputPtr input)
|
||||||
{
|
{
|
||||||
if (!outputFileStream.is_open()) {
|
if (!outputFileStream.is_open()) {
|
||||||
suffix = "";
|
|
||||||
std::string ofName = getOutputFileName();
|
std::string ofName = getOutputFileName();
|
||||||
|
|
||||||
// TODO: Check if file exists, sequence the suffix
|
|
||||||
outputFileStream.open(ofName.c_str(), std::ios::binary);
|
outputFileStream.open(ofName.c_str(), std::ios::binary);
|
||||||
|
|
||||||
// Based on simple wav file output code from
|
// Based on simple wav file output code from
|
||||||
@ -94,12 +87,12 @@ bool AudioFileWAV::writeToFile(AudioThreadInputPtr input)
|
|||||||
float intScale = (input->peak < 1.0) ? 32767.0f : (32767.0f / input->peak);
|
float intScale = (input->peak < 1.0) ? 32767.0f : (32767.0f / input->peak);
|
||||||
|
|
||||||
if (input->channels == 1) {
|
if (input->channels == 1) {
|
||||||
for (int i = 0, iMax = input->data.size(); i < iMax; i++) {
|
for (size_t i = 0, iMax = input->data.size(); i < iMax; i++) {
|
||||||
write_word(outputFileStream, int(input->data[i] * intScale), 2);
|
write_word(outputFileStream, int(input->data[i] * intScale), 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (input->channels == 2) {
|
else if (input->channels == 2) {
|
||||||
for (int i = 0, iMax = input->data.size() / 2; i < iMax; i++) {
|
for (size_t i = 0, iMax = input->data.size() / 2; i < iMax; i++) {
|
||||||
write_word(outputFileStream, int(input->data[i * 2] * intScale), 2);
|
write_word(outputFileStream, int(input->data[i * 2] * intScale), 2);
|
||||||
write_word(outputFileStream, int(input->data[i * 2 + 1] * intScale), 2);
|
write_word(outputFileStream, int(input->data[i * 2 + 1] * intScale), 2);
|
||||||
}
|
}
|
||||||
|
@ -14,13 +14,11 @@ public:
|
|||||||
~AudioFileWAV();
|
~AudioFileWAV();
|
||||||
|
|
||||||
std::string getExtension();
|
std::string getExtension();
|
||||||
std::string getSuffix();
|
|
||||||
|
|
||||||
bool writeToFile(AudioThreadInputPtr input);
|
bool writeToFile(AudioThreadInputPtr input);
|
||||||
bool closeFile();
|
bool closeFile();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::ofstream outputFileStream;
|
std::ofstream outputFileStream;
|
||||||
std::string suffix;
|
|
||||||
size_t dataChunkPos;
|
size_t dataChunkPos;
|
||||||
};
|
};
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
#include "DemodulatorInstance.h"
|
#include "DemodulatorInstance.h"
|
||||||
#include "CubicSDR.h"
|
#include "CubicSDR.h"
|
||||||
@ -627,8 +628,23 @@ void DemodulatorInstance::startRecording() {
|
|||||||
AudioSinkFileThread *newSinkThread = new AudioSinkFileThread();
|
AudioSinkFileThread *newSinkThread = new AudioSinkFileThread();
|
||||||
AudioFileWAV *afHandler = new AudioFileWAV();
|
AudioFileWAV *afHandler = new AudioFileWAV();
|
||||||
|
|
||||||
|
time_t t = std::time(nullptr);
|
||||||
|
tm ltm = *std::localtime(&t);
|
||||||
|
|
||||||
std::stringstream fileName;
|
std::stringstream fileName;
|
||||||
fileName << getLabel() << "_" << std::time(nullptr);
|
|
||||||
|
std::wstring userLabel = getDemodulatorUserLabel();
|
||||||
|
|
||||||
|
// TODO: Can we support wstring filenames for user labels?
|
||||||
|
std::string userLabelStr(userLabel.begin(), userLabel.end());
|
||||||
|
|
||||||
|
if (!userLabelStr.empty()) {
|
||||||
|
fileName << userLabelStr;
|
||||||
|
} else {
|
||||||
|
fileName << getLabel();
|
||||||
|
}
|
||||||
|
|
||||||
|
fileName << "_" << std::put_time(<m, "%d-%m-%Y_%H-%M-%S");
|
||||||
|
|
||||||
afHandler->setOutputFileName(fileName.str());
|
afHandler->setOutputFileName(fileName.str());
|
||||||
newSinkThread->setAudioFileHandler(afHandler);
|
newSinkThread->setAudioFileHandler(afHandler);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user