Skip to content

Commit 8e8698a

Browse files
committed
Rename "sv" to "cell"
1 parent da3640e commit 8e8698a

File tree

8 files changed

+296
-299
lines changed

8 files changed

+296
-299
lines changed

bevy/src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
use bevy::{input::keyboard::KeyboardInput, prelude::*, window::PrimaryWindow};
22
use caw_computer_keyboard::Keyboard;
3-
use caw_core::{Sig, SigVar, sig_var};
3+
use caw_core::{Sig, Variable, variable};
44

55
#[derive(Resource, Clone)]
66
pub struct BevyInput {
7-
pub mouse_x: Sig<SigVar<f32>>,
8-
pub mouse_y: Sig<SigVar<f32>>,
9-
pub keyboard: Keyboard<Sig<SigVar<bool>>>,
7+
pub mouse_x: Sig<Variable<f32>>,
8+
pub mouse_y: Sig<Variable<f32>>,
9+
pub keyboard: Keyboard<Sig<Variable<bool>>>,
1010
}
1111

1212
impl Default for BevyInput {
1313
fn default() -> Self {
14-
let keyboard = Keyboard::new(|_| sig_var(false));
15-
let mouse_x = sig_var(0.5);
16-
let mouse_y = sig_var(0.5);
14+
let keyboard = Keyboard::new(|_| variable(false));
15+
let mouse_x = variable(0.5);
16+
let mouse_y = variable(0.5);
1717
Self {
1818
keyboard,
1919
mouse_x,
@@ -88,7 +88,7 @@ fn get_key<K>(key_code: KeyCode, keyboard: &Keyboard<K>) -> Option<&K> {
8888

8989
fn update_key_state(
9090
input: &KeyboardInput,
91-
keyboard: &Keyboard<Sig<SigVar<bool>>>,
91+
keyboard: &Keyboard<Sig<Variable<bool>>>,
9292
) {
9393
get_key(input.key_code, keyboard)
9494
.iter()
@@ -104,8 +104,8 @@ fn update_key_state(
104104

105105
fn update_mouse_position(
106106
window: &Window,
107-
mouse_x: &Sig<SigVar<f32>>,
108-
mouse_y: &Sig<SigVar<f32>>,
107+
mouse_x: &Sig<Variable<f32>>,
108+
mouse_y: &Sig<Variable<f32>>,
109109
) {
110110
if let Some(position) = window.cursor_position() {
111111
let window_size = window.size();

caw/examples/live_example.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::thread;
33

44
fn main() {
55
env_logger::init();
6-
let volume = sv_default();
6+
let volume = cell_default();
77
let out: LiveStereoOut = live_stereo_viz_udp(oscilloscope::Config {
88
..Default::default()
99
})
@@ -14,16 +14,18 @@ fn main() {
1414
.axis_label_x("cutoff_hz")
1515
.axis_label_y("resonance")
1616
.build();
17-
let cutoff_hz = sv(cutoff_hz);
18-
let res = sv(res * 2.);
19-
let clock = sv(periodic_trig_s(tempo_s.clone())
20-
.build()
21-
.viz_blink("clock".to_string(), Default::default()));
17+
let cutoff_hz = cell(cutoff_hz);
18+
let res = cell(res * 2.);
19+
let clock = cell(
20+
periodic_trig_s(tempo_s.clone())
21+
.build()
22+
.viz_blink("clock".to_string(), Default::default()),
23+
);
2224
let keyboard = computer_keyboard("keys").start_note(note::B_1).build_();
2325
let space_button = keyboard.space_button().shared();
2426
let key_events = keyboard.key_events().shared();
2527
let (drum_bits, drum_space) = num_keys_bits_7_with_space("drums").build();
26-
let length = 8;
28+
let length = 16;
2729
let drum_bits_loop = value_looper(drum_bits, clock.clone(), drum_space)
2830
.persist_with_name("drums")
2931
.length(length)

core/src/cell.rs

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
use crate::{
2+
sig::{
3+
Buf, Sig, SigBoxed, SigCtx, SigSampleIntoBufT, SigShared, SigT,
4+
sig_boxed, sig_shared,
5+
},
6+
stereo::{Channel, Stereo, StereoPair},
7+
};
8+
use std::sync::{Arc, RwLock};
9+
10+
/// Private inner type of the `Cell_` type.
11+
struct Unshared<T: Clone> {
12+
sig_boxed: Arc<RwLock<SigBoxed<T>>>,
13+
buf: Vec<T>,
14+
}
15+
16+
impl<T: Clone> Clone for Unshared<T> {
17+
fn clone(&self) -> Self {
18+
Self {
19+
sig_boxed: Arc::clone(&self.sig_boxed),
20+
buf: Vec::new(),
21+
}
22+
}
23+
}
24+
25+
impl<T: Clone> Unshared<T> {
26+
fn new<S>(sig: S) -> Self
27+
where
28+
S: SigT<Item = T> + Sync + Send + 'static,
29+
{
30+
Unshared {
31+
sig_boxed: Arc::new(RwLock::new(sig_boxed(sig).0)),
32+
buf: Vec::new(),
33+
}
34+
}
35+
36+
fn set<S>(&self, sig: S)
37+
where
38+
S: SigT<Item = T> + Sync + Send + 'static,
39+
{
40+
*self.sig_boxed.write().unwrap() = sig_boxed(sig).0;
41+
}
42+
}
43+
44+
impl<T: Clone> SigT for Unshared<T> {
45+
type Item = T;
46+
47+
fn sample(&mut self, ctx: &SigCtx) -> impl Buf<Self::Item> {
48+
let mut unlocked = self.sig_boxed.write().unwrap();
49+
unlocked.sample_into_buf(ctx, &mut self.buf);
50+
&self.buf
51+
}
52+
}
53+
54+
impl<T: Clone> SigSampleIntoBufT for Unshared<T> {
55+
type Item = T;
56+
57+
fn sample_into_buf(&mut self, ctx: &SigCtx, buf: &mut Vec<Self::Item>) {
58+
self.sig_boxed.write().unwrap().sample_into_buf(ctx, buf);
59+
}
60+
61+
fn sample_into_slice(
62+
&mut self,
63+
ctx: &SigCtx,
64+
stride: usize,
65+
offset: usize,
66+
out: &mut [Self::Item],
67+
) {
68+
self.sig_boxed
69+
.write()
70+
.unwrap()
71+
.sample_into_slice(ctx, stride, offset, out);
72+
}
73+
}
74+
75+
pub fn cell_<S>(sig: S) -> Sig<Cell_<S::Item>>
76+
where
77+
S: SigT + Sync + Send + 'static,
78+
{
79+
Sig(Cell_::new(sig))
80+
}
81+
82+
#[derive(Clone)]
83+
pub struct Cell_<T: Clone>(Sig<SigShared<Unshared<T>>>);
84+
85+
impl<T: Clone> SigT for Cell_<T> {
86+
type Item = T;
87+
88+
fn sample(&mut self, ctx: &SigCtx) -> impl Buf<Self::Item> {
89+
self.0.sample(ctx)
90+
}
91+
}
92+
93+
impl<T: Clone> Cell_<T> {
94+
pub fn new<S>(sig: S) -> Self
95+
where
96+
S: SigT<Item = T> + Sync + Send + 'static,
97+
{
98+
Self(sig_shared(Unshared::new(sig)))
99+
}
100+
101+
pub fn set<S>(&self, sig: S)
102+
where
103+
S: SigT<Item = T> + Sync + Send + 'static,
104+
{
105+
self.0.0.with_inner_mut(|unshared| unshared.set(sig));
106+
}
107+
}
108+
109+
impl<T: Clone> Sig<Cell_<T>> {
110+
pub fn set<S>(&self, sig: S)
111+
where
112+
S: SigT<Item = T> + Sync + Send + 'static,
113+
{
114+
self.0.set(sig);
115+
}
116+
}
117+
118+
impl<T: Clone> SigSampleIntoBufT for Cell_<T> {
119+
type Item = T;
120+
121+
fn sample_into_buf(&mut self, ctx: &SigCtx, buf: &mut Vec<Self::Item>) {
122+
self.0.sample_into_buf(ctx, buf);
123+
}
124+
125+
fn sample_into_slice(
126+
&mut self,
127+
ctx: &SigCtx,
128+
stride: usize,
129+
offset: usize,
130+
out: &mut [Self::Item],
131+
) {
132+
self.0.sample_into_slice(ctx, stride, offset, out);
133+
}
134+
}
135+
136+
/// A container for a signal yielding a type `T` whose contentents (a signal) can be changed to a
137+
/// different signal later. The type of the signal is erased (`T` is the type _yielded_ by the
138+
/// signal) so signals of different types may be placed into the same cell. Call its `set` method
139+
/// to update the signal contained in the cell.
140+
pub type Cell<T> = Sig<Cell_<T>>;
141+
pub type CellF32 = Cell<f32>;
142+
143+
pub fn cell<T>(
144+
initial_sig: impl SigT<Item = T> + Sync + Send + 'static,
145+
) -> Cell<T>
146+
where
147+
T: Clone,
148+
{
149+
cell_(initial_sig)
150+
}
151+
152+
pub fn cell_f32(
153+
initial_sig: impl SigT<Item = f32> + Sync + Send + 'static,
154+
) -> CellF32 {
155+
cell_(initial_sig)
156+
}
157+
158+
/// Create a new signal cell initialized with a constant signal yielding the default value of the
159+
/// type `T`.
160+
pub fn cell_default<T>() -> Cell<T>
161+
where
162+
T: SigT<Item = T> + Clone + Default + Sync + Send + 'static,
163+
{
164+
cell(T::default())
165+
}
166+
167+
impl<T> Stereo<Cell<T>, Cell<T>>
168+
where
169+
T: Clone,
170+
{
171+
pub fn set<F, S>(&self, mut f: F)
172+
where
173+
S: SigT<Item = T> + Sync + Send + 'static,
174+
F: FnMut() -> S,
175+
{
176+
self.as_ref().with(|s| s.set(f()));
177+
}
178+
179+
pub fn set_channel<F, S>(&self, mut f: F)
180+
where
181+
S: SigT<Item = T> + Sync + Send + 'static,
182+
F: FnMut(Channel) -> S,
183+
{
184+
self.as_ref().with_channel(|channel, s| s.set(f(channel)));
185+
}
186+
}
187+
188+
impl Stereo<Cell<f32>, Cell<f32>> {
189+
pub fn with_volume<V>(self, volume: V) -> Self
190+
where
191+
V: SigT<Item = f32> + Sync + Send + 'static,
192+
{
193+
let out = StereoPair::new_fn(|| cell(0.));
194+
let volume = sig_shared(volume);
195+
self.set_channel(|channel| out.get(channel).clone() * volume.clone());
196+
out
197+
}
198+
}

core/src/lib.rs

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,11 @@ mod sig;
33
pub mod sig_ops;
44
pub use sig::{
55
Buf, Const, ConstBuf, Filter, GateToTrigRisingEdge, IsNegative, IsPositive,
6-
Sig, SigAbs, SigBoxed, SigBoxedVar, SigConst, SigCtx, SigSampleIntoBufT,
7-
SigShared, SigT, SigVar, Triggerable, Zip, Zip3, Zip4, sig_boxed,
8-
sig_boxed_var, sig_option_first_some, sig_shared, sig_var,
6+
Sig, SigAbs, SigBoxed, SigConst, SigCtx, SigSampleIntoBufT, SigShared,
7+
SigT, Triggerable, Variable, Zip, Zip3, Zip4, sig_boxed,
8+
sig_option_first_some, sig_shared, variable,
99
};
10+
pub mod cell;
11+
pub use cell::{Cell, CellF32, cell, cell_default, cell_f32};
1012
pub mod stereo;
1113
pub use stereo::{Channel, Stereo, StereoPair};
12-
13-
pub type SV<T> = Sig<SigBoxedVar<T>>;
14-
pub type SVF32 = SV<f32>;
15-
16-
pub fn sv<T>(initial_sig: impl SigT<Item = T> + Sync + Send + 'static) -> SV<T>
17-
where
18-
T: Clone,
19-
{
20-
sig_boxed_var(initial_sig)
21-
}
22-
23-
pub fn svf32(
24-
initial_sig: impl SigT<Item = f32> + Sync + Send + 'static,
25-
) -> SV<f32> {
26-
sig_boxed_var(initial_sig)
27-
}
28-
29-
pub fn sv_default<T>() -> SV<T>
30-
where
31-
T: SigT<Item = T> + Clone + Default + Sync + Send + 'static,
32-
{
33-
sv(T::default())
34-
}
35-
36-
impl<T> Stereo<SV<T>, SV<T>>
37-
where
38-
T: Clone,
39-
{
40-
pub fn set<F, S>(&self, mut f: F)
41-
where
42-
S: SigT<Item = T> + Sync + Send + 'static,
43-
F: FnMut() -> S,
44-
{
45-
self.as_ref().with(|s| s.set(f()));
46-
}
47-
48-
pub fn set_channel<F, S>(&self, mut f: F)
49-
where
50-
S: SigT<Item = T> + Sync + Send + 'static,
51-
F: FnMut(Channel) -> S,
52-
{
53-
self.as_ref().with_channel(|channel, s| s.set(f(channel)));
54-
}
55-
}
56-
57-
impl Stereo<SV<f32>, SV<f32>> {
58-
pub fn with_volume<V>(self, volume: V) -> Self
59-
where
60-
V: SigT<Item = f32> + Sync + Send + 'static,
61-
{
62-
let out = StereoPair::new_fn(|| sv(0.));
63-
let volume = sig_shared(volume);
64-
self.set_channel(|channel| out.get(channel).clone() * volume.clone());
65-
out
66-
}
67-
}

0 commit comments

Comments
 (0)