failsafes and more notification sounds

This commit is contained in:
MathMan05 2024-06-21 22:22:20 -05:00
parent ae43fd497a
commit 95f6bc202d
4 changed files with 78 additions and 11 deletions

View file

@ -1,5 +1,5 @@
class voice{
constructor(wave,freq){
constructor(wave,freq,volume=1){
this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
this.info={wave:wave,freq:freq}
this.playing=false;
@ -8,6 +8,9 @@ class voice{
this.audioCtx.sampleRate,
this.audioCtx.sampleRate,
);
this.gainNode = this.audioCtx.createGain();
this.gainNode.gain.value=volume;
this.gainNode.connect(this.audioCtx.destination);
this.buffer=this.myArrayBuffer.getChannelData(0);
this.source = this.audioCtx.createBufferSource();
this.source.buffer = this.myArrayBuffer;
@ -37,6 +40,9 @@ class voice{
}
waveFucnion(){
if(typeof this.wave === 'function'){
return this.wave;
}
switch(this.wave){
case "sin":
return (t,freq)=>{
@ -68,7 +74,7 @@ class voice{
if(this.playing){
return;
}
this.source.connect(this.audioCtx.destination);
this.source.connect(this.gainNode);
this.playing=true;
}
@ -78,4 +84,52 @@ class voice{
this.playing=false;
}
}
static noises(noise){
switch(noise){
case "three":{
const voicy=new voice("sin",800);
voicy.play();
setTimeout(_=>{voicy.freq=1000},50);
setTimeout(_=>{voicy.freq=1300},100);
setTimeout(_=>{voicy.stop()},150);
break;
}
case "zip":{
const voicy=new voice((t,freq)=>{
return Math.sin(((t+2)**(Math.cos(t*4)))*Math.PI*2*freq);
},700);
voicy.play();
setTimeout(_=>{voicy.stop()},150);
break;
}
case "square":{
const voicy=new voice("square",600,.4);
voicy.play()
setTimeout(_=>{voicy.freq=800},50);
setTimeout(_=>{voicy.freq=1000},100);
setTimeout(_=>{voicy.stop()},150);
break;
}
case "beep":{
const voicy=new voice("sin",800);
voicy.play();
setTimeout(_=>{voicy.stop()},50);
setTimeout(_=>{voicy.play();},100);
setTimeout(_=>{voicy.stop()},150);
break;
}
}
}
static get sounds(){
return ["three","zip","square","beep"];
}
static setNotificationSound(sound){
let userinfos=JSON.parse(localStorage.getItem("userinfos"));
userinfos.preferances.notisound=sound;
localStorage.setItem("userinfos",JSON.stringify(userinfos));
}
static getNotificationSound(){
let userinfos=JSON.parse(localStorage.getItem("userinfos"));
return userinfos.preferances.notisound;
}
}