Skip to content

Audio IO

audio_reader_flac

audio_reader_flac class

template <typename T> audio_reader_flac

FLAC format reader

Source code
template <typename T>
struct audio_reader_flac : audio_reader<T>
{
    /// @brief Constructs FLAC reader
    audio_reader_flac(std::shared_ptr<abstract_reader<>>&& reader) : reader(std::move(reader))
    {
        f              = drflac_open((drflac_read_proc)&internal_generic::drflac_reader_read_proc,
                        (drflac_seek_proc)&internal_generic::drflac_reader_seek_proc, this->reader.get());
        fmt.channels   = f->channels;
        fmt.samplerate = f->sampleRate;
        fmt.length     = f->totalSampleCount / fmt.channels;
        fmt.type       = audio_sample_type::i32;
    }
    ~audio_reader_flac() { drflac_close(f); }

    /// @brief Returns audio format description
    const audio_format_and_length& format() const override { return fmt; }

    /// @brief Reads and decodes audio data
    size_t read(T* data, size_t size) override
    {
        if (fmt.type == audio_sample_type::unknown)
            return 0;
        if (audio_sample_traits<T>::type == audio_sample_type::i32)
        {
            return drflac_read_s32(f, size, reinterpret_cast<i32*>(data));
        }
        else
        {
            univector<i32> native(size * sizeof(i32));
            const size_t sz = drflac_read_s32(f, size, native.data());
            convert(data, native.data(), sz);
            return sz;
        }
    }

    /// @brief Returns current position
    imax tell() const override { return position; }

    /// @brief Seeks to specific sample
    bool seek(imax offset, seek_origin origin) override
    {
        switch (origin)
        {
        case seek_origin::current:
            return drflac_seek_to_sample(f, this->position + offset);
        case seek_origin::begin:
            return drflac_seek_to_sample(f, offset);
        case seek_origin::end:
            return drflac_seek_to_sample(f, fmt.length + offset);
        default:
            return false;
        }
    }

private:
    std::shared_ptr<abstract_reader<>> reader;
    drflac* f;
    audio_format_and_length fmt;
    imax position = 0;
}

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L329

audio_reader_flac<T> function

audio_reader_flac(
    std::shared_ptr<abstract_reader<>> &&reader)
    : reader(std::move(reader))

Constructs FLAC reader

Source code
audio_reader_flac(std::shared_ptr<abstract_reader<>>&& reader) : reader(std::move(reader))
{
    f              = drflac_open((drflac_read_proc)&internal_generic::drflac_reader_read_proc,
                    (drflac_seek_proc)&internal_generic::drflac_reader_seek_proc, this->reader.get());
    fmt.channels   = f->channels;
    fmt.samplerate = f->sampleRate;
    fmt.length     = f->totalSampleCount / fmt.channels;
    fmt.type       = audio_sample_type::i32;
}

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L332

format function

const audio_format_and_length &format() const override

Returns audio format description

Source code
const audio_format_and_length& format() const override { return fmt; }

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L344

read function

size_t read(T *data, size_t size) override

Reads and decodes audio data

Source code
size_t read(T* data, size_t size) override
{
    if (fmt.type == audio_sample_type::unknown)
        return 0;
    if (audio_sample_traits<T>::type == audio_sample_type::i32)
    {
        return drflac_read_s32(f, size, reinterpret_cast<i32*>(data));
    }
    else
    {
        univector<i32> native(size * sizeof(i32));
        const size_t sz = drflac_read_s32(f, size, native.data());
        convert(data, native.data(), sz);
        return sz;
    }
}

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L347

tell function

imax tell() const override

Returns current position

Source code
imax tell() const override { return position; }

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L365

seek function

bool seek(imax offset, seek_origin origin) override

Seeks to specific sample

