Skip to content

Commit 54dd65b

Browse files
committed
Minor conveniences
1 parent ee00e6b commit 54dd65b

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

core/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ pub fn svf32(
2626
sig_boxed_var(initial_sig)
2727
}
2828

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+
2936
impl<T> Stereo<SV<T>, SV<T>>
3037
where
3138
T: Clone,

core/src/sig.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ where
393393
}
394394

395395
impl SigT for f32 {
396-
type Item = f32;
396+
type Item = Self;
397397

398398
fn sample(&mut self, ctx: &SigCtx) -> impl Buf<Self::Item> {
399399
ConstBuf {
@@ -404,7 +404,7 @@ impl SigT for f32 {
404404
}
405405

406406
impl SigT for u32 {
407-
type Item = u32;
407+
type Item = Self;
408408

409409
fn sample(&mut self, ctx: &SigCtx) -> impl Buf<Self::Item> {
410410
ConstBuf {
@@ -416,7 +416,7 @@ impl SigT for u32 {
416416

417417
/// For gate and trigger signals
418418
impl SigT for bool {
419-
type Item = bool;
419+
type Item = Self;
420420

421421
fn sample(&mut self, ctx: &SigCtx) -> impl Buf<Self::Item> {
422422
ConstBuf {

keyboard/src/a440_12tet.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// 12-tone equal temperament following the A440Hz convention
2-
use caw_core::{Sig, SigT};
2+
use caw_core::{Buf, ConstBuf, Sig, SigCtx, SigT};
33

44
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
55
pub struct Octave(u8);
@@ -220,8 +220,28 @@ where
220220
}
221221
}
222222

223+
impl SigT for Note {
224+
type Item = Self;
225+
226+
fn sample(&mut self, ctx: &SigCtx) -> impl Buf<Self::Item> {
227+
ConstBuf {
228+
value: *self,
229+
count: ctx.num_samples,
230+
}
231+
}
232+
}
233+
234+
/// Arbitrary default value to help when a temporary value must be set before updating a note by
235+
/// some other process, as signals cannot have undefined values. The default note is C4.
236+
impl Default for Note {
237+
fn default() -> Self {
238+
note::C4
239+
}
240+
}
241+
223242
pub mod chord {
224-
use super::{Note, NoteName, Octave};
243+
use super::{Note, NoteName, Octave, note_name};
244+
use caw_core::{Buf, ConstBuf, SigCtx, SigT};
225245
use smallvec::{SmallVec, smallvec};
226246

227247
pub struct Notes(SmallVec<[Note; 4]>);
@@ -424,7 +444,7 @@ pub mod chord {
424444
}
425445

426446
impl Chord {
427-
pub fn new(root: NoteName, typ: ChordType) -> Self {
447+
pub const fn new(root: NoteName, typ: ChordType) -> Self {
428448
Self {
429449
root,
430450
typ,
@@ -505,9 +525,28 @@ pub mod chord {
505525
}
506526
}
507527

508-
pub fn chord(root: NoteName, typ: ChordType) -> Chord {
528+
pub const fn chord(root: NoteName, typ: ChordType) -> Chord {
509529
Chord::new(root, typ)
510530
}
531+
532+
impl SigT for Chord {
533+
type Item = Self;
534+
535+
fn sample(&mut self, ctx: &SigCtx) -> impl Buf<Self::Item> {
536+
ConstBuf {
537+
value: *self,
538+
count: ctx.num_samples,
539+
}
540+
}
541+
}
542+
543+
/// Arbitrary default value to help when a temporary value must be set before updating a note by
544+
/// some other process, as signals cannot have undefined values. The default chord is C major.
545+
impl Default for Chord {
546+
fn default() -> Self {
547+
chord(note_name::C, MAJOR)
548+
}
549+
}
511550
}
512551

513552
impl Note {

keyboard/src/event.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ impl KeyEvents {
5757
}
5858
}
5959

60+
impl SigT for KeyEvents {
61+
type Item = Self;
62+
63+
fn sample(&mut self, ctx: &SigCtx) -> impl Buf<Self::Item> {
64+
ConstBuf {
65+
value: self.clone(),
66+
count: ctx.num_samples,
67+
}
68+
}
69+
}
70+
6071
impl IntoIterator for KeyEvents {
6172
type Item = KeyEvent;
6273

0 commit comments

Comments
 (0)