Skip to content

Instantly share code, notes, and snippets.

@haruki7049
Last active June 14, 2026 02:40
Show Gist options
  • Select an option

  • Save haruki7049/9577eff602f7bcee7d8abdb4a304e901 to your computer and use it in GitHub Desktop.

Select an option

Save haruki7049/9577eff602f7bcee7d8abdb4a304e901 to your computer and use it in GitHub Desktop.
const std = @import("std");
const lightmix = @import("lightmix");
const T = f64;
const SAMPLE_RATE: u32 = 44100;
const CHANNELS: u16 = 1;
const LENGTH: usize = SAMPLE_RATE * 2;
const FREQUENCY: T = 440.0;
const VOLUME: T = 0.5;
pub fn gen(allocator: std.mem.Allocator) !lightmix.Wave(T) {
var samples = try allocator.alloc(T, LENGTH);
for (0..samples.len) |i| {
// Modulator
const radians_per_sec_mod: T = FREQUENCY * 10.0 * std.math.pi;
const mod: T = std.math.sin(
@as(T, @floatFromInt(i)) * radians_per_sec_mod / @as(T, @floatFromInt(SAMPLE_RATE)),
);
// Modulation index
const mod_idx: T = 1.0;
// Carrier
const radians_per_sec_car: T = FREQUENCY * 3.33 * std.math.pi;
const car: T = std.math.sin(
@as(T, @floatFromInt(i)) * radians_per_sec_car / @as(T, @floatFromInt(SAMPLE_RATE)) + (mod * mod_idx),
);
// Carrier index
const car_idx: T = VOLUME;
// result
const result: T = car * car_idx;
samples[i] = result;
}
return lightmix.Wave(T){
.allocator = allocator,
.samples = samples,
.sample_rate = SAMPLE_RATE,
.channels = CHANNELS,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment