Skip to content

Commit

Permalink
some small updates soo it's easier to hack
Browse files Browse the repository at this point in the history
  • Loading branch information
khanghugo committed Feb 27, 2023
1 parent c9ae635 commit 3c54d02
Show file tree
Hide file tree
Showing 23 changed files with 110,032 additions and 37,863 deletions.
Binary file added examples/.mscbackup/.chopin.mscz~
Binary file not shown.
Binary file added examples/.mscbackup/.megaman_better.mscz~
Binary file not shown.
Binary file added examples/.mscbackup/.ninja_gaiden.mscz~
Binary file not shown.
Binary file added examples/chopin.mid
Binary file not shown.
Binary file added examples/chopin.mscz
Binary file not shown.
Binary file added examples/chopin_cut.mid
Binary file not shown.
Binary file added examples/chopin_cut2.mid
Binary file not shown.
Binary file added examples/megaman.mid
Binary file not shown.
Binary file added examples/megaman.mscz
Binary file not shown.
Binary file added examples/megaman_better.mid
Binary file not shown.
Binary file added examples/megaman_better.mscz
Binary file not shown.
Binary file added examples/megaman_better.mscz.autosave
Binary file not shown.
Binary file added examples/megaman_better_cut.mid
Binary file not shown.
Binary file added examples/megaman_cut.mid
Binary file not shown.
Binary file added examples/ninja_gaiden.mid
Binary file not shown.
Binary file added examples/ninja_gaiden.mscz
Binary file not shown.
Binary file added examples/ninja_gaiden2.mid
Binary file not shown.
Binary file added examples/ninja_gaiden3.mid
Binary file not shown.
Binary file added examples/undertale.mid
Binary file not shown.
Binary file added examples/undertale_dbg.mid
Binary file not shown.
Binary file not shown.
147,660 changes: 109,882 additions & 37,778 deletions foo.txt

Large diffs are not rendered by default.

235 changes: 150 additions & 85 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,47 +30,68 @@ fn frametime_tick_to_repeat(tempo: u32, frametime: f64, tick: u32) -> u32 {
(midi_tick_to_duration(tempo, tick) / frametime) as u32
}

#[derive(Clone, Copy, PartialEq)]
enum Command {
None,
/// `impulse 101`
Flashlight,
/// `slot3; slot3; slot3;...`
SwitchScroll(u8),
/// `slot3; slot2; slot3;...`
SwitchGroup(u8),
SwitchGroup,
/// `+use; wait; -use`
Use(bool),
Use,
/// Ducktap in combination of `sv_gravity 11550`, `sv_gravity 11650`, or bogus with `god`
Ducktap,
///
Nice,
Nice2,
Nice3,
Stopsound,
Attack1,
}

static mut switch: u8 = 1;
fn format_bulk(frametime: f64, repeat: u32, command: Command) -> String {
format!(
"----------|------|------|{}|-|-|{}|{}\n",
frametime,
repeat,
match command {
Command::None => "",
Command::Flashlight => "impulse 100",
Command::Nice => "speak accelerating",
Command::Nice2 => "speak cleanup",
Command::Nice3 => "speak squad",
Command::SwitchScroll(num) => match num {
0 => "slot0",
1 => "slot1",
2 => "slot2",
3 => "slot3",
4 => "slot4",
5 => "slot5",
if command == Command::Ducktap {
format!("-----d----|------|------|{}|-|-|{}\n", frametime, repeat)
} else if command == Command::Use {
format!("----------|------|--u---|{}|-|-|{}\n", frametime, repeat)
} else {
format!(
"----------|------|------|{}|-|-|{}|{}\n",
frametime,
repeat,
match command {
Command::None => "",
Command::Flashlight => "impulse 100",
Command::Nice => "speak player/sprayer",
Command::Nice2 => "speak \"common/bodysplat(v60)\"",
Command::Nice3 => "speak squad",
Command::SwitchScroll(num) => match num {
0 => "slot0",
1 => "slot1",
2 => "slot2",
3 => "slot3",
4 => "slot4",
5 => "slot5",
_ => "",
},
Command::SwitchGroup => {
unsafe {
switch = switch % 2 + 1;
match switch {
1 => "slot1",
2 => "slot2",
_ => "slot0",
}
}
}
Command::Stopsound => "stopsound",
Command::Attack1 => "+attack; wait; -attack",
_ => "",
},
_ => "",
}
)
}
)
}
}

