-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0be59d3
commit 1471a6e
Showing
10 changed files
with
213 additions
and
14 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "soundfont" | ||
version = "1.0.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
dlal-component-base = { path = "../base" } | ||
rustysynth = "1.3.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use dlal_component_base::{component, serde_json, Body, CmdResult}; | ||
|
||
use rustysynth; | ||
|
||
use std::fs::File; | ||
use std::sync::Arc; | ||
|
||
component!( | ||
{"in": ["midi"], "out": ["audio"]}, | ||
[ | ||
"run_size", | ||
"sample_rate", | ||
"uni", | ||
"check_audio", | ||
{"name": "field_helpers", "fields": ["soundfont_path"], "kinds": ["r", "json"]}, | ||
], | ||
{ | ||
soundfont_path: String, | ||
synth: Option<rustysynth::Synthesizer>, | ||
buffer: Vec<f32>, | ||
}, | ||
{ | ||
"soundfont_load": { | ||
"args": ["path"], | ||
}, | ||
}, | ||
); | ||
|
||
impl ComponentTrait for Component { | ||
fn run(&mut self) { | ||
let synth = match self.synth.as_mut() { | ||
Some(synth) => synth, | ||
_ => return, | ||
}; | ||
let audio = match &self.output { | ||
Some(output) => output.audio(self.run_size).unwrap(), | ||
None => return, | ||
}; | ||
synth.render(audio, &mut self.buffer); | ||
} | ||
|
||
fn midi(&mut self, msg: &[u8]) { | ||
let synth = match self.synth.as_mut() { | ||
Some(synth) => synth, | ||
_ => return, | ||
}; | ||
if msg.len() < 3 { | ||
return; | ||
} | ||
synth.process_midi_message( | ||
(msg[0] & 0x0f) as i32, | ||
(msg[0] & 0xf0) as i32, | ||
msg[1] as i32, | ||
msg[2] as i32, | ||
); | ||
} | ||
|
||
fn join(&mut self, _body: serde_json::Value) -> CmdResult { | ||
self.buffer.resize(self.run_size, 0.0); | ||
if !self.soundfont_path.is_empty() { | ||
self.soundfont_load() | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
|
||
fn from_json_cmd(&mut self, body: serde_json::Value) -> CmdResult { | ||
field_helper_from_json!(self, body); | ||
if !self.soundfont_path.is_empty() { | ||
self.soundfont_load() | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
} | ||
|
||
impl Component { | ||
fn soundfont_load_cmd(&mut self, body: serde_json::Value) -> CmdResult { | ||
self.soundfont_path = body.arg(0)?; | ||
self.soundfont_load()?; | ||
Ok(None) | ||
} | ||
|
||
fn soundfont_load(&mut self) -> CmdResult { | ||
let mut file = File::open(&self.soundfont_path)?; | ||
let soundfont = Arc::new(rustysynth::SoundFont::new(&mut file)?); | ||
let mut settings = rustysynth::SynthesizerSettings::new(self.sample_rate as i32); | ||
settings.block_size = self.run_size; | ||
self.synth = Some(rustysynth::Synthesizer::new(&soundfont, &settings)?); | ||
Ok(None) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from ._component import Component | ||
|
||
from ._utils import ASSETS_DIR | ||
|
||
import math | ||
import os | ||
|
||
class Soundfont(Component): | ||
def __init__(self, **kwargs): | ||
Component.__init__(self, 'soundfont', **kwargs) | ||
from ._skeleton import Immediate | ||
with Immediate(): | ||
self.soundfont_load(ASSETS_DIR / 'soundfont/32MbGMStereo.sf2') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import dlal | ||
|
||
import argparse | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--midi-path', '-m') | ||
args = parser.parse_args() | ||
|
||
audio = dlal.Audio(driver=True) | ||
comm = dlal.Comm() | ||
liner = dlal.Liner() | ||
soundfont = dlal.Soundfont() | ||
tape = dlal.Tape() | ||
|
||
for i in range(16): | ||
liner.connect(soundfont) | ||
dlal.connect( | ||
soundfont, | ||
[audio, tape], | ||
) | ||
|
||
duration = None | ||
if args.midi_path: | ||
liner.load(args.midi_path, immediate=True) | ||
duration = liner.duration() | ||
|
||
dlal.typical_setup(duration=duration) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters