feat: core
This commit is contained in:
11
crates/core/src/buffer.rs
Normal file
11
crates/core/src/buffer.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use crate::constant::SAMPLE;
|
||||
|
||||
pub struct AudioBuffer<const N: usize>(pub [SAMPLE; N]);
|
||||
|
||||
impl<const N: usize> AudioBuffer<N> {
|
||||
pub fn add_mut(&mut self, other: &Self) {
|
||||
for i in 0..N {
|
||||
self.0[i] = self.0[i].saturating_add(other.0[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
1
crates/core/src/constant.rs
Normal file
1
crates/core/src/constant.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub type SAMPLE = i16;
|
||||
4
crates/core/src/lib.rs
Normal file
4
crates/core/src/lib.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
mod buffer;
|
||||
mod constant;
|
||||
pub mod stream;
|
||||
pub mod transformer;
|
||||
13
crates/core/src/stream.rs
Normal file
13
crates/core/src/stream.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use crate::{buffer::AudioBuffer, constant::SAMPLE};
|
||||
|
||||
pub trait AudioStream<const N: usize>: Send {
|
||||
fn update(&self, upstream: &mut AudioBuffer<N>);
|
||||
}
|
||||
|
||||
pub trait AudioSource<const N: usize>: Send {
|
||||
fn update(&self) -> AudioBuffer<N>;
|
||||
}
|
||||
|
||||
pub trait BinaryStream<const N: usize>: Send {
|
||||
fn next(&self) -> [u8; N];
|
||||
}
|
||||
1
crates/core/src/transformer.rs
Normal file
1
crates/core/src/transformer.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod mixer;
|
||||
31
crates/core/src/transformer/mixer.rs
Normal file
31
crates/core/src/transformer/mixer.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use crate::{buffer::AudioBuffer, stream::AudioStream};
|
||||
use rayon::prelude::ParallelIterator;
|
||||
|
||||
pub struct Mixed<S, const N: usize>
|
||||
where
|
||||
S: AudioStream<N>,
|
||||
{
|
||||
upstreams: Vec<S>,
|
||||
}
|
||||
|
||||
impl<S, const N: usize> AudioStream<N> for Mixed<S, N>
|
||||
where
|
||||
S: AudioStream<N>,
|
||||
{
|
||||
fn update(&self, out: &mut AudioBuffer<N>) {
|
||||
let bufs: Vec<AudioBuffer<N>> = self
|
||||
.upstreams
|
||||
.iter() // TODO = DO IT PARALLEL. It's the main purpose of the project. Why
|
||||
// do you even do it in single thread if you develop the engine?
|
||||
.map(|upstream| {
|
||||
let mut out = AudioBuffer([0; N]);
|
||||
upstream.update(&mut out);
|
||||
out
|
||||
})
|
||||
.collect();
|
||||
|
||||
for buf in bufs {
|
||||
out.add_mut(&buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user