fn print_events(smf: &Smf) {
Expand All @@ -93,6 +114,7 @@ struct TrackSegment {
remainder: f64,
// stores the frametime difference between current note and the next one
quantize_remainder: f64,
new_beat: bool,
}

impl TrackSegment {
Expand All @@ -105,24 +127,37 @@ impl TrackSegment {
tick: 0,
remainder: 0.,
quantize_remainder: 0.,
new_beat: false,
}
}
}

fn main() {
let smf = Smf::parse(include_bytes!("../examples/1003_fugue_split.mid")).unwrap();
// EDIT HERE
let smf = Smf::parse(include_bytes!("../examples/undertale_death_by_glamour_simplified.mid")).unwrap();
const ZEROFRAMETIME: f64 = 0.0000000000000000001;
let sounds: Vec<Command> = vec![
Command::Nice2,
Command::SwitchScroll(2),
Command::Flashlight,
Command::Use,
];
let legato = true; // play notes in succession or have like 0.002s downtime to enounciate, true = saves more line
let mut file = File::create("foo.txt").unwrap();

// MAYBE NO NEED TO GO FROM HERE
const IJUSTWANTTOREAD: bool = false;
let mut tempo = 0;
let mut result: Vec<String> = Vec::new();
// print_events(&smf);
print_events(&smf);

let mut curr_track = 0;
let mut track_segments: Vec<TrackSegment> = Vec::new();
const ZEROFRAMETIME: f64 = 0.0000000000000000001;
for _ in 0..smf.tracks.len() {
track_segments.push(TrackSegment::new());
}

while !track_segments.iter().fold(true, |acc, e| acc && e.end) {
while !track_segments.iter().fold(true, |acc, e| acc && e.end) && !IJUSTWANTTOREAD {
let curr = &mut track_segments[curr_track];

// subarray of read_idx to continue reading from previous break
Expand All @@ -133,6 +168,7 @@ fn main() {
}

curr.read_idx += 1;
curr.new_beat = true;

match event.kind {
TrackEventKind::Meta(message) => match message {
Expand All @@ -157,7 +193,7 @@ fn main() {
_ => (),
}

if event.delta > 0 {
if event.delta > (0 + legato as u32) {
// Read until there is delta, which now starts to break into hltas for the segment
curr.tick = u32::from(event.delta);
curr.remainder = midi_tick_to_duration(tempo, curr.tick);
Expand All @@ -179,43 +215,58 @@ fn main() {
if track_segments.iter().fold(true, |acc, e| acc && e.end) {
break;
}

// println!("write");
let mut alt = 1;
// Write to hltas
// If there is one track is done analyzed, loop will break.
// If there is one track is done quantized, loop will break.
// Then continue reading from the previous left off index.
while track_segments
.iter()
.filter(|e| !e.end && e.remainder <= 0. + f64::EPSILON)
.filter(|e| !e.end && e.remainder <= 0.)
.count()
== 0
{
// For notes octaves higher and beginning of each duration
// See how many tracks will be worked on.
let work_count = track_segments
.iter()
.filter(|e| !e.end && e.vel == 0)
.count();

// Attempts to add note
// if work_count != 1 {
track_segments
.clone()
.iter()
.enumerate()
.filter(|(_, e)| !e.end && e.key != 0 && e.quantize_remainder <= 0. + f64::EPSILON)
.filter(|(_, e)| !e.end && e.key != 0 && e.quantize_remainder <= 0.)
.for_each(|(i, _)| {
if track_segments[i].vel == 0 {
// Because of MIDI, info in an event is applied after delta
// vel > 0 for that event means it is a rest.
result.push(format_bulk(ZEROFRAMETIME, 1, {
match i {
0 => Command::Nice,
1 => Command::SwitchScroll(2),
2 => Command::Nice2,
3 => Command::Nice3,
_ => Command::None,
// write!(file, "{}", format_bulk(ZEROFRAMETIME, 1, Command::Stopsound));
if !track_segments[i].new_beat && sounds[i] == Command::Ducktap {
} else {
if sounds[i] == Command::Attack1 {
write!(file, "{}", format_bulk(ZEROFRAMETIME, 1, Command::Attack1));
}
}));
write!(file, "{}", format_bulk(ZEROFRAMETIME, 1, sounds[i]));
}

track_segments[i].quantize_remainder =
pitch_to_frametime(track_segments[i].key) - ZEROFRAMETIME;
track_segments[i].new_beat = false;
} else {
write!(
file,
"{}",
format_bulk(ZEROFRAMETIME, 1, Command::Stopsound)
);
track_segments[i].quantize_remainder = track_segments[i].remainder;
}
});

// Find the track with the current lowest remainder, play the note with 0ms frame, then wait for that remainder time.
// This loops until the tick of any note is fully decreased, which now prompt the reader to read said track.
// }
// After adding a note, it will start finding the note with lowest frame time and play that sound.
// This loops until the one of any note is fully played, which then prompts the reader to read said track.
// Safety: loop condition makes sure that `unwrap()` always have a value.
let lowest_index = track_segments
.iter()
Expand All @@ -226,47 +277,59 @@ fn main() {
.nth(0)
.unwrap()
.0;
let work_count = track_segments
.iter()
.filter(|e| !e.end && e.vel == 0)
.count();
let quantize_remainder = track_segments[lowest_index].quantize_remainder;
let mut quantize_remainder = track_segments[lowest_index].quantize_remainder;
let frametime = pitch_to_frametime(track_segments[lowest_index].key);

// // If this is either monophonic or just a really long note, hltas line count will be reasonably reduced
// if work_count == 1 {
// // println!("this prints {} {}", frametime, work_track.key);
// result.push(format_bulk(
// frametime,
// frametime_tick_to_repeat(
// tempo,
// frametime,
// track_segments[lowest_index].tick + 1,
// ), // add 1 tick from meta 1-tick
// Command::Nice,
// ));

// // track_segments = track_segments
// // .iter()
// // .map(|e| TrackSegment {
// // remainder: e.remainder - quantize_remainder,
// // quantize_remainder: e.quantize_remainder - quantize_remainder,
// // ..*e
// // })
// // .collect_vec();

// track_segments[lowest_index].remainder = 0.;
// track_segments[lowest_index].quantize_remainder = 0.;
// If no notes are played, skip every notes.
// if track_segments
// .iter()
// .fold(true, |acc, e| !e.end && e.vel > 0 && acc)
// {
// // println!("{:?}", track_segments);
// result.push("this bulk no playing\n".to_string());
// result.push(format_bulk(frametime, frametime_tick_to_repeat(tempo, frametime, track_segments[lowest_index].tick), Command::None));
// track_segments = track_segments
// .iter()
// .map(|e| TrackSegment {
// remainder: e.remainder - quantize_remainder,
// quantize_remainder: e.quantize_remainder - quantize_remainder,
// ..*e
// })
// .collect_vec();

// break;
// }

result.push(format_bulk(
track_segments[lowest_index].quantize_remainder,
1,
Command::None,
));
// if work_count == 1 {
// if quantize_remainder > track_segments[lowest_index].remainder
// || quantize_remainder == 0.
// {
// quantize_remainder = track_segments[lowest_index].remainder;
// }

// println!("this prints {:?} {}", track_segments, quantize_remainder);
// // result.push("this bulk one playing\n".to_string());
// write!(
// file,
// "{}",
// format_bulk(
// frametime,
// (quantize_remainder / frametime) as u32,
// sounds[lowest_index],
// )
// );
// } else
{
write!(
file,
"{}",
format_bulk(
track_segments[lowest_index].quantize_remainder,
1,
Command::None,
)
);
}
// update
track_segments = track_segments
.iter()
Expand All @@ -276,13 +339,15 @@ fn main() {
..*e
})
.collect_vec();

// if track_segments[lowest_index].quantize_remainder <= 0. {
// track_segments[lowest_index].quantize_remainder = 0.;
// }
}
}

let mut file = File::create("foo.txt").unwrap();

for i in result {
// print!("{}", i)
write!(file, "{}", i);
}
// for i in result {
// // print!("{}", i)
// write!(file, "{}", i);
// }
}

0 comments on commit 3c54d02

Please sign in to comment.