hschumann2/TempleOS-Source-Code
0847
1 2#help_index "Snd"3 4#define SE_NOISE 05#define SE_SWEEP 16 7class CSoundEffectFrame8{9 I32 type;10 I8 ona1,ona2;11 F64 duration;12};13 14U0 SoundEffectEndTaskCB()15{16 Free(FramePtr("CSoundEffectFrame"));17 music.mute--;18 SndTaskEndCB;19}20 21U0 SoundEffectTask(CSoundEffectFrame *ns)22{23 I64 i,ona;24 F64 t0=tS,t,timeout=t0+ns->duration;25 FramePtrAdd("CSoundEffectFrame",ns);26 Fs->task_end_cb=&SoundEffectEndTaskCB;27 switch (ns->type) {28 case SE_NOISE:29 i=MaxI64(ns->ona2-ns->ona1,1);30 while (tS<timeout) {31 ona=RandU16%i+ns->ona1;32 Snd(ona);33 t=Clamp(3000.0/Ona2Freq(ona),1.0,50.0);34 if (t+tS>timeout)35 t=timeout-tS;36 Sleep(t);37 }38 break;39 case SE_SWEEP:40 while (tS<timeout) {41 t=(tS-t0)/ns->duration;42 ona=(1.0-t)*ns->ona1+t*ns->ona2;43 Snd(ona);44 t=Clamp(3000.0/Ona2Freq(ona),1.0,50.0);45 if (t+tS>timeout)46 t=timeout-tS;47 Sleep(t);48 }49 break;50 }51}52 53public CTask *Noise(I64 mS,F64 min_ona,F64 max_ona)54{//Make white noise for given number of mS.55 CSoundEffectFrame *ns;56 if (mS>0) {57 ns=MAlloc(sizeof(CSoundEffectFrame));58 ns->type=SE_NOISE;59 ns->duration=mS/1000.0;60 ns->ona1=min_ona;61 ns->ona2=max_ona;62 music.mute++;63 return Spawn(&SoundEffectTask,ns,"Noise",,Fs);64 } else65 return NULL;66}67 68public CTask *Sweep(I64 mS,F64 ona1,F64 ona2)69{//Sweep through freq range in given number of mS.70 CSoundEffectFrame *ns;71 if (mS>0) {72 ns=MAlloc(sizeof(CSoundEffectFrame));73 ns->type=SE_SWEEP;74 ns->duration=mS/1000.0;75 ns->ona1=ona1;76 ns->ona2=ona2;77 music.mute++;78 return Spawn(&SoundEffectTask,ns,"Noise",,Fs);79 } else80 return NULL;81}82 