Initial commit
This commit is contained in:
50
packages/media/src/qoa.zig
Normal file
50
packages/media/src/qoa.zig
Normal file
@@ -0,0 +1,50 @@
|
||||
const std = @import("std");
|
||||
|
||||
const Header = union(enum) {
|
||||
streaming: HeaderStreaming,
|
||||
static: HeaderStatic,
|
||||
};
|
||||
|
||||
const HeaderStreaming = void;
|
||||
|
||||
const HeaderStatic = struct {
|
||||
samples: u32,
|
||||
channels: u8,
|
||||
sample_rate: u24,
|
||||
|
||||
pub fn lengthInSeconds(self: HeaderStatic) f32 {
|
||||
const samples: f64 = @floatFromInt(self.samples);
|
||||
const sample_rate: f64 = @floatFromInt(self.sample_rate);
|
||||
return @floatCast(samples / sample_rate);
|
||||
}
|
||||
};
|
||||
|
||||
/// The caller asserts that the buffer is at least 12 bytes long, which can
|
||||
/// contain the entirety of a QOA file header and the relevant information in
|
||||
/// the first frame header.
|
||||
pub fn info(buffer: []const u8) ?Header {
|
||||
std.debug.assert(buffer.len >= 12);
|
||||
|
||||
const magic = buffer[0..4];
|
||||
const samples = std.mem.readInt(u32, buffer[4..8], .big);
|
||||
const channels = buffer[8];
|
||||
const sample_rate = std.mem.readInt(u24, buffer[9..12], .big);
|
||||
|
||||
if (!std.mem.eql(u8, magic, "qoaf") or channels == 0 or channels > 8 or sample_rate == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (samples == 0) {
|
||||
return .{
|
||||
.streaming = {},
|
||||
};
|
||||
} else {
|
||||
return .{
|
||||
.static = .{
|
||||
.samples = samples,
|
||||
.channels = channels,
|
||||
.sample_rate = sample_rate,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user