From db8debb31a76c4e30cbc17315327993c48924d5a Mon Sep 17 00:00:00 2001 From: polsevev Date: Sun, 5 Mar 2023 01:51:07 +0100 Subject: [PATCH] SOUND! --- src/GuiHookVec.rs | 7 +++++-- src/soundGenerator.rs | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/GuiHookVec.rs b/src/GuiHookVec.rs index 9bb6098..940318d 100644 --- a/src/GuiHookVec.rs +++ b/src/GuiHookVec.rs @@ -1,7 +1,7 @@ use async_trait::async_trait; -use macroquad::audio::{play_sound_once, Sound}; +use macroquad::audio::{play_sound_once, Sound, play_sound, PlaySoundParams}; use macroquad::color::{BROWN, WHITE}; use macroquad::{hash, time}; use macroquad::prelude::{clear_background, Vec2, BLACK}; @@ -179,7 +179,10 @@ impl SortingList for GuiVec{ if time::get_time() + 0.05 >= self.lastPlayed{ - play_sound_once(self.sounds[ (self.list[index1].position * 1000 / self.list.len()) ]); + play_sound(self.sounds[ (self.list[index1].position * 1000 / self.list.len()) ], PlaySoundParams{ + looped:false, + volume:0.5 + }); self.lastPlayed = time::get_time()+0.05; } diff --git a/src/soundGenerator.rs b/src/soundGenerator.rs index 713f310..a923b47 100644 --- a/src/soundGenerator.rs +++ b/src/soundGenerator.rs @@ -1,4 +1,4 @@ -use std::f32::consts::PI; +use std::{f32::consts::PI}; use macroquad::{audio::{Sound, load_sound_from_bytes}, window::{next_frame, screen_width, screen_height, clear_background}, text::draw_text, prelude::{BLACK, WHITE}}; @@ -64,9 +64,9 @@ 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 = f32::min(i as f32 * 100., 1500.)/ SAMPLE_RATE as f32 * MAX_AMPLITUDE as f32; + let amplitude = 500. * f32::sin((i as f32 - 300.) / 1200.); let value = f32::sin((2. * PI * (i as f32) * (frequency as f32)) / SAMPLE_RATE as f32); - let channel = (amplitude * value); + let channel = (amplitude * value); collect.push(channel); soundFileBytes.append(&mut (channel as i16).to_le_bytes().to_vec());