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
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::Weak;
use std::thread::Builder;
use cpal::Device;
use cpal::EventLoop;
use cpal::Sample as CpalSample;
use cpal::StreamData;
use cpal::StreamId;
use cpal::UnknownTypeOutputBuffer;
use dynamic_mixer;
use source::Source;
pub fn play_raw<S>(device: &Device, source: S)
where
S: Source<Item = f32> + Send + 'static,
{
lazy_static! {
static ref ENGINE: Arc<Engine> = {
let engine = Arc::new(Engine {
events_loop: EventLoop::new(),
dynamic_mixers: Mutex::new(HashMap::with_capacity(1)),
end_points: Mutex::new(HashMap::with_capacity(1)),
});
Builder::new()
.name("rodio audio processing".to_string())
.spawn({
let engine = engine.clone();
move || {
engine.events_loop.run(|stream_id, buffer| {
audio_callback(&engine, stream_id, buffer);
})
}
})
.ok()
.map(|jg| jg.thread().clone());
engine
};
}
start(&ENGINE, device, source);
}
struct Engine {
events_loop: EventLoop,
dynamic_mixers: Mutex<HashMap<StreamId, dynamic_mixer::DynamicMixer<f32>>>,
end_points: Mutex<HashMap<String, Weak<dynamic_mixer::DynamicMixerController<f32>>>>,
}
fn audio_callback(engine: &Arc<Engine>, stream_id: StreamId, buffer: StreamData) {
let mut dynamic_mixers = engine.dynamic_mixers.lock().unwrap();
let mixer_rx = match dynamic_mixers.get_mut(&stream_id) {
Some(m) => m,
None => return,
};
match buffer {
StreamData::Output {
buffer: UnknownTypeOutputBuffer::U16(mut buffer),
} => for d in buffer.iter_mut() {
*d = mixer_rx
.next()
.map(|s| s.to_u16())
.unwrap_or(u16::max_value() / 2);
},
StreamData::Output {
buffer: UnknownTypeOutputBuffer::I16(mut buffer),
} => for d in buffer.iter_mut() {
*d = mixer_rx.next().map(|s| s.to_i16()).unwrap_or(0i16);
},
StreamData::Output {
buffer: UnknownTypeOutputBuffer::F32(mut buffer),
} => for d in buffer.iter_mut() {
*d = mixer_rx.next().unwrap_or(0f32);
},
StreamData::Input { buffer: _ } => {
panic!("Can't play an input stream!");
},
};
}
fn start<S>(engine: &Arc<Engine>, device: &Device, source: S)
where
S: Source<Item = f32> + Send + 'static,
{
let mut stream_to_start = None;
let mixer = {
let mut end_points = engine.end_points.lock().unwrap();
match end_points.entry(device.name()) {
Entry::Vacant(e) => {
let (mixer, stream) = new_output_stream(engine, device);
e.insert(Arc::downgrade(&mixer));
stream_to_start = Some(stream);
mixer
},
Entry::Occupied(mut e) => {
if let Some(m) = e.get().upgrade() {
m.clone()
} else {
let (mixer, stream) = new_output_stream(engine, device);
e.insert(Arc::downgrade(&mixer));
stream_to_start = Some(stream);
mixer
}
},
}
};
if let Some(stream) = stream_to_start {
engine.events_loop.play_stream(stream);
}
mixer.add(source);
}
fn new_output_stream(
engine: &Arc<Engine>, device: &Device,
) -> (Arc<dynamic_mixer::DynamicMixerController<f32>>, StreamId) {
let format = device
.default_output_format()
.expect("The device doesn't support any format!?");
let stream_id = engine
.events_loop
.build_output_stream(device, &format)
.unwrap();
let (mixer_tx, mixer_rx) =
{ dynamic_mixer::mixer::<f32>(format.channels, format.sample_rate.0) };
engine
.dynamic_mixers
.lock()
.unwrap()
.insert(stream_id.clone(), mixer_rx);
(mixer_tx, stream_id)
}