mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2025-08-13 16:16:18 +00:00
Refactor capture file writer classes -- lots of common code.
This commit is contained in:
@@ -32,18 +32,14 @@ using namespace portapack;
|
|||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
class RawFileWriter : public Writer {
|
class FileWriter : public Writer {
|
||||||
public:
|
public:
|
||||||
RawFileWriter(
|
FileWriter(
|
||||||
const std::string& filename
|
const std::string& filename
|
||||||
) : file { filename, File::openmode::out | File::openmode::binary | File::openmode::trunc }
|
) : file { filename, File::openmode::out | File::openmode::binary | File::openmode::trunc }
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bool write(const void* const buffer, const size_t bytes) override {
|
|
||||||
return file.write(buffer, bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
Optional<std::filesystem::filesystem_error> error() const override {
|
Optional<std::filesystem::filesystem_error> error() const override {
|
||||||
if( file.bad() ) {
|
if( file.bad() ) {
|
||||||
return { file.error() };
|
return { file.error() };
|
||||||
@@ -52,25 +48,6 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
File file;
|
|
||||||
};
|
|
||||||
|
|
||||||
class WAVFileWriter : public Writer {
|
|
||||||
public:
|
|
||||||
WAVFileWriter(
|
|
||||||
const std::string& filename,
|
|
||||||
size_t sampling_rate
|
|
||||||
) : file { filename, File::openmode::out | File::openmode::binary | File::openmode::trunc },
|
|
||||||
header { sampling_rate }
|
|
||||||
{
|
|
||||||
update_header();
|
|
||||||
}
|
|
||||||
|
|
||||||
~WAVFileWriter() {
|
|
||||||
update_header();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool write(const void* const buffer, const size_t bytes) override {
|
bool write(const void* const buffer, const size_t bytes) override {
|
||||||
const auto success = file.write(buffer, bytes) ;
|
const auto success = file.write(buffer, bytes) ;
|
||||||
if( success ) {
|
if( success ) {
|
||||||
@@ -79,12 +56,26 @@ public:
|
|||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<std::filesystem::filesystem_error> error() const override {
|
protected:
|
||||||
if( file.bad() ) {
|
File file;
|
||||||
return { file.error() };
|
uint64_t bytes_written { 0 };
|
||||||
} else {
|
};
|
||||||
return { };
|
|
||||||
}
|
using RawFileWriter = FileWriter;
|
||||||
|
|
||||||
|
class WAVFileWriter : public FileWriter {
|
||||||
|
public:
|
||||||
|
WAVFileWriter(
|
||||||
|
const std::string& filename,
|
||||||
|
size_t sampling_rate
|
||||||
|
) : RawFileWriter { filename },
|
||||||
|
header { sampling_rate }
|
||||||
|
{
|
||||||
|
update_header();
|
||||||
|
}
|
||||||
|
|
||||||
|
~WAVFileWriter() {
|
||||||
|
update_header();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -137,9 +128,7 @@ private:
|
|||||||
data_t data;
|
data_t data;
|
||||||
};
|
};
|
||||||
|
|
||||||
File file;
|
|
||||||
header_t header;
|
header_t header;
|
||||||
uint64_t bytes_written { 0 };
|
|
||||||
|
|
||||||
void update_header() {
|
void update_header() {
|
||||||
header.set_data_size(bytes_written);
|
header.set_data_size(bytes_written);
|
||||||
|
Reference in New Issue
Block a user