使用 Rust 製作 MIDI 鋼琴程序
本文讓我們使用 Rust 實現一個簡單的 MIDI Piano 應用程序。
首先,使用以下命令創建一個 Rust 新項目:
cargo new midi-rs
然後在 Cargo.toml 文件中加入依賴項:
[dependencies]
eframe = "0.27.2"
itertools = "0.12.1"
phf = { version = "0.11", features = ["macros"] }
rustysynth = "1.3.1"
tinyaudio = "0.1.3"
-
**eframe:**EGUI 框架——編寫可以編譯爲 web 或本機的 GUI 應用程序
-
**itertools:**擴展的迭代器適配器、函數和宏。
-
**phf:**使用完美的散列函數在編譯時生成高效的查找表。
-
**rustysynth:**用純 Rust 編寫的 MIDI 音色庫合成器
-
**tinyaudio:**是一個跨平臺,易於使用,底層的音頻輸出庫。
這個應用程序將打開一個接收鍵盤事件的 egui 窗口,這些事件被髮送到 rustysynth 庫作爲 midi 音符並通過 tinyaudio 庫輸出聲音。
首先,在 src/main.rs 文件中引入這些庫:
use eframe::egui;
use itertools::Itertools;
use phf::{phf_map, Map};
use rustysynth::{SoundFont, Synthesizer, SynthesizerSettings};
use std::{
fs::File,
sync::{Arc, Mutex},
};
use tinyaudio::prelude::*;
接下來,定義靜態變量和常量:
const OUTPUT_PARAMS: OutputDeviceParameters = OutputDeviceParameters {
channels_count: 2,
sample_rate: 44100,
channel_sample_count: 441, // 樣本的最大長度
};
#[derive(Debug)]
pub struct MidiNote {
pub note: i32,
pub velocity: i32,
}
pub static NOTE_KEY_MAP: Map<&'static str, MidiNote> = phf_map! {
"A" => MidiNote {
note: 60,
velocity: 100,
},
"S" => MidiNote {
note: 62,
velocity: 100,
},
"D" => MidiNote {
note: 64,
velocity: 100,
},
"F" => MidiNote {
note: 65,
velocity: 100,
},
"G" => MidiNote {
note: 67,
velocity: 100,
},
};
OUTPUT_PARAMS 是 tinyaudio 的參數。MidiNote 持有 MIDI 音符的音符數和速度,用 rustysynth 播放它。它們被保存在一個靜態映射中,使用一個由按鍵值索引的 phf_map! 宏。
讓我們定義 SynthApp 結構體,它是一個 egui 應用程序。它有合成器對象和方法來執行音符的開 / 關,在 eframe::App 的 update 方法中處理鍵盤事件。
struct SynthApp {
synthesizer: Arc<Mutex<Synthesizer>>,
midi_channel: i32,
}
impl SynthApp {
fn note_on(&mut self, key: &str) {
let note = match NOTE_KEY_MAP.get(key) {
Some(note) => note,
None => return,
};
self.synthesizer
.lock()
.unwrap()
.note_on(self.midi_channel, note.note, note.velocity)
}
fn note_off(&mut self, key: &str) {
let note = match NOTE_KEY_MAP.get(key) {
Some(note) => note,
None => return,
};
self.synthesizer
.lock()
.unwrap()
.note_off(self.midi_channel, note.note);
}
}
impl eframe::App for SynthApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
ctx.input(|i| {
for key_str in NOTE_KEY_MAP.keys() {
if let Some(key) = egui::Key::from_name(key_str) {
if i.key_pressed(key) {
self.note_on(key_str);
} else if i.key_released(key) {
self.note_off(key_str);
}
}
}
});
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("My egui Application");
ui.label(format!("Midi channel {}", self.midi_channel));
});
}
}
在互聯網上有很多不錯的音色庫,我們使用 TimGM6mb.sf2,
可以在以下地址下載:
https://github.com/craffel/pretty-midi/blob/main/pretty_midi/TimGM6mb.sf2
將下載好的文件放入到項目的根目錄下。
最後,我們編寫 main 函數,合成器保存在 Arc<Mutex<…>> 中,以便 run_output_device 和 SynthApp 都可以訪問它。
fn main() -> Result<(), eframe::Error> {
// 加載音色庫
let mut sf2 = File::open("TimGM6mb.sf2").unwrap();
let sound_font = Arc::new(SoundFont::new(&mut sf2).unwrap());
// 創建MIDI文件序列器
let settings = SynthesizerSettings::new(OUTPUT_PARAMS.sample_rate as i32);
let synthesizer = Arc::new(Mutex::new(
Synthesizer::new(&sound_font, &settings).unwrap(),
));
// 運行輸出設備
let synth_c = synthesizer.clone();
let mut left: Vec<f32> = vec![0_f32; OUTPUT_PARAMS.channel_sample_count];
let mut right: Vec<f32> = vec![0_f32; OUTPUT_PARAMS.channel_sample_count];
let _device = run_output_device(OUTPUT_PARAMS, move |data| {
synth_c
.lock()
.unwrap()
.render(&mut left[..], &mut right[..]);
for (i, value) in left.iter().interleave(right.iter()).enumerate() {
data[i] = *value;
}
})
.unwrap();
// eframe
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([640.0, 480.0]),
..Default::default()
};
eframe::run_native(
"My egui App",
options,
Box::new(|_cc| {
Box::new(SynthApp {
synthesizer,
midi_channel: 0,
})
}),
)
}
執行 cargo run,結果如圖:
一旦出現窗口,按鍵盤的 ASDFG 鍵,就會播放音符。
爲了進一步探索,你可以通過添加一些 UI 和樂器來嘗試 egui 和 rustysynth 的各種功能。
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/URJrSSVntu-8NiZSkQIhUw