Skip to content

Commit

Permalink
Improve color layers and make them configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Woyten committed Jul 4, 2024
1 parent 4982281 commit a4847fa
Show file tree
Hide file tree
Showing 8 changed files with 3,264 additions and 3,181 deletions.
6,050 changes: 3,025 additions & 3,025 deletions edo-colors-1-to-99.txt

Large diffs are not rendered by default.

94 changes: 36 additions & 58 deletions microwave/src/app/resources/virtual_keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use std::{
cmp::Ordering,
fmt::{self, Display},
sync::Arc,
};

use bevy::prelude::*;
use tune::{layout::IsomorphicLayout, pergen::Mos, pitch::Ratio, scala::Scl};
use tune::{
layout::{IsomorphicLayout, Layer},
pergen::Mos,
pitch::Ratio,
scala::Scl,
};

use crate::{app::Toggle, CustomKeyboardOptions};
use crate::{app::Toggle, profile::ColorPalette, CustomKeyboardOptions};

#[derive(Resource)]
pub struct VirtualKeyboardResource {
Expand Down Expand Up @@ -75,7 +79,11 @@ pub enum Inclination {
}

impl VirtualKeyboardResource {
pub fn new(scl: &Scl, options: CustomKeyboardOptions) -> VirtualKeyboardResource {
pub fn new(
scl: &Scl,
options: CustomKeyboardOptions,
palette: &ColorPalette,
) -> VirtualKeyboardResource {
let on_screen_keyboards = vec![
OnScreenKeyboards::Isomorphic,
OnScreenKeyboards::Scale,
Expand Down Expand Up @@ -116,7 +124,7 @@ impl VirtualKeyboardResource {
mos: mos.coprime(),
orig_mos: mos,
},
generate_colors(&isomorphic_layout),
generate_colors(&isomorphic_layout, palette),
)
})
.chain({
Expand Down Expand Up @@ -247,58 +255,28 @@ impl VirtualKeyboardResource {
}
}

fn generate_colors(layout: &IsomorphicLayout) -> Vec<Color> {
let color_indexes = layout.get_colors();

let colors = [
SHARP_COLOR,
FLAT_COLOR,
DOUBLE_SHARP_COLOR,
DOUBLE_FLAT_COLOR,
TRIPLE_SHARP_COLOR,
TRIPLE_FLAT_COLOR,
];

(0..layout.pergen().period())
.map(|index| {
const CYCLE_DARKNESS_FACTOR: f32 = 0.5;

let generation = layout.pergen().get_generation(index);
let degree = generation.degree;
let color_index = color_indexes[usize::from(degree)];

// The shade logic combines two requirements:
// - High contrast in the sharp (north-east) direction => Alternation
// - High contrast in the secondary (south-east) direction => Exception to the alternation rule for the middle cycle
let cycle_darkness = match (generation.cycle.unwrap_or_default() * 2 + 1)
.cmp(&layout.pergen().num_cycles())
{
Ordering::Less => {
CYCLE_DARKNESS_FACTOR * f32::from(generation.cycle.unwrap_or_default() % 2 != 0)
}
Ordering::Equal => CYCLE_DARKNESS_FACTOR / 2.0,
Ordering::Greater => {
CYCLE_DARKNESS_FACTOR
* f32::from(
(layout.pergen().num_cycles() - generation.cycle.unwrap_or_default())
% 2
!= 0,
)
}
};

(match color_index {
0 => NATURAL_COLOR,
x => colors[(x - 1) % colors.len()],
}) * (1.0 - cycle_darkness)
fn generate_colors(layout: &IsomorphicLayout, palette: &ColorPalette) -> Vec<Color> {
let layers = layout.get_layers();

let naturals_only = layers.iter().all(|&layer| layer == Layer::Natural);

let mut colors: Vec<_> = layers
.into_iter()
.map(|layer| {
let get_color = |colors: &[Color], index| colors[usize::from(index) % colors.len()];

match layer {
Layer::Natural => palette.natural_color,
Layer::Sharp(index) => get_color(&palette.sharp_colors, index),
Layer::Flat(index) => get_color(&palette.flat_colors, index),
Layer::Enharmonic(index) => get_color(&palette.enharmonic_colors, index),
}
})
.collect()
}
.collect();

if naturals_only {
colors[0] = palette.root_color
}

const NATURAL_COLOR: Color = Color::WHITE;
const SHARP_COLOR: Color = Color::rgb(0.5, 0.0, 1.0);
const FLAT_COLOR: Color = Color::rgb(0.5, 1.0, 0.5);
const DOUBLE_SHARP_COLOR: Color = Color::rgb(0.5, 0.5, 1.0);
const DOUBLE_FLAT_COLOR: Color = Color::rgb(0.0, 0.5, 0.5);
const TRIPLE_SHARP_COLOR: Color = Color::rgb(0.5, 0.0, 0.5);
const TRIPLE_FLAT_COLOR: Color = Color::rgb(1.0, 0.0, 0.5);
colors
}
6 changes: 4 additions & 2 deletions microwave/src/app/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,13 @@ fn create_keyboards(
}

let colors = &virtual_keyboard.colors();
let get_key_color =
|key| colors[usize::from(math::i32_rem_u(key, u16::try_from(colors.len()).unwrap()))];

if let Some(scale_keyboard_location) = scale_keyboard_location {
creator.create_linear(
(state.scl.clone(), kbm_root),
|key| colors[usize::from(math::i32_rem_u(key, u16::try_from(colors.len()).unwrap()))],
get_key_color,
scale_keyboard_location * SCENE_HEIGHT_3D,
);
}
Expand All @@ -261,7 +263,7 @@ fn create_keyboards(
creator.create_isomorphic(
virtual_keyboard,
(state.scl.clone(), kbm_root),
|key| colors[usize::from(math::i32_rem_u(key, u16::try_from(colors.len()).unwrap()))],
get_key_color,
keyboard_location * SCENE_HEIGHT_3D,
);
}
Expand Down
22 changes: 21 additions & 1 deletion microwave/src/assets.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy::render::color::Color;
use magnetron::envelope::EnvelopeSpec;
use tune_cli::shared::midi::TuningMethod;

Expand All @@ -18,7 +19,7 @@ use crate::{
ProcessorSpec, ProcessorType, StageType, StereoProcessorSpec, StereoProcessorType,
},
midi::MidiOutSpec,
profile::{AudioStageSpec, MicrowaveProfile},
profile::{AudioStageSpec, ColorPalette, MicrowaveProfile},
synth::MagnetronSpec,
};

Expand Down Expand Up @@ -268,13 +269,32 @@ pub fn get_default_profile() -> MicrowaveProfile {
}),
];

let color_palette = ColorPalette {
root_color: Color::rgb(1.0, 1.0, 0.5),
natural_color: Color::rgb(1.0, 1.0, 1.0),
enharmonic_colors: vec![Color::rgb(0.5, 0.5, 0.5)],
sharp_colors: vec![
Color::rgb(0.0, 0.0, 1.0),
Color::rgb(0.5, 0.0, 1.0),
Color::rgb(0.0, 0.5, 1.0),
Color::rgb(0.5, 0.5, 1.0),
],
flat_colors: vec![
Color::rgb(0.0, 1.0, 0.0),
Color::rgb(0.5, 1.0, 0.0),
Color::rgb(0.0, 1.0, 0.5),
Color::rgb(0.5, 1.0, 0.5),
],
};

MicrowaveProfile {
num_buffers: 16,
audio_buffers: (14, 15),
globals,
templates,
envelopes,
stages,
color_palette,
}
}

Expand Down
5 changes: 3 additions & 2 deletions microwave/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,11 @@ impl RunOptions {
.unwrap()
});

