sound by polygons and ui for add polygons and change global rotation

This commit is contained in:
2025-06-02 14:43:10 +02:00
parent 051d4b66e5
commit 26d7c9c514
3 changed files with 123 additions and 38 deletions

BIN
assets/ah.ogg Normal file

Binary file not shown.

View File

@@ -1,9 +1,9 @@
mod polygon_draw; mod polygon_draw;
use iced::{ use iced::{
Task, Theme, Element, Task, Theme,
time::{self, Duration}, time::{self, Duration},
widget::{button, canvas, column, row, text}, widget::{Column, button, canvas, column, pick_list, row, slider, text},
}; };
use polygon_draw::{Polygon, PolygonFrame}; use polygon_draw::{Polygon, PolygonFrame};
@@ -27,10 +27,12 @@ enum Message {
ButtonPressedIncrement, ButtonPressedIncrement,
ButtonPressedDecrement, ButtonPressedDecrement,
Tick, Tick,
AddPolygon(String),
ChangeTeta(usize, f32),
} }
struct MyApp { struct MyApp {
polys: PolygonFrame, poly_frame: PolygonFrame,
time_last_frame: Instant, time_last_frame: Instant,
nb_sec_for_rev: u32, nb_sec_for_rev: u32,
audio_manager: AudioManager, audio_manager: AudioManager,
@@ -47,14 +49,12 @@ impl MyApp {
nb_sec_for_rev: 4, nb_sec_for_rev: 4,
time_last_frame: Instant::now(), time_last_frame: Instant::now(),
audio_manager: manager, audio_manager: manager,
default_sound: sound_data, default_sound: sound_data.clone(),
polys: PolygonFrame { poly_frame: PolygonFrame {
teta: 0.0, teta: 0.0,
polygons: vec![ polygons: vec![
//Polygon::n_gon(0.0, 12), //Polygon::n_gon(0.0, 12),
//Polygon::triangle(0.0), //Polygon::triangle(0.0),
//Polygon::square(PI / 4.0),
Polygon::nr_6_in_30(1.5),
], ],
}, },
}, },
@@ -69,39 +69,92 @@ impl MyApp {
self.nb_sec_for_rev -= 1; self.nb_sec_for_rev -= 1;
} }
} }
Message::AddPolygon(s) => {
let mut poly: Polygon;
match s.as_str() {
"segment" => {
poly = Polygon::segment(0.0, self.default_sound.clone());
poly.name = "segment".to_string()
}
"triangle" => {
poly = Polygon::triangle(0.0, self.default_sound.clone());
poly.name = "triangle".to_string()
}
"square" => {
poly = Polygon::square(0.0, self.default_sound.clone());
poly.name = "square".to_string()
}
_ => poly = Polygon::n_gon(0.0, 0, self.default_sound.clone()),
}
self.poly_frame.polygons.push(poly);
}
Message::Tick => { Message::Tick => {
let time_btw = Instant::now().duration_since(self.time_last_frame); let time_btw = Instant::now().duration_since(self.time_last_frame);
let teta_temp = self.polys.teta; let teta_temp = self.poly_frame.teta;
self.polys.teta += 2.0 self.poly_frame.teta += 2.0
* PI * PI
* (1.0 / self.nb_sec_for_rev as f32) * (1.0 / self.nb_sec_for_rev as f32)
* (time_btw.as_millis() as f32 / 1_000.0); * (time_btw.as_millis() as f32 / 1_000.0);
self.polys.teta %= 2.0 * PI; self.poly_frame.teta %= 2.0 * PI;
if self let sound_to_play = self
.polys .poly_frame
.have_point_polygon_btw(teta_temp, self.polys.teta) .all_sound_to_play_btw(teta_temp, self.poly_frame.teta);
{ for sound in sound_to_play {
self.audio_manager self.audio_manager
.play(self.default_sound.clone()) .play(sound.clone())
.expect("Error to play sound"); .expect("Error to play sound");
} }
self.time_last_frame = Instant::now(); self.time_last_frame = Instant::now();
} }
Message::ChangeTeta(i, teta) => self.poly_frame.polygons[i].global_teta = teta,
} }
} }
fn view(&self) -> iced::Element<Message> { fn view(&self) -> iced::Element<Message> {
let txt_nb_rev = format!("Revolution per second : {}", self.nb_sec_for_rev); let txt_nb_rev = format!("Revolution per second : {}", self.nb_sec_for_rev);
let mut i = 0;
let polygon_rows: Vec<Element<Message>> = self
.poly_frame
.polygons
.iter()
.map(|polygon| {
let current_index = i;
i += 1; // Incrémenter l'index pour le prochain polygone
row![
text(&polygon.name),
slider(
0.0..=2.0 * std::f32::consts::PI,
polygon.global_teta,
move |f| { Message::ChangeTeta(current_index, f) }
)
.step(2.0 * PI / 42f32)
]
.into()
})
.collect();
// Utiliser Column::with_children pour ajouter dynamiquement les boutons
let polygon_column = Column::with_children(polygon_rows);
column![ column![
text("Polymusic").size(32.0), text("Polymusic").size(32.0),
row![ row![
canvas(&self.polys).height(500).width(500), canvas(&self.poly_frame).height(500).width(500),
column![ column![
text(txt_nb_rev), text(txt_nb_rev),
row![ row![
button("Increment").on_press(Message::ButtonPressedIncrement), button("Increment").on_press(Message::ButtonPressedIncrement),
button("Decrement").on_press(Message::ButtonPressedDecrement), button("Decrement").on_press(Message::ButtonPressedDecrement),
], ],
text("Polygon options"),
pick_list(
["segment", "triangle", "square"]
.map(|s| s.to_string())
.to_vec(),
Some("Chose polygon".to_string()),
|s| { Message::AddPolygon(s) }
),
polygon_column,
], ],
] ]
] ]

View File

@@ -6,6 +6,10 @@ use iced::widget::canvas;
use iced::widget::canvas::Stroke; use iced::widget::canvas::Stroke;
use iced::widget::canvas::Style; use iced::widget::canvas::Style;
use iced::{Color, Rectangle, Renderer, Theme}; use iced::{Color, Rectangle, Renderer, Theme};
use kira::PlaySoundError;
use kira::{
AudioManager, AudioManagerSettings, DefaultBackend, sound::static_sound::StaticSoundData,
};
use std::time::Instant; use std::time::Instant;
pub trait RotationExt { pub trait RotationExt {
@@ -26,13 +30,16 @@ pub struct PolygonFrame {
} }
impl PolygonFrame { impl PolygonFrame {
pub fn have_point_polygon_btw(&self, before: f32, after: f32) -> bool { pub fn all_sound_to_play_btw(&self, before: f32, after: f32) -> Vec<&StaticSoundData> {
let mut all_sound: Vec<&StaticSoundData> = vec![];
for poly in &self.polygons { for poly in &self.polygons {
all_sound.extend(poly.sound_to_play_btw(before, after));
/*
if poly.have_points_btw(before, after) { if poly.have_points_btw(before, after) {
return true; return true;
}*/
} }
} all_sound
false
} }
} }
@@ -103,26 +110,33 @@ impl<Message> canvas::Program<Message> for PolygonFrame {
} }
} }
#[derive(Debug)]
pub struct Polygon { pub struct Polygon {
pub global_teta: f32, pub global_teta: f32,
pub points_teta: Vec<f32>, pub points_teta: Vec<f32>,
pub sound: StaticSoundData,
pub name: String,
} }
#[warn(dead_code)] #[warn(dead_code)]
impl Polygon { impl Polygon {
pub fn have_points_btw(&self, before: f32, after: f32) -> bool { pub fn sound_to_play_btw(&self, before: f32, after: f32) -> Vec<&StaticSoundData> {
let mut sound_to_play: Vec<&StaticSoundData> = vec![];
if after < before { if after < before {
return self.have_points_btw(before, 2.0 * PI) || self.have_points_btw(0.0, after); sound_to_play = self.sound_to_play_btw(before, 2.0 * PI);
sound_to_play.extend(self.sound_to_play_btw(0.0, after));
return sound_to_play;
} }
let mut p_g; let mut p_g;
for p in self.points_teta.clone() { for p in self.points_teta.clone() {
p_g = (p + self.global_teta) % (2.0 * PI); p_g = (p + self.global_teta) % (2.0 * PI);
if before <= p_g && p_g <= after { if before <= p_g && p_g <= after {
return true; sound_to_play.push(&self.sound);
} }
} }
false sound_to_play
} }
pub fn n_gon(teta: f32, n_side: u8) -> Self {
pub fn n_gon(teta: f32, n_side: u8, sound: StaticSoundData) -> Self {
let mut v: Vec<f32> = Vec::with_capacity(n_side as usize); let mut v: Vec<f32> = Vec::with_capacity(n_side as usize);
for i in 0..n_side { for i in 0..n_side {
v.push((i as f32 * 2.0 * PI) / n_side as f32); v.push((i as f32 * 2.0 * PI) / n_side as f32);
@@ -130,22 +144,26 @@ impl Polygon {
Polygon { Polygon {
global_teta: teta, global_teta: teta,
points_teta: v, points_teta: v,
sound: sound,
name: "".to_string(),
} }
} }
pub fn segment(teta: f32) -> Self { pub fn segment(teta: f32, sound: StaticSoundData) -> Self {
Polygon::n_gon(teta, 2) Polygon::n_gon(teta, 2, sound)
} }
pub fn triangle(teta: f32) -> Self { pub fn triangle(teta: f32, sound: StaticSoundData) -> Self {
Polygon::n_gon(teta, 3) Polygon::n_gon(teta, 3, sound)
} }
pub fn square(teta: f32) -> Self { pub fn square(teta: f32, sound: StaticSoundData) -> Self {
Polygon::n_gon(teta, 4) Polygon::n_gon(teta, 4, sound)
} }
pub fn nr_6_in_30(teta: f32) -> Self { pub fn nr_6_in_30(teta: f32, sound: StaticSoundData) -> Self {
Polygon { Polygon {
sound: sound,
name: "".to_string(),
global_teta: teta, global_teta: teta,
points_teta: vec![ points_teta: vec![
2.0 * 5.0 * PI / 30.0, 2.0 * 5.0 * PI / 30.0,
@@ -157,8 +175,10 @@ impl Polygon {
], ],
} }
} }
pub fn nr_7_in_30(teta: f32) -> Self { pub fn nr_7_in_30(teta: f32, sound: StaticSoundData) -> Self {
Polygon { Polygon {
sound: sound,
name: "".to_string(),
global_teta: teta, global_teta: teta,
points_teta: vec![ points_teta: vec![
0.0, 0.0,
@@ -171,8 +191,10 @@ impl Polygon {
], ],
} }
} }
pub fn nr_8_in_30(teta: f32) -> Self { pub fn nr_8_in_30(teta: f32, sound: StaticSoundData) -> Self {
Polygon { Polygon {
sound: sound,
name: "".to_string(),
global_teta: teta, global_teta: teta,
points_teta: vec![ points_teta: vec![
2.0 * PI / 30.0, 2.0 * PI / 30.0,
@@ -186,8 +208,10 @@ impl Polygon {
], ],
} }
} }
pub fn nr_9_in_30(teta: f32) -> Self { pub fn nr_9_in_30(teta: f32, sound: StaticSoundData) -> Self {
Polygon { Polygon {
sound: sound,
name: "".to_string(),
global_teta: teta, global_teta: teta,
points_teta: vec![ points_teta: vec![
0.0, 0.0,
@@ -202,8 +226,10 @@ impl Polygon {
], ],
} }
} }
pub fn nr_8_in_42(teta: f32) -> Self { pub fn nr_8_in_42(teta: f32, sound: StaticSoundData) -> Self {
Polygon { Polygon {
sound: sound,
name: "".to_string(),
global_teta: teta, global_teta: teta,
points_teta: vec![ points_teta: vec![
2.0 * 3.0 * PI / 42.0, 2.0 * 3.0 * PI / 42.0,
@@ -217,8 +243,10 @@ impl Polygon {
], ],
} }
} }
pub fn nr_9_in_42(teta: f32) -> Self { pub fn nr_9_in_42(teta: f32, sound: StaticSoundData) -> Self {
Polygon { Polygon {
sound: sound,
name: "".to_string(),
global_teta: teta, global_teta: teta,
points_teta: vec![ points_teta: vec![
0.0, 0.0,
@@ -233,8 +261,10 @@ impl Polygon {
], ],
} }
} }
pub fn nr_10a_in_42(teta: f32) -> Self { pub fn nr_10a_in_42(teta: f32, sound: StaticSoundData) -> Self {
Polygon { Polygon {
sound: sound,
name: "".to_string(),
global_teta: teta, global_teta: teta,
points_teta: vec![ points_teta: vec![
2.0 * 6.0 * PI / 42.0, 2.0 * 6.0 * PI / 42.0,
@@ -250,8 +280,10 @@ impl Polygon {
], ],
} }
} }
pub fn nr_10b_in_42(teta: f32) -> Self { pub fn nr_10b_in_42(teta: f32, sound: StaticSoundData) -> Self {
Polygon { Polygon {
sound: sound,
name: "".to_string(),
global_teta: teta, global_teta: teta,
points_teta: vec![ points_teta: vec![
0.0, 0.0,