feat: core
This commit is contained in:
21
.gitignore
vendored
Normal file
21
.gitignore
vendored
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# Created by https://www.toptal.com/developers/gitignore/api/rust
|
||||||
|
# Edit at https://www.toptal.com/developers/gitignore?templates=rust
|
||||||
|
|
||||||
|
### Rust ###
|
||||||
|
# Generated by Cargo
|
||||||
|
# will have compiled files and executables
|
||||||
|
debug/
|
||||||
|
target/
|
||||||
|
|
||||||
|
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||||
|
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||||
|
Cargo.lock
|
||||||
|
|
||||||
|
# These are backup files generated by rustfmt
|
||||||
|
**/*.rs.bk
|
||||||
|
|
||||||
|
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||||
|
*.pdb
|
||||||
|
|
||||||
|
# End of https://www.toptal.com/developers/gitignore/api/rust
|
||||||
|
|
||||||
3
Cargo.toml
Executable file
3
Cargo.toml
Executable file
@@ -0,0 +1,3 @@
|
|||||||
|
[workspace]
|
||||||
|
resolver = "3"
|
||||||
|
members = ["crates/core"]
|
||||||
10
crates/core/Cargo.toml
Normal file
10
crates/core/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "soft-mixer-core"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
crossbeam = { version = "0.8.4", features = ["crossbeam-channel"] }
|
||||||
|
parking_lot = "0.12.4"
|
||||||
|
rayon = "1.11.0"
|
||||||
|
thiserror = "2.0.16"
|
||||||
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