Skip to content

Commit 0923ba0

Browse files
committed
updates
1 parent 4280366 commit 0923ba0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+3871
-0
lines changed

Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "pa-dsp"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
lazy_static = "1.4.0"
8+
samplerate = "0.2.4"
9+
dasp_signal = "0.11.0"
10+
dasp_interpolate = { version = "0.11.0", default-features = false, features = ["floor", "linear", "sinc"]}
11+
dasp_frame = { version = "0.11.0" }
12+
rand = "0.8.5"
13+
hound = "3.5.0"
14+
serde = { version = "1.0", features = ["derive", "rc"] }
15+
serde_json = "1.0.91"
16+
17+
[lib]
18+
path = "lib.rs"

buffers/block.rs

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
use std::ops::{Add, Sub, Mul, Div};
2+
3+
use crate::Sample;
4+
5+
pub trait Block {
6+
type Item;
7+
8+
fn as_slice<'a>(&'a self) -> &'a [Self::Item];
9+
fn as_slice_mut<'a>(&'a mut self) -> &'a mut [Self::Item];
10+
11+
fn len(&self) -> usize {
12+
self.as_slice().len()
13+
}
14+
15+
fn copy_to<B: Block<Item = Self::Item>>(&self, dest: &mut B) where Self: Sized, Self::Item: Copy {
16+
dest.copy_from(self);
17+
}
18+
19+
fn rms(&self) -> Self::Item where Self::Item: Sample {
20+
let mut total = Self::Item::EQUILIBRIUM;
21+
let mut count = Self::Item::EQUILIBRIUM;
22+
23+
for s in self.as_slice() {
24+
total += *s;
25+
count += Self::Item::from_f32(1.0);
26+
}
27+
28+
total = total / count;
29+
30+
return total;
31+
}
32+
33+
fn apply<F: Fn(Self::Item) -> Self::Item>(&mut self, f: F) where Self::Item: Copy {
34+
for v in self.as_slice_mut() {
35+
*v = f(*v);
36+
}
37+
}
38+
39+
fn zip_apply<I: Copy, B: Block<Item = I>, F: Fn(Self::Item, I) -> Self::Item>(&mut self, b: &B, f: F) where Self::Item: Copy {
40+
for (v, i) in self.as_slice_mut().iter_mut().zip(b.as_slice()) {
41+
*v = f(*v, *i);
42+
}
43+
}
44+
45+
/* Element operations */
46+
47+
fn fill(&mut self, value: Self::Item) where Self::Item: Copy {
48+
self.apply(| _v | { value } );
49+
}
50+
51+
/* Block operations */
52+
53+
fn copy_from<B: Block<Item = Self::Item>>(&mut self, src: &B) where Self::Item: Copy {
54+
self.as_slice_mut().copy_from_slice(src.as_slice());
55+
}
56+
57+
fn add_from<B: Block<Item = Self::Item>>(&mut self, src: &B) where Self::Item: Copy + Add<Output = Self::Item> {
58+
self.zip_apply(src, | a, b | a + b);
59+
}
60+
61+
fn sub_from<B: Block<Item = Self::Item>>(&mut self, src: &B) where Self::Item: Copy + Sub<Output = Self::Item> {
62+
self.zip_apply(src, | a, b | a - b);
63+
}
64+
65+
fn mul_from<B: Block<Item = Self::Item>>(&mut self, src: &B) where Self::Item: Copy + Mul<Output = Self::Item> {
66+
self.zip_apply(src, | a, b | a * b);
67+
}
68+
69+
fn div_from<B: Block<Item = Self::Item>>(&mut self, src: &B) where Self::Item: Copy + Div<Output = Self::Item> {
70+
self.zip_apply(src, | a, b | a / b);
71+
}
72+
73+
/* Sample specific */
74+
75+
fn gain(&mut self, db: <Self::Item as Sample>::Float) where Self::Item: Sample {
76+
self.apply(| v | v.gain(db))
77+
}
78+
79+
fn equilibrate(&mut self) where Self::Item: Sample {
80+
self.fill(Self::Item::EQUILIBRIUM);
81+
}
82+
83+
/* Note specific */
84+
85+
// fn transpose
86+
}
87+
88+
/* Slice implementations */
89+
90+
impl<S> Block for [S] {
91+
type Item = S;
92+
93+
fn as_slice<'a>(&'a self) -> &'a [Self::Item] {
94+
self
95+
}
96+
97+
fn as_slice_mut<'a>(&'a mut self) -> &'a mut [Self::Item] {
98+
self
99+
}
100+
}
101+
102+
impl<S> Block for &[S] {
103+
type Item = S;
104+
105+
fn as_slice<'a>(&'a self) -> &'a [Self::Item] {
106+
self
107+
}
108+
109+
fn as_slice_mut<'a>(&'a mut self) -> &'a mut [Self::Item] {
110+
unreachable!()
111+
}
112+
}
113+
114+
impl<S> Block for &mut [S] {
115+
type Item = S;
116+
117+
fn as_slice<'a>(&'a self) -> &'a [Self::Item] {
118+
self
119+
}
120+
121+
fn as_slice_mut<'a>(&'a mut self) -> &'a mut [Self::Item] {
122+
self
123+
}
124+
}
125+
126+
127+
/* Raw pointer implementations */
128+
129+
impl<S> Block for (*const S, usize) {
130+
type Item = S;
131+
132+
fn as_slice<'a>(&'a self) -> &'a [Self::Item] {
133+
unsafe {
134+
std::slice::from_raw_parts(self.0, self.1)
135+
}
136+
}
137+
138+
fn as_slice_mut<'a>(&'a mut self) -> &'a mut [Self::Item] {
139+
unreachable!()
140+
}
141+
}
142+
143+
impl<S> Block for (*mut S, usize) {
144+
type Item = S;
145+
146+
fn as_slice<'a>(&'a self) -> &'a [Self::Item] {
147+
unsafe {
148+
std::slice::from_raw_parts(self.0, self.1)
149+
}
150+
}
151+
152+
fn as_slice_mut<'a>(&'a mut self) -> &'a mut [Self::Item] {
153+
unsafe {
154+
std::slice::from_raw_parts_mut(self.0, self.1)
155+
}
156+
}
157+
}

