-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource_gain_ext.mbt
More file actions
159 lines (146 loc) · 4.18 KB
/
Copy pathsource_gain_ext.mbt
File metadata and controls
159 lines (146 loc) · 4.18 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
// 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.
///|
fn gain_duration_to_nanos(duration : @moon_cpal.Duration) -> Double {
Double::from_int(duration.secs.to_int()) * 1_000_000_000.0 +
Double::from_int(duration.nanos)
}
///|
pub fn[S : Source] channel_volume(
source : S,
channel_factors : Array[Sample],
) -> DynSource {
guard !channel_factors.is_empty() else { panic() }
let src = to_dyn(source)
let from_channels = src.channels()
let out_channels = channel_factors.length()
let current_channel = @ref.new(out_channels)
let current_sample = @ref.new(None)
DynSource::new_dynamic(
fn() {
if current_channel.val >= out_channels {
current_channel.val = 0
current_sample.val = None
for _ in 0..<from_channels {
match src.next() {
None => ()
Some(s) =>
current_sample.val = Some(current_sample.val.unwrap_or(0.0) + s)
}
}
current_sample.val = current_sample.val.map(fn(v) {
v / Double::from_int(from_channels)
})
}
let result = current_sample.val.map(fn(v) {
v * channel_factors[current_channel.val]
})
current_channel.val += 1
result
},
fn() { out_channels },
fn() { src.sample_rate() },
)
}
///|
pub fn[S : Source] linear_gain_ramp(
source : S,
start_gain : Sample,
end_gain : Sample,
duration : @moon_cpal.Duration,
) -> DynSource {
linear_gain_ramp_with_clamp(source, start_gain, end_gain, duration, true)
}
///|
pub fn[S : Source] linear_gain_ramp_with_clamp(
source : S,
start_gain : Sample,
end_gain : Sample,
duration : @moon_cpal.Duration,
clamp_end : Bool,
) -> DynSource {
guard duration.secs > (0 : UInt64) || duration.nanos > 0 else { panic() }
let src = to_dyn(source)
let elapsed_ns = @ref.new(0.0)
let total_ns = gain_duration_to_nanos(duration)
let sample_idx = @ref.new(0)
DynSource::new_dynamic(
fn() {
match src.next() {
None => None
Some(v) => {
let remaining_ns = total_ns - elapsed_ns.val
let factor = if remaining_ns < 0.0 && clamp_end {
end_gain
} else if remaining_ns < 0.0 {
1.0
} else {
let p = elapsed_ns.val / total_ns
start_gain * (1.0 - p) + end_gain * p
}
sample_idx.val += 1
let channels = src.channels()
if channels > 0 && sample_idx.val % channels == 0 {
elapsed_ns.val += 1_000_000_000.0 /
Double::from_int(src.sample_rate())
}
Some(v * factor)
}
}
},
fn() { src.channels() },
fn() { src.sample_rate() },
current_span_len=fn() { src.current_span_len() },
total_duration=fn() { src.total_duration() },
try_seek=fn(pos : @moon_cpal.Duration) {
elapsed_ns.val = gain_duration_to_nanos(pos)
try {
src.try_seek(pos)
Ok(())
} catch {
err => Err(err)
}
},
)
}
///|
pub fn[S : Source] fade_in(
source : S,
duration : @moon_cpal.Duration,
) -> DynSource {
linear_gain_ramp(source, 0.0, 1.0, duration)
}
///|
pub fn[S : Source] fadein(
source : S,
duration : @moon_cpal.Duration,
) -> DynSource {
fade_in(source, duration)
}
///|
pub fn[S : Source] fade_out(
source : S,
duration : @moon_cpal.Duration,
) -> DynSource {
// Match rodio fadeout semantics: apply a forward linear ramp 1.0 -> 0.0,
// then keep output clamped at 0.0 after the ramp duration.
linear_gain_ramp(source, 1.0, 0.0, duration)
}
///|
pub fn[S : Source] fadeout(
source : S,
duration : @moon_cpal.Duration,
) -> DynSource {
fade_out(source, duration)
}