Skip to content

Commit 0fb3044

Browse files
committed
feat: shift pc keys along config.piano_range()
2 parents 9adc786 + e327358 commit 0fb3044

7 files changed

Lines changed: 395 additions & 29 deletions

File tree

flake.lock

Lines changed: 146 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
inputs = {
3+
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
4+
systems.url = "github:nix-systems/default";
5+
6+
naersk = {
7+
url = "github:nix-community/naersk";
8+
inputs.nixpkgs.follows = "nixpkgs";
9+
};
10+
rust-overlay = {
11+
url = "github:oxalica/rust-overlay";
12+
inputs.nixpkgs.follows = "nixpkgs";
13+
};
14+
15+
gitignore = {
16+
url = "github:hercules-ci/gitignore.nix";
17+
inputs.nixpkgs.follows = "nixpkgs";
18+
};
19+
};
20+
21+
outputs = inputs @ {self, ...}: let
22+
forAllSystems = f:
23+
inputs.nixpkgs.lib.genAttrs
24+
(import inputs.systems)
25+
(system: let
26+
pkgs = import inputs.nixpkgs {
27+
inherit system;
28+
overlays = [inputs.rust-overlay.overlays.default];
29+
};
30+
in
31+
f system pkgs);
32+
33+
meta = (inputs.nixpkgs.lib.importTOML ./neothesia/Cargo.toml).package;
34+
in {
35+
packages = forAllSystems (_system: pkgs: let
36+
rustToolchain =
37+
pkgs.rust-bin.nightly.latest.default.override
38+
{extensions = ["rust-src" "rust-analyzer"];};
39+
40+
naersk = pkgs.callPackage inputs.naersk {
41+
rustc = rustToolchain;
42+
cargo = rustToolchain;
43+
clippy = rustToolchain;
44+
};
45+
in {
46+
default = naersk.buildPackage {
47+
meta.mainProgram = "neothesia";
48+
src = pkgs.lib.cleanSourceWith {
49+
name = "${meta.name}-${meta.version}-src";
50+
src = ./.;
51+
filter = inputs.gitignore.lib.gitignoreFilterWith {
52+
basePath = ./.;
53+
extraRules =
54+
# gitignore
55+
''
56+
flake.*
57+
LICENSE.md
58+
README.md
59+
.github
60+
makefile
61+
*.sf2
62+
*.mid
63+
docs
64+
'';
65+
};
66+
};
67+
};
68+
});
69+
70+
devShells = forAllSystems (system: pkgs: {
71+
default =
72+
pkgs.mkShell
73+
{inputsFrom = [self.packages.${system}.default];};
74+
});
75+
};
76+
}

neothesia/src/scene/freeplay/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
NeothesiaEvent,
1212
context::Context,
1313
scene::{
14-
MouseToMidiEventState, NuonRenderer, Scene,
14+
MouseToMidiEventState, NuonRenderer, Scene, pc_keyboard::PcKeyboard,
1515
freeplay::recorder::{FreeplayRecorder, Preview, RecorderStatus},
1616
playing_scene::Keyboard,
1717
},
@@ -51,6 +51,7 @@ pub struct FreeplayScene {
5151
nuon_renderer: NuonRenderer,
5252
nuon: nuon::Ui,
5353
mouse_to_midi_state: MouseToMidiEventState,
54+
pc_keyboard: PcKeyboard,
5455
deduced_chord_name: String,
5556

5657
recorder: FreeplayRecorder,
@@ -98,6 +99,7 @@ impl FreeplayScene {
9899
nuon_renderer: NuonRenderer::new(ctx),
99100
nuon: nuon::Ui::new(),
100101
mouse_to_midi_state: MouseToMidiEventState::default(),
102+
pc_keyboard: PcKeyboard::default(),
101103
deduced_chord_name: String::new(),
102104
recorder: FreeplayRecorder::default(),
103105
recorder_status: RecorderStatus::default(),
@@ -247,7 +249,7 @@ impl Scene for FreeplayScene {
247249
}
248250

249251
super::handle_nuon_window_event(&mut self.nuon, event, ctx);
250-
super::handle_pc_keyboard_to_midi_event(ctx, event);
252+
super::handle_pc_keyboard_to_midi_event(ctx, &mut self.pc_keyboard, event);
251253
super::handle_mouse_to_midi_event(
252254
&mut self.keyboard,
253255
&mut self.mouse_to_midi_state,

neothesia/src/scene/mod.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod freeplay;
22
pub mod menu_scene;
3+
pub mod pc_keyboard;
34
pub mod playing_scene;
45

56
use crate::{
@@ -21,7 +22,11 @@ pub trait Scene {
2122
fn midi_event(&mut self, _ctx: &mut Context, _channel: u8, _message: &MidiMessage) {}
2223
}
2324

24-
pub fn handle_pc_keyboard_to_midi_event(ctx: &mut Context, event: &WindowEvent) {
25+
pub fn handle_pc_keyboard_to_midi_event(
26+
ctx: &mut Context,
27+
pc_keyboard: &mut pc_keyboard::PcKeyboard,
28+
event: &WindowEvent,
29+
) {
2530
let WindowEvent::KeyboardInput {
2631
event:
2732
KeyEvent {
@@ -36,31 +41,24 @@ pub fn handle_pc_keyboard_to_midi_event(ctx: &mut Context, event: &WindowEvent)
3641
return;
3742
};
3843

39-
let mut note: u8 = match ch.as_str() {
40-
"a" => 0,
41-
"w" => 1,
42-
"s" => 2,
43-
"e" => 3,
44-
"d" => 4,
45-
"f" => 5,
46-
"t" => 6,
47-
"g" => 7,
48-
"y" => 8,
49-
"h" => 9,
50-
"u" => 10,
51-
"j" => 11,
52-
"k" => 12,
53-
"o" => 13,
54-
"l" => 14,
55-
"p" => 15,
56-
";" => 16,
57-
"'" => 17,
58-
_ => return,
44+
if let Some(delta) = match ch.as_str() {
45+
"z" => Some(-1),
46+
"x" => Some(1),
47+
_ => None,
48+
} {
49+
if *state == ElementState::Pressed {
50+
pc_keyboard.shift_octave(delta);
51+
}
52+
return;
53+
}
54+
55+
let Some(index) = pc_keyboard::key_index(ch.as_str()) else {
56+
return;
5957
};
6058

61-
note += 21; // Start of 88 keyboard
62-
note += 3; // Offset to C
63-
note += 12 * 3; // Move 3oct up
59+
let Some(note) = pc_keyboard.note_for(index, &ctx.config.piano_range()) else {
60+
return;
61+
};
6462

6563
let message = match state {
6664
ElementState::Pressed => MidiMessage::NoteOn {

0 commit comments

Comments
 (0)