74 lines
1.7 KiB
Zig
74 lines
1.7 KiB
Zig
|
const std = @import("std");
|
||
|
const c = @cImport({
|
||
|
@cInclude("sndfile.h");
|
||
|
});
|
||
|
|
||
|
pub const ImageError = error{OpenFail};
|
||
|
|
||
|
/// Low level integration function with libsndfile.
|
||
|
fn sopen(
|
||
|
allocator: *std.mem.Allocator,
|
||
|
path: []const u8,
|
||
|
mode: i32,
|
||
|
fmt: *c.SF_INFO,
|
||
|
) !*c.SNDFILE {
|
||
|
var cstr_path = try std.cstr.addNullByte(allocator, path);
|
||
|
defer allocator.free(cstr_path);
|
||
|
|
||
|
var file = c.sf_open(cstr_path.ptr, mode, fmt);
|
||
|
const st: i32 = c.sf_error(file);
|
||
|
|
||
|
if (st != 0) {
|
||
|
std.debug.warn(
|
||
|
"Failed to open {} ({})\n",
|
||
|
path,
|
||
|
c.sf_error_number(st),
|
||
|
);
|
||
|
|
||
|
return ImageError.OpenFail;
|
||
|
}
|
||
|
|
||
|
return file.?;
|
||
|
}
|
||
|
|
||
|
pub const Image = struct {
|
||
|
allocator: *std.mem.Allocator,
|
||
|
sndfile: *c.SNDFILE,
|
||
|
path: []const u8,
|
||
|
|
||
|
/// Open a BMP file.
|
||
|
pub fn open(allocator: *std.mem.Allocator, path: []const u8) !*Image {
|
||
|
var in_fmt = c.SF_INFO{
|
||
|
.frames = c_int(0),
|
||
|
.samplerate = c_int(44100),
|
||
|
.channels = c_int(1),
|
||
|
.format = c.SF_FORMAT_ULAW | c.SF_FORMAT_RAW | c.SF_ENDIAN_BIG,
|
||
|
.sections = c_int(0),
|
||
|
.seekable = c_int(0),
|
||
|
};
|
||
|
|
||
|
var sndfile = try sopen(allocator, path, c.SFM_READ, &in_fmt);
|
||
|
var image = try allocator.create(Image);
|
||
|
|
||
|
image.* = Image{
|
||
|
.allocator = allocator,
|
||
|
.sndfile = sndfile,
|
||
|
.path = path,
|
||
|
};
|
||
|
|
||
|
return image;
|
||
|
}
|
||
|
|
||
|
pub fn close(self: *Image) void {
|
||
|
var st: i32 = c.sf_close(self.sndfile);
|
||
|
|
||
|
if (st != 0) {
|
||
|
std.debug.warn(
|
||
|
"Failed to close {} ({})\n",
|
||
|
self.path,
|
||
|
c.sf_error_number(st),
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
};
|