forked from webraa/domik
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added prelaminar MidiAudio and dependent
- Loading branch information
1 parent
476e8b4
commit a21fa26
Showing
10 changed files
with
647 additions
and
18 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
mod log_view; | ||
mod root_app; | ||
mod midi_audio; | ||
mod midi_lib; | ||
use root_app::RootApp; | ||
|
||
mod base_domik_view; | ||
|
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,36 @@ | ||
use tinyaudio::prelude::OutputDeviceParameters; | ||
|
||
use crate::raadbg::log; | ||
|
||
pub struct AudioDeviceParameters { | ||
pub sample_rate: usize, | ||
pub block_size: usize, | ||
pub blocks_count: usize, | ||
} | ||
|
||
impl AudioDeviceParameters { | ||
pub fn new() -> Self { | ||
Self { | ||
sample_rate: 44100, | ||
block_size: 441, | ||
blocks_count: 8 | ||
} | ||
} | ||
pub fn get_output_device_parameters(&self) -> OutputDeviceParameters { | ||
OutputDeviceParameters{ | ||
sample_rate: self.sample_rate, | ||
channels_count: 2, | ||
channel_sample_count: self.block_size * self.blocks_count | ||
} | ||
} | ||
pub fn get_tick_time(&self) -> f32 { | ||
let res = 2. * (self.block_size as f32) / (self.sample_rate as f32); | ||
log::simple( format!("tick time = {res}").as_str() ); | ||
return res; | ||
} | ||
} | ||
impl Default for AudioDeviceParameters { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} |
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,67 @@ | ||
use std::sync::{Arc,Mutex}; | ||
use crate::raadbg::log; | ||
use super::super::midi_lib::{MidiMessage,MidiReceiver,MidiSequence}; | ||
|
||
|
||
pub trait SoundRender: MidiReceiver + Sync + Send { | ||
fn render(&mut self, left: &mut [f32], right: &mut [f32]); | ||
fn get_as_midi_receiver(&mut self) -> &mut dyn MidiReceiver; | ||
} | ||
|
||
pub(crate) struct RenderHolder { | ||
test_seq: MidiSequence, | ||
pub(crate) tick_time: f32, | ||
pub(crate) sound_render: Option< Arc<Mutex<dyn SoundRender>> >, | ||
} | ||
impl RenderHolder { | ||
pub fn new_arc_mutex() -> Arc<Mutex<Self>> { | ||
Arc::new(Mutex::new( Self::new() )) | ||
} | ||
pub fn new() -> Self { | ||
let mut seq = MidiSequence::new(); | ||
seq.push( 0.0, &MidiMessage::NoteOn( 1,90,80) ); | ||
seq.push( 0.5, &MidiMessage::NoteOff(1,90,80) ); | ||
seq.push( 0., &MidiMessage::NoteOn( 1,91,80) ); | ||
seq.push( 0.5, &MidiMessage::NoteOff(1,91,80) ); | ||
seq.push( 0., &MidiMessage::NoteOn( 1,92,80) ); | ||
seq.push( 1., &MidiMessage::NoteOff(1,92,80) ); | ||
seq.push( 1., &MidiMessage::NoteOff(1,92,80) ); | ||
let res = Self{ | ||
test_seq: seq, | ||
tick_time: 0., | ||
sound_render: None | ||
}; | ||
log::create("RenderHolder"); | ||
return res; | ||
} | ||
|
||
pub fn render(&mut self, left: &mut [f32], right: &mut [f32]) { | ||
match &self.sound_render { | ||
None => { | ||
for sample in left { | ||
*sample = 0_f32; | ||
} | ||
for sample in right { | ||
*sample = 0_f32; | ||
} | ||
}, | ||
Some(sound_render) => { | ||
let mut locked_sound_render = sound_render.lock() | ||
.expect("FATAL: can't lock SoundRender!"); | ||
let midi_recevier: &mut dyn MidiReceiver = locked_sound_render.get_as_midi_receiver(); | ||
self.test_seq.send_next_sequence( self.tick_time, midi_recevier ); | ||
locked_sound_render.render(left, right); | ||
if self.test_seq.is_finished() { | ||
self.test_seq.restart(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Drop for RenderHolder { | ||
fn drop(&mut self) { | ||
log::on_drop("RenderHolder"); | ||
} | ||
} | ||
|
Oops, something went wrong.