1 extern crate alto;
2
3 use std::sync::Arc;
4 use alto::*;
5
6
main()7 fn main() {
8 let alto = if let Ok(alto) = Alto::load_default() {
9 alto
10 } else {
11 println!("No OpenAL implementation present!");
12 return;
13 };
14
15 println!("Using output: {:?}", alto.default_output().unwrap());
16 let dev = alto.open(None).unwrap();
17 let ctx = dev.new_context(None).unwrap();
18
19 let mut slot = if dev.is_extension_present(alto::ext::Alc::Efx) {
20 println!("Using EFX reverb");
21 if let Ok(slot) = (|| -> AltoResult<_> {
22 let mut slot = ctx.new_aux_effect_slot()?;
23 let mut reverb: efx::EaxReverbEffect = ctx.new_effect()?;
24 reverb.set_preset(&efx::REVERB_PRESET_GENERIC)?;
25 slot.set_effect(&reverb)?;
26 Ok(slot)
27 })() {
28 Some(slot)
29 } else {
30 println!("Broken router detected; disabling EFX");
31 None
32 }
33 } else {
34 println!("EFX not present");
35 None
36 };
37
38 {
39 let buf = ctx.new_buffer(SinWave::new(44_000 / 440, 0.25).render().take(44_000 / 440).collect::<Vec<_>>(), 44_000).unwrap();
40 let buf = Arc::new(buf);
41
42 let mut src = ctx.new_static_source().unwrap();
43 src.set_buffer(buf).unwrap();
44 src.set_looping(true);
45 if let Some(ref mut slot) = slot {
46 src.set_aux_send(0, slot).unwrap();
47 }
48
49 println!("Playing static 440hz sine wave...");
50 src.play();
51
52 std::thread::sleep(std::time::Duration::new(2, 0));
53 }
54
55 std::thread::sleep(std::time::Duration::new(1, 0));
56
57 {
58 let mut wave = SinWave::new(44_000 / 220, 0.25);
59
60 let mut src = ctx.new_streaming_source().unwrap();
61 if let Some(ref mut slot) = slot {
62 src.set_aux_send(0, slot).unwrap();
63 }
64 for _ in 0 .. 5 {
65 let buf = ctx.new_buffer(wave.render().take(44_000 / 10).collect::<Vec<_>>(), 44_000).unwrap();
66 src.queue_buffer(buf).unwrap();
67 }
68
69 println!("Playing streaming 220hz sine wave...");
70 src.play();
71
72 for _ in 0 .. 15 {
73 while src.buffers_processed() == 0 { }
74
75 let mut buf = src.unqueue_buffer().unwrap();
76 buf.set_data(wave.render().take(44_000 / 10).collect::<Vec<_>>(), 44_000).unwrap();
77 src.queue_buffer(buf).unwrap();
78 }
79
80 while src.buffers_processed() < 5 { }
81 }
82
83 std::thread::sleep(std::time::Duration::new(1, 0));
84 }
85
86
87 struct SinWave {
88 len: i32,
89 vol: f32,
90 cursor: i32,
91 }
92
93 struct SinWaveRenderer<'w>(&'w mut SinWave);
94
95
96 impl SinWave {
new(len: i32, vol: f32) -> SinWave97 pub fn new(len: i32, vol: f32) -> SinWave {
98 SinWave{len: len, vol: vol, cursor: 0}
99 }
100
101
render(&mut self) -> SinWaveRenderer102 pub fn render(&mut self) -> SinWaveRenderer {
103 SinWaveRenderer(self)
104 }
105 }
106
107
108 impl<'w> Iterator for SinWaveRenderer<'w> {
109 type Item = Mono<i16>;
110
next(&mut self) -> Option<Mono<i16>>111 fn next(&mut self) -> Option<Mono<i16>> {
112 let cursor = self.0.cursor;
113 self.0.cursor += 1;
114 if self.0.cursor == self.0.len { self.0.cursor = 0 }
115
116 Some(Mono{center: ((cursor as f32 / self.0.len as f32 * 2.0 * std::f32::consts::PI).sin() * self.0.vol * std::i16::MAX as f32) as i16})
117 }
118 }
119