Skip to content

Commit

Permalink
Merge pull request #2 from ykong1337/dev
Browse files Browse the repository at this point in the history
feat: 实现手动调节偏移和自定义音轨
  • Loading branch information
yk0n9 authored Aug 21, 2023
2 parents d20ff83 + 0bb0d63 commit 90dd5be
Show file tree
Hide file tree
Showing 9 changed files with 343 additions and 310 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lyred"
version = "1.1.0"
version = "1.2.0"
edition = "2021"
build = "build.rs"

Expand Down
18 changes: 10 additions & 8 deletions src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use crate::midi::{Event, Midi};
use rfd::MessageButtons;
use std::fs::File;
use std::io::Write;

use rfd::MessageButtons;

use crate::midi::{Event, Midi};
use crate::POOL;

impl Midi {
pub fn convert_from_midi(&self, file_name: String) {
let mid = self.clone();
self.pool.spawn(move || {
let mut key = File::create(format!("{}.txt", file_name.to_string())).unwrap();
let mut key_phone = File::create(format!("phone-{}.txt", file_name)).unwrap();
pub fn convert_from_midi(self, name: String) {
POOL.spawn(move || {
let mut key = File::create(format!("{}.txt", &name)).unwrap();
let mut key_phone = File::create(format!("phone-{}.txt", &name)).unwrap();
let mut res = String::new();
let events = mid.events.lock().unwrap();
let events = self.events.lock().unwrap();
let mut iter = events.iter().peekable();
let mut count = 0;
while let Some(e) = iter.next() {
Expand Down
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
use std::sync::Arc;
use std::sync::atomic::AtomicBool;

use once_cell::sync::Lazy;
use rayon::{ThreadPool, ThreadPoolBuilder};

pub mod convert;
pub mod font;
pub mod maps;
pub mod midi;
pub mod ui;

pub static SPACE: AtomicBool = AtomicBool::new(false);
pub static CTRL: AtomicBool = AtomicBool::new(false);
pub static BACK: AtomicBool = AtomicBool::new(false);

pub static POOL: Lazy<Arc<ThreadPool>> = Lazy::new(|| {
let pool = ThreadPoolBuilder::new()
.num_threads(2)
.build()
.unwrap();
Arc::new(pool)
});
39 changes: 14 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![windows_subsystem = "windows"]

use eframe::egui::Vec2;
use eframe::{IconData, NativeOptions};
use lyred::midi::{BACK, CTRL, SPACE};
use lyred::ui::Play;
use rdev::{listen, Event, EventType, Key};
use std::sync::atomic::Ordering;
use eframe::{IconData, NativeOptions};
use eframe::egui::Vec2;
use rdev::{Event, EventType};
use lyred::{BACK, CTRL, POOL, SPACE};
use lyred::ui::play::Play;

fn main() {
run();
Expand All @@ -18,31 +18,20 @@ fn run() {
initial_window_size: Some(Vec2::new(400.0, 500.0)),
..NativeOptions::default()
};
let icon_bytes: &[u8] = include_bytes!("../resources/lyre.ico");
let icon_buffer = image::load_from_memory(icon_bytes).unwrap();
let icon = icon_buffer.to_rgba8();
let (icon_width, icon_height) = icon.dimensions();
let pixels = icon.into_vec();
let icon_data = IconData {
rgba: pixels,
width: icon_width,
height: icon_height,
};
let icon_data = IconData::try_from_png_bytes(include_bytes!("../resources/lyre.ico")).unwrap();
options.icon_data = Some(icon_data);
rayon::spawn(move || {
listen(callback).unwrap();
});
POOL.spawn(move || rdev::listen(callback).unwrap());
eframe::run_native("Lyred", options, Box::new(|cc| Box::new(Play::new(cc)))).unwrap();
}

fn callback(event: Event) {
match event.event_type {
EventType::KeyPress(Key::Space) => SPACE.store(true, Ordering::Relaxed),
EventType::KeyRelease(Key::Space) => SPACE.store(false, Ordering::Relaxed),
EventType::KeyPress(Key::ControlLeft) => CTRL.store(true, Ordering::Relaxed),
EventType::KeyRelease(Key::ControlLeft) => CTRL.store(false, Ordering::Relaxed),
EventType::KeyPress(Key::Backspace) => BACK.store(true, Ordering::Relaxed),
EventType::KeyRelease(Key::Backspace) => BACK.store(false, Ordering::Relaxed),
EventType::KeyPress(rdev::Key::Space) => SPACE.store(true, Ordering::Relaxed),
EventType::KeyRelease(rdev::Key::Space) => SPACE.store(false, Ordering::Relaxed),
EventType::KeyPress(rdev::Key::ControlLeft) => CTRL.store(true, Ordering::Relaxed),
EventType::KeyRelease(rdev::Key::ControlLeft) => CTRL.store(false, Ordering::Relaxed),
EventType::KeyPress(rdev::Key::Backspace) => BACK.store(true, Ordering::Relaxed),
EventType::KeyRelease(rdev::Key::Backspace) => BACK.store(false, Ordering::Relaxed),
_ => {}
}
}
}
2 changes: 1 addition & 1 deletion src/maps.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rdev::{EventType, Key, simulate};

#[inline(always)]
pub fn send(key: Key) {
fn send(key: Key) {
let _ = simulate(&EventType::KeyPress(key));
let _ = simulate(&EventType::KeyRelease(key));
}
Expand Down
Loading

0 comments on commit 90dd5be

Please sign in to comment.