Commit 0086f8ed authored by Stefan Westerfeld's avatar Stefan Westerfeld

Support command line options to set input/output raw stream format.

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent 19147744
...@@ -60,6 +60,9 @@ namespace Params ...@@ -60,6 +60,9 @@ namespace Params
static Format input_format = Format::AUTO; static Format input_format = Format::AUTO;
static Format output_format = Format::AUTO; static Format output_format = Format::AUTO;
static RawFormat raw_input_format;
static RawFormat raw_output_format;
} }
void void
...@@ -221,6 +224,34 @@ parse_options (int *argc_p, ...@@ -221,6 +224,34 @@ parse_options (int *argc_p,
{ {
Params::input_format = Params::output_format = parse_format (opt_arg); Params::input_format = Params::output_format = parse_format (opt_arg);
} }
else if (check_arg (argc, argv, &i, "--raw-input-bits", &opt_arg))
{
int b = atoi (opt_arg);
Params::raw_input_format.set_bit_depth (b);
}
else if (check_arg (argc, argv, &i, "--raw-output-bits", &opt_arg))
{
int b = atoi (opt_arg);
Params::raw_output_format.set_bit_depth (b);
}
else if (check_arg (argc, argv, &i, "--raw-bits", &opt_arg))
{
int b = atoi (opt_arg);
Params::raw_input_format.set_bit_depth (b);
Params::raw_output_format.set_bit_depth (b);
}
else if (check_arg (argc, argv, &i, "--raw-channels", &opt_arg))
{
int c = atoi (opt_arg);
Params::raw_input_format.set_channels (c);
Params::raw_output_format.set_channels (c);
}
else if (check_arg (argc, argv, &i, "--raw-rate", &opt_arg))
{
int r = atoi (opt_arg);
Params::raw_input_format.set_sample_rate (r);
Params::raw_output_format.set_sample_rate (r);
}
} }
/* resort argc/argv */ /* resort argc/argv */
...@@ -1140,7 +1171,7 @@ add_watermark (const string& infile, const string& outfile, const string& bits) ...@@ -1140,7 +1171,7 @@ add_watermark (const string& infile, const string& outfile, const string& bits)
{ {
RawInputStream *ristream = new RawInputStream(); RawInputStream *ristream = new RawInputStream();
in_stream.reset (ristream); in_stream.reset (ristream);
Error err = ristream->open (infile); Error err = ristream->open (infile, Params::raw_input_format);
if (err) if (err)
{ {
fprintf (stderr, "audiowmark: error opening %s: %s\n", infile.c_str(), err.message()); fprintf (stderr, "audiowmark: error opening %s: %s\n", infile.c_str(), err.message());
......
...@@ -5,16 +5,51 @@ ...@@ -5,16 +5,51 @@
using std::string; using std::string;
using std::vector; using std::vector;
RawFormat::RawFormat()
{
}
RawFormat::RawFormat (int n_channels, int sample_rate, int bit_depth) :
m_n_channels (n_channels),
m_sample_rate (sample_rate),
m_bit_depth (bit_depth)
{
}
void
RawFormat::set_channels (int channels)
{
m_n_channels = channels;
}
void
RawFormat::set_sample_rate (int rate)
{
m_sample_rate = rate;
}
void
RawFormat::set_bit_depth (int bits)
{
m_bit_depth = bits;
}
RawInputStream::~RawInputStream() RawInputStream::~RawInputStream()
{ {
close(); close();
} }
Error Error
RawInputStream::open (const string& filename) RawInputStream::open (const string& filename, const RawFormat& format)
{ {
assert (m_state == State::NEW); assert (m_state == State::NEW);
if (!format.n_channels())
return Error ("RawInputStream: input format: missing number of channels");
if (!format.bit_depth())
return Error ("RawInputStream: input format: missing bit depth");
if (!format.sample_rate())
return Error ("RawInputStream: input format: missing sample rate");
#if 0 #if 0
SF_INFO sfinfo = { 0, }; SF_INFO sfinfo = { 0, };
...@@ -65,20 +100,21 @@ RawInputStream::open (const string& filename) ...@@ -65,20 +100,21 @@ RawInputStream::open (const string& filename)
} }
#endif #endif
m_state = State::OPEN; m_format = format;
m_state = State::OPEN;
return Error::Code::NONE; return Error::Code::NONE;
} }
int int
RawInputStream::sample_rate() const RawInputStream::sample_rate() const
{ {
return 44100; return m_format.sample_rate();
} }
int int
RawInputStream::bit_depth() const RawInputStream::bit_depth() const
{ {
return 16; return m_format.bit_depth();
} }
size_t size_t
...@@ -90,7 +126,7 @@ RawInputStream::n_frames() const ...@@ -90,7 +126,7 @@ RawInputStream::n_frames() const
int int
RawInputStream::n_channels() const RawInputStream::n_channels() const
{ {
return 2; return m_format.n_channels();
} }
Error Error
...@@ -98,12 +134,15 @@ RawInputStream::read_frames (vector<float>& samples, size_t count) ...@@ -98,12 +134,15 @@ RawInputStream::read_frames (vector<float>& samples, size_t count)
{ {
assert (m_state == State::OPEN); assert (m_state == State::OPEN);
vector<unsigned char> input_bytes (count * n_channels() * (bit_depth() / 8)); const int n_channels = m_format.n_channels();
size_t r_count = fread (input_bytes.data(), n_channels() * (bit_depth() / 8), count, stdin); const int sample_width = m_format.bit_depth() / 8;
vector<unsigned char> input_bytes (count * n_channels * sample_width);
size_t r_count = fread (input_bytes.data(), n_channels * sample_width, count, stdin);
unsigned char *ptr = reinterpret_cast<unsigned char *> (input_bytes.data()); unsigned char *ptr = reinterpret_cast<unsigned char *> (input_bytes.data());
samples.resize (r_count * n_channels()); samples.resize (r_count * n_channels);
const double norm = 1.0 / 0x80000000LL; const double norm = 1.0 / 0x80000000LL;
for (size_t i = 0; i < samples.size(); i++) for (size_t i = 0; i < samples.size(); i++)
{ {
......
...@@ -7,6 +7,24 @@ ...@@ -7,6 +7,24 @@
#include "audiostream.hh" #include "audiostream.hh"
class RawFormat
{
int m_n_channels = 0;
int m_sample_rate = 0;
int m_bit_depth = 0;
public:
RawFormat();
RawFormat (int n_channels, int sample_rate, int bit_depth);
int n_channels() const { return m_n_channels; }
int sample_rate() const { return m_sample_rate; }
int bit_depth() const { return m_bit_depth; }
void set_channels (int channels);
void set_sample_rate (int rate);
void set_bit_depth (int bits);
};
class RawInputStream : public AudioInputStream class RawInputStream : public AudioInputStream
{ {
enum class State { enum class State {
...@@ -15,11 +33,12 @@ class RawInputStream : public AudioInputStream ...@@ -15,11 +33,12 @@ class RawInputStream : public AudioInputStream
CLOSED CLOSED
}; };
State m_state = State::NEW; State m_state = State::NEW;
RawFormat m_format;
public: public:
~RawInputStream(); ~RawInputStream();
Error open (const std::string& filename); Error open (const std::string& filename, const RawFormat& format);
Error read_frames (std::vector<float>& samples, size_t count) override; Error read_frames (std::vector<float>& samples, size_t count) override;
void close(); void close();
......
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