Skip to content

Commit

Permalink
Change mono<->stereo conversion handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MyBlackMIDIScore committed Oct 28, 2023
1 parent 40f7e09 commit 5c90642
Show file tree
Hide file tree
Showing 10 changed files with 523 additions and 373 deletions.
6 changes: 1 addition & 5 deletions core/src/channel/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,25 @@ use super::{
channel_sf::ChannelSoundfont, event::KeyNoteEvent, voice_buffer::VoiceBuffer,
ChannelInitOptions, VoiceControlData,
};
use crate::ChannelCount;

pub struct KeyData {
key: u8,
voices: VoiceBuffer,
last_voice_count: usize,
shared_voice_counter: Arc<AtomicU64>,
channels: ChannelCount,
}

impl KeyData {
pub fn new(
key: u8,
shared_voice_counter: Arc<AtomicU64>,
options: ChannelInitOptions,
channels: ChannelCount,
) -> KeyData {
KeyData {
key,
voices: VoiceBuffer::new(options),
last_voice_count: 0,
shared_voice_counter,
channels,
}
}

Expand Down Expand Up @@ -76,7 +72,7 @@ impl KeyData {
}

for voice in &mut self.voices.iter_voices_mut() {
voice.render_to(self.channels, out);
voice.render_to(out);
}
self.voices.remove_ended_voices();

Expand Down
15 changes: 3 additions & 12 deletions core/src/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,9 @@ struct Key {
}

impl Key {
pub fn new(
key: u8,
shared_voice_counter: Arc<AtomicU64>,
options: ChannelInitOptions,
channels: ChannelCount,
) -> Self {
pub fn new(key: u8, shared_voice_counter: Arc<AtomicU64>, options: ChannelInitOptions) -> Self {
Key {
data: KeyData::new(key, shared_voice_counter, options, channels),
data: KeyData::new(key, shared_voice_counter, options),
audio_cache: Vec::new(),
event_cache: Vec::new(),
}
Expand Down Expand Up @@ -183,13 +178,9 @@ impl VoiceChannel {
control_event_data.bank = 128;
}

let channels = stream_params.channels;

VoiceChannel {
params,
key_voices: fill_key_array(|i| {
Key::new(i, shared_voice_counter.clone(), options, channels)
}),
key_voices: fill_key_array(|i| Key::new(i, shared_voice_counter.clone(), options)),

threadpool,

Expand Down
30 changes: 25 additions & 5 deletions core/src/soundfont/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use symphonia::core::{audio::AudioBufferRef, meta::MetadataOptions};
use symphonia::core::{audio::Signal, io::MediaSourceStream};
use symphonia::core::{codecs::DecoderOptions, errors::Error};

use thiserror::Error;

use self::resample::SincResampler;
use crate::{AudioStreamParams, ChannelCount};
use thiserror::Error;

pub mod resample;

Expand Down Expand Up @@ -39,8 +39,10 @@ type ProcessedSample = (Arc<[Arc<[f32]>]>, u32);

pub fn load_audio_file(
path: &PathBuf,
new_sample_rate: f32,
stream_params: AudioStreamParams,
) -> Result<ProcessedSample, AudioLoadError> {
let new_sample_rate = stream_params.sample_rate as f32;

let extension = path.extension().and_then(|ext| ext.to_str());

let file = Box::new(File::open(path)?);
Expand Down Expand Up @@ -115,7 +117,7 @@ pub fn load_audio_file(
}
}

let built = builder.finish(sample_rate as f32, new_sample_rate);
let built = builder.finish(sample_rate as f32, new_sample_rate, stream_params.channels);

Ok((built, sample_rate))
}
Expand Down Expand Up @@ -161,8 +163,26 @@ impl BuilderVecs {
}
}

fn finish(self, sample_rate: f32, new_sample_rate: f32) -> Arc<[Arc<[f32]>]> {
fn finish(
self,
sample_rate: f32,
new_sample_rate: f32,
channels: ChannelCount,
) -> Arc<[Arc<[f32]>]> {
let mut vecs = self.vecs;

if channels == ChannelCount::Mono && vecs.len() >= 2 {
let right = vecs.pop().unwrap_or_default();
let left = vecs.pop().unwrap_or_default();

let combined: Vec<f32> = left
.iter()
.zip(right.iter())
.map(|(&l, &r)| (l + r) * 0.5)
.collect();
vecs.push(combined);
}

for chan in vecs.iter_mut() {
chan.shrink_to_fit();
}
Expand Down
Loading

0 comments on commit 5c90642

Please sign in to comment.