about summary refs log tree commit diff
path: root/src/sf.zig
blob: 1d8f1dd789f4ff1ff3b6a5823193dae98c74894c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// libsndfile wrapper
// Copyright (C) 2021-2023  Nguyễn Gia Phong
//
// This file is part of zeal.
//
// Zeal is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Zeal is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with zeal.  If not, see <https://www.gnu.org/licenses/>.

const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const span = std.mem.span;
const std = @import("std");

const c = @import("cimport.zig");

pub const Mode = enum {
    read,
    write,
    read_write,
};

pub const Error = Allocator.Error || error{
    UnrecognizedFormat,
    SystemError,
    MalformedFile,
    UnsupportedEncoding,
};

pub const SndFile = struct {
    pimpl: *c.SNDFILE,
    frames: usize,
    sample_rate: c_int,
    channels: c_int,
    format: c_int,
    sections: c_int,
    seekable: bool,

    /// Open the sound file at the specified path.
    pub fn open(path: [:0]const u8, mode: Mode) Error!SndFile {
        const c_mode = switch (mode) {
            .read => c.SFM_READ,
            .write => c.SFM_WRITE,
            .read_write => c.SFM_RDWR,
        };

        var info: c.SF_INFO = undefined;
        info.format = 0;
        const pimpl = c.sf_open(path.ptr, c_mode, &info);
        _ = c.sf_command(pimpl, c.SFC_SET_SCALE_FLOAT_INT_READ,
                         null, c.SF_TRUE);
        return SndFile{
            .pimpl = pimpl orelse return switch (c.sf_error(null)) {
                c.SF_ERR_UNRECOGNISED_FORMAT => Error.UnrecognizedFormat,
                c.SF_ERR_SYSTEM => Error.SystemError,
                c.SF_ERR_MALFORMED_FILE => Error.MalformedFile,
                c.SF_ERR_UNSUPPORTED_ENCODING => Error.UnsupportedEncoding,
                else => unreachable,
            },
            .frames = @intCast(info.frames),
            .sample_rate = info.samplerate,
            .channels = info.channels,
            .format = info.format,
            .sections = info.sections,
            .seekable = info.seekable != 0,
        };
    }

    /// Read the requested number of frames.
    /// The returned memory is managed by the caller.
    pub fn read(self: SndFile, allocator: Allocator,
                frames: usize) Error![]const i16 {
        const items = frames * @as(usize, @intCast(self.channels));
        const memory = try allocator.alloc(i16, items);
        errdefer allocator.free(memory);
        const n = c.sf_read_short(self.pimpl, memory.ptr, @intCast(items));
        return try allocator.realloc(memory, @intCast(n));
    }

    /// Read the entire file.  The returned memory is managed by the caller.
    pub fn readAll(self: SndFile, allocator: Allocator) Error![]const i16 {
        return self.read(allocator, self.frames);
    }

    /// Close the file and deallocate its internal buffers.
    /// Like std.os.close, this function does not return
    /// any indication of failure.
    pub fn close(self: SndFile) void {
        assert(c.sf_close(self.pimpl) == 0);
    }
};