Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/gen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub const Generator = struct {
pub fn generate(self: Generator, allocator: std.mem.Allocator, sample: u8) ![]u8 {
var buffer = std.ArrayList(u8).init(allocator);
var i: usize = 0;
const N: f32 = @floatFromInt(sample_count);
while (i < sample_count) : (i += 1) {
// Calculate the frequency according to the equal temperament.
// Hertz = 440 * 2^(semitone distance / 12)
Expand All @@ -45,6 +46,12 @@ pub const Generator = struct {
// Apply the volume control.
const volume: f32 = @floatFromInt(self.config.volume);
amp = amp * volume / 100;

// Apply the Hann window function.
const index: f32 = @floatFromInt(i);
const hann_window = 0.5 * (1.0 - std.math.cos(2.0 * std.math.pi * index / (N - 1)));
amp = amp * hann_window;

try buffer.append(@intFromFloat(amp));
}
return buffer.toOwnedSlice();
Expand Down
Loading