Commit b046b704 authored by Stefan Westerfeld's avatar Stefan Westerfeld

Use Error objects as result for WavData::load().

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent d21bd0bc
...@@ -1909,9 +1909,10 @@ int ...@@ -1909,9 +1909,10 @@ int
get_watermark (const string& infile, const string& orig_pattern) get_watermark (const string& infile, const string& orig_pattern)
{ {
WavData wav_data; WavData wav_data;
if (!wav_data.load (infile)) Error err = wav_data.load (infile);
if (err)
{ {
fprintf (stderr, "audiowmark: error loading %s: %s\n", infile.c_str(), wav_data.error_blurb()); fprintf (stderr, "audiowmark: error loading %s: %s\n", infile.c_str(), err.message());
return 1; return 1;
} }
...@@ -1942,9 +1943,10 @@ gentest (const string& infile, const string& outfile) ...@@ -1942,9 +1943,10 @@ gentest (const string& infile, const string& outfile)
printf ("generating test sample from '%s' to '%s'\n", infile.c_str(), outfile.c_str()); printf ("generating test sample from '%s' to '%s'\n", infile.c_str(), outfile.c_str());
WavData wav_data; WavData wav_data;
if (!wav_data.load (infile)) Error err = wav_data.load (infile);
if (err)
{ {
fprintf (stderr, "audiowmark: error loading %s: %s\n", infile.c_str(), wav_data.error_blurb()); fprintf (stderr, "audiowmark: error loading %s: %s\n", infile.c_str(), err.message());
return 1; return 1;
} }
const vector<float>& in_signal = wav_data.samples(); const vector<float>& in_signal = wav_data.samples();
...@@ -1976,9 +1978,10 @@ int ...@@ -1976,9 +1978,10 @@ int
cut_start (const string& infile, const string& outfile, const string& start_str) cut_start (const string& infile, const string& outfile, const string& start_str)
{ {
WavData wav_data; WavData wav_data;
if (!wav_data.load (infile)) Error err = wav_data.load (infile);
if (err)
{ {
fprintf (stderr, "audiowmark: error loading %s: %s\n", infile.c_str(), wav_data.error_blurb()); fprintf (stderr, "audiowmark: error loading %s: %s\n", infile.c_str(), err.message());
return 1; return 1;
} }
...@@ -2002,15 +2005,17 @@ int ...@@ -2002,15 +2005,17 @@ int
test_subtract (const string& infile1, const string& infile2, const string& outfile) test_subtract (const string& infile1, const string& infile2, const string& outfile)
{ {
WavData in1_data; WavData in1_data;
if (!in1_data.load (infile1)) Error err = in1_data.load (infile1);
if (err)
{ {
error ("audiowmark: error loading %s: %s\n", infile1.c_str(), in1_data.error_blurb()); error ("audiowmark: error loading %s: %s\n", infile1.c_str(), err.message());
return 1; return 1;
} }
WavData in2_data; WavData in2_data;
if (!in2_data.load (infile2)) err = in2_data.load (infile2);
if (err)
{ {
error ("audiowmark: error loading %s: %s\n", infile2.c_str(), in2_data.error_blurb()); error ("audiowmark: error loading %s: %s\n", infile2.c_str(), err.message());
return 1; return 1;
} }
if (in1_data.n_values() != in2_data.n_values()) if (in1_data.n_values() != in2_data.n_values())
......
...@@ -24,7 +24,7 @@ WavData::WavData (const vector<float>& samples, int n_channels, int sample_rate, ...@@ -24,7 +24,7 @@ WavData::WavData (const vector<float>& samples, int n_channels, int sample_rate,
m_bit_depth = bit_depth; m_bit_depth = bit_depth;
} }
bool Error
WavData::load (const string& filename) WavData::load (const string& filename)
{ {
std::unique_ptr<AudioInputStream> in_stream; // FIXME: virtual constructor std::unique_ptr<AudioInputStream> in_stream; // FIXME: virtual constructor
...@@ -39,26 +39,18 @@ WavData::load (const string& filename) ...@@ -39,26 +39,18 @@ WavData::load (const string& filename)
err = mistream->open (filename); err = mistream->open (filename);
if (err) if (err)
{ return err;
m_error_blurb = err.message();
return false;
}
} }
else if (err) else if (err)
{ return err;
m_error_blurb = err.message();
return false;
}
vector<float> m_buffer; vector<float> m_buffer;
while (true) while (true)
{ {
err = in_stream->read_frames (m_buffer, 1024); err = in_stream->read_frames (m_buffer, 1024);
if (err) if (err)
{ return err;
m_error_blurb = err.message();
return false;
}
if (!m_buffer.size()) if (!m_buffer.size())
{ {
/* reached eof */ /* reached eof */
...@@ -70,7 +62,7 @@ WavData::load (const string& filename) ...@@ -70,7 +62,7 @@ WavData::load (const string& filename)
m_n_channels = in_stream->n_channels(); m_n_channels = in_stream->n_channels();
m_bit_depth = in_stream->bit_depth(); m_bit_depth = in_stream->bit_depth();
return true; return Error::Code::NONE;
} }
bool bool
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "utils.hh"
class WavData class WavData
{ {
std::vector<float> m_samples; std::vector<float> m_samples;
...@@ -16,7 +18,7 @@ public: ...@@ -16,7 +18,7 @@ public:
WavData(); WavData();
WavData (const std::vector<float>& samples, int n_channels, int sample_rate, int bit_depth); WavData (const std::vector<float>& samples, int n_channels, int sample_rate, int bit_depth);
bool load (const std::string& filename); Error load (const std::string& filename);
bool save (const std::string& filename); bool save (const std::string& filename);
int sample_rate() const; int sample_rate() const;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment