aboutsummaryrefslogtreecommitdiffstats
path: root/source/wav.hpp
blob: 9ff06e5f36856979493bd4ed7b69a0cbcc72c107 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
#ifndef WAV_HPP_
#define WAV_HPP_

#include <cstdint>
#include <cstring>
#include <fstream>

namespace wav
{
    struct header {
        char riff[4];      // "RIFF"
        uint32_t filesize; // Total file size minus eight bytes
        char wave[4];      // "WAVE"

        bool valid() const {
            return strncmp(riff, "RIFF", 4) == 0 && filesize > 8 && strncmp(wave, "WAVE", 4) == 0;
        }
    } __attribute__ ((packed));

    struct format {
        char fmt_[4]; // "fmt "
        uint32_t size;
        uint16_t type;
        uint16_t channelcount;
        uint32_t samplerate;
        uint32_t byterate;
        uint16_t unused;
        uint16_t bps;

        bool valid() const {
            return strncmp(fmt_, "fmt ", 4) == 0;
        }
    } __attribute__ ((packed));

    struct data {
        char data[4]; // "data"
        uint32_t size;

        bool valid() const {
            return strncmp(data, "data", 4) == 0;
        }
    } __attribute__ ((packed));

    class clip {
    public:
        clip(const char *path) {
            std::ifstream file (path);
            if (!file.good())
                return;
            {
                header h;
                file.read(reinterpret_cast<char *>(&h), sizeof(header));
                if (!h.valid())
                    return;
            }
            {
                format f;
                file.read(reinterpret_cast<char *>(&f), sizeof(format));
                if (!f.valid() || f.type != 1) // ensure PCM
                    return;
            }
            {
                wav::data d;
                file.read(reinterpret_cast<char *>(&d), sizeof(wav::data));
                if (!d.valid())
                    return;
                m_data = new char[d.size + 4096 - (d.size % 4096)];
                m_size = d.size;
                file.read(m_data, d.size);
            }
        }
        clip() = default;

        bool valid() const {
            return m_data != nullptr && m_size > 0;
        }
        auto data() const {
            return m_data;
        }
        auto next(unsigned int chunksize = 3000) {
            if (m_pos == m_size) {
                m_pos = 0;
            }
            auto ret = m_data + m_pos;
            m_pos = std::min(m_pos + chunksize, m_size);
            return ret;
        }
    private:
        char *m_data = nullptr;
        uint32_t m_size = 0;
        uint32_t m_pos = 0;
    };
}

#endif // WAV_HPP_