Skip to content

Commit

Permalink
Merge and sort events in waterfall
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Sep 24, 2023
1 parent 66bccc0 commit e6034c8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
2 changes: 1 addition & 1 deletion neothesia-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Recorder {

let mut waterfall = WaterfallRenderer::new(
&gpu,
midi.tracks.clone(),
&midi.tracks,
&config,
&transform_uniform,
keyboard_layout,
Expand Down
76 changes: 39 additions & 37 deletions neothesia-core/src/render/waterfall/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::sync::Arc;

use crate::config::Config;
use crate::TransformUniform;
use crate::Uniform;
use midi_file::MidiNote;
use midi_file::MidiTrack;
use wgpu_jumpstart::Color;
use wgpu_jumpstart::Gpu;
Expand All @@ -12,23 +11,28 @@ use pipeline::{NoteInstance, WaterfallPipeline};

pub struct WaterfallRenderer {
notes_pipeline: WaterfallPipeline,
tracks: Arc<[MidiTrack]>,
notes: Box<[MidiNote]>,
}

impl WaterfallRenderer {
pub fn new(
gpu: &Gpu,
tracks: Arc<[MidiTrack]>,
tracks: &[MidiTrack],
config: &Config,
transform_uniform: &Uniform<TransformUniform>,
layout: piano_math::KeyboardLayout,
) -> Self {
let notes_count = tracks.iter().fold(0, |v, t| v + t.notes.len());

let notes_pipeline = WaterfallPipeline::new(gpu, transform_uniform, notes_count);
let mut notes: Vec<_> = tracks
.iter()
.flat_map(|track| track.notes.iter().cloned())
.collect();
// We want to render newer notes on top of old notes
notes.sort_unstable_by_key(|note| note.start);

let notes_pipeline = WaterfallPipeline::new(gpu, transform_uniform, notes.len());
let mut notes = Self {
notes_pipeline,
tracks,
notes: notes.into(),
};
notes
.notes_pipeline
Expand All @@ -52,36 +56,34 @@ impl WaterfallRenderer {
let mut instances = Vec::new();

let mut longer_than_range = false;
for track in self.tracks.iter() {
for note in track.notes.iter() {
if layout.range.contains(note.note) && note.channel != 9 {
let key = &layout.keys[note.note as usize - range_start];

let color_schema = &config.color_schema;

let color = &color_schema[note.track_color_id % color_schema.len()];
let color = if key.kind().is_sharp() {
color.dark
} else {
color.base
};
let color: Color = color.into();

let h = if note.duration.as_secs_f32() >= 0.1 {
note.duration.as_secs_f32()
} else {
0.1
};

instances.push(NoteInstance {
position: [key.x(), note.start.as_secs_f32()],
size: [key.width() - 1.0, h - 0.01], // h - 0.01 to make a litle gap bettwen successive notes
color: color.into_linear_rgb(),
radius: key.width() * 0.2,
});
for note in self.notes.iter() {
if layout.range.contains(note.note) && note.channel != 9 {
let key = &layout.keys[note.note as usize - range_start];

let color_schema = &config.color_schema;

let color = &color_schema[note.track_color_id % color_schema.len()];
let color = if key.kind().is_sharp() {
color.dark
} else {
color.base
};
let color: Color = color.into();

let h = if note.duration.as_secs_f32() >= 0.1 {
note.duration.as_secs_f32()
} else {
longer_than_range = true;
}
0.1
};

instances.push(NoteInstance {
position: [key.x(), note.start.as_secs_f32()],
size: [key.width() - 1.0, h - 0.01], // h - 0.01 to make a litle gap bettwen successive notes
color: color.into_linear_rgb(),
radius: key.width() * 0.2,
});
} else {
longer_than_range = true;
}
}

Expand Down
2 changes: 1 addition & 1 deletion neothesia/src/scene/playing_scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl PlayingScene {

let mut notes = WaterfallRenderer::new(
&target.gpu,
midi_file.tracks.clone(),
&midi_file.tracks,
&target.config,
&target.transform_uniform,
keyboard_layout.clone(),
Expand Down

0 comments on commit e6034c8

Please sign in to comment.