-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSound.zig
More file actions
95 lines (73 loc) · 3.08 KB
/
Sound.zig
File metadata and controls
95 lines (73 loc) · 3.08 KB
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
const std = @import("std");
const Bass = @cImport({
@cInclude("bass.h");
});
pub const Sound = struct {
pub fn InitBass(window: ?*anyopaque) void {
if (Bass.BASS_Init(-1, 44100, Bass.BASS_DEVICE_LATENCY | Bass.BASS_DEVICE_STEREO, window, null) == 0) {
const bassErr = Bass.BASS_ErrorGetCode();
std.debug.print("Couldn't init bass: {d}\n", .{bassErr});
} else {
const bassVer = Bass.BASS_GetVersion();
var info: Bass.BASS_INFO = undefined;
_ = Bass.BASS_GetInfo(&info);
std.debug.print("Bass loaded: {d} Latency: {d} Speakers: {d}\n", .{ bassVer, info.latency, info.speakers });
}
}
Stream: Bass.HSTREAM,
//なんかデュプリケートした
pub fn FromFile(file: []const u8) Sound {
const stream = Bass.BASS_StreamCreateFile(Bass.FALSE, &file[0], 0, 0, Bass.BASS_STREAM_PRESCAN | Bass.BASS_SAMPLE_FLOAT);
const bassErr = Bass.BASS_ErrorGetCode();
if (bassErr != Bass.BASS_OK) {
std.debug.print("[StreamCreateFile BassError: {d}]\n", .{bassErr});
}
const sound = Sound{ .Stream = stream };
return sound;
}
pub fn FromData(data: []const u8) Sound {
const stream = Bass.BASS_StreamCreateFile(Bass.TRUE, &data[0], 0, data.len, Bass.BASS_STREAM_PRESCAN | Bass.BASS_SAMPLE_FLOAT);
const bassErr = Bass.BASS_ErrorGetCode();
if (bassErr != Bass.BASS_OK) {
std.debug.print("[StreamCreateFile BassError: {d}]\n", .{bassErr});
}
const sound = Sound{ .Stream = stream };
return sound;
}
pub fn GetPlaybackPositionInSeconds(self: *const Sound) f64 {
const byte_pos = Bass.BASS_ChannelGetPosition(self.Stream, Bass.BASS_POS_BYTE);
return Bass.BASS_ChannelBytes2Seconds(self.Stream, byte_pos);
}
pub fn SetPlaybackPositionSecs(self: *Sound, secs: f64) void {
const byte_pos = Bass.BASS_ChannelSeconds2Bytes(self.Stream, secs);
_ = Bass.BASS_ChannelSetPosition(self.Stream, byte_pos, Bass.BASS_POS_BYTE);
}
pub fn SetVolume(self: *Sound, volume: f32) void {
_ = Bass.BASS_ChannelSetAttribute(self.Stream, Bass.BASS_FX_VOLUME, volume);
}
pub fn Pause(self: *Sound) void {
_ = Bass.BASS_ChannelPause(self.Stream);
}
pub fn TogglePlay(self: *Sound) void {
if (Bass.BASS_ChannelIsActive(self.Stream) == Bass.BASS_ACTIVE_PAUSED) {
self.Play(false);
} else {
self.Pause();
}
}
pub fn Play(self: *Sound, restart: bool) void {
_ = Bass.BASS_ChannelPlay(self.Stream, @intFromBool(restart));
}
pub fn GetFrequency(self: *Sound) f32 {
const freq: f32 = 0.0;
_ = Bass.BASS_ChannelGetAttribute(self.Stream, Bass.BASS_ATTRIB_FREQ, &freq);
return freq;
}
pub fn SetFrequency(self: *Sound, value: f32) void {
Bass.BASS_ChannelSetAttribute(self.Stream, Bass.BASS_ATTRIB_FREQ, value);
}
pub fn Deinit(self: *Sound) void {
_ = Bass.BASS_StreamFree(self.Stream);
self.Stream = 0;
}
};