From fbf5ccbd5e22f09ad334960b0734549dfd9760bd Mon Sep 17 00:00:00 2001 From: Dukantic Date: Sat, 31 May 2025 17:03:18 +0200 Subject: [PATCH] add audio but better with kira --- Cargo.lock | 478 +++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + "\\" | 108 ++++++++++ assets/tick.mp3 | 3 + assets/tick.ogg | Bin 0 -> 6752 bytes src/main.rs | 39 +++- src/polygon_draw.rs | 29 ++- 7 files changed, 646 insertions(+), 12 deletions(-) create mode 100644 "\\" create mode 100644 assets/tick.mp3 create mode 100644 assets/tick.ogg diff --git a/Cargo.lock b/Cargo.lock index 111ad64..0aaccab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -57,12 +57,43 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "allocator-api2" version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" +[[package]] +name = "alsa" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed7572b7ba83a31e20d1b48970ee402d2e3e0537dcfe0a3ff4d6eb7508617d43" +dependencies = [ + "alsa-sys", + "bitflags 2.9.1", + "cfg-if", + "libc", +] + +[[package]] +name = "alsa-sys" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db8fee663d06c4e303404ef5f40488a53e062f89ba8bfed81f42325aafad1527" +dependencies = [ + "libc", + "pkg-config", +] + [[package]] name = "android-activity" version = "0.6.0" @@ -77,7 +108,7 @@ dependencies = [ "jni-sys", "libc", "log", - "ndk", + "ndk 0.9.0", "ndk-context", "ndk-sys 0.6.0+11769913", "num_enum", @@ -279,6 +310,12 @@ dependencies = [ "syn 2.0.101", ] +[[package]] +name = "atomic-arena" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73e8ed45f88ed32e6827a96b62d8fd4086d72defc754c5c6bd08470c1aaf648e" + [[package]] name = "atomic-waker" version = "1.1.2" @@ -306,6 +343,24 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "bindgen" +version = "0.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f49d8fed880d473ea71efb9bf597651e77201bdd4893efe54c9e5d65ae04ce6f" +dependencies = [ + "bitflags 2.9.1", + "cexpr", + "clang-sys", + "itertools", + "proc-macro2", + "quote", + "regex", + "rustc-hash 1.1.0", + "shlex", + "syn 2.0.101", +] + [[package]] name = "bit-set" version = "0.5.3" @@ -451,6 +506,15 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" +[[package]] +name = "cexpr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -469,6 +533,17 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "clang-sys" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4" +dependencies = [ + "glob", + "libc", + "libloading 0.8.7", +] + [[package]] name = "clipboard-win" version = "5.4.0" @@ -642,6 +717,26 @@ dependencies = [ "libc", ] +[[package]] +name = "coreaudio-rs" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "321077172d79c662f64f5071a03120748d5bb652f5231570141be24cfcd2bace" +dependencies = [ + "bitflags 1.3.2", + "core-foundation-sys", + "coreaudio-sys", +] + +[[package]] +name = "coreaudio-sys" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ce857aa0b77d77287acc1ac3e37a05a8c95a2af3647d23b15f263bdaeb7562b" +dependencies = [ + "bindgen", +] + [[package]] name = "cosmic-text" version = "0.12.1" @@ -665,6 +760,29 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "cpal" +version = "0.15.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "873dab07c8f743075e57f524c583985fbaf745602acbe916a01539364369a779" +dependencies = [ + "alsa", + "core-foundation-sys", + "coreaudio-rs", + "dasp_sample", + "jni", + "js-sys", + "libc", + "mach2", + "ndk 0.8.0", + "ndk-context", + "oboe", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "windows 0.54.0", +] + [[package]] name = "cpufeatures" version = "0.2.17" @@ -763,6 +881,12 @@ dependencies = [ "zbus", ] +[[package]] +name = "dasp_sample" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c87e182de0887fd5361989c677c4e8f5000cd9491d6d563161a8f3a5519fc7f" + [[package]] name = "dconf_rs" version = "0.3.0" @@ -883,6 +1007,15 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" +[[package]] +name = "encoding_rs" +version = "0.8.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" +dependencies = [ + "cfg-if", +] + [[package]] name = "endi" version = "1.1.0" @@ -972,6 +1105,12 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "extended" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af9673d8203fcb076b19dfd17e38b3d4ae9f44959416ea532ce72415a6020365" + [[package]] name = "fast-srgb8" version = "1.0.0" @@ -1237,6 +1376,21 @@ version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "151665d9be52f9bb40fc7966565d39666f2d1e69233571b71b87791c7e0528b3" +[[package]] +name = "glam" +version = "0.30.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b46b9ca4690308844c644e7c634d68792467260e051c8543e0c7871662b3ba7" +dependencies = [ + "mint", +] + +[[package]] +name = "glob" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2" + [[package]] name = "glow" version = "0.13.1" @@ -1287,7 +1441,7 @@ dependencies = [ "presser", "thiserror", "winapi", - "windows", + "windows 0.52.0", ] [[package]] @@ -1417,7 +1571,7 @@ dependencies = [ "bitflags 2.9.1", "bytes", "dark-light", - "glam", + "glam 0.25.0", "log", "num-traits", "once_cell", @@ -1528,7 +1682,7 @@ dependencies = [ "bitflags 2.9.1", "bytemuck", "futures", - "glam", + "glam 0.25.0", "guillotiere", "iced_glyphon", "iced_graphics", @@ -1594,6 +1748,15 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + [[package]] name = "jni" version = "0.21.1" @@ -1653,6 +1816,23 @@ version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" +[[package]] +name = "kira" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "315cbdaa382944922b0d19a78890d0344396d7ed840044e4ed9c93dfff9aafe7" +dependencies = [ + "atomic-arena", + "cpal", + "glam 0.30.3", + "mint", + "paste", + "rtrb", + "send_wrapper", + "symphonia", + "triple_buffer", +] + [[package]] name = "kurbo" version = "0.10.4" @@ -1663,6 +1843,12 @@ dependencies = [ "smallvec", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.172" @@ -1798,6 +1984,15 @@ dependencies = [ "num-traits", ] +[[package]] +name = "mach2" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b955cdeb2a02b9117f121ce63aa52d08ade45de53e48fe6a38b39c10f6f709" +dependencies = [ + "libc", +] + [[package]] name = "malloc_buf" version = "0.0.6" @@ -1846,6 +2041,12 @@ dependencies = [ "paste", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.8.8" @@ -1856,6 +2057,12 @@ dependencies = [ "simd-adler32", ] +[[package]] +name = "mint" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e53debba6bda7a793e5f99b8dacf19e626084f525f7829104ba9898f367d85ff" + [[package]] name = "naga" version = "0.19.2" @@ -1876,6 +2083,20 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "ndk" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2076a31b7010b17a38c01907c45b945e8f11495ee4dd588309718901b1f7a5b7" +dependencies = [ + "bitflags 2.9.1", + "jni-sys", + "log", + "ndk-sys 0.5.0+25.2.9519653", + "num_enum", + "thiserror", +] + [[package]] name = "ndk" version = "0.9.0" @@ -1928,6 +2149,27 @@ dependencies = [ "memoffset", ] +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.101", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -2200,6 +2442,29 @@ dependencies = [ "memchr", ] +[[package]] +name = "oboe" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8b61bebd49e5d43f5f8cc7ee2891c16e0f41ec7954d36bcb6c14c5e0de867fb" +dependencies = [ + "jni", + "ndk 0.8.0", + "ndk-context", + "num-derive", + "num-traits", + "oboe-sys", +] + +[[package]] +name = "oboe-sys" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8bb09a4a2b1d668170cfe0a7d5bc103f8999fb316c98099b6a9939c9f2e79d" +dependencies = [ + "cc", +] + [[package]] name = "once_cell" version = "1.21.3" @@ -2458,6 +2723,7 @@ name = "polygomusic" version = "0.1.0" dependencies = [ "iced", + "kira", "tokio", ] @@ -2640,6 +2906,35 @@ dependencies = [ "thiserror", ] +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "renderdoc-sys" version = "1.1.0" @@ -2652,6 +2947,12 @@ version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97" +[[package]] +name = "rtrb" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad8388ea1a9e0ea807e442e8263a699e7edcb320ecbcd21b4fa8ff859acce3ba" + [[package]] name = "rust-ini" version = "0.18.0" @@ -2769,6 +3070,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f7d95a54511e0c7be3f51e8867aa8cf35148d7b9445d44de2f943e2b206e749" +[[package]] +name = "send_wrapper" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd0b0ec5f1c1ca621c432a25813d8d60c88abe6d3e08a3eb9cf37d97a0fe3d73" + [[package]] name = "serde" version = "1.0.219" @@ -2987,6 +3294,127 @@ dependencies = [ "zeno", ] +[[package]] +name = "symphonia" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "815c942ae7ee74737bb00f965fa5b5a2ac2ce7b6c01c0cc169bbeaf7abd5f5a9" +dependencies = [ + "lazy_static", + "symphonia-bundle-flac", + "symphonia-bundle-mp3", + "symphonia-codec-pcm", + "symphonia-codec-vorbis", + "symphonia-core", + "symphonia-format-ogg", + "symphonia-format-riff", + "symphonia-metadata", +] + +[[package]] +name = "symphonia-bundle-flac" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72e34f34298a7308d4397a6c7fbf5b84c5d491231ce3dd379707ba673ab3bd97" +dependencies = [ + "log", + "symphonia-core", + "symphonia-metadata", + "symphonia-utils-xiph", +] + +[[package]] +name = "symphonia-bundle-mp3" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c01c2aae70f0f1fb096b6f0ff112a930b1fb3626178fba3ae68b09dce71706d4" +dependencies = [ + "lazy_static", + "log", + "symphonia-core", + "symphonia-metadata", +] + +[[package]] +name = "symphonia-codec-pcm" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f395a67057c2ebc5e84d7bb1be71cce1a7ba99f64e0f0f0e303a03f79116f89b" +dependencies = [ + "log", + "symphonia-core", +] + +[[package]] +name = "symphonia-codec-vorbis" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a98765fb46a0a6732b007f7e2870c2129b6f78d87db7987e6533c8f164a9f30" +dependencies = [ + "log", + "symphonia-core", + "symphonia-utils-xiph", +] + +[[package]] +name = "symphonia-core" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "798306779e3dc7d5231bd5691f5a813496dc79d3f56bf82e25789f2094e022c3" +dependencies = [ + "arrayvec", + "bitflags 1.3.2", + "bytemuck", + "lazy_static", + "log", +] + +[[package]] +name = "symphonia-format-ogg" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ada3505789516bcf00fc1157c67729eded428b455c27ca370e41f4d785bfa931" +dependencies = [ + "log", + "symphonia-core", + "symphonia-metadata", + "symphonia-utils-xiph", +] + +[[package]] +name = "symphonia-format-riff" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f7be232f962f937f4b7115cbe62c330929345434c834359425e043bfd15f50" +dependencies = [ + "extended", + "log", + "symphonia-core", + "symphonia-metadata", +] + +[[package]] +name = "symphonia-metadata" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc622b9841a10089c5b18e99eb904f4341615d5aa55bbf4eedde1be721a4023c" +dependencies = [ + "encoding_rs", + "lazy_static", + "log", + "symphonia-core", +] + +[[package]] +name = "symphonia-utils-xiph" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "484472580fa49991afda5f6550ece662237b00c6f562c7d9638d1b086ed010fe" +dependencies = [ + "symphonia-core", + "symphonia-metadata", +] + [[package]] name = "syn" version = "1.0.109" @@ -3172,6 +3600,15 @@ dependencies = [ "once_cell", ] +[[package]] +name = "triple_buffer" +version = "8.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "420466259f9fa5decc654c490b9ab538400e5420df8237f84ecbe20368bcf72b" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "ttf-parser" version = "0.20.0" @@ -3677,7 +4114,17 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be" dependencies = [ - "windows-core", + "windows-core 0.52.0", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows" +version = "0.54.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9252e5725dbed82865af151df558e754e4a3c2c30818359eb17465f1346a1b49" +dependencies = [ + "windows-core 0.54.0", "windows-targets 0.52.6", ] @@ -3690,6 +4137,25 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-core" +version = "0.54.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12661b9c89351d684a50a8a643ce5f608e20243b9fb84687800163429f161d65" +dependencies = [ + "windows-result", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-result" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-sys" version = "0.45.0" @@ -3981,7 +4447,7 @@ dependencies = [ "js-sys", "libc", "memmap2", - "ndk", + "ndk 0.9.0", "objc2", "objc2-app-kit", "objc2-foundation", diff --git a/Cargo.toml b/Cargo.toml index 8066058..1755eec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,5 @@ edition = "2024" [dependencies] iced = { version = "0.13.1", features = ["canvas", "tokio"] } +kira = "0.10.6" tokio = {version = "1.45.1", features = ["time"]} diff --git "a/\\" "b/\\" new file mode 100644 index 0000000..e893aab --- /dev/null +++ "b/\\" @@ -0,0 +1,108 @@ +mod polygon_draw; + +use iced::{ + 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; + +fn main() -> iced::Result { + iced::application("My App", MyApp::update, MyApp::view) + .theme(|_| Theme::Dark) + .subscription(MyApp::subscription) + .run_with(MyApp::new) +} + +#[derive(Debug, Clone)] +enum Message { + ButtonPressedIncrement, + ButtonPressedDecrement, + Tick, +} + +struct MyApp { + polys: PolygonFrame, + time_last_frame: Instant, + nb_sec_for_rev: u32, +} + +impl MyApp { + fn new() -> (Self, Task) { + ( + Self { + nb_sec_for_rev: 4, + time_last_frame: Instant::now(), + 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), + ], + }, + }, + Task::none(), + ) + } + fn update(&mut self, message: Message) { + match message { + Message::ButtonPressedIncrement => self.nb_sec_for_rev += 1, + Message::ButtonPressedDecrement => { + if 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) + { + do_sound("assets/tick.mp3"); + } + self.time_last_frame = Instant::now(); + } + } + } + + fn view(&self) -> iced::Element { + let txt_nb_rev = format!("Revolution per second : {}", self.nb_sec_for_rev); + column![ + text("Polymusic").size(32.0), + row![ + canvas(&self.polys).height(500).width(500), + column![ + text(txt_nb_rev), + row![ + button("Increment").on_press(Message::ButtonPressedIncrement), + button("Decrement").on_press(Message::ButtonPressedDecrement), + ], + ], + ] + ] + .into() + } + fn subscription(&self) -> iced::Subscription { + time::every(Duration::from_millis(16)).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(); + } +} diff --git a/assets/tick.mp3 b/assets/tick.mp3 new file mode 100644 index 0000000..86e10b8 --- /dev/null +++ b/assets/tick.mp3 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:09abc91c94ea4f00256035175b42dc2065ea24ddbd3e27c1469250701aa2583e +size 3857 diff --git a/assets/tick.ogg b/assets/tick.ogg new file mode 100644 index 0000000000000000000000000000000000000000..bd6ad69706e468e319237f69e4c71cc915fd641e GIT binary patch literal 6752 zcmeG=c~sNK)|0S>MPvyW5H)NPFj#^CiNzAaB18}fTR^FV5Oz>OsMxA00zy~{AyN#8 zArO`TzJP+FUE+eG2!ep1xS*nFv3*u;U8?2HFF~JQzn<6coSyH$H)k@Lx%bZ8-`r*H zoe7AK4+dy3U;f#20*7>mQJcR)Ek$K*Nsmd)KmcgZHslYMZA#+fr~qW<`-03w!BO~D zV~T<2+#gE0igGD3q+lj)O;{O_9*<8+j0saP!29AI9UL7TRy&aK1O_KIlbx6rkM~N; zN=)C9mdZ)nZi}!%sVUd6rFn#*00u%xe)wMQP+2GddH_UQ+3Hrr`PxbZb%g9Hfe+H_ zbttS76!zlS#iVoZ8az4H2mn>UC1{q`Kk!W&q{QQFO4CJ@#0@S@cnx0A6h-s&FU#3! zjYk`k(*|v=F$*Hppa1~X$2YuPo6as>EL{G4eYxxQ3#2NxjzyYjXgbL_NJ59WsuKjxWc+_yXOUg-@tRfh3}Mrs2u<*$*gyq*##&K zhW$voQ+USO@eKcQC<;U(|G9jruot)y^Mi4fNnFgTGL}LtT(~T*4&$MtZb9+ zA>p$Dimvd!s0CX+a;h+-+NP(as#>AJltGhwP?xqB5A=!-3<)y^eRYHqWiQ*HTBE(? z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-!vzbt^9WAT<_=|5eN9mJFzR1w z*@dObRD*_1^EZO6>;v*Xf)&P44+LQxf!b+;df=#dwDoRhF}+XixxCNC!AI3{Y(ZF< z1+Lc^d($^7b6|S1*CMCG@3wWgC6CDYMwV4|tJZyF8|~Mjmd5zqZN#RJq@Ao2`#hX= z){caa&wcUwo7h^DOp}%x{=LHVB?iDqv?Et`V`C&0RRz zS8}NS(}Hf(>w%$_s#b_Ih=%MwVmWZinPNsk!;;iz<*W}EHSa}Kt_LCvUU*SRA 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) { + let manager = AudioManager::::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(); +}*/ diff --git a/src/polygon_draw.rs b/src/polygon_draw.rs index 98b1a65..f965be9 100644 --- a/src/polygon_draw.rs +++ b/src/polygon_draw.rs @@ -6,6 +6,7 @@ use iced::widget::canvas; use iced::widget::canvas::Stroke; use iced::widget::canvas::Style; use iced::{Color, Rectangle, Renderer, Theme}; +use std::time::Instant; pub trait RotationExt { fn rotate(&mut self, teta: f32) -> Self; @@ -24,6 +25,17 @@ pub struct PolygonFrame { pub polygons: Vec, } +impl PolygonFrame { + pub fn have_point_polygon_btw(&self, before: f32, after: f32) -> bool { + for poly in &self.polygons { + if poly.have_points_btw(before, after) { + return true; + } + } + false + } +} + impl canvas::Program for PolygonFrame { // No internal state type State = (); @@ -97,6 +109,19 @@ pub struct Polygon { } #[warn(dead_code)] impl Polygon { + pub fn have_points_btw(&self, before: f32, after: f32) -> bool { + let mut p_g; + for p in self.points_teta.clone() { + p_g = p + self.global_teta; + if before < p_g && p_g < after { + return true; + } + if p_g > after { + return false; + }; + } + false + } pub fn n_gon(teta: f32, n_side: u8) -> Self { let mut v: Vec = Vec::with_capacity(n_side as usize); for i in 0..n_side { @@ -112,11 +137,11 @@ impl Polygon { } pub fn triangle(teta: f32) -> Self { - Polygon::n_gon(teta, 2) + Polygon::n_gon(teta, 3) } pub fn square(teta: f32) -> Self { - Polygon::n_gon(teta, 2) + Polygon::n_gon(teta, 4) } pub fn nr_6_in_30(teta: f32) -> Self {