Source code
bool seek(imax offset, seek_origin origin) override
{
    switch (origin)
    {
    case seek_origin::current:
        return drflac_seek_to_sample(f, this->position + offset);
    case seek_origin::begin:
        return drflac_seek_to_sample(f, offset);
    case seek_origin::end:
        return drflac_seek_to_sample(f, fmt.length + offset);
    default:
        return false;
    }
}

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L368

audio_reader_wav

audio_reader_wav class

template <typename T> audio_reader_wav

WAV format reader

Source code
template <typename T>
struct audio_reader_wav : audio_reader<T>
{
    using audio_reader<T>::read;

    /// @brief Constructs WAV reader
    audio_reader_wav(std::shared_ptr<abstract_reader<>>&& reader) : reader(std::move(reader))
    {
        f              = drwav_open((drwav_read_proc)&internal_generic::drwav_reader_read_proc,
                       (drwav_seek_proc)&internal_generic::drwav_reader_seek_proc, this->reader.get());
        fmt.channels   = f->channels;
        fmt.samplerate = f->sampleRate;
        fmt.length     = f->totalSampleCount / fmt.channels;
        switch (f->translatedFormatTag)
        {
        case DR_WAVE_FORMAT_IEEE_FLOAT:
            switch (f->bitsPerSample)
            {
            case 32:
                fmt.type = audio_sample_type::f32;
                break;
            case 64:
                fmt.type = audio_sample_type::f64;
                break;
            default:
                fmt.type = audio_sample_type::unknown;
                break;
            }
            break;
        case DR_WAVE_FORMAT_PCM:
            switch (f->bitsPerSample)
            {
            case 8:
                fmt.type = audio_sample_type::i8;
                break;
            case 16:
                fmt.type = audio_sample_type::i16;
                break;
            case 24:
                fmt.type = audio_sample_type::i24;
                break;
            case 32:
                fmt.type = audio_sample_type::i32;
                break;
            case 64:
                fmt.type = audio_sample_type::i64;
                break;
            default:
                fmt.type = audio_sample_type::unknown;
                break;
            }
            break;
        default:
            fmt.type = audio_sample_type::unknown;
            break;
        }
    }
    ~audio_reader_wav() { drwav_close(f); }

    /// @brief Returns audio format description
    const audio_format_and_length& format() const override { return fmt; }

    /// @brief Reads and decodes audio data
    size_t read(T* data, size_t size) override
    {
        if (fmt.type == audio_sample_type::unknown)
            return 0;
        if (fmt.type == audio_sample_traits<T>::type)
        {
            return drwav_read(f, size, data);
        }
        else
        {
            univector<uint8_t> native(size * audio_sample_sizeof(fmt.type));
            const size_t sz = drwav_read(f, size, native.data());
            convert(data, native.data(), fmt.type, sz);
            return sz;
        }
    }

    /// @brief Returns current position
    imax tell() const override { return position; }

    /// @brief Seeks to specific sample
    bool seek(imax offset, seek_origin origin) override
    {
        switch (origin)
        {
        case seek_origin::current:
            return drwav_seek_to_sample(f, this->position + offset);
        case seek_origin::begin:
            return drwav_seek_to_sample(f, offset);
        case seek_origin::end:
            return drwav_seek_to_sample(f, fmt.length + offset);
        default:
            return false;
        }
    }

private:
    std::shared_ptr<abstract_reader<>> reader;
    drwav* f;
    audio_format_and_length fmt;
    imax position = 0;
}

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L219

audio_reader_wav<T> function

audio_reader_wav(
    std::shared_ptr<abstract_reader<>> &&reader)
    : reader(std::move(reader))

Constructs WAV reader

