From 58aaf78f5aba3ee3fd74f32b75134f133fd37e6b Mon Sep 17 00:00:00 2001 From: polsevev Date: Sat, 4 Mar 2023 23:27:01 +0100 Subject: [PATCH] fixed audio, still needs to do static removal --- src/GuiHookVec.rs | 15 +++++++++++---- src/soundGenerator.rs | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/GuiHookVec.rs b/src/GuiHookVec.rs index 282a3ac..e784e2e 100644 --- a/src/GuiHookVec.rs +++ b/src/GuiHookVec.rs @@ -2,7 +2,7 @@ use async_trait::async_trait; use macroquad::color::{BROWN, WHITE}; -use macroquad::hash; +use macroquad::{hash, time}; use macroquad::prelude::{clear_background, Vec2, BLACK}; use macroquad::rand::ChooseRandom; use macroquad::shapes::draw_rectangle; @@ -29,7 +29,7 @@ pub struct GuiVec{ renderSkip:i32, skipped:i32, lastTouched:Vec, - + lastPlayed:f64 } #[async_trait] pub trait SortingList{ @@ -84,7 +84,8 @@ impl SortingList for GuiVec{ done:false, renderSkip:1, skipped:0, - lastTouched:Vec::with_capacity(2) + lastTouched:Vec::with_capacity(2), + lastPlayed:0., } } @@ -163,7 +164,13 @@ impl SortingList for GuiVec{ self.writes += 2; self.reads += 2; self.list.swap(index1, index2); - self.list[index1].playSound(); + + + if time::get_time() + 0.5 >= self.lastPlayed{ + self.list[index1].playSound(); + self.lastPlayed = time::get_time()+0.5; + } + //self.list[index2].playSound(); self.lastTouched.clear(); self.lastTouched.push(index1); diff --git a/src/soundGenerator.rs b/src/soundGenerator.rs index e34348b..94b79b6 100644 --- a/src/soundGenerator.rs +++ b/src/soundGenerator.rs @@ -64,7 +64,7 @@ pub async fn generateTone(frequency: f32, duration:f32) -> Sound{ let mut collect = Vec::new(); for i in 0..((SAMPLE_RATE as f32 * duration) as usize){ - let amplitude = 10000./ SAMPLE_RATE as f32 * MAX_AMPLITUDE as f32; + let amplitude = 1500. / SAMPLE_RATE as f32 * MAX_AMPLITUDE as f32; let value = f32::sin((2. * PI * (i as f32) * (frequency as f32)) / SAMPLE_RATE as f32); let channel = (amplitude * value);