let virtual_keyboard = VirtualKeyboardResource::new(&scl, self.custom_keyboard);

let profile = MicrowaveProfile::load(&self.profile_location).await?;

let virtual_keyboard =
VirtualKeyboardResource::new(&scl, self.custom_keyboard, &profile.color_palette);

let mut factory = AutomationFactory::new(HashMap::new());

let globals = profile
Expand Down
11 changes: 11 additions & 0 deletions microwave/src/profile.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy::render::color::Color;
use cpal::SampleRate;
use flume::Sender;
use magnetron::{automation::AutomationFactory, envelope::EnvelopeSpec, stage::Stage};
Expand Down Expand Up @@ -31,6 +32,7 @@ pub struct MicrowaveProfile {
pub templates: Vec<FragmentSpec<WaveformAutomatableValue>>,
pub envelopes: Vec<NamedEnvelopeSpec<WaveformAutomatableValue>>,
pub stages: Vec<AudioStageSpec>,
pub color_palette: ColorPalette,
}

impl MicrowaveProfile {
Expand Down Expand Up @@ -111,6 +113,15 @@ impl AudioStageSpec {
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ColorPalette {
pub root_color: Color,
pub natural_color: Color,
pub enharmonic_colors: Vec<Color>,
pub sharp_colors: Vec<Color>,
pub flat_colors: Vec<Color>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct NoAudioInfo;

Expand Down
Loading

0 comments on commit a4847fa

Please sign in to comment.