files.audio.cpp

1
2
3
4
5
6
7
8
#include <planet/audio/files.hpp>

#include <felspar/exceptions/runtime_error.hpp>
#include <felspar/memory/accumulation_buffer.hpp>

#include <optional>

#include <vorbis/codec.h>

planet::audio::ogg::impl

 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
struct planet::audio::ogg::impl {
    impl(std::span<std::byte>, felspar::source_location const &);
    ~impl();

    vorbis_info vi;
    vorbis_comment vc;
    felspar::source_location loc;

    felspar::coro::generator<
            planet::audio::buffer_storage<planet::audio::sample_clock, 2>>
            stereo();

  private:
    std::span<std::byte> ogg;
    felspar::coro::generator<ogg_packet> packets;
    felspar::coro::generator<ogg_packet> vorbis_packets();
};


planet::audio::ogg::impl::impl(
        std::span<std::byte> o, felspar::source_location const &l)
: loc{l}, ogg{o}, packets{vorbis_packets()} {
    vorbis_info_init(&vi);
    vorbis_comment_init(&vc);
    for (std::size_t expected{3}; expected; --expected) {
        auto vip = packets.next();
        if (vorbis_synthesis_headerin(&vi, &vc, &*vip) < 0) {
            throw felspar::stdexcept::runtime_error{
                    "Not Vorbis audio data", loc};
        }
    }
}


planet::audio::ogg::impl::~impl() {
    vorbis_comment_clear(&vc);
    vorbis_info_clear(&vi);
}


felspar::coro::generator<ogg_packet> planet::audio::ogg::impl::vorbis_packets() {
    struct ogg_resources {
        ogg_resources() {
            ogg_sync_init(&sync); // Always works
        }
        ~ogg_resources() {
            if (stream) { ogg_stream_clear(&*stream); }
            ogg_sync_clear(&sync);
        }
        ogg_sync_state sync;
        std::optional<ogg_stream_state> stream;
    };
    ogg_resources oggr;

    ogg_page op;
    if (ogg_sync_pageout(&oggr.sync, &op) != 0) {
        throw felspar::stdexcept::runtime_error{
                "ogg_sync_pageout not requesting data", loc};
    }

    std::byte *const buffer = reinterpret_cast<std::byte *>(
            ogg_sync_buffer(&oggr.sync, ogg.size()));
    std::copy(ogg.begin(), ogg.end(), buffer);
    if (ogg_sync_wrote(&oggr.sync, ogg.size()) == -1) {
        throw felspar::stdexcept::runtime_error{"ogg_sync_wrote failed", loc};
    }

    std::optional<int> streamid;
    while (ogg_sync_pageout(&oggr.sync, &op) == 1) {
        int serialno = ogg_page_serialno(&op);
        if (not streamid) {
            streamid = serialno;
            oggr.stream = ogg_stream_state{};
            ogg_stream_init(&*oggr.stream, serialno);
        }
        if (serialno == streamid) {
            if (ogg_stream_pagein(&*oggr.stream, &op) == -1) {
                throw felspar::stdexcept::runtime_error{
                        "ogg_stream_pagein failed", loc};
            }

            ogg_packet packet;
            while (ogg_stream_packetout(&*oggr.stream, &packet) == 1) {
                co_yield packet;
            }
        }
    }
}


felspar::coro::generator<
        planet::audio::buffer_storage<planet::audio::sample_clock, 2>>
        planet::audio::ogg::impl::stereo() {
    if (vi.channels != 2) {
        throw felspar::stdexcept::runtime_error{
                "This ogg file is not stereo", loc};
    } else if (vi.rate != planet::audio::sample_clock::period::den) {
        throw felspar::stdexcept::runtime_error{
                "The sample rate is wrong. Should be 48kHz and is "
                        + std::to_string(vi.rate) + "Hz",
                loc};
    }
    struct vorbis_resources {
        vorbis_resources(impl *i) {
            vorbis_synthesis_init(&d, &i->vi);
            vorbis_block_init(&d, &b);
        }
        ~vorbis_resources() {
            vorbis_block_clear(&b);
            vorbis_dsp_clear(&d);
        }
        vorbis_dsp_state d;
        vorbis_block b;
    };
    vorbis_resources v{this};

    felspar::memory::accumulation_buffer<float> output{
            default_buffer_samples * 50};

    for (auto &&packet : packets) {
        if (vorbis_synthesis(&v.b, &packet) == 0) {
            vorbis_synthesis_blockin(&v.d, &v.b);
        } else {
            throw felspar::stdexcept::runtime_error{
                    "vorbis_synthesis failed", loc};
        }
        float **pcm = nullptr;
        while (int isamples = vorbis_synthesis_pcmout(&v.d, &pcm)) {
            auto const samples = static_cast<std::size_t>(isamples);
            output.ensure_length(samples * stereo_buffer::channels);
            for (std::size_t sample{}; sample < samples; ++sample) {
                output.at(sample * stereo_buffer::channels + 0) =
                        pcm[0][sample];
                output.at(sample * stereo_buffer::channels + 1) =
                        pcm[1][sample];
            }
            vorbis_synthesis_read(&v.d, samples);
            co_yield output.first(samples * stereo_buffer::channels);
        }
    }
}

planet::audio::ogg

160
161
162
163
164
165
166
167
168
169
170
171
172
planet::audio::ogg::ogg(
        std::vector<std::byte> o, felspar::source_location const &l)
: filedata{std::move(o)}, loc{l} {}


felspar::coro::generator<
        planet::audio::buffer_storage<planet::audio::sample_clock, 2>>
        planet::audio::ogg::stereo() {
    impl decoder{filedata, loc};
    for (auto s = decoder.stereo(); auto p = s.next();) {
        co_yield std::move(*p);
    }
}