Skip to main content

xsynth_core/
voice.rs

1#![allow(dead_code)]
2#![allow(non_camel_case_types)] // For the SIMD library
3
4mod envelopes;
5pub(crate) use envelopes::*;
6
7mod simd;
8pub(crate) use simd::*;
9
10mod simdvoice;
11pub(crate) use simdvoice::*;
12
13mod base;
14pub(crate) use base::*;
15
16mod squarewave;
17#[allow(unused_imports)]
18pub(crate) use squarewave::*;
19
20mod channels;
21#[allow(unused_imports)]
22pub(crate) use channels::*;
23
24mod constant;
25pub(crate) use constant::*;
26
27mod sampler;
28pub(crate) use sampler::*;
29
30mod control;
31pub(crate) use control::*;
32
33mod cutoff;
34pub(crate) use cutoff::*;
35
36/// Options to modify the envelope of a voice.
37#[derive(Copy, Clone)]
38pub struct EnvelopeControlData {
39    /// Controls the attack. Can take values from 0 to 128
40    /// according to the MIDI CC spec.
41    pub attack: Option<u8>,
42
43    /// Controls the release. Can take values from 0 to 128
44    /// according to the MIDI CC spec.
45    pub release: Option<u8>,
46}
47
48/// How a voice should be released.
49#[derive(Copy, Clone, PartialEq)]
50pub enum ReleaseType {
51    /// Standard release. Uses the voice's envelope.
52    Standard,
53
54    /// Kills the voice with a fadeout of 1ms.
55    Kill,
56}
57
58/// Options to control the parameters of a voice.
59#[derive(Copy, Clone)]
60pub struct VoiceControlData {
61    /// Pitch multiplier
62    pub voice_pitch_multiplier: f32,
63
64    /// Envelope control
65    pub envelope: EnvelopeControlData,
66}
67
68impl VoiceControlData {
69    pub fn new_defaults() -> Self {
70        VoiceControlData {
71            voice_pitch_multiplier: 1.0,
72            envelope: EnvelopeControlData {
73                attack: None,
74                release: None,
75            },
76        }
77    }
78}
79
80pub trait VoiceGeneratorBase: Sync + Send {
81    fn ended(&self) -> bool;
82    fn signal_release(&mut self, rel_type: ReleaseType);
83    fn process_controls(&mut self, control: &VoiceControlData);
84}
85
86pub trait VoiceSampleGenerator: VoiceGeneratorBase {
87    fn render_to(&mut self, buffer: &mut [f32]);
88}
89
90pub trait Voice: VoiceSampleGenerator + Send + Sync {
91    fn is_releasing(&self) -> bool;
92    fn is_killed(&self) -> bool;
93
94    fn velocity(&self) -> u8;
95    fn exclusive_class(&self) -> Option<u8>;
96}