Audio: rewrite sample mixing to use SIMD properly

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-12-29 15:15:56 -07:00
parent 1a7753936b
commit 6450e8abbf
5 changed files with 234 additions and 52 deletions

View file

@ -275,7 +275,7 @@ pub fn tick(
sprite.objects.delete(sprite_id);
// Play a new sound
const samples = try app.allocator.dupe(f32, app.sfx.samples);
const samples = try app.allocator.alignedAlloc(f32, mach.Audio.alignment, app.sfx.samples.len);
@memcpy(samples, app.sfx.samples);
audio.buffers.lock();
defer audio.buffers.unlock();
@ -283,7 +283,7 @@ pub fn tick(
.samples = samples,
.channels = app.sfx.channels,
});
_ = sound_id; // autofix
_ = sound_id;
app.score += 1;
} else {
var transform = Mat4x4.ident;

View file

@ -203,13 +203,13 @@ pub fn tick(
window.queue.submit(&[_]*gpu.CommandBuffer{command});
}
fn fillTone(app: *App, audio: *mach.Audio, frequency: f32) ![]const f32 {
fn fillTone(app: *App, audio: *mach.Audio, frequency: f32) ![]align(mach.Audio.alignment) const f32 {
const channels = audio.player.channels().len;
const sample_rate: f32 = @floatFromInt(audio.player.sampleRate());
const duration: f32 = 1.5 * @as(f32, @floatFromInt(channels)) * sample_rate; // play the tone for 1.5s
const gain = 0.1;
const samples = try app.allocator.alloc(f32, @intFromFloat(duration));
const samples = try app.allocator.alignedAlloc(f32, mach.Audio.alignment, @intFromFloat(duration));
var i: usize = 0;
while (i < samples.len) : (i += channels) {