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
static Format input_format = Format::AUTO;
static Format output_format = Format::AUTO;
static RawFormat raw_input_format;
static RawFormat raw_output_format;
}
void
......@@ -221,6 +224,34 @@ parse_options (int *argc_p,
{
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 */
......@@ -1140,7 +1171,7 @@ add_watermark (const string& infile, const string& outfile, const string& bits)
{
RawInputStream *ristream = new RawInputStream();
in_stream.reset (ristream);
Error err = ristream->open (infile);
Error err = ristream->open (infile, Params::raw_input_format);
if (err)
{
fprintf (stderr, "audiowmark: error opening %s: %s\n", infile.c_str(), err.message());
......
......@@ -5,16 +5,51 @@
using std::string;
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()
{
close();
}
Error
RawInputStream::open (const string& filename)
RawInputStream::open (const string& filename, const RawFormat& format)
{
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
SF_INFO sfinfo = { 0, };
......@@ -65,20 +100,21 @@ RawInputStream::open (const string& filename)
}
#endif
m_state = State::OPEN;
m_format = format;
m_state = State::OPEN;
return Error::Code::NONE;
}
int
RawInputStream::sample_rate() const
{
return 44100;
return m_format.sample_rate();
}
int
RawInputStream::bit_depth() const
{
return 16;
return m_format.bit_depth();
}
size_t
......@@ -90,7 +126,7 @@ RawInputStream::n_frames() const
int
RawInputStream::n_channels() const
{
return 2;
return m_format.n_channels();
}
Error
......@@ -98,12 +134,15 @@ RawInputStream::read_frames (vector<float>& samples, size_t count)
{
assert (m_state == State::OPEN);
vector<unsigned char> input_bytes (count * n_channels() * (bit_depth() / 8));
size_t r_count = fread (input_bytes.data(), n_channels() * (bit_depth() / 8), count, stdin);
const int n_channels = m_format.n_channels();
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());
samples.resize (r_count * n_channels());
samples.resize (r_count * n_channels);
const double norm = 1.0 / 0x80000000LL;
for (size_t i = 0; i < samples.size(); i++)
{
......
......@@ -7,6 +7,24 @@
#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
{
enum class State {
......@@ -15,11 +33,12 @@ class RawInputStream : public AudioInputStream
CLOSED
};
State m_state = State::NEW;
RawFormat m_format;
public:
~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;
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