From 7ee54e8333632db0110d592fef8d0b958825e514 Mon Sep 17 00:00:00 2001 From: BlackPhlox Date: Sun, 11 Jul 2021 20:27:48 +0200 Subject: [PATCH 1/3] Added midi msg remap and added test example for adsick --- examples/adsick_piano.rs | 275 +++++++++++++++++++++++++++++++++++++++ examples/piano.rs | 10 +- examples/simple.rs | 9 +- src/lib.rs | 19 ++- 4 files changed, 299 insertions(+), 14 deletions(-) create mode 100644 examples/adsick_piano.rs diff --git a/examples/adsick_piano.rs b/examples/adsick_piano.rs new file mode 100644 index 0000000..0998053 --- /dev/null +++ b/examples/adsick_piano.rs @@ -0,0 +1,275 @@ +use bevy::{pbr::AmbientLight, prelude::*}; +use bevy_config_cam::ConfigCam; +use bevy_midi::{Midi, MidiEvent, MidiSettings, translate}; + +#[derive(Debug)] +struct Key(String, f32); + +fn main() { + App::build() + .insert_resource(AmbientLight { + color: Color::WHITE, + brightness: 1.0 / 5.0f32, + }) + .insert_resource(Msaa { samples: 4 }) + .add_plugin(ConfigCam) + .add_plugin(Midi) + .insert_resource(MidiSettings { + note_on: 156, + note_off: 140, + ..Default::default() + }) + .add_plugins(DefaultPlugins) + .add_startup_system(setup.system()) + .add_startup_system(setup_piano.system()) + .add_system(rotator_system.system()) + .add_system(midi_listener.system()) + //.add_system(key_bow_system.system()) + .run(); +} + +fn setup_piano(mut commands: Commands, asset_server: Res) { + let pos: Vec3 = Vec3::new(0., 0., 0.); + + let black_key: Handle = asset_server.load("models/black_key.gltf#Scene0"); + let white_key_0: Handle = asset_server.load("models/white_key_0.gltf#Scene0"); + let white_key_1: Handle = asset_server.load("models/white_key_1.gltf#Scene0"); + let white_key_2: Handle = asset_server.load("models/white_key_2.gltf#Scene0"); + + //Building the keyboard of every octave section + for i in 0..8 { + commands + .spawn_bundle(( + Transform { + translation: Vec3::new(pos.x, pos.y, pos.z - (1.61 * i as f32)), + scale: Vec3::new(10., 10., 10.), + ..Default::default() + }, + GlobalTransform::identity(), + )) + .insert(Key(format!("C{}", i), 0.)) + .with_children(|cell| { + cell.spawn_scene(white_key_0.clone()); + }); + + commands + .spawn_bundle(( + Transform { + translation: Vec3::new(pos.x, pos.y + 0.06, pos.z - 0.15 - (1.61 * i as f32)), + scale: Vec3::new(10., 10., 10.), + ..Default::default() + }, + GlobalTransform::identity(), + )) + .insert(Key(format!("C#{}", i), 0.06)) + .with_children(|cell| { + cell.spawn_scene(black_key.clone()); + }); + + commands + .spawn_bundle(( + Transform { + translation: Vec3::new(pos.x, pos.y, pos.z - 0.27 - (1.61 * i as f32)), + scale: Vec3::new(10., 10., 10.), + ..Default::default() + }, + GlobalTransform::identity(), + )) + .insert(Key(format!("D{}", i), 0.)) + .with_children(|cell| { + cell.spawn_scene(white_key_1.clone()); + }); + + commands + .spawn_bundle(( + Transform { + translation: Vec3::new(pos.x, pos.y + 0.06, pos.z - 0.39 - (1.61 * i as f32)), + scale: Vec3::new(10., 10., 10.), + ..Default::default() + }, + GlobalTransform::identity(), + )) + .insert(Key(format!("D#{}", i), 0.06)) + .with_children(|cell| { + cell.spawn_scene(black_key.clone()); + }); + + commands + .spawn_bundle(( + Transform { + translation: Vec3::new(pos.x, pos.y, pos.z - 0.54 - (1.61 * i as f32)), + scale: Vec3::new(10., 10., 10.), + ..Default::default() + }, + GlobalTransform::identity(), + )) + .insert(Key(format!("E{}", i), 0.)) + .with_children(|cell| { + cell.spawn_scene(white_key_2.clone()); + }); + + commands + .spawn_bundle(( + Transform { + translation: Vec3::new(pos.x, pos.y, pos.z - 0.69 - (1.61 * i as f32)), + scale: Vec3::new(10., 10., 10.), + ..Default::default() + }, + GlobalTransform::identity(), + )) + .insert(Key(format!("F{}", i), 0.)) + .with_children(|cell| { + cell.spawn_scene(white_key_0.clone()); + }); + + commands + .spawn_bundle(( + Transform { + translation: Vec3::new(pos.x, pos.y + 0.06, pos.z - 0.85 - (1.61 * i as f32)), + scale: Vec3::new(10., 10., 10.), + ..Default::default() + }, + GlobalTransform::identity(), + )) + .insert(Key(format!("F#{}", i), 0.06)) + .with_children(|cell| { + cell.spawn_scene(black_key.clone()); + }); + + commands + .spawn_bundle(( + Transform { + translation: Vec3::new(pos.x, pos.y, pos.z - 0.96 - (1.61 * i as f32)), + scale: Vec3::new(10., 10., 10.), + ..Default::default() + }, + GlobalTransform::identity(), + )) + .insert(Key(format!("G{}", i), 0.)) + .with_children(|cell| { + cell.spawn_scene(white_key_1.clone()); + }); + + commands + .spawn_bundle(( + Transform { + translation: Vec3::new(pos.x, pos.y + 0.06, pos.z - 1.08 - (1.61 * i as f32)), + scale: Vec3::new(10., 10., 10.), + ..Default::default() + }, + GlobalTransform::identity(), + )) + .insert(Key(format!("G#{}", i), 0.06)) + .with_children(|cell| { + cell.spawn_scene(black_key.clone()); + }); + + commands + .spawn_bundle(( + Transform { + translation: Vec3::new(pos.x, pos.y, pos.z - 1.19 - (1.61 * i as f32)), + scale: Vec3::new(10., 10., 10.), + ..Default::default() + }, + GlobalTransform::identity(), + )) + .insert(Key(format!("A{}", i), 0.)) + .with_children(|cell| { + cell.spawn_scene(white_key_1.clone()); + }); + + commands + .spawn_bundle(( + Transform { + translation: Vec3::new(pos.x, pos.y + 0.06, pos.z - 1.31 - (1.61 * i as f32)), + scale: Vec3::new(10., 10., 10.), + ..Default::default() + }, + GlobalTransform::identity(), + )) + .insert(Key(format!("A#{}", i), 0.06)) + .with_children(|cell| { + cell.spawn_scene(black_key.clone()); + }); + + commands + .spawn_bundle(( + Transform { + translation: Vec3::new(pos.x, pos.y, pos.z - 1.46 - (1.61 * i as f32)), + scale: Vec3::new(10., 10., 10.), + ..Default::default() + }, + GlobalTransform::identity(), + )) + .insert(Key(format!("B{}", i), 0.)) + .with_children(|cell| { + cell.spawn_scene(white_key_2.clone()); + }); + } +} + +fn setup(mut commands: Commands) { + commands + .spawn_bundle(LightBundle { + transform: Transform::from_xyz(3.0, 5.0, 3.0), + ..Default::default() + }) + .insert(Rotates); +} + +/// this component indicates what entities should rotate +struct Rotates; + +fn rotator_system(time: Res