plays a chord

This commit is contained in:
2026-03-10 10:13:17 -06:00
parent facb9b957e
commit 61352aad41
5 changed files with 400 additions and 69 deletions

View File

@@ -1,78 +1,51 @@
use std::error::Error;
use std::io::{stdin, stdout, Write};
use std::thread::sleep;
use std::time::Duration;
use midir::{MidiOutput, MidiOutputPort};
use itertools::Itertools;
use rustysynth::MidiFile;
use rustysynth::MidiFileSequencer;
use rustysynth::SoundFont;
use rustysynth::Synthesizer;
use rustysynth::SynthesizerSettings;
use std::fs::File;
use std::sync::Arc;
use tinyaudio::prelude::*;
fn main() {
env_logger::init();
match run() {
Ok(_) => (),
Err(err) => println!("Error: {}", err),
}
}
// Load the SoundFont.
let mut sf2 = File::open("FatBoy.sf2").unwrap();
let sound_font = Arc::new(SoundFont::new(&mut sf2).unwrap());
fn run() -> Result<(), Box<dyn Error>> {
let midi_out = MidiOutput::new("My Test Output")?;
// Get an output port (read from console if multiple are available)
let out_ports = midi_out.ports();
let out_port: &MidiOutputPort = match out_ports.len() {
0 => return Err("no output port found".into()),
1 => {
println!(
"Choosing the only available output port: {}",
midi_out.port_name(&out_ports[0]).unwrap()
);
&out_ports[0]
}
_ => {
println!("\nAvailable output ports:");
for (i, p) in out_ports.iter().enumerate() {
println!("{}: {}", i, midi_out.port_name(p).unwrap());
}
print!("Please select output port: ");
stdout().flush()?;
let mut input = String::new();
stdin().read_line(&mut input)?;
out_ports
.get(input.trim().parse::<usize>()?)
.ok_or("invalid output port selected")?
}
// Setup the audio output.
let params = OutputDeviceParameters {
channels_count: 2,
sample_rate: 44100,
channel_sample_count: 4410,
};
println!("\nOpening connection");
let mut conn_out = midi_out.connect(out_port, "midir-test")?;
println!("Connection open. Listen!");
{
// Define a new scope in which the closure `play_note` borrows conn_out, so it can be called easily
let mut play_note = |note: u8, duration: u64| {
const NOTE_ON_MSG: u8 = 0x90;
const NOTE_OFF_MSG: u8 = 0x80;
const VELOCITY: u8 = 0x64;
// We're ignoring errors in here
let _ = conn_out.send(&[NOTE_ON_MSG, note, VELOCITY]);
sleep(Duration::from_millis(duration * 150));
let _ = conn_out.send(&[NOTE_OFF_MSG, note, VELOCITY]);
};
// Create the synthesizer.
let settings = SynthesizerSettings::new(params.sample_rate as i32);
let mut synthesizer = Synthesizer::new(&sound_font, &settings).unwrap();
sleep(Duration::from_millis(4 * 150));
// Play some notes (middle C, E, G).
synthesizer.note_on(0, 60, 100);
synthesizer.note_on(0, 64, 100);
synthesizer.note_on(0, 67, 100);
play_note(66, 4);
play_note(65, 3);
play_note(63, 1);
play_note(61, 6);
play_note(59, 2);
play_note(58, 4);
play_note(56, 4);
play_note(54, 4);
}
sleep(Duration::from_millis(150));
println!("\nClosing connection");
// This is optional, the connection would automatically be closed as soon as it goes out of scope
conn_out.close();
println!("Connection closed");
Ok(())
// The output buffer
let sample_count = (params.channel_sample_count) as usize;
let mut left: Vec<f32> = vec![0_f32; sample_count];
let mut right: Vec<f32> = vec![0_f32; sample_count];
// Start the audio output.
let _device = run_output_device(params, {
move |data| {
synthesizer.render(&mut left[..], &mut right[..]);
for (i, value) in left.iter().interleave(right.iter()).enumerate() {
data[i] = *value;
}
}
})
.unwrap();
// Wait for 10 seconds.
std::thread::sleep(std::time::Duration::from_secs(10));
}