aboutsummaryrefslogtreecommitdiff
path: root/src/sfml/audio/SoundBuffer.zig
blob: 39ed082e470311bfff754c0838e67efaf9c2d33c (plain)
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
//! Storage for audio samples defining a sound. 

const sf = @import("../sfml.zig");

const SoundBuffer = @This();

// Constructor/destructor
/// Loads music from a file
pub fn createFromFile(path: [:0]const u8) !SoundBuffer {
    var sound = sf.c.sfSoundBuffer_createFromFile(path);
    if (sound == null)
        return sf.Error.resourceLoadingError;
    return SoundBuffer{ ._ptr = sound.? };
}
/// Creates a sound buffer from sample data
pub fn createFromSamples(samples: []const i16, channel_count: usize, sample_rate: usize) !SoundBuffer {
    var sound = sf.c.sfSoundBuffer_createFromSamples(@ptrCast([*c]const c_short, samples.ptr), samples.len, @intCast(c_uint, channel_count), @intCast(c_uint, sample_rate));
    if (sound == null)
        return sf.Error.resourceLoadingError;
    return SoundBuffer{ ._ptr = sound.? };
}

pub const initFromMemory = @compileError("Function is not implemented yet.");
pub const initFromStream = @compileError("Function is not implemented yet.");

/// Destroys this music object
pub fn destroy(self: *SoundBuffer) void {
    sf.c.sfSoundBuffer_destroy(self._ptr);
}

// Getters / Setters

/// Gets the duration of the sound
pub fn getDuration(self: SoundBuffer) sf.system.Time {
    return sf.system.Time._fromCSFML(sf.c.sfSoundBuffer_getDuration(self._ptr));
}

/// Gets the sample count of this sound
pub fn getSampleCount(self: SoundBuffer) usize {
    return @intCast(usize, sf.c.sfSoundBuffer_getSampleCount(self._ptr));
}

/// Gets the sample rate of this sound (n° of samples per second, often 44100)
pub fn getSampleRate(self: SoundBuffer) usize {
    return @intCast(usize, sf.c.sfSoundBuffer_getSampleRate(self._ptr));
}

/// Gets the channel count (2 is stereo for instance)
pub fn getChannelCount(self: SoundBuffer) usize {
    return @intCast(usize, sf.c.sfSoundBuffer_getChannelCount(self._ptr));
}

// Misc

/// Save the sound buffer to an audio file
pub fn saveToFile(self: SoundBuffer, path: [:0]const u8) !void {
    if (sf.c.sfSoundBuffer_saveToFile(self._ptr, path) != 1)
        return sf.Error.savingInFileFailed;
}

/// Pointer to the csfml texture
_ptr: *sf.c.sfSoundBuffer,

test "sound buffer: sane getter and setters" {
    const std = @import("std");
    const tst = std.testing;
    const allocator = std.heap.page_allocator;

    var samples = try allocator.alloc(i16, 44100 * 3);
    defer allocator.free(samples);

    var buffer = try SoundBuffer.createFromSamples(samples, 1, 44100);
    defer buffer.destroy();

    try tst.expectApproxEqAbs(@as(f32, 3), buffer.getDuration().asSeconds(), 0.001);
    try tst.expectEqual(@as(usize, 44100 * 3), buffer.getSampleCount());
    try tst.expectEqual(@as(usize, 44100), buffer.getSampleRate());
    try tst.expectEqual(@as(usize, 1), buffer.getChannelCount());
}