Source code
audio_reader_wav(std::shared_ptr<abstract_reader<>>&& reader) : reader(std::move(reader))
{
    f              = drwav_open((drwav_read_proc)&internal_generic::drwav_reader_read_proc,
                   (drwav_seek_proc)&internal_generic::drwav_reader_seek_proc, this->reader.get());
    fmt.channels   = f->channels;
    fmt.samplerate = f->sampleRate;
    fmt.length     = f->totalSampleCount / fmt.channels;
    switch (f->translatedFormatTag)
    {
    case DR_WAVE_FORMAT_IEEE_FLOAT:
        switch (f->bitsPerSample)
        {
        case 32:
            fmt.type = audio_sample_type::f32;
            break;
        case 64:
            fmt.type = audio_sample_type::f64;
            break;
        default:
            fmt.type = audio_sample_type::unknown;
            break;
        }
        break;
    case DR_WAVE_FORMAT_PCM:
        switch (f->bitsPerSample)
        {
        case 8:
            fmt.type = audio_sample_type::i8;
            break;
        case 16:
            fmt.type = audio_sample_type::i16;
            break;
        case 24:
            fmt.type = audio_sample_type::i24;
            break;
        case 32:
            fmt.type = audio_sample_type::i32;
            break;
        case 64:
            fmt.type = audio_sample_type::i64;
            break;
        default:
            fmt.type = audio_sample_type::unknown;
            break;
        }
        break;
    default:
        fmt.type = audio_sample_type::unknown;
        break;
    }
}

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L224

format function

const audio_format_and_length &format() const override

Returns audio format description

Source code
const audio_format_and_length& format() const override { return fmt; }

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L278

read function

size_t read(T *data, size_t size) override

Reads and decodes audio data

Source code
size_t read(T* data, size_t size) override
{
    if (fmt.type == audio_sample_type::unknown)
        return 0;
    if (fmt.type == audio_sample_traits<T>::type)
    {
        return drwav_read(f, size, data);
    }
    else
    {
        univector<uint8_t> native(size * audio_sample_sizeof(fmt.type));
        const size_t sz = drwav_read(f, size, native.data());
        convert(data, native.data(), fmt.type, sz);
        return sz;
    }
}

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L281

tell function

imax tell() const override

Returns current position

Source code
imax tell() const override { return position; }

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L299

seek function

bool seek(imax offset, seek_origin origin) override

Seeks to specific sample

Source code
bool seek(imax offset, seek_origin origin) override
{
    switch (origin)
    {
    case seek_origin::current:
        return drwav_seek_to_sample(f, this->position + offset);
    case seek_origin::begin:
        return drwav_seek_to_sample(f, offset);
    case seek_origin::end:
        return drwav_seek_to_sample(f, fmt.length + offset);
    default:
        return false;
    }
}

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L302

audio_writer_wav

audio_writer_wav class

template <typename T> audio_writer_wav

WAV format writer

Source code
template <typename T>
struct audio_writer_wav : audio_writer<T>
{
    /// @brief Constructs WAV writer using target writer and format
    audio_writer_wav(std::shared_ptr<abstract_writer<>>&& writer, const audio_format& fmt)
        : writer(std::move(writer)), f(nullptr), fmt(fmt)
    {
        drwav_data_format wav_fmt;
        wav_fmt.channels   = static_cast<drwav_uint32>(fmt.channels);
        wav_fmt.sampleRate = static_cast<drwav_uint32>(fmt.samplerate);
        wav_fmt.format =
            fmt.type >= audio_sample_type::first_float ? DR_WAVE_FORMAT_IEEE_FLOAT : DR_WAVE_FORMAT_PCM;
        wav_fmt.bitsPerSample = static_cast<drwav_uint32>(audio_sample_bit_depth(fmt.type));
        wav_fmt.container     = fmt.use_w64 ? drwav_container_w64 : drwav_container_riff;
        f = drwav_open_write(&wav_fmt, (drwav_write_proc)&internal_generic::drwav_writer_write_proc,
                             (drwav_seek_proc)&internal_generic::drwav_writer_seek_proc, this->writer.get());
    }
    ~audio_writer_wav() { close(); }

    using audio_writer<T>::write;

