44 lines
1.2 KiB
Zig
44 lines
1.2 KiB
Zig
const std = @import("std");
|
|
|
|
pub fn show_usage() void {
|
|
std.debug.print("Usage: zig build generate <sample-rate> <sample-count> <filename>\n", .{});
|
|
}
|
|
|
|
pub fn main() !void {
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
const allocator = gpa.allocator();
|
|
defer _ = gpa.deinit();
|
|
|
|
var args = try std.process.argsWithAllocator(allocator);
|
|
defer args.deinit();
|
|
|
|
_ = args.next();
|
|
|
|
const sample_rate_str = args.next() orelse {
|
|
show_usage();
|
|
std.process.exit(1);
|
|
};
|
|
|
|
const sample_count_str = args.next() orelse {
|
|
show_usage();
|
|
std.process.exit(1);
|
|
};
|
|
|
|
const filename = args.next() orelse {
|
|
show_usage();
|
|
std.process.exit(1);
|
|
};
|
|
|
|
const sample_rate = try std.fmt.parseFloat(f64, sample_rate_str);
|
|
const sample_count = try std.fmt.parseInt(u32, sample_count_str, 10);
|
|
|
|
const f = try std.fs.cwd().createFile(filename, .{ .exclusive = true });
|
|
defer f.close();
|
|
|
|
for (0..sample_count) |i| {
|
|
const i_f64: f64 = @floatFromInt(i);
|
|
const sample: f64 = std.math.sin( i_f64 / sample_rate * std.math.pi * 2 ) * 10;
|
|
const sample_bytes = std.mem.toBytes(sample);
|
|
try f.writeAll(&sample_bytes);
|
|
}
|
|
} |