game-2026-01-18/src/engine/audio/data.zig
2026-01-18 07:06:01 +02:00

65 lines
2.0 KiB
Zig

const std = @import("std");
const assert = std.debug.assert;
const STBVorbis = @import("stb_vorbis");
pub const Data = union(enum) {
raw: struct {
channels: [][*]f32,
sample_count: u32,
sample_rate: u32
},
vorbis: struct {
alloc_buffer: []u8,
stb_vorbis: STBVorbis,
},
pub fn streamChannel(
self: Data,
buffer: []f32,
cursor: u32,
channel_index: u32,
sample_rate: u32
) []f32 {
// var result: std.ArrayList(f32) = .initBuffer(buffer);
switch (self) {
.raw => |opts| {
if (opts.sample_rate == sample_rate) {
assert(channel_index < opts.channels.len); // TODO:
const channel = opts.channels[channel_index];
var memcpy_len: usize = 0;
if (cursor + buffer.len <= opts.sample_count) {
memcpy_len = buffer.len;
} else if (cursor < opts.sample_count) {
memcpy_len = opts.sample_count - cursor;
}
@memcpy(buffer[0..memcpy_len], channel[cursor..][0..memcpy_len]);
return buffer[0..memcpy_len];
} else {
// const in_sample_rate: f32 = @floatFromInt(opts.sample_rate);
// const out_sample_rate: f32 = @floatFromInt(sample_rate);
// const increment = in_sample_rate / out_sample_rate;
// _ = increment; // autofix
unreachable;
}
},
.vorbis => |opts| {
_ = opts; // autofix
unreachable;
},
}
// return result.items;
}
pub fn getSampleCount(self: Data) u32 {
return switch (self) {
.raw => |opts| opts.sample_count,
.vorbis => |opts| opts.stb_vorbis.getStreamLengthInSamples()
};
}
pub const Id = enum (u16) { _ };
};