add audio but better with kira
This commit is contained in:
39
src/main.rs
39
src/main.rs
@@ -1,15 +1,20 @@
|
||||
mod polygon_draw;
|
||||
|
||||
use iced::{
|
||||
Settings, Task, Theme,
|
||||
Task, Theme,
|
||||
time::{self, Duration},
|
||||
widget::{button, canvas, column, row, text},
|
||||
};
|
||||
use polygon_draw::{Polygon, PolygonFrame};
|
||||
|
||||
use std::f32::consts::PI;
|
||||
use std::io::BufReader;
|
||||
use std::time::Instant;
|
||||
|
||||
use kira::{
|
||||
AudioManager, AudioManagerSettings, DefaultBackend, sound::static_sound::StaticSoundData,
|
||||
};
|
||||
|
||||
fn main() -> iced::Result {
|
||||
iced::application("My App", MyApp::update, MyApp::view)
|
||||
.theme(|_| Theme::Dark)
|
||||
@@ -28,20 +33,27 @@ struct MyApp {
|
||||
polys: PolygonFrame,
|
||||
time_last_frame: Instant,
|
||||
nb_sec_for_rev: u32,
|
||||
audio_manager: AudioManager,
|
||||
default_sound: StaticSoundData,
|
||||
}
|
||||
|
||||
impl MyApp {
|
||||
fn new() -> (Self, Task<Message>) {
|
||||
let manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())
|
||||
.expect("Error to load AudioManager");
|
||||
let sound_data = StaticSoundData::from_file("assets/tick.ogg").expect("Fail to load audio");
|
||||
(
|
||||
Self {
|
||||
nb_sec_for_rev: 4,
|
||||
time_last_frame: Instant::now(),
|
||||
audio_manager: manager,
|
||||
default_sound: sound_data,
|
||||
polys: PolygonFrame {
|
||||
teta: 0.0,
|
||||
polygons: vec![
|
||||
//Polygon::n_gon(0.0, 12),
|
||||
Polygon::nr_10a_in_42(0.0),
|
||||
Polygon::nr_10b_in_42(0.0),
|
||||
Polygon::triangle(0.1),
|
||||
Polygon::square(1.0),
|
||||
],
|
||||
},
|
||||
},
|
||||
@@ -53,17 +65,27 @@ impl MyApp {
|
||||
Message::ButtonPressedIncrement => self.nb_sec_for_rev += 1,
|
||||
Message::ButtonPressedDecrement => {
|
||||
if self.nb_sec_for_rev > 1 {
|
||||
self.nb_sec_for_rev -= 1
|
||||
self.nb_sec_for_rev -= 1;
|
||||
}
|
||||
}
|
||||
Message::Tick => {
|
||||
let time_btw = Instant::now().duration_since(self.time_last_frame);
|
||||
let teta_temp = self.polys.teta;
|
||||
self.polys.teta += 2.0
|
||||
* PI
|
||||
* (1.0 / self.nb_sec_for_rev as f32)
|
||||
* (time_btw.as_millis() as f32 / 1_000.0);
|
||||
self.polys.teta %= 2.0 * PI;
|
||||
//println!("Teta : {}", self.polys.teta);
|
||||
if self
|
||||
.polys
|
||||
.have_point_polygon_btw(teta_temp, self.polys.teta)
|
||||
{
|
||||
self.audio_manager
|
||||
.play(self.default_sound.clone())
|
||||
.expect("Error to play sound");
|
||||
//do_sound("assets/tick.mp3");
|
||||
}
|
||||
self.time_last_frame = Instant::now();
|
||||
}
|
||||
}
|
||||
@@ -90,3 +112,12 @@ impl MyApp {
|
||||
time::every(Duration::from_millis(1)).map(|_| Message::Tick)
|
||||
}
|
||||
}
|
||||
/*
|
||||
fn do_sound(file_name: &str) {
|
||||
let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
|
||||
let sink = rodio::Sink::try_new(&handle).unwrap();
|
||||
|
||||
let file = std::fs::File::open(file_name).unwrap();
|
||||
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());
|
||||
sink.sleep_until_end();
|
||||
}*/
|
||||
|
||||
Reference in New Issue
Block a user