-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource_controls.mbt
More file actions
290 lines (243 loc) · 5.68 KB
/
source_controls.mbt
File metadata and controls
290 lines (243 loc) · 5.68 KB
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
// Copyright 2026 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///|
pub struct Pausable {
input : DynSource
paused_channels : Ref[ChannelCount?]
remaining_paused_samples : Ref[Int]
}
///|
pub fn[S : Source] pausable(source : S, paused : Bool) -> Pausable {
let input = to_dyn(source)
{
input,
paused_channels: @ref.new(
if paused {
Some(input.channels())
} else {
None
},
),
remaining_paused_samples: @ref.new(0),
}
}
///|
pub fn Pausable::set_paused(self : Pausable, paused : Bool) -> Unit {
match (self.paused_channels.val, paused) {
(None, true) => self.paused_channels.val = Some(self.input.channels())
(Some(_), false) => self.paused_channels.val = None
_ => ()
}
}
///|
pub fn Pausable::is_paused(self : Pausable) -> Bool {
self.paused_channels.val is Some(_)
}
///|
pub fn Pausable::inner(self : Pausable) -> DynSource {
self.input
}
///|
pub fn Pausable::inner_mut(self : Pausable) -> DynSource {
self.input
}
///|
pub fn Pausable::into_inner(self : Pausable) -> DynSource {
self.input
}
///|
pub fn Pausable::next(self : Pausable) -> Sample? {
if self.remaining_paused_samples.val > 0 {
self.remaining_paused_samples.val -= 1
return Some(0.0)
}
match self.paused_channels.val {
Some(paused_channels) => {
self.remaining_paused_samples.val = paused_channels - 1
Some(0.0)
}
None => self.input.next()
}
}
///|
pub fn Pausable::channels(self : Pausable) -> ChannelCount {
self.input.channels()
}
///|
pub fn Pausable::sample_rate(self : Pausable) -> SampleRate {
self.input.sample_rate()
}
///|
pub impl Source for Pausable with next(self : Pausable) {
self.next()
}
///|
pub impl Source for Pausable with channels(self : Pausable) {
self.channels()
}
///|
pub impl Source for Pausable with sample_rate(self : Pausable) {
self.sample_rate()
}
///|
pub impl Source for Pausable with current_span_len(_self : Pausable) {
_self.input.current_span_len()
}
///|
pub impl Source for Pausable with total_duration(_self : Pausable) {
_self.input.total_duration()
}
///|
pub impl Source for Pausable with try_seek(
_self : Pausable,
pos : @moon_cpal.Duration,
) -> Unit raise SeekError {
_self.input.try_seek(pos)
}
///|
pub struct Stoppable {
input : DynSource
stopped : Ref[Bool]
}
///|
pub fn[S : Source] stoppable(source : S) -> Stoppable {
{ input: to_dyn(source), stopped: @ref.new(false) }
}
///|
pub fn Stoppable::stop(self : Stoppable) -> Unit {
self.stopped.val = true
}
///|
pub fn Stoppable::inner(self : Stoppable) -> DynSource {
self.input
}
///|
pub fn Stoppable::inner_mut(self : Stoppable) -> DynSource {
self.input
}
///|
pub fn Stoppable::into_inner(self : Stoppable) -> DynSource {
self.input
}
///|
pub fn Stoppable::next(self : Stoppable) -> Sample? {
if self.stopped.val {
None
} else {
self.input.next()
}
}
///|
pub fn Stoppable::channels(self : Stoppable) -> ChannelCount {
self.input.channels()
}
///|
pub fn Stoppable::sample_rate(self : Stoppable) -> SampleRate {
self.input.sample_rate()
}
///|
pub impl Source for Stoppable with next(self : Stoppable) {
self.next()
}
///|
pub impl Source for Stoppable with channels(self : Stoppable) {
self.channels()
}
///|
pub impl Source for Stoppable with sample_rate(self : Stoppable) {
self.sample_rate()
}
///|
pub impl Source for Stoppable with current_span_len(_self : Stoppable) {
_self.input.current_span_len()
}
///|
pub impl Source for Stoppable with total_duration(_self : Stoppable) {
_self.input.total_duration()
}
///|
pub impl Source for Stoppable with try_seek(
_self : Stoppable,
pos : @moon_cpal.Duration,
) -> Unit raise SeekError {
_self.input.try_seek(pos)
}
///|
pub struct Skippable {
input : DynSource
do_skip : Ref[Bool]
}
///|
pub fn[S : Source] skippable(source : S) -> Skippable {
{ input: to_dyn(source), do_skip: @ref.new(false) }
}
///|
pub fn Skippable::skip(self : Skippable) -> Unit {
self.do_skip.val = true
}
///|
pub fn Skippable::inner(self : Skippable) -> DynSource {
self.input
}
///|
pub fn Skippable::inner_mut(self : Skippable) -> DynSource {
self.input
}
///|
pub fn Skippable::into_inner(self : Skippable) -> DynSource {
self.input
}
///|
pub fn Skippable::next(self : Skippable) -> Sample? {
if self.do_skip.val {
None
} else {
self.input.next()
}
}
///|
pub fn Skippable::channels(self : Skippable) -> ChannelCount {
self.input.channels()
}
///|
pub fn Skippable::sample_rate(self : Skippable) -> SampleRate {
self.input.sample_rate()
}
///|
pub impl Source for Skippable with next(self : Skippable) {
self.next()
}
///|
pub impl Source for Skippable with channels(self : Skippable) {
self.channels()
}
///|
pub impl Source for Skippable with sample_rate(self : Skippable) {
self.sample_rate()
}
///|
pub impl Source for Skippable with current_span_len(_self : Skippable) {
_self.input.current_span_len()
}
///|
pub impl Source for Skippable with total_duration(_self : Skippable) {
_self.input.total_duration()
}
///|
pub impl Source for Skippable with try_seek(
_self : Skippable,
pos : @moon_cpal.Duration,
) -> Unit raise SeekError {
_self.input.try_seek(pos)
}