2019-05-11 20:39:46 +00:00
|
|
|
use common::assets;
|
2019-05-18 19:28:12 +00:00
|
|
|
use rand::prelude::*;
|
2019-05-11 20:39:46 +00:00
|
|
|
use rodio::{Decoder, Device, Source, SpatialSink};
|
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
fs::File,
|
|
|
|
io::BufReader,
|
|
|
|
path::PathBuf,
|
|
|
|
sync::mpsc::{channel, Receiver, Sender, TryRecvError},
|
|
|
|
thread,
|
|
|
|
thread::{sleep, JoinHandle},
|
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
use vek::*;
|
|
|
|
|
|
|
|
pub struct AudioFrontend {
|
|
|
|
device: Device,
|
2019-05-18 19:28:12 +00:00
|
|
|
// streams: HashMap<String, SpatialSink>, //always use SpatialSink even if no possition is used for now
|
|
|
|
stream: SpatialSink,
|
2019-05-11 20:39:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AudioFrontend {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let mut device = rodio::default_output_device().unwrap();
|
|
|
|
|
|
|
|
for d in rodio::devices() {
|
|
|
|
if d.name().contains("jack") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
device = d;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-05-18 19:28:12 +00:00
|
|
|
let mut sink =
|
|
|
|
rodio::SpatialSink::new(&device, [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [-1.0, 0.0, 0.0]);
|
|
|
|
|
2019-05-11 20:39:46 +00:00
|
|
|
AudioFrontend {
|
|
|
|
device,
|
2019-05-18 19:28:12 +00:00
|
|
|
// streams: HashMap::<String, SpatialSink>::new(),
|
|
|
|
stream: sink,
|
2019-05-11 20:39:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn play_music(&mut self, path: &str) {
|
2019-05-18 19:28:12 +00:00
|
|
|
let bufreader = assets::load_from_path(path).unwrap();
|
|
|
|
let src = Decoder::new(bufreader).unwrap();
|
2019-05-11 20:39:46 +00:00
|
|
|
|
|
|
|
let mut sink = rodio::SpatialSink::new(
|
|
|
|
&self.device,
|
|
|
|
[0.0, 0.0, 0.0],
|
|
|
|
[1.0, 0.0, 0.0],
|
|
|
|
[-1.0, 0.0, 0.0],
|
|
|
|
);
|
|
|
|
|
|
|
|
sink.append(src);
|
|
|
|
|
2019-05-18 19:28:12 +00:00
|
|
|
// self.streams.insert(path.to_string(), sink);
|
|
|
|
self.stream = sink;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maintain(&mut self) {
|
|
|
|
let music = [
|
|
|
|
"voxygen/audio/soundtrack/Ethereal_Bonds.ogg",
|
2019-05-18 20:26:16 +00:00
|
|
|
"voxygen/audio/soundtrack/Field_Grazing.mp3",
|
2019-05-18 19:28:12 +00:00
|
|
|
"voxygen/audio/soundtrack/fiesta_del_pueblo.ogg",
|
|
|
|
"voxygen/audio/soundtrack/library_theme_with_harpsichord.ogg",
|
|
|
|
"voxygen/audio/soundtrack/Mineral_Deposits.ogg",
|
|
|
|
"voxygen/audio/soundtrack/Ruination.ogg",
|
|
|
|
"voxygen/audio/soundtrack/sacred_temple.ogg",
|
|
|
|
"voxygen/audio/soundtrack/Snowtop.ogg",
|
|
|
|
"voxygen/audio/soundtrack/veloren_title_tune-3.ogg",
|
|
|
|
];
|
|
|
|
if self.stream.empty() {
|
|
|
|
let i = rand::random::<usize>() % music.len();
|
|
|
|
self.play_music(music[i])
|
|
|
|
}
|
2019-05-11 20:39:46 +00:00
|
|
|
}
|
|
|
|
}
|