examples: sysaudio: provide better controls
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
c90dcb4275
commit
58ffa2c870
1 changed files with 17 additions and 4 deletions
|
|
@ -37,12 +37,20 @@ pub const components = .{
|
||||||
.play_after = .{ .type = f32 },
|
.play_after = .{ .type = f32 },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ghost_key_mode: bool = false,
|
||||||
|
|
||||||
fn init(audio: *mach.Audio.Mod, piano: *Mod) void {
|
fn init(audio: *mach.Audio.Mod, piano: *Mod) void {
|
||||||
// Initialize audio module
|
// Initialize audio module
|
||||||
audio.send(.init, .{});
|
audio.send(.init, .{});
|
||||||
|
|
||||||
// Initialize piano module state
|
// Initialize piano module state
|
||||||
piano.init(.{});
|
piano.init(.{});
|
||||||
|
|
||||||
|
std.debug.print("controls:\n", .{});
|
||||||
|
std.debug.print("[typing] Play piano noises\n", .{});
|
||||||
|
std.debug.print("[spacebar] enable ghost-key mode (demonstrate seamless back-to-back sound playback)\n", .{});
|
||||||
|
std.debug.print("[arrow up] increase volume 10%\n", .{});
|
||||||
|
std.debug.print("[arrow down] decrease volume 10%\n", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn audioStateChange(
|
fn audioStateChange(
|
||||||
|
|
@ -75,9 +83,12 @@ fn tick(
|
||||||
.key_press => |ev| {
|
.key_press => |ev| {
|
||||||
const vol = try audio.state().player.volume();
|
const vol = try audio.state().player.volume();
|
||||||
switch (ev.key) {
|
switch (ev.key) {
|
||||||
// Arrow keys turn volume up/down
|
// Controls
|
||||||
|
.space => piano.state().ghost_key_mode = !piano.state().ghost_key_mode,
|
||||||
.down => try audio.state().player.setVolume(@max(0.0, vol - 0.1)),
|
.down => try audio.state().player.setVolume(@max(0.0, vol - 0.1)),
|
||||||
.up => try audio.state().player.setVolume(@min(1.0, vol + 0.1)),
|
.up => try audio.state().player.setVolume(@min(1.0, vol + 0.1)),
|
||||||
|
|
||||||
|
// Piano keys
|
||||||
else => {
|
else => {
|
||||||
// Play a new sound
|
// Play a new sound
|
||||||
const entity = try audio.newEntity();
|
const entity = try audio.newEntity();
|
||||||
|
|
@ -85,9 +96,11 @@ fn tick(
|
||||||
try audio.set(entity, .playing, true);
|
try audio.set(entity, .playing, true);
|
||||||
try audio.set(entity, .index, 0);
|
try audio.set(entity, .index, 0);
|
||||||
|
|
||||||
|
if (piano.state().ghost_key_mode) {
|
||||||
// After that sound plays, we'll chain on another sound that is one semi-tone higher.
|
// After that sound plays, we'll chain on another sound that is one semi-tone higher.
|
||||||
const one_semi_tone_higher = keyToFrequency(ev.key) * math.pow(f32, 2.0, (1.0 / 12.0));
|
const one_semi_tone_higher = keyToFrequency(ev.key) * math.pow(f32, 2.0, (1.0 / 12.0));
|
||||||
try piano.set(entity, .play_after, one_semi_tone_higher);
|
try piano.set(entity, .play_after, one_semi_tone_higher);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue