Commit e5270756 authored by Stefan Westerfeld's avatar Stefan Westerfeld

Use RawConverter in RawInputStream.

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent 9c595731
......@@ -9,10 +9,7 @@ class RawConverterImpl : public RawConverter
{
public:
void to_raw (const std::vector<float>& samples, std::vector<unsigned char>& bytes);
void
from_raw (const std::vector<unsigned char>& bytes, std::vector<float>& samples)
{
}
void from_raw (const std::vector<unsigned char>& bytes, std::vector<float>& samples);
};
RawConverter *
......@@ -57,3 +54,19 @@ RawConverterImpl<BIT_DEPTH>::to_raw (const vector<float>& samples, vector<unsign
}
}
}
template<int BIT_DEPTH>
void
RawConverterImpl<BIT_DEPTH>::from_raw (const vector<unsigned char>& input_bytes, vector<float>& samples)
{
const unsigned char *ptr = input_bytes.data();
samples.resize (input_bytes.size() / (BIT_DEPTH / 8));
const double norm = 1.0 / 0x80000000LL;
for (size_t i = 0; i < samples.size(); i++)
{
int s32 = (ptr[1] << 24) + (ptr[0] << 16);
samples[i] = s32 * norm;
ptr += 2;
}
}
#include "rawinputstream.hh"
#include "rawconverter.hh"
#include <assert.h>
......@@ -51,6 +52,13 @@ RawInputStream::open (const string& filename, const RawFormat& format)
if (!format.sample_rate())
return Error ("RawInputStream: input format: missing sample rate");
Error err = Error::Code::NONE;
RawConverter *rc = RawConverter::create (format, err);
if (err)
return err;
assert (rc);
m_raw_converter.reset (rc);
if (filename == "-")
{
m_input_file = stdin;
......@@ -101,17 +109,9 @@ RawInputStream::read_frames (vector<float>& samples, size_t count)
vector<unsigned char> input_bytes (count * n_channels * sample_width);
size_t r_count = fread (input_bytes.data(), n_channels * sample_width, count, m_input_file);
input_bytes.resize (r_count * n_channels * sample_width);
unsigned char *ptr = reinterpret_cast<unsigned char *> (input_bytes.data());
samples.resize (r_count * n_channels);
const double norm = 1.0 / 0x80000000LL;
for (size_t i = 0; i < samples.size(); i++)
{
int s32 = (ptr[1] << 24) + (ptr[0] << 16);
samples[i] = s32 * norm;
ptr += 2;
}
m_raw_converter->from_raw (input_bytes, samples);
return Error::Code::NONE;
}
......
......@@ -2,6 +2,7 @@
#define AUDIOWMARK_RAW_INPUT_STREAM_HH
#include <string>
#include <memory>
#include <sndfile.h>
......@@ -25,6 +26,8 @@ public:
void set_bit_depth (int bits);
};
class RawConverter;
class RawInputStream : public AudioInputStream
{
enum class State {
......@@ -37,6 +40,8 @@ class RawInputStream : public AudioInputStream
FILE *m_input_file = nullptr;
bool m_close_file = false;
std::unique_ptr<RawConverter> m_raw_converter;
public:
~RawInputStream();
......
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