Skip to content

Commit cb6f11e

Browse files
committed
Update some packages
1 parent b7a82e6 commit cb6f11e

File tree

8 files changed

+116
-110
lines changed

8 files changed

+116
-110
lines changed

computer-keyboard/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "caw_computer_keyboard"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
description = "Use a computer keyboard to control caw synthesizer modules"
55
authors = ["Stephen Sherratt <[email protected]>"]
66
license = "MIT"

computer-keyboard/src/lib.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,58 @@ where
165165
}
166166

167167
impl<K> Keyboard<K> {
168+
pub fn new<F: FnMut(Key) -> K>(mut f: F) -> Self {
169+
Keyboard {
170+
a: f(Key::A),
171+
b: f(Key::B),
172+
c: f(Key::C),
173+
d: f(Key::D),
174+
e: f(Key::E),
175+
f: f(Key::F),
176+
g: f(Key::G),
177+
h: f(Key::H),
178+
i: f(Key::I),
179+
j: f(Key::J),
180+
k: f(Key::K),
181+
l: f(Key::L),
182+
m: f(Key::M),
183+
n: f(Key::N),
184+
o: f(Key::O),
185+
p: f(Key::P),
186+
q: f(Key::Q),
187+
r: f(Key::R),
188+
s: f(Key::S),
189+
t: f(Key::T),
190+
u: f(Key::U),
191+
v: f(Key::V),
192+
w: f(Key::W),
193+
x: f(Key::X),
194+
y: f(Key::Y),
195+
z: f(Key::Z),
196+
n0: f(Key::N0),
197+
n1: f(Key::N1),
198+
n2: f(Key::N2),
199+
n3: f(Key::N3),
200+
n4: f(Key::N4),
201+
n5: f(Key::N5),
202+
n6: f(Key::N6),
203+
n7: f(Key::N7),
204+
n8: f(Key::N8),
205+
n9: f(Key::N9),
206+
left_bracket: f(Key::LeftBracket),
207+
right_bracket: f(Key::RightBracket),
208+
semicolon: f(Key::Semicolon),
209+
apostrophe: f(Key::Apostrophe),
210+
comma: f(Key::Comma),
211+
period: f(Key::Period),
212+
minus: f(Key::Minus),
213+
equals: f(Key::Equals),
214+
slash: f(Key::Slash),
215+
space: f(Key::Space),
216+
backspace: f(Key::Backspace),
217+
backslash: f(Key::Backslash),
218+
}
219+
}
168220
pub fn map<K_, F: FnMut(&K) -> K_>(&self, mut f: F) -> Keyboard<K_> {
169221
Keyboard {
170222
a: f(&self.a),

core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "caw_core"
3-
version = "0.3.2"
3+
version = "0.3.3"
44
description = "Core types for caw software-defined modular synthesizer"
55
authors = ["Stephen Sherratt <[email protected]>"]
66
license = "MIT"

core/src/frame_sig.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
use crate::{Buf, ConstBuf, Filter, Sig, SigCtx, SigT};
2-
use std::{cell::RefCell, fmt::Debug, rc::Rc};
2+
use std::{
3+
cell::RefCell,
4+
fmt::Debug,
5+
rc::Rc,
6+
sync::{Arc, RwLock},
7+
};
38

49
/// A signal with values produced each audio frame. This is distinct from the `SigT` trait whose
510
/// values are produced for each audio sample. Each audio frame corresponds to the sound driver
@@ -531,6 +536,40 @@ where
531536
})
532537
}
533538

539+
#[derive(Default)]
540+
pub struct FrameSigVar<T>(Arc<RwLock<T>>);
541+
542+
impl<T> FrameSigVar<T> {
543+
pub fn new(value: T) -> Self {
544+
Self(Arc::new(RwLock::new(value)))
545+
}
546+
547+
pub fn set(&self, value: T) {
548+
*self.0.write().unwrap() = value;
549+
}
550+
}
551+
552+
impl<T> Clone for FrameSigVar<T> {
553+
fn clone(&self) -> Self {
554+
Self(Arc::clone(&self.0))
555+
}
556+
}
557+
558+
impl<T> FrameSigT for FrameSigVar<T>
559+
where
560+
T: Clone,
561+
{
562+
type Item = T;
563+
564+
fn frame_sample(&mut self, _ctx: &SigCtx) -> Self::Item {
565+
self.0.read().unwrap().clone()
566+
}
567+
}
568+
569+
pub fn frame_sig_var<T: Clone>(value: T) -> FrameSig<FrameSigVar<T>> {
570+
FrameSig(FrameSigVar::new(value))
571+
}
572+
534573
pub struct FrameSigBoxed<T>(Box<dyn FrameSigT<Item = T>>);
535574

