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
use parking_lot::Mutex;
use audiopus::{Bitrate, SampleRate};
use std::{
sync::Arc,
time::Duration,
};
pub const HEADER_LEN: usize = 12;
pub const SAMPLE_RATE: SampleRate = SampleRate::Hz48000;
pub const DEFAULT_BITRATE: Bitrate = Bitrate::BitsPerSecond(128_000);
pub trait AudioSource: Send {
fn is_stereo(&mut self) -> bool;
fn get_type(&self) -> AudioType;
fn read_pcm_frame(&mut self, buffer: &mut [i16]) -> Option<usize>;
fn read_opus_frame(&mut self) -> Option<Vec<u8>>;
fn decode_and_add_opus_frame(&mut self, float_buffer: &mut [f32; 1920], volume: f32) -> Option<usize>;
}
pub trait AudioReceiver: Send {
fn speaking_update(&mut self, _ssrc: u32, _user_id: u64, _speaking: bool) { }
#[allow(clippy::too_many_arguments)]
fn voice_packet(&mut self,
_ssrc: u32,
_sequence: u16,
_timestamp: u32,
_stereo: bool,
_data: &[i16],
_compressed_size: usize) { }
fn client_connect(&mut self, _ssrc: u32, _user_id: u64) { }
fn client_disconnect(&mut self, _user_id: u64) { }
}
#[derive(Clone, Copy)]
pub enum AudioType {
Opus,
Pcm,
#[doc(hidden)]
__Nonexhaustive,
}
pub struct Audio {
pub playing: bool,
pub volume: f32,
pub finished: bool,
pub source: Box<dyn AudioSource>,
pub position: Duration,
pub position_modified: bool,
}
impl Audio {
pub fn new(source: Box<dyn AudioSource>) -> Self {
Self {
playing: true,
volume: 1.0,
finished: false,
source,
position: Duration::new(0, 0),
position_modified: false,
}
}
pub fn play(&mut self) -> &mut Self {
self.playing = true;
self
}
pub fn pause(&mut self) -> &mut Self {
self.playing = false;
self
}
pub fn volume(&mut self, volume: f32) -> &mut Self {
self.volume = volume;
self
}
pub fn position(&mut self, position: Duration) -> &mut Self {
self.position = position;
self.position_modified = true;
self
}
pub(crate) fn step_frame(&mut self) {
self.position += Duration::from_millis(20);
self.position_modified = false;
}
}
pub type LockedAudio = Arc<Mutex<Audio>>;