rustysynth icon indicating copy to clipboard operation
rustysynth copied to clipboard

The example code does not work

Open Yuconium opened this issue 1 year ago • 2 comments

Hey, this example code does not work on my computer: `use rustysynth::MidiFile; use rustysynth::MidiFileSequencer; use rustysynth::SoundFont; use rustysynth::Synthesizer; use rustysynth::SynthesizerSettings; use std::fs::File; use std::io::Write; use std::sync::Arc;

fn main() { // Load the SoundFont. let mut sf2 = File::open("Grand Piano.sf2").unwrap(); let sound_font = Arc::new(SoundFont::new(&mut sf2).unwrap());

// Create the synthesizer.
let settings = SynthesizerSettings::new(44100);
let mut synthesizer = Synthesizer::new(&sound_font, &settings).unwrap();

// 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);

// The output buffer (3 seconds).
let sample_count = (3 * settings.sample_rate) as usize;
let mut left: Vec<f32> = vec![0_f32; sample_count];
let mut right: Vec<f32> = vec![0_f32; sample_count];

// Render the waveform.
synthesizer.render(&mut left[..], &mut right[..]);

} `

it runs, but it does not play any sounds or does it render anything.

OS: Ubuntu 24.04.1 LTS x86_64 Kernel: 6.8.0-45-generic CPU: Intel i7-8565U (8) @ 1.800GHz GPU: NVIDIA GeForce MX230

Yuconium avatar Sep 29 '24 17:09 Yuconium

The render method generates a PCM waveform in memory. It does not play any sound or display anything on the screen.

RustySynth itself does not have the ability to play sound from speakers. To make the sound audible, export the generated waveform as an audio file (e.g., WAV file) or pass it to some audio driver. Try the TinyAudio example (the 3rd example) if you want to hear the sound in real-time.

sinshu avatar Sep 30 '24 00:09 sinshu

If you use Rodio for audio, check out my implementation for reference:

~~https://github.com/sevonj/sfontplayer/blob/master/src/audio/midisource.rs~~

Edit: the file has moved. Here's a permalink: https://github.com/sevonj/sfontplayer/blob/8049ec5c73ff8b53834062e496431d5a0af2594b/src/player/audio/midisource.rs

sevonj avatar Sep 30 '24 11:09 sevonj