1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use crate::server::Config;
use eyre::{eyre, Result};
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Formatter};
use std::path::Path;
use std::time::Duration;
#[cfg(feature = "rodio")]
mod rodio;
#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum AudioBackend {
None,
Inherit,
#[cfg(feature = "rodio")]
Rodio,
}
#[derive(Clone)]
pub enum AudioBuffer {
None,
#[cfg(feature = "rodio")]
Rodio(rodio::Buffer),
}
pub enum AudioSink {
None,
#[cfg(feature = "rodio")]
Rodio(rodio::Sink),
}
pub enum AudioDevice {
None,
#[cfg(feature = "rodio")]
Rodio(rodio::Device),
}
impl Default for AudioBackend {
#[inline(always)]
fn default() -> Self {
AudioBackend::Inherit
}
}
impl AudioBackend {
pub fn or(&self, other: &Self) -> Self {
if let Self::Inherit = self {
*other
} else {
*self
}
}
}
#[derive(Copy, Clone, Deserialize, Serialize)]
#[serde(untagged)]
pub enum Volume {
Inherit,
Value(f32),
}
impl Default for Volume {
#[inline(always)]
fn default() -> Self {
Volume::Inherit
}
}
impl Debug for Volume {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if let Volume::Value(vol) = self {
write!(f, "{vol}")
} else {
write!(f, "inherit")
}
}
}
impl Volume {
pub fn and(&self, other: &Self) -> Self {
match (self, other) {
(&Self::Value(x), &Self::Value(y)) => Self::Value(x * y),
(Self::Value(_), _) => *self,
(Self::Inherit, _) => *other,
}
}
pub fn or(&self, other: &Self) -> Self {
if let Self::Inherit = self {
*other
} else {
*self
}
}
pub fn value(&self) -> f32 {
if let &Self::Value(x) = self {
x
} else {
1.0
}
}
}
impl From<f32> for Volume {
fn from(v: f32) -> Self {
Self::Value(v.max(0.0))
}
}
#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum UseTrigger {
Inherit,
Yes,
No,
}
impl Default for UseTrigger {
#[inline(always)]
fn default() -> Self {
UseTrigger::Inherit
}
}
impl UseTrigger {
pub fn or(&self, other: &Self) -> Self {
if let Self::Inherit = self {
*other
} else {
*self
}
}
pub fn value(&self) -> bool {
!matches!(self, UseTrigger::No)
}
}
#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum TimePrecision {
Inherit,
RespectIntervals,
RespectBoundaries,
}
impl Default for TimePrecision {
#[inline(always)]
fn default() -> Self {
TimePrecision::Inherit
}
}
impl TimePrecision {
pub fn or(&self, other: &Self) -> Self {
if let Self::Inherit = self {
*other
} else {
*self
}
}
}
#[allow(unused_variables)]
pub fn audio_from_file(path: &Path, config: &Config) -> Result<AudioBuffer> {
match config.audio_backend() {
AudioBackend::None => Err(eyre!("Cannot load audio file with backend=None.")),
AudioBackend::Inherit => Err(eyre!("Cannot load audio file with backend=Inherit.")),
#[cfg(feature = "rodio")]
AudioBackend::Rodio => rodio::Buffer::new(path, config).map(AudioBuffer::Rodio),
}
}
impl AudioBuffer {
pub fn duration(&self) -> Duration {
match self {
AudioBuffer::None => Duration::default(),
#[cfg(feature = "rodio")]
AudioBuffer::Rodio(x) => x.duration(),
}
}
pub fn sample_rate(&self) -> u32 {
match self {
AudioBuffer::None => 0,
#[cfg(feature = "rodio")]
AudioBuffer::Rodio(x) => x.sample_rate(),
}
}
pub fn channels(&self) -> u16 {
match self {
AudioBuffer::None => 0,
#[cfg(feature = "rodio")]
AudioBuffer::Rodio(x) => x.channels(),
}
}
pub fn interlace(self, other: AudioBuffer) -> Result<AudioBuffer> {
match (self, other) {
#[cfg(feature = "rodio")]
(AudioBuffer::Rodio(x), AudioBuffer::Rodio(y)) => {
x.interlace(y).map(AudioBuffer::Rodio)
}
(_, _) => Err(eyre!("Cannot interlace audio buffers of different types.")),
}
}
pub fn drop_last(self) -> Result<AudioBuffer> {
match self {
AudioBuffer::None => Err(eyre!("Cannot interlace audio buffers of different types.")),
#[cfg(feature = "rodio")]
AudioBuffer::Rodio(x) => x.drop_last().map(AudioBuffer::Rodio),
}
}
}
impl AudioSink {
pub fn pause(&mut self) -> Result<()> {
match self {
AudioSink::None => Err(eyre!("Cannot pause audio sink with backend=None.")),
#[cfg(feature = "rodio")]
AudioSink::Rodio(sink) => {
sink.pause();
Ok(())
}
}
}
#[allow(unused_variables)]
pub fn set_volume(&mut self, volume: f32) -> Result<()> {
match self {
AudioSink::None => Err(eyre!("Cannot pause audio sink with backend=None.")),
#[cfg(feature = "rodio")]
AudioSink::Rodio(sink) => {
sink.set_volume(volume);
Ok(())
}
}
}
pub fn queue(&mut self, buffer: AudioBuffer) -> Result<()> {
match (self, buffer) {
(AudioSink::None, _) => Err(eyre!("Cannot queue audio on sink=None.")),
#[cfg(feature = "rodio")]
(AudioSink::Rodio(sink), AudioBuffer::Rodio(buffer)) => {
sink.queue(buffer);
Ok(())
}
#[allow(unreachable_patterns)]
(_, _) => Err(eyre!("Cannot queue audio on incompatible sink.")),
}
}
pub fn repeat(&mut self, buffer: AudioBuffer) -> Result<()> {
match (self, buffer) {
(AudioSink::None, _) => Err(eyre!("Cannot repeat audio on sink=None.")),
#[cfg(feature = "rodio")]
(AudioSink::Rodio(sink), AudioBuffer::Rodio(buffer)) => {
sink.repeat(buffer);
Ok(())
}
#[allow(unreachable_patterns)]
(_, _) => Err(eyre!("Cannot repeat audio on incompatible sink.")),
}
}
pub fn play(&mut self) -> Result<()> {
match self {
AudioSink::None => Err(eyre!("Cannot play audio sink with backend=None.")),
#[cfg(feature = "rodio")]
AudioSink::Rodio(sink) => {
sink.play();
Ok(())
}
}
}
pub fn stop(&mut self) -> Result<()> {
match self {
AudioSink::None => Err(eyre!("Cannot stop audio sink with backend=None.")),
#[cfg(feature = "rodio")]
AudioSink::Rodio(sink) => {
sink.stop();
Ok(())
}
}
}
pub fn empty(&self) -> Result<bool> {
match self {
AudioSink::None => Ok(true),
#[cfg(feature = "rodio")]
AudioSink::Rodio(sink) => Ok(sink.empty()),
}
}
pub fn detach(self) -> Result<()> {
match self {
AudioSink::None => Ok(()),
#[cfg(feature = "rodio")]
AudioSink::Rodio(sink) => {
sink.detach();
Ok(())
}
}
}
}
impl AudioDevice {
pub fn new(config: &Config) -> Result<Self> {
match config.audio_backend() {
AudioBackend::None => Err(eyre!("Cannot obtain audio device with backend=None.")),
AudioBackend::Inherit => Err(eyre!("Cannot obtain audio device with backend=None.")),
#[cfg(feature = "rodio")]
AudioBackend::Rodio => rodio::Device::new().map(Self::Rodio),
}
}
pub fn sink(&self) -> Result<AudioSink> {
match self {
AudioDevice::None => Err(eyre!("Cannot create audio sink with backend=None.")),
#[cfg(feature = "rodio")]
AudioDevice::Rodio(device) => device.sink().map(AudioSink::Rodio),
}
}
}