1#ifndef NNGN_AUDIO_WAV_H
2#define NNGN_AUDIO_WAV_H
12#include "utils/literals.h"
13#include "utils/utils.h"
17using namespace std::string_view_literals;
22 static_assert(std::endian::native == std::endian::little,
"TODO");
27 constexpr explicit WAV(std::span<std::byte> buffer);
37 constexpr void fill(
void)
const;
38 constexpr std::span<std::byte>
data(
void)
const;
58 static constexpr auto RIFF =
"RIFF"sv;
59 static constexpr auto WAVE =
"WAVE"sv;
60 static constexpr auto FMT =
"fmt "sv;
61 static constexpr auto DATA =
"data"sv;
65 template<
typename T>
constexpr void write(
offset o,
const T &x)
const;
66 constexpr std::size_t
size(
void)
const;
67 std::span<std::byte>
b = {};
70inline constexpr WAV::WAV(std::span<std::byte> buffer) : b{buffer} {
71 assert(WAV::HEADER_SIZE <= this->
b.size());
75 offset o, std::size_t
n
77 const auto oz =
static_cast<std::size_t
>(o);
78 assert(oz + n <= this->
b.size());
79 return this->
b.subspan(oz,
n);
85 std::memcpy(&ret, this->
subspan(o,
sizeof(ret)).
data(),
sizeof(ret));
90constexpr void WAV::write(offset o,
const T &x)
const {
91 if constexpr(std::ranges::range<T>) {
93 std::memcpy(
s.data(), x.data(), x.size());
96 std::memcpy(
s.data(), &x,
sizeof(
T));
124inline constexpr std::size_t
WAV::size(
void)
const {
128inline constexpr std::span<std::byte>
WAV::data(
void)
const {
146inline constexpr void WAV::fill(
void)
const {
149 const auto r = this->
rate();
158 static_cast<u32>(
r * bytes_per_sample *
c));
A non-owning wrapper for a byte buffer containing a WAV file.
Definition: wav.h:21
static constexpr std::size_t HEADER_SIZE
Definition: wav.h:24
constexpr void set_rate(u32 r) const
constexpr u16 bits_per_sample(void) const
WAV(void)=default
Constructs an empty object, must be initialized before it is used.
constexpr void write(offset o, const T &x) const
constexpr void set_size(u32 s) const
offset
Definition: wav.h:41
constexpr std::span< std::byte > data(void) const
constexpr u32 fmt_size(void) const
std::span< std::byte > b
Definition: wav.h:67
constexpr u16 channels(void) const
constexpr WAV(std::span< std::byte > buffer)
constexpr void fill(void) const
constexpr u16 format(void) const
constexpr T read(offset o) const
constexpr u32 rate(void) const
constexpr std::span< std::byte > subspan(offset o, std::size_t n) const
constexpr std::size_t size(void) const
constexpr std::size_t n_samples(void) const
constexpr void set_channels(u16 c) const
c
Definition: gamma.lua:11
assert
Definition: debug.lua:3
n
Definition: dump_lights.lua:5
std::chrono::seconds s
Definition: timing.cpp:6
std::uint32_t u32
Definition: def.h:14
std::uint16_t u16
Definition: def.h:13
static constexpr auto DATA
Definition: wav.h:61
static constexpr auto RIFF
Definition: wav.h:58
static constexpr auto WAVE
Definition: wav.h:59
static constexpr auto FMT
Definition: wav.h:60