Add a base class to Wav{Reader,Writer} to access shared parameters.
Use it to clean up some code in audioproc_float.cc. R=kwiberg@webrtc.org Review URL: https://codereview.webrtc.org/1308893002 . Cr-Commit-Position: refs/heads/master@{#9771}
This commit is contained in:
parent
41eeff49fa
commit
bc2296de9e
@ -17,11 +17,23 @@
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
|
||||
#include "webrtc/base/constructormagic.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// Interface to provide access to WAV file parameters.
|
||||
class WavFile {
|
||||
public:
|
||||
virtual ~WavFile() {}
|
||||
|
||||
virtual int sample_rate() const = 0;
|
||||
virtual int num_channels() const = 0;
|
||||
virtual uint32_t num_samples() const = 0;
|
||||
};
|
||||
|
||||
// Simple C++ class for writing 16-bit PCM WAV files. All error handling is
|
||||
// by calls to CHECK(), making it unsuitable for anything but debug code.
|
||||
class WavWriter {
|
||||
class WavWriter final : public WavFile {
|
||||
public:
|
||||
// Open a new WAV file for writing.
|
||||
WavWriter(const std::string& filename, int sample_rate, int num_channels);
|
||||
@ -35,9 +47,9 @@ class WavWriter {
|
||||
void WriteSamples(const float* samples, size_t num_samples);
|
||||
void WriteSamples(const int16_t* samples, size_t num_samples);
|
||||
|
||||
int sample_rate() const { return sample_rate_; }
|
||||
int num_channels() const { return num_channels_; }
|
||||
uint32_t num_samples() const { return num_samples_; }
|
||||
int sample_rate() const override { return sample_rate_; }
|
||||
int num_channels() const override { return num_channels_; }
|
||||
uint32_t num_samples() const override { return num_samples_; }
|
||||
|
||||
private:
|
||||
void Close();
|
||||
@ -45,10 +57,12 @@ class WavWriter {
|
||||
const int num_channels_;
|
||||
uint32_t num_samples_; // Total number of samples written to file.
|
||||
FILE* file_handle_; // Output file, owned by this class
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(WavWriter);
|
||||
};
|
||||
|
||||
// Follows the conventions of WavWriter.
|
||||
class WavReader {
|
||||
class WavReader final : public WavFile {
|
||||
public:
|
||||
// Opens an existing WAV file for reading.
|
||||
explicit WavReader(const std::string& filename);
|
||||
@ -61,9 +75,9 @@ class WavReader {
|
||||
size_t ReadSamples(size_t num_samples, float* samples);
|
||||
size_t ReadSamples(size_t num_samples, int16_t* samples);
|
||||
|
||||
int sample_rate() const { return sample_rate_; }
|
||||
int num_channels() const { return num_channels_; }
|
||||
uint32_t num_samples() const { return num_samples_; }
|
||||
int sample_rate() const override { return sample_rate_; }
|
||||
int num_channels() const override { return num_channels_; }
|
||||
uint32_t num_samples() const override { return num_samples_; }
|
||||
|
||||
private:
|
||||
void Close();
|
||||
@ -72,6 +86,8 @@ class WavReader {
|
||||
uint32_t num_samples_; // Total number of samples in the file.
|
||||
uint32_t num_samples_remaining_;
|
||||
FILE* file_handle_; // Input file, owned by this class.
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(WavReader);
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
@ -63,6 +63,15 @@ const char kUsage[] =
|
||||
"All components are disabled by default. If any bi-directional components\n"
|
||||
"are enabled, only debug dump files are permitted.";
|
||||
|
||||
// Returns a StreamConfig corresponding to wav_file if it's non-nullptr.
|
||||
// Otherwise returns a default initialized StreamConfig.
|
||||
StreamConfig MakeStreamConfig(const WavFile* wav_file) {
|
||||
if (wav_file) {
|
||||
return {wav_file->sample_rate(), wav_file->num_channels()};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
@ -162,21 +171,11 @@ int main(int argc, char* argv[]) {
|
||||
TickInterval accumulated_time;
|
||||
int num_chunks = 0;
|
||||
|
||||
const StreamConfig input_config = {
|
||||
in_file.sample_rate(), in_buf.num_channels(),
|
||||
};
|
||||
const StreamConfig output_config = {
|
||||
out_file.sample_rate(), out_buf.num_channels(),
|
||||
};
|
||||
const auto input_config = MakeStreamConfig(&in_file);
|
||||
const auto output_config = MakeStreamConfig(&out_file);
|
||||
const auto reverse_input_config = MakeStreamConfig(in_rev_file.get());
|
||||
const auto reverse_output_config = MakeStreamConfig(out_rev_file.get());
|
||||
|
||||
StreamConfig reverse_input_config = {};
|
||||
StreamConfig reverse_output_config = {};
|
||||
if (process_reverse) {
|
||||
reverse_input_config = {in_rev_file->sample_rate(),
|
||||
in_rev_file->num_channels()};
|
||||
reverse_output_config = {out_rev_file->sample_rate(),
|
||||
out_rev_file->num_channels()};
|
||||
}
|
||||
while (in_file.ReadSamples(in_interleaved.size(),
|
||||
&in_interleaved[0]) == in_interleaved.size()) {
|
||||
// Have logs display the file time rather than wallclock time.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user