536575
impl<T> FrameSigT for FrameSigBoxed<T>

core/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ pub mod frame_sig_ops;
44
mod sig;
55
pub mod sig_ops;
66
pub use frame_sig::{
7-
frame_sig_shared, FrameSig, FrameSigEdges, FrameSigShared, FrameSigT,
8-
Triggerable,
7+
frame_sig_shared, frame_sig_var, FrameSig, FrameSigEdges, FrameSigShared,
8+
FrameSigT, FrameSigVar, Triggerable,
99
};
1010
pub use sig::{
1111
sig_shared, Buf, ConstBuf, Filter, GateToTrigRisingEdge, Sig, SigAbs,
1212
SigBoxed, SigCtx, SigSampleIntoBufT, SigShared, SigT,
1313
};
1414
pub mod stereo;
15-
pub use stereo::{Channel, Stereo};
15+
pub use stereo::{Channel, Stereo, StereoPair};

core/src/stereo.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub struct Stereo<L, R> {
3131
pub right: R,
3232
}
3333

34+
pub type StereoPair<T> = Stereo<T, T>;
35+
3436
impl<L, R> Stereo<L, R> {
3537
pub fn new(left: L, right: R) -> Self {
3638
Self { left, right }

interactive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "caw_interactive"
3-
version = "0.3.2"
3+
version = "0.3.3"
44
description = "Interactive keyboard and mouse control, and visualization for the caw synthesizer framework"
55
authors = ["Stephen Sherratt <[email protected]>"]
66
license = "MIT"

interactive/src/input.rs

Lines changed: 16 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
pub use caw_computer_keyboard::{Key, Keyboard as KeyboardGeneric};
2-
use caw_core::{FrameSig, FrameSigT, SigCtx};
2+
use caw_core::{frame_sig_var, FrameSig, FrameSigT, FrameSigVar};
33
use sdl2::{keyboard::Scancode, mouse::MouseButton as SdlMouseButton};
4-
use std::sync::{Arc, RwLock};
54

65
#[derive(Debug, Clone, Copy)]
76
pub enum MouseButton {
@@ -19,22 +18,6 @@ pub struct MouseGeneric<P, B> {
1918
pub middle: B,
2019
}
2120

22-
impl<P, B> MouseGeneric<P, B> {
23-
fn map<P_, B_, FP: FnMut(&P) -> P_, FB: FnMut(&B) -> B_>(
24-
&self,
25-
mut f_position: FP,
26-
mut f_button: FB,
27-
) -> MouseGeneric<P_, B_> {
28-
MouseGeneric {
29-
x_01: f_position(&self.x_01),
30-
y_01: f_position(&self.y_01),
31-
left: f_button(&self.left),
32-
right: f_button(&self.right),
33-
middle: f_button(&self.middle),
34-
}
35-
}
36-
}
37-
3821
impl<P: Clone, B: Clone> MouseGeneric<P, B> {
3922
pub fn button(&self, mouse_button: MouseButton) -> B {
4023
use MouseButton::*;
@@ -54,27 +37,9 @@ impl<P: Clone, B: Clone> MouseGeneric<P, B> {
5437
}
5538
}
5639

57-
/// A signal for keyboard and mouse inputs from sdl. Always yields the same value during any given
58-
/// frame.
59-
pub struct FrameSigInput<T: Copy>(Arc<RwLock<T>>);
60-
61-
impl<T: Copy> FrameSigT for FrameSigInput<T> {
62-
type Item = T;
63-
64-
fn frame_sample(&mut self, _ctx: &SigCtx) -> Self::Item {
65-
*self.0.read().unwrap()
66-
}
67-
}
68-
69-
impl<T: Copy> Clone for FrameSigInput<T> {
70-
fn clone(&self) -> Self {
71-
FrameSigInput(Arc::clone(&self.0))
72-
}
73-
}
74-
75-
pub type Keyboard = KeyboardGeneric<FrameSig<FrameSigInput<bool>>>;
40+
pub type Keyboard = KeyboardGeneric<FrameSig<FrameSigVar<bool>>>;
7641
pub type Mouse =
77-
MouseGeneric<FrameSig<FrameSigInput<f32>>, FrameSig<FrameSigInput<bool>>>;
42+
MouseGeneric<FrameSig<FrameSigVar<f32>>, FrameSig<FrameSigVar<bool>>>;
7843

7944
#[derive(Clone)]
8045
pub struct Input {
@@ -105,66 +70,18 @@ impl Input {
10570

10671
#[derive(Clone)]
10772
pub struct InputState {
108-
keyboard: KeyboardGeneric<Arc<RwLock<bool>>>,
109-
mouse: MouseGeneric<Arc<RwLock<f32>>, Arc<RwLock<bool>>>,
73+
keyboard: KeyboardGeneric<FrameSig<FrameSigVar<bool>>>,
74+
mouse:
75+
MouseGeneric<FrameSig<FrameSigVar<f32>>, FrameSig<FrameSigVar<bool>>>,
11076
}
11177

11278
impl InputState {
11379
pub(crate) fn new() -> Self {
114-
let mk_key = || Arc::new(RwLock::new(false));
115-
let mk_position = || Arc::new(RwLock::new(0.0));
116-
let mk_button = || Arc::new(RwLock::new(false));
80+
let mk_key = |_| frame_sig_var(false);
81+
let mk_position = || frame_sig_var(0.0);
82+
let mk_button = || frame_sig_var(false);
11783
Self {
118-
keyboard: KeyboardGeneric {
119-
a: mk_key(),
120-
b: mk_key(),
121-
c: mk_key(),
122-
d: mk_key(),
123-
e: mk_key(),
124-
f: mk_key(),
125-
g: mk_key(),
126-
h: mk_key(),
127-
i: mk_key(),
128-
j: mk_key(),
129-
k: mk_key(),
130-
l: mk_key(),
131-
m: mk_key(),
132-
n: mk_key(),
133-
o: mk_key(),
134-
p: mk_key(),
135-
q: mk_key(),
136-
r: mk_key(),
137-
s: mk_key(),
138-
t: mk_key(),
139-
u: mk_key(),
140-
v: mk_key(),
141-
w: mk_key(),
142-
x: mk_key(),
143-
y: mk_key(),
144-
z: mk_key(),
145-
n0: mk_key(),
146-
n1: mk_key(),
147-
n2: mk_key(),
148-
n3: mk_key(),
149-
n4: mk_key(),
150-
n5: mk_key(),
151-
n6: mk_key(),
152-
n7: mk_key(),
153-
n8: mk_key(),
154-
n9: mk_key(),
155-
left_bracket: mk_key(),
156-
right_bracket: mk_key(),
157-
semicolon: mk_key(),
158-
apostrophe: mk_key(),
159-
comma: mk_key(),
160-
period: mk_key(),
161-
minus: mk_key(),
162-
equals: mk_key(),
163-
slash: mk_key(),
164-
space: mk_key(),
165-
backspace: mk_key(),
166-
backslash: mk_key(),
167-
},
84+
keyboard: KeyboardGeneric::new(mk_key),
16885
mouse: MouseGeneric {
16986
x_01: mk_position(),
17087
y_01: mk_position(),
@@ -227,12 +144,12 @@ impl InputState {
227144
Scancode::Backslash => &self.keyboard.backslash,
228145
_ => return,
229146
};
230-
*key_state.write().unwrap() = pressed;
147+
key_state.0.set(pressed);
231148
}
232149

233150
pub(crate) fn set_mouse_position(&self, x_01: f32, y_01: f32) {
234-
*self.mouse.x_01.write().unwrap() = x_01;
235-
*self.mouse.y_01.write().unwrap() = y_01;
151+
self.mouse.x_01.0.set(x_01);
152+
self.mouse.y_01.0.set(y_01);
236153
}
237154

238155
pub(crate) fn set_mouse_button(
@@ -246,19 +163,15 @@ impl InputState {
246163
SdlMouseButton::Middle => &self.mouse.middle,
247164
_ => return,
248165
};
249-
*button_state.write().unwrap() = pressed;
166+
button_state.0.set(pressed);
250167
}
251168

252169
pub fn keyboard(&self) -> Keyboard {
253-
self.keyboard
254-
.map(|key| FrameSig(FrameSigInput(Arc::clone(key))))
170+
self.keyboard.clone()
255171
}
256172

257173
pub fn mouse(&self) -> Mouse {
258-
self.mouse.map(
259-
|position| FrameSig(FrameSigInput(Arc::clone(position))),
260-
|button| FrameSig(FrameSigInput(Arc::clone(button))),
261-
)
174+
self.mouse.clone()
262175
}
263176

264177
pub fn input(&self) -> Input {

0 commit comments

Comments
 (0)