-
Notifications
You must be signed in to change notification settings - Fork 0
/
windowed.rs
142 lines (122 loc) · 3.19 KB
/
windowed.rs
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
use super::{TimeSpan, DirectAccess, FloatWindow, Resettable};
use rodio::{Source, Sample as Sample_};
use std::fmt;
use std::time::Duration;
use std::ops::Deref;
pub struct FloatWindowSource<T>
where T: DirectAccess,
<T as Iterator>::Item: Sample_
{
start: usize,
end: usize,
source: T
}
impl<T> FloatWindowSource<T>
where T: DirectAccess,
<T as Iterator>::Item: Sample_
{
#[inline]
pub fn new(source: T) -> Self {
FloatWindowSource {
source,
start: 0,
end: 0
}
}
#[inline]
fn sample_rate_for_all_channels(&self) -> usize {
self.source.channels() as usize * self.source.sample_rate() as usize
}
}
impl<T> Resettable for FloatWindowSource<T>
where T: DirectAccess,
<T as Iterator>::Item: Sample_
{
#[inline]
fn reset(&mut self) {
self.source.set_current(self.start);
}
}
impl<T> FloatWindow for FloatWindowSource<T>
where T: DirectAccess,
<T as Iterator>::Item: Sample_
{
#[inline]
fn play(&mut self, time: TimeSpan) {
let multiplayer = self.sample_rate_for_all_channels();
self.start = multiplayer * time.start as usize;
self.end = self.start + (multiplayer * time.duration as usize);
if self.source.current() < self.start || self.source.current() > self.end {
self.source.set_current(self.start);
}
}
#[inline]
fn cursor(&self) -> usize {
self.source.current()
}
#[inline]
fn end(&self) -> usize {
self.end
}
}
impl<T> Source for FloatWindowSource<T>
where T: DirectAccess,
<T as Iterator>::Item: Sample_
{
#[inline]
fn current_frame_len(&self) -> Option<usize> {
self.source.current_frame_len()
}
#[inline]
fn channels(&self) -> u16 {
self.source.channels()
}
#[inline]
fn sample_rate(&self) -> u32 {
self.source.sample_rate()
}
#[inline]
fn total_duration(&self) -> Option<Duration> {
self.source.total_duration()
}
}
impl<T> Deref for FloatWindowSource<T>
where T: DirectAccess,
<T as Iterator>::Item: Sample_
{
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
&self.source
}
}
impl<T> Iterator for FloatWindowSource<T>
where T: DirectAccess,
<T as Iterator>::Item: Sample_
{
type Item = <T as Iterator>::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.current() < self.end {
self.source.next()
} else {
None
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.source.size_hint()
}
}
impl<T> fmt::Debug for FloatWindowSource<T>
where T: DirectAccess,
<T as Iterator>::Item: Sample_
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("FloatWindowSource")
.field("start", &self.start)
.field("end", &self.end)
.field("source", &self.source)
.finish()
}
}