buffers/buffer.rs

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
use std::slice;
2+
use std::ops::{Add, Mul, Sub, Div, Index, IndexMut, AddAssign, SubAssign, MulAssign, DivAssign};
3+
4+
use crate::{event::*, Stereo};
5+
6+
use crate::float::sample::*;
7+
use crate::buffers::block::*;
8+
9+
pub type AudioBuffer = Buffer<f32>;
10+
pub type StereoBuffer = Buffer<Stereo<f32>>;
11+
pub type NoteBuffer = Buffer<NoteMessage>;
12+
13+
pub struct Buffer<T> {
14+
items: Vec<T>,
15+
}
16+
17+
impl<T> Buffer<T> {
18+
pub fn from(items: Vec<T>) -> Self {
19+
Self { items }
20+
}
21+
22+
pub unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Self {
23+
Self {
24+
items: Vec::from_raw_parts(ptr, length, capacity),
25+
}
26+
}
27+
28+
pub fn capacity(&self) -> usize {
29+
self.items.capacity()
30+
}
31+
32+
pub fn as_ptr(&self) -> *const T {
33+
self.items.as_ptr()
34+
}
35+
36+
pub fn as_mut_ptr(&mut self) -> *mut T {
37+
self.items.as_mut_ptr()
38+
}
39+
40+
pub fn push(&mut self, item: T) {
41+
self.items.push(item);
42+
}
43+
44+
pub fn clear(&mut self) {
45+
self.items.clear();
46+
}
47+
}
48+
49+
impl<T: Copy> Buffer<T> {
50+
pub fn init(value: T, size: usize) -> Self {
51+
let mut items = Vec::with_capacity(size);
52+
53+
for _ in 0..size {
54+
items.push(value);
55+
}
56+
57+
Self { items }
58+
}
59+
}
60+
61+
impl<T> Block for Buffer<T> {
62+
type Item = T;
63+
64+
fn as_slice<'a>(&'a self) -> &'a [T] {
65+
self.items.as_slice()
66+
}
67+
68+
fn as_slice_mut<'a>(&'a mut self) -> &'a mut [T] {
69+
self.items.as_mut_slice()
70+
}
71+
}
72+
73+
// Replace letters with useful names
74+
impl Buffer<NoteMessage> {
75+
pub fn new() -> Self {
76+
Self {
77+
items: Vec::new()
78+
}
79+
}
80+
81+
pub fn with_capacity(capacity: usize) -> Self {
82+
Self {
83+
items: Vec::with_capacity(capacity)
84+
}
85+
}
86+
87+
pub fn replace(&mut self, src: &Buffer<NoteMessage>) {
88+
self.items.clear();
89+
90+
for s in src.as_slice() {
91+
self.items.push(*s);
92+
}
93+
}
94+
95+
pub fn append(&mut self, src: &Buffer<NoteMessage>) {
96+
for s in src.as_slice() {
97+
self.items.push(*s);
98+
}
99+
}
100+
}
101+
102+
/* Opeator assign implementations */
103+
104+
impl<T: Add<Output = T> + Copy, B: Block<Item = T>> AddAssign<&B> for Buffer<T> {
105+
fn add_assign(&mut self, rhs: &B) {
106+
for (a, b) in self.as_slice_mut().iter_mut().zip(rhs.as_slice()) {
107+
*a = *a + *b;
108+
}
109+
}
110+
}
111+
112+
impl<T: Sub<Output = T> + Copy, B: Block<Item = T>> SubAssign<&B> for Buffer<T> {
113+
fn sub_assign(&mut self, rhs: &B) {
114+
for (a, b) in self.as_slice_mut().iter_mut().zip(rhs.as_slice()) {
115+
*a = *a - *b;
116+
}
117+
}
118+
}
119+
120+
impl<T: Mul<Output = T> + Copy, B: Block<Item = T>> MulAssign<&B> for Buffer<T> {
121+
fn mul_assign(&mut self, rhs: &B) {
122+
for (a, b) in self.as_slice_mut().iter_mut().zip(rhs.as_slice()) {
123+
*a = *a * *b;
124+
}
125+
}
126+
}
127+
128+
impl<T: Div<Output = T> + Copy, B: Block<Item = T>> DivAssign<&B> for Buffer<T> {
129+
fn div_assign(&mut self, rhs: &B) {
130+
for (a, b) in self.as_slice_mut().iter_mut().zip(rhs.as_slice()) {
131+
*a = *a / *b;
132+
}
133+
}
134+
}
135+
136+
/* Index trait implementations */
137+
138+
impl<T: Copy> Index<usize> for Buffer<T> {
139+
type Output = T;
140+
141+
fn index(&self, index: usize) -> &Self::Output {
142+
&self.items[index]
143+
}
144+
}
145+
146+
impl<T: Copy> IndexMut<usize> for Buffer<T> {
147+
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
148+
&mut self.items[index]
149+
}
150+
}
151+
152+
/* Iterator trait implementations */
153+
154+
impl<'a, T: Copy + Clone> IntoIterator for &'a Buffer<T> {
155+
type Item = &'a T;
156+
type IntoIter = slice::Iter<'a, T>;
157+
158+
fn into_iter(self) -> slice::Iter<'a, T> {
159+
self.as_slice().into_iter()
160+
}
161+
}
162+
163+
impl<'a, T: Copy + Clone> IntoIterator for &'a mut Buffer<T> {
164+
type Item = &'a mut T;
165+
type IntoIter = slice::IterMut<'a, T>;
166+
167+
fn into_iter(self) -> slice::IterMut<'a, T> {
168+
self.as_slice_mut().into_iter()
169+
}
170+
}
171+
172+
/* Ring buffer - here temporarily until private types are unneeded */
173+
174+
pub struct RingBuffer<S> {
175+
buffer: Buffer<S>,
176+
length: usize,
177+
index: usize
178+
}
179+
180+
impl<S: Sample> RingBuffer<S> {
181+
pub fn init(value: S, size: usize) -> Self {
182+
Self {
183+
buffer: Buffer::init(value, size),
184+
length: 1,
185+
index: 0
186+
}
187+
}
188+
189+
pub fn len(&self) -> usize {
190+
self.length
191+
}
192+
193+
pub fn capacity(&self) -> usize {
194+
self.buffer.capacity()
195+
}
196+
197+
pub fn resize(&mut self, length: usize) {
198+
if length > self.buffer.capacity() {
199+
self.buffer.items.resize(length, S::EQUILIBRIUM);
200+
} else {
201+
self.length = length;
202+
for sample in self.buffer.as_slice_mut().iter_mut().skip(length) {
203+
*sample = S::EQUILIBRIUM;
204+
}
205+
}
206+
}
207+
208+
pub fn next(&mut self, input: S) -> S {
209+
let output = self.buffer.items[self.index];
210+
self.buffer.items[self.index] = input;
211+
self.index = (self.index + 1) % self.length;
212+
output
213+
}
214+
}

0 commit comments

Comments
 (0)