Prepare WAV structures for extraction.

This commit is contained in:
Jared Boone 2016-10-04 09:59:47 -07:00
parent 1bdca0fd8d
commit 1a2fd3e127
2 changed files with 29 additions and 33 deletions

View File

@ -31,12 +31,7 @@
class WAVFileWriter : public FileWriter { class WAVFileWriter : public FileWriter {
public: public:
WAVFileWriter( WAVFileWriter() = default;
size_t sampling_rate
) : header { sampling_rate }
{
}
WAVFileWriter(const WAVFileWriter&) = delete; WAVFileWriter(const WAVFileWriter&) = delete;
WAVFileWriter& operator=(const WAVFileWriter&) = delete; WAVFileWriter& operator=(const WAVFileWriter&) = delete;
@ -48,8 +43,10 @@ public:
} }
Optional<File::Error> create( Optional<File::Error> create(
const std::filesystem::path& filename const std::filesystem::path& filename,
size_t sampling_rate
) { ) {
sampling_rate = sampling_rate;
const auto create_error = FileWriter::create(filename); const auto create_error = FileWriter::create(filename);
if( create_error.is_valid() ) { if( create_error.is_valid() ) {
return create_error; return create_error;
@ -68,50 +65,51 @@ private:
} }
private: private:
const uint8_t ckID[4] { 'f', 'm', 't', ' ' }; uint8_t ckID[4] { 'f', 'm', 't', ' ' };
const uint32_t cksize { 16 }; uint32_t cksize { 16 };
const uint16_t wFormatTag { 0x0001 }; uint16_t wFormatTag { 0x0001 };
const uint16_t nChannels { 1 }; uint16_t nChannels { 1 };
const uint32_t nSamplesPerSec; uint32_t nSamplesPerSec;
const uint32_t nAvgBytesPerSec; uint32_t nAvgBytesPerSec;
const uint16_t nBlockAlign { 2 }; uint16_t nBlockAlign { 2 };
const uint16_t wBitsPerSample { 16 }; uint16_t wBitsPerSample { 16 };
}; };
struct data_t { struct data_t {
void set_size(const uint32_t value) { constexpr data_t(
cksize = value; const uint32_t size
) : cksize { size }
{
} }
private: private:
const uint8_t ckID[4] { 'd', 'a', 't', 'a' }; uint8_t ckID[4] { 'd', 'a', 't', 'a' };
uint32_t cksize { 0 }; uint32_t cksize { 0 };
}; };
struct header_t { struct header_t {
constexpr header_t( constexpr header_t(
const uint32_t sampling_rate const uint32_t sampling_rate,
) : fmt { sampling_rate } const uint32_t data_chunk_size
) : cksize { sizeof(header_t) + data_chunk_size - 8 },
fmt { sampling_rate },
data { data_chunk_size }
{ {
} }
void set_data_size(const uint32_t value) {
data.set_size(value);
cksize = sizeof(header_t) + value - 8;
}
private: private:
const uint8_t riff_id[4] { 'R', 'I', 'F', 'F' }; uint8_t riff_id[4] { 'R', 'I', 'F', 'F' };
uint32_t cksize { 0 }; uint32_t cksize { 0 };
const uint8_t wave_id[4] { 'W', 'A', 'V', 'E' }; uint8_t wave_id[4] { 'W', 'A', 'V', 'E' };
fmt_pcm_t fmt; fmt_pcm_t fmt;
data_t data; data_t data;
}; };
header_t header; uint32_t sampling_rate;
uint32_t bytes_written;
Optional<File::Error> update_header() { Optional<File::Error> update_header() {
header.set_data_size(bytes_written); header_t header { sampling_rate, bytes_written };
const auto seek_0_result = file.seek(0); const auto seek_0_result = file.seek(0);
if( seek_0_result.is_error() ) { if( seek_0_result.is_error() ) {
return seek_0_result.error(); return seek_0_result.error();

View File

@ -121,10 +121,8 @@ void RecordView::start() {
switch(file_type) { switch(file_type) {
case FileType::WAV: case FileType::WAV:
{ {
auto p = std::make_unique<WAVFileWriter>( auto p = std::make_unique<WAVFileWriter>();
sampling_rate auto create_error = p->create(base_path.replace_extension(u".WAV"), sampling_rate);
);
auto create_error = p->create(base_path.replace_extension(u".WAV"));
if( create_error.is_valid() ) { if( create_error.is_valid() ) {
handle_error(create_error.value()); handle_error(create_error.value());
} else { } else {