hello
I apologize if it’s frowned on/not allowed to send this much content, please inform me if so
so I have this file
const std = @import("std");
const c = @cImport({
@cInclude("miniaudio.h");
});
pub const Sound = struct {
sound: c.ma_sound,
allocator: std.mem.Allocator,
engine: *c.ma_engine,
pub fn init(allocator: std.mem.Allocator, engine: *c.ma_engine, path: []const u8) anyerror!Sound {
var self: Sound = .{ .engine = engine, .allocator = allocator, .sound = undefined };
const result = c.ma_sound_init_from_file(engine, path.ptr, 0, null, null, &self.sound);
if (result != c.MA_SUCCESS) {
return error.MiniaudioSoundInitializationFailed;
}
return self;
}
pub fn deinit(self: *Sound) void {
c.ma_sound_uninit(&self.sound);
}
pub fn setLooping(self: *Sound, looping: bool) void {
c.ma_sound_set_looping(&self.sound, if (looping) 1 else 0);
}
pub fn getLooping(self: Sound) bool {
return if (c.ma_sound_is_looping(&self.sound) == 1) true else false;
}
pub fn playing(self: *Sound) bool {
return if (c.ma_sound_is_playing(&self.sound) == 1) true else false;
}
pub fn setVolume(self: *Sound, volume: f32) anyerror!void {
c.ma_sound_set_volume(&self.sound, volume);
}
pub fn getVolume(self: Sound) f32 {
return c.ma_sound_get_volume(&self.sound);
}
pub fn setPan(self: *Sound, pan: f32) anyerror!void {
c.ma_sound_set_pan(&self.sound, pan);
}
pub fn getPan(self: Sound) f32 {
return c.ma_sound_get_pan(&self.sound);
}
pub fn setPitch(self: *Sound, pitch: f32) anyerror!void {
c.ma_sound_set_pitch(&self.sound, pitch);
}
pub fn getPitch(self: Sound) f32 {
return c.ma_sound_get_pitch(&self.sound);
}
pub fn play(self: *Sound, looping: bool) anyerror!void {
self.setLooping(looping);
const result = c.ma_sound_start(&self.sound);
if (result != c.MA_SUCCESS) {
return error.MiniAudioSoundStartFailed;
}
}
};
pub const SoundManager = struct {
sounds: std.ArrayList(Sound),
cleanFrequency: isize,
engine: *c.ma_engine,
allocator: std.mem.Allocator,
pub fn init(allocator: std.mem.Allocator) anyerror!SoundManager {
const engine: *c.ma_engine = try allocator.create(c.ma_engine);
const result = c.ma_engine_init(null, engine);
if (result != c.MA_SUCCESS) {
return error.FailedToInitializeMiniaudioEngine;
}
return .{
.engine = engine,
.sounds = std.ArrayList(Sound).init(allocator),
.allocator = allocator,
.cleanFrequency = 3,
};
}
pub fn deinit(self: *SoundManager) void {
for (self.sounds.items) |*sound| {
sound.deinit();
}
self.sounds.deinit();
c.ma_engine_uninit(self.engine);
self.allocator.destroy(self.engine);
}
pub fn play(self: *SoundManager, path: []const u8, looping: bool) anyerror!*Sound {
var sound = try Sound.init(self.allocator, self.engine, path);
try sound.play(looping);
try self.sounds.append(sound);
try self.clean();
return &self.sounds.items[self.sounds.items.len - 1];
}
pub fn clean(self: *SoundManager) anyerror!void {
self.cleanFrequency -= 1;
if (self.cleanFrequency <= 0) {
for (0..self.sounds.items.len - 1) |i| {
if (i >= self.sounds.items.len or self.sounds.items[i].playing() or self.sounds.items[i].getLooping()) {
continue;
}
var sound = self.sounds.orderedRemove(i);
sound.deinit();
}
}
}
};
it crashs with a segmentation fault when I create an instance of SoundManager then call play(), however, when I instead allocate sound into the heap, the result is different
const std = @import("std");
const c = @cImport({
@cInclude("miniaudio.h");
});
pub const Sound = struct {
sound: *c.ma_sound,
allocator: std.mem.Allocator,
engine: *c.ma_engine,
pub fn init(allocator: std.mem.Allocator, engine: *c.ma_engine, path: []const u8) anyerror!*Sound {
const self = try allocator.create(Sound);
self.engine = engine;
self.allocator = allocator;
self.sound = try allocator.create(c.ma_sound);
_ = c.ma_sound_init_from_file(engine, path.ptr, 0, null, null, self.sound);
return self;
}
pub fn deinit(self: *Sound) void {
c.ma_sound_uninit(self.sound);
self.allocator.destroy(self.sound);
self.allocator.destroy(self);
}
pub fn setLooping(self: *Sound, looping: bool) void {
c.ma_sound_set_looping(self.sound, if (looping) 1 else 0);
}
pub fn getLooping(self: Sound) bool {
return if (c.ma_sound_is_looping(self.sound) == 1) true else false;
}
pub fn playing(self: *Sound) bool {
return if (c.ma_sound_is_playing(self.sound) == 1) true else false;
}
pub fn setVolume(self: *Sound, volume: f32) anyerror!void {
c.ma_sound_set_volume(self.sound, volume);
}
pub fn getVolume(self: Sound) f32 {
return c.ma_sound_get_volume(self.sound);
}
pub fn setPan(self: *Sound, pan: f32) anyerror!void {
c.ma_sound_set_pan(self.sound, pan);
}
pub fn getPan(self: Sound) f32 {
return c.ma_sound_get_pan(self.sound);
}
pub fn setPitch(self: *Sound, pitch: f32) anyerror!void {
c.ma_sound_set_pitch(self.sound, pitch);
}
pub fn getPitch(self: Sound) f32 {
return c.ma_sound_get_pitch(self.sound);
}
pub fn play(self: *Sound, looping: bool) anyerror!void {
self.setLooping(looping);
const result = c.ma_sound_start(self.sound);
if (result != c.MA_SUCCESS) {
return error.MiniAudioSoundStartFailed;
}
}
};
pub const SoundManager = struct {
sounds: std.ArrayList(*Sound),
cleanFrequency: isize,
engine: *c.ma_engine,
allocator: std.mem.Allocator,
pub fn init(allocator: std.mem.Allocator) anyerror!SoundManager {
const engine: *c.ma_engine = try allocator.create(c.ma_engine);
const result = c.ma_engine_init(null, engine);
if (result != c.MA_SUCCESS) {
return error.FailedToInitializeMiniaudioEngine;
}
return .{
.engine = engine,
.sounds = std.ArrayList(*Sound).init(allocator),
.allocator = allocator,
.cleanFrequency = 3,
};
}
pub fn deinit(self: *SoundManager) void {
for (self.sounds.items) |sound| {
sound.deinit();
}
self.sounds.deinit();
c.ma_engine_uninit(self.engine);
self.allocator.destroy(self.engine);
}
pub fn play(self: *SoundManager, path: []const u8, looping: bool) anyerror!*Sound {
const sound = try Sound.init(self.allocator, self.engine, path);
try sound.play(looping);
try self.sounds.append(sound);
try self.clean();
return sound;
}
pub fn clean(self: *SoundManager) anyerror!void {
self.cleanFrequency -= 1;
if (self.cleanFrequency <= 0) {
for (0..self.sounds.items.len - 1) |i| {
if (i >= self.sounds.items.len or self.sounds.items[i].playing() or self.sounds.items[i].getLooping()) {
continue;
}
var sound = self.sounds.orderedRemove(i);
sound.deinit();
}
}
}
};
this works perfectly fine
I want to know the reason?
if this is not enough information then do tell.