rodio/examples/spatial.rs

37 lines
1.2 KiB
Rust
Raw Normal View History

2017-07-01 14:57:59 +00:00
extern crate rodio;
use std::io::BufReader;
use std::thread;
use std::time::Duration;
fn main() {
2018-04-18 19:23:40 +00:00
let device = rodio::default_output_device().unwrap();
let sink = rodio::SpatialSink::new(
2018-04-19 08:03:48 +00:00
&device,
[-10.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
);
2017-07-01 14:57:59 +00:00
let file = std::fs::File::open("examples/music.ogg").unwrap();
let source = rodio::Decoder::new(BufReader::new(file)).unwrap();
sink.append(source);
2017-07-03 08:09:10 +00:00
2017-07-01 14:57:59 +00:00
// A sound emitter playing the music starting at the left gradually moves to the right
2017-07-03 08:09:10 +00:00
// eventually passing through the listener, then it continues on to the right for a distance
// until it stops and begins traveling to the left, it will eventually pass through the
// listener again.
2017-07-01 14:57:59 +00:00
// This is repeated 5 times.
2017-07-03 08:09:10 +00:00
for _ in 0 .. 5 {
for i in 1 .. 1001 {
2017-07-01 14:57:59 +00:00
thread::sleep(Duration::from_millis(5));
sink.set_emitter_position([(i - 500) as f32 / 50.0, 0.0, 0.0]);
}
2017-07-03 08:09:10 +00:00
for i in 1 .. 1001 {
2017-07-01 14:57:59 +00:00
thread::sleep(Duration::from_millis(5));
sink.set_emitter_position([-(i - 500) as f32 / 50.0, 0.0, 0.0]);
}
}
sink.sleep_until_end();
}