Audio cleanup and fix audio playing for swap on algorithm side
This commit is contained in:
parent
db8debb31a
commit
8b89189902
4 changed files with 69 additions and 5 deletions
|
@ -79,7 +79,7 @@ impl SortingList for GuiVec{
|
|||
|
||||
//Generate sounds
|
||||
let mut sounds = Vec::with_capacity(1000);
|
||||
for i in (50..2100).step_by(2){
|
||||
for i in (50..2051).step_by(2){
|
||||
sounds.push(soundGenerator::generateTone(i as f32, 0.05).await);
|
||||
}
|
||||
|
||||
|
@ -186,7 +186,6 @@ impl SortingList for GuiVec{
|
|||
self.lastPlayed = time::get_time()+0.05;
|
||||
}
|
||||
|
||||
//self.list[index2].playSound();
|
||||
self.lastTouched.clear();
|
||||
self.lastTouched.push(index1);
|
||||
self.lastTouched.push(index2);
|
||||
|
@ -235,6 +234,13 @@ impl SortingList for GuiVec{
|
|||
self.reads += 1;
|
||||
self.list[i] = elem;
|
||||
self.draw().await;
|
||||
if time::get_time() + 0.05 >= self.lastPlayed{
|
||||
play_sound(self.sounds[ (self.list[i].position * 1000 / self.list.len()) ], PlaySoundParams{
|
||||
looped:false,
|
||||
volume:0.5
|
||||
});
|
||||
self.lastPlayed = time::get_time()+0.05;
|
||||
}
|
||||
self.lastTouched.clear();
|
||||
self.lastTouched.push(i);
|
||||
self.done
|
||||
|
|
|
@ -8,7 +8,7 @@ pub async fn cocktailShaker(list:&mut impl SortingList){
|
|||
swapped = false;
|
||||
for i in lowerBound..upperBound {
|
||||
if list.lessThan(i+1, i) {
|
||||
if list.swap(i+1, i).await {return};
|
||||
if list.swap(i, i+1).await {return};
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ async fn main() {
|
|||
let mut algorithm = algorithm::Algorithm::new();
|
||||
let mut buttonDropDown = ButtonDropDown::new(&algorithm.getAlgorithms());
|
||||
|
||||
let sound = soundGenerator::generateTone(50., 0.05).await;
|
||||
|
||||
|
||||
loop{
|
||||
clear_background(WHITE);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
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}};
|
||||
use macroquad::{audio::{Sound, load_sound_from_bytes, play_sound_once}, window::{next_frame, screen_width, screen_height, clear_background}, text::draw_text, prelude::{BLACK, WHITE}};
|
||||
|
||||
|
||||
const CHUNK_ID:&str = "RIFF";
|
||||
|
@ -91,5 +91,63 @@ pub async fn generateTone(frequency: f32, duration:f32) -> Sound{
|
|||
|
||||
|
||||
let sound = load_sound_from_bytes(&soundFileBytes).await.expect("Failed to load");
|
||||
play_sound_once(sound);
|
||||
let frequency = 1200.0;
|
||||
soundFileBytes.clear();
|
||||
|
||||
soundFileBytes.append(&mut CHUNK_ID.clone().as_bytes().to_vec());
|
||||
|
||||
soundFileBytes.append(&mut CHUNK_SIZE.clone().as_bytes().to_vec());
|
||||
|
||||
soundFileBytes.append(&mut FORMAT.clone().as_bytes().to_vec());
|
||||
|
||||
soundFileBytes.append(&mut SUBCHUNK_1_ID.clone().as_bytes().to_vec());
|
||||
|
||||
write_as_bytes(&mut soundFileBytes, SUBCHUNK_1_SIZE, 4);
|
||||
|
||||
write_as_bytes(&mut soundFileBytes, AUDIO_FORMAT, 2) ;
|
||||
|
||||
write_as_bytes(&mut soundFileBytes, NUM_CHANNELS, 2);
|
||||
|
||||
write_as_bytes(&mut soundFileBytes, SAMPLE_RATE, 4);
|
||||
|
||||
write_as_bytes(&mut soundFileBytes, BYTE_RATE, 4);
|
||||
|
||||
write_as_bytes(&mut soundFileBytes, BLOACK_ALIGN, 2);
|
||||
write_as_bytes(&mut soundFileBytes, BITS_PR_SAMPLE, 2);
|
||||
|
||||
soundFileBytes.append(&mut SUBCHUNK_2_ID.clone().as_bytes().to_vec());
|
||||
|
||||
soundFileBytes.append(&mut SUBCHUNK_2_SIZE.clone().as_bytes().to_vec());
|
||||
|
||||
|
||||
let startAudio = soundFileBytes.len();
|
||||
|
||||
let mut collect = Vec::new();
|
||||
for i in 0..((SAMPLE_RATE as f32 * duration) as usize){
|
||||
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);
|
||||
|
||||
collect.push(channel);
|
||||
soundFileBytes.append(&mut (channel as i16).to_le_bytes().to_vec());
|
||||
|
||||
|
||||
}
|
||||
let endAudio = soundFileBytes.len();
|
||||
|
||||
|
||||
let mut holder = Vec::new();
|
||||
write_as_bytes(&mut holder, endAudio-startAudio, 4);
|
||||
for i in 0..4{
|
||||
soundFileBytes[(startAudio-4)+i] = holder[i];
|
||||
}
|
||||
holder.clear();
|
||||
write_as_bytes(&mut holder, 36+endAudio-startAudio, 4);
|
||||
|
||||
for i in 0..4{
|
||||
soundFileBytes[4+i] = holder[i];
|
||||
}
|
||||
play_sound_once(sound);
|
||||
sound
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue