Skip to content
SenryokuPublic

About

FLAC decoder for Zig

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

60 Commits

Folders and files

Repository files navigation

zflac

FLAC decoder for Zig implemented from specifications.

Getting started

Add the library to your build.zig.zon:

zig fetch --save git+https://github.com/Senryoku/zflac

Declare the dependency in your build.zig:

const zflac = b.dependency("zflac", .{});
exe.root_module.addImport("zflac", zflac.module("zflac"));

Usage

const zflac = @import("zflac");

pub fn main() !void {
	const allocator = std.heap.page_allocator;
	const file = try std.fs.cwd().openFile("music.flac", .{});
	defer file.close();
	
    const buffer = try allocator.alloc(u8, 8192);
    defer allocator.free(buffer);
    var reader = file.reader(buffer);
	
	const decoded = try zflac.decode(allocator, &reader.interface); 
	defer decoded.deinit(allocator);
	
	// The returned structure holds some basic information on your file:
	std.debug.print("Channel count: {d}\n", .{decoded.channels});
	std.debug.print("Sample rate: {d}\n", .{decoded.sample_rate});
	// This is the number of significant bits and can be lower than the bit size of samples 
	// in the final array (e.g. 12bits samples are shifted left by 4 bits to 16bits).
	std.debug.print("Bits per samples: {d}\n", .{decoded.bits_per_sample}); 
	
	// `samples` is a tagged union based on the bit depth used in the file.
	switch (decoded.samples) {
		.s8 => |samples| play(i8, samples),
		.s16 => |samples| play(i16, samples),
		.s32 => |samples| play(i32, samples),
	}
}

See the examples folder for a more complete example.

TODOs

  • Better handle signed/unsigned 8bit samples (Probably remove the i8 interface and auto convert to u8).
  • Add a mode to sync. to the next frame and start/resume decoding from there.

About

FLAC decoder for Zig

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages