Skip to content

Commit 455dd83

Browse files
committed
Add some utilities for live-coding
1 parent 1efd2b1 commit 455dd83

File tree

2 files changed

+160
-10
lines changed

2 files changed

+160
-10
lines changed

core/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ mod arith;
22
mod sig;
33
pub mod sig_ops;
44
pub use sig::{
5-
sig_option_first_some, sig_shared, sig_var, Buf, ConstBuf, Filter,
6-
GateToTrigRisingEdge, Sig, SigAbs, SigBoxed, SigCtx, SigSampleIntoBufT,
7-
SigShared, SigT, SigVar, Triggerable,
5+
sig_boxed, sig_boxed_var_const, sig_boxed_var_default,
6+
sig_option_first_some, sig_shared, sig_var, Buf, Const, ConstBuf, Filter,
7+
GateToTrigRisingEdge, Sig, SigAbs, SigBoxed, SigBoxedVar, SigCtx,
8+
SigSampleIntoBufT, SigShared, SigT, SigVar, Triggerable,
89
};
910
pub mod stereo;
1011
pub use stereo::{Channel, Stereo, StereoPair};

core/src/sig.rs

Lines changed: 156 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,24 @@ pub trait SigSampleIntoBufT {
252252
);
253253
}
254254

255+
pub struct Const<T>(T)
256+
where
257+
T: Clone;
258+
259+
impl<T> SigT for Const<T>
260+
where
261+
T: Clone,
262+
{
263+
type Item = T;
264+
265+
fn sample(&mut self, ctx: &SigCtx) -> impl Buf<Self::Item> {
266+
ConstBuf {
267+
value: self.0.clone(),
268+
count: ctx.num_samples,
269+
}
270+
}
271+
}
272+
255273
impl SigT for f32 {
256274
type Item = f32;
257275

@@ -333,11 +351,13 @@ impl<S: SigT> SigSampleIntoBufT for Sig<S> {
333351
}
334352
}
335353

336-
pub struct SigBoxed<T>(
337-
Box<dyn SigSampleIntoBufT<Item = T> + Send + Sync + 'static>,
338-
)
354+
pub struct SigBoxed<T>
339355
where
340-
T: Clone;
356+
T: Clone,
357+
{
358+
sig: Box<dyn SigSampleIntoBufT<Item = T> + Send + Sync + 'static>,
359+
buf: Vec<T>,
360+
}
341361

342362
impl<T> SigSampleIntoBufT for SigBoxed<T>
343363
where
@@ -346,7 +366,7 @@ where
346366
type Item = T;
347367

348368
fn sample_into_buf(&mut self, ctx: &SigCtx, buf: &mut Vec<Self::Item>) {
349-
self.0.sample_into_buf(ctx, buf);
369+
self.sig.sample_into_buf(ctx, buf);
350370
}
351371

352372
fn sample_into_slice(
@@ -356,7 +376,19 @@ where
356376
offset: usize,
357377
out: &mut [Self::Item],
358378
) {
359-
self.0.sample_into_slice(ctx, stride, offset, out);
379+
self.sig.sample_into_slice(ctx, stride, offset, out);
380+
}
381+
}
382+
383+
impl<T> SigT for SigBoxed<T>
384+
where
385+
T: Clone,
386+
{
387+
type Item = T;
388+
389+
fn sample(&mut self, ctx: &SigCtx) -> impl Buf<Self::Item> {
390+
self.sig.sample_into_buf(ctx, &mut self.buf);
391+
&self.buf
360392
}
361393
}
362394

@@ -429,12 +461,22 @@ where
429461
}
430462
}
431463

464+
pub fn sig_boxed<S>(sig: S) -> SigBoxed<S::Item>
465+
where
466+
S: SigT + Send + Sync + 'static,
467+
{
468+
Sig(sig).boxed()
469+
}
470+
432471
impl<S> Sig<S>
433472
where
434473
S: SigT + Send + Sync + 'static,
435474
{
436475
pub fn boxed(self) -> SigBoxed<S::Item> {
437-
SigBoxed(Box::new(self))
476+
SigBoxed {
477+
sig: Box::new(self),
478+
buf: Vec::new(),
479+
}
438480
}
439481
}
440482

@@ -977,6 +1019,15 @@ impl<T> SigVar<T> {
9771019
}
9781020
}
9791021

1022+
impl<T> Sig<SigVar<T>>
1023+
where
1024+
T: Clone,
1025+
{
1026+
pub fn set(&self, value: T) {
1027+
self.0.set(value)
1028+
}
1029+
}
1030+
9801031
pub fn sig_var<T: Clone>(value: T) -> Sig<SigVar<T>> {
9811032
Sig(SigVar::new(value))
9821033
}
@@ -1001,6 +1052,104 @@ where
10011052
}
10021053
}
10031054

1055+
pub struct SigBoxedVar<T: Clone> {
1056+
sig_boxed: Arc<RwLock<SigBoxed<T>>>,
1057+
buf: Vec<T>,
1058+
}
1059+
1060+
impl<T: Clone> Clone for SigBoxedVar<T> {
1061+
fn clone(&self) -> Self {
1062+
Self {
1063+
sig_boxed: Arc::clone(&self.sig_boxed),
1064+
buf: Vec::new(),
1065+
}
1066+
}
1067+
}
1068+
1069+
impl<T: Clone> SigBoxedVar<T> {
1070+
pub fn set<S>(&self, sig: S)
1071+
where
1072+
S: SigT<Item = T> + Sync + Send + 'static,
1073+
{
1074+
*self.sig_boxed.write().unwrap() = sig_boxed(sig);
1075+
}
1076+
}
1077+
1078+
impl<T: Clone> Sig<SigBoxedVar<T>> {
1079+
pub fn set<S>(&self, sig: S)
1080+
where
1081+
S: SigT<Item = T> + Sync + Send + 'static,
1082+
{
1083+
self.0.set(sig);
1084+
}
1085+
}
1086+
1087+
impl<T> SigBoxedVar<T>
1088+
where
1089+
T: Clone + Sync + Send + 'static,
1090+
{
1091+
pub fn new_const(value: T) -> Self {
1092+
SigBoxedVar {
1093+
sig_boxed: Arc::new(RwLock::new(sig_boxed(Const(value)))),
1094+
buf: Vec::new(),
1095+
}
1096+
}
1097+
}
1098+
1099+
impl<T> SigBoxedVar<T>
1100+
where
1101+
T: Clone + Default + Sync + Send + 'static,
1102+
{
1103+
pub fn new_default() -> Self {
1104+
Self::new_const(T::default())
1105+
}
1106+
}
1107+
1108+
impl<T: Clone> SigT for SigBoxedVar<T> {
1109+
type Item = T;
1110+
1111+
fn sample(&mut self, ctx: &SigCtx) -> impl Buf<Self::Item> {
1112+
let mut unlocked = self.sig_boxed.write().unwrap();
1113+
unlocked.sample_into_buf(ctx, &mut self.buf);
1114+
&self.buf
1115+
}
1116+
}
1117+
1118+
impl<T: Clone> SigSampleIntoBufT for SigBoxedVar<T> {
1119+
type Item = T;
1120+
1121+
fn sample_into_buf(&mut self, ctx: &SigCtx, buf: &mut Vec<Self::Item>) {
1122+
self.sig_boxed.write().unwrap().sample_into_buf(ctx, buf);
1123+
}
1124+
1125+
fn sample_into_slice(
1126+
&mut self,
1127+
ctx: &SigCtx,
1128+
stride: usize,
1129+
offset: usize,
1130+
out: &mut [Self::Item],
1131+
) {
1132+
self.sig_boxed
1133+
.write()
1134+
.unwrap()
1135+
.sample_into_slice(ctx, stride, offset, out);
1136+
}
1137+
}
1138+
1139+
pub fn sig_boxed_var_default<T>() -> Sig<SigBoxedVar<T>>
1140+
where
1141+
T: Clone + Default + Sync + Send + 'static,
1142+
{
1143+
Sig(SigBoxedVar::new_default())
1144+
}
1145+
1146+
pub fn sig_boxed_var_const<T>(value: T) -> Sig<SigBoxedVar<T>>
1147+
where
1148+
T: Clone + Sync + Send + 'static,
1149+
{
1150+
Sig(SigBoxedVar::new_const(value))
1151+
}
1152+
10041153
pub trait Triggerable {
10051154
type Item: Clone;
10061155

0 commit comments

Comments
 (0)