    /// @brief Write data to underlying binary writer
    size_t write(const T* data, size_t size) override
    {
        if (!f)
            return 0;
        if (fmt.type == audio_sample_type::unknown)
            return 0;
        if (fmt.type == audio_sample_traits<T>::type)
        {
            const size_t sz = drwav_write(f, size, data);
            fmt.length += sz / fmt.channels;
            return sz;
        }
        else
        {
            univector<uint8_t> native(size * audio_sample_sizeof(fmt.type));
            convert(native.data(), fmt.type, data, size);
            const size_t sz = drwav_write(f, size, native.data());
            fmt.length += sz / fmt.channels;
            return sz;
        }
    }

    void close() override
    {
        drwav_close(f);
        f = nullptr;
        writer.reset();
    }

    const audio_format_and_length& format() const override { return fmt; }

    imax tell() const override { return fmt.length; }

    bool seek(imax, seek_origin) override { return false; }

private:
    std::shared_ptr<abstract_writer<>> writer;
    drwav* f;
    audio_format_and_length fmt;
}

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L155

audio_writer_wav<T> function

audio_writer_wav(
    std::shared_ptr<abstract_writer<>> &&writer,
    const audio_format &fmt)
    : writer(std::move(writer)), f(nullptr), fmt(fmt)

Constructs WAV writer using target writer and format

Source code
audio_writer_wav(std::shared_ptr<abstract_writer<>>&& writer, const audio_format& fmt)
    : writer(std::move(writer)), f(nullptr), fmt(fmt)
{
    drwav_data_format wav_fmt;
    wav_fmt.channels   = static_cast<drwav_uint32>(fmt.channels);
    wav_fmt.sampleRate = static_cast<drwav_uint32>(fmt.samplerate);
    wav_fmt.format =
        fmt.type >= audio_sample_type::first_float ? DR_WAVE_FORMAT_IEEE_FLOAT : DR_WAVE_FORMAT_PCM;
    wav_fmt.bitsPerSample = static_cast<drwav_uint32>(audio_sample_bit_depth(fmt.type));
    wav_fmt.container     = fmt.use_w64 ? drwav_container_w64 : drwav_container_riff;
    f = drwav_open_write(&wav_fmt, (drwav_write_proc)&internal_generic::drwav_writer_write_proc,
                         (drwav_seek_proc)&internal_generic::drwav_writer_seek_proc, this->writer.get());
}

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L158

write function

size_t write(const T *data, size_t size) override

Write data to underlying binary writer

Source code
size_t write(const T* data, size_t size) override
{
    if (!f)
        return 0;
    if (fmt.type == audio_sample_type::unknown)
        return 0;
    if (fmt.type == audio_sample_traits<T>::type)
    {
        const size_t sz = drwav_write(f, size, data);
        fmt.length += sz / fmt.channels;
        return sz;
    }
    else
    {
        univector<uint8_t> native(size * audio_sample_sizeof(fmt.type));
        convert(native.data(), fmt.type, data, size);
        const size_t sz = drwav_write(f, size, native.data());
        fmt.length += sz / fmt.channels;
        return sz;
    }
}

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L176

close function

virtual void close() = 0

Finishes writing and closes underlying writer

Source code
virtual void close() = 0

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L111

format function

virtual const audio_format_and_length &format() const = 0

Returns audio format description

Source code
virtual const audio_format_and_length& format() const = 0

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L91

virtual const audio_format_and_length &format() const = 0

Returns audio format description

Source code
virtual const audio_format_and_length& format() const = 0

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L108

read function

using abstract_reader<T>::read

Reads interleaved audio

Source code
using abstract_reader<T>::read

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L77

write function

using abstract_writer<T>::write

Writes interleaved audio

Source code
using abstract_writer<T>::write

https://github.com/kfrlib/kfr/blob//include/kfr/io/audiofile.hpp#L98


Auto-generated from sources, Revision , https://github.com/kfrlib/kfr/blob//include/kfr/