Refactor capture file writer classes -- lots of common code.

This commit is contained in:
Jared Boone 2016-05-12 22:31:56 -07:00
parent 537296b8c3
commit ff5e20a7a4

View File

@ -32,18 +32,14 @@ using namespace portapack;
#include <cstdint>
class RawFileWriter : public Writer {
class FileWriter : public Writer {
public:
RawFileWriter(
FileWriter(
const std::string& filename
) : 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 {
if( file.bad() ) {
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 {
const auto success = file.write(buffer, bytes) ;
if( success ) {
@ -79,12 +56,26 @@ public:
return success;
}
Optional<std::filesystem::filesystem_error> error() const override {
if( file.bad() ) {
return { file.error() };
} else {
return { };
}
protected:
File file;
uint64_t bytes_written { 0 };
};
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:
@ -137,9 +128,7 @@ private:
data_t data;
};
File file;
header_t header;
uint64_t bytes_written { 0 };
void update_header() {
header.set_data_size(bytes_written);