Skip to content
Snippets Groups Projects
Commit ec9d2ea1 authored by deuce's avatar deuce
Browse files

Allow specifying a frequency of zero for silence.

parent 8afd241b
Branches
Tags
No related merge requests found
......@@ -190,60 +190,65 @@ void makewave(double freq, unsigned char *wave, int samples, enum WAVE_SHAPE sha
double pos;
BOOL endhigh;
midpoint=samples/2;
inc=8.0*atan(1.0);
inc *= ((double)freq / (double)S_RATE);
for(i=0;i<samples;i++) {
pos=(inc*(double)i);
pos -= (int)(pos/WAVE_TPI)*WAVE_TPI;
switch(shape) {
case WAVE_SHAPE_SINE:
wave[i]=(sin (pos))*127+128;
break;
case WAVE_SHAPE_SINE_HARM:
wave[i]=(sin (pos))*64+128;
wave[i]=(sin ((inc*2)*(double)i))*24;
wave[i]=(sin ((inc*3)*(double)i))*16;
break;
case WAVE_SHAPE_SAWTOOTH:
wave[i]=(WAVE_TPI-pos)*40.5;
break;
case WAVE_SHAPE_SQUARE:
wave[i]=(pos<WAVE_PI)?255:0;
break;
case WAVE_SHAPE_SINE_SAW:
wave[i]=(((sin (pos))*127+128)+((WAVE_TPI-pos)*40.5))/2;
break;
case WAVE_SHAPE_SINE_SAW_CHORD:
wave[i]=(((sin (pos))*64+128)+((WAVE_TPI-pos)*6.2))/2;
wave[i]+=(sin ((inc/2)*(double)i))*24;
wave[i]+=(sin ((inc/3)*(double)i))*16;
break;
case WAVE_SHAPE_SINE_SAW_HARM:
wave[i]=(((sin (pos))*64+128)+((WAVE_TPI-pos)*6.2))/2;
wave[i]+=(sin ((inc*2)*(double)i))*24;
wave[i]+=(sin ((inc*3)*(double)i))*16;
break;
}
if(freq==0) {
memset(wave, 128, samples);
}
else {
midpoint=samples/2;
inc=8.0*atan(1.0);
inc *= ((double)freq / (double)S_RATE);
for(i=0;i<samples;i++) {
pos=(inc*(double)i);
pos -= (int)(pos/WAVE_TPI)*WAVE_TPI;
switch(shape) {
case WAVE_SHAPE_SINE:
wave[i]=(sin (pos))*127+128;
break;
case WAVE_SHAPE_SINE_HARM:
wave[i]=(sin (pos))*64+128;
wave[i]=(sin ((inc*2)*(double)i))*24;
wave[i]=(sin ((inc*3)*(double)i))*16;
break;
case WAVE_SHAPE_SAWTOOTH:
wave[i]=(WAVE_TPI-pos)*40.5;
break;
case WAVE_SHAPE_SQUARE:
wave[i]=(pos<WAVE_PI)?255:0;
break;
case WAVE_SHAPE_SINE_SAW:
wave[i]=(((sin (pos))*127+128)+((WAVE_TPI-pos)*40.5))/2;
break;
case WAVE_SHAPE_SINE_SAW_CHORD:
wave[i]=(((sin (pos))*64+128)+((WAVE_TPI-pos)*6.2))/2;
wave[i]+=(sin ((inc/2)*(double)i))*24;
wave[i]+=(sin ((inc/3)*(double)i))*16;
break;
case WAVE_SHAPE_SINE_SAW_HARM:
wave[i]=(((sin (pos))*64+128)+((WAVE_TPI-pos)*6.2))/2;
wave[i]+=(sin ((inc*2)*(double)i))*24;
wave[i]+=(sin ((inc*3)*(double)i))*16;
break;
}
}
/* Now we have a "perfect" wave...
* we must clean it up now to avoid click/pop
*/
if(wave[samples-1]>128)
endhigh=TRUE;
else
endhigh=FALSE;
/* Completely remove the last wave fragment */
i=samples-1;
if(wave[i]!=128) {
for(;i>midpoint;i--) {
if(endhigh && wave[i]<128)
break;
if(!endhigh && wave[i]>128)
break;
wave[i]=128;
/* Now we have a "perfect" wave...
* we must clean it up now to avoid click/pop
*/
if(wave[samples-1]>128)
endhigh=TRUE;
else
endhigh=FALSE;
/* Completely remove the last wave fragment */
i=samples-1;
if(wave[i]!=128) {
for(;i>midpoint;i--) {
if(endhigh && wave[i]<128)
break;
if(!endhigh && wave[i]>128)
break;
wave[i]=128;
}
}
}
}
......@@ -842,12 +847,14 @@ BOOL DLLCALL xptone(double freq, DWORD duration, enum WAVE_SHAPE shape)
unsigned char wave[S_RATE*15/2+1];
int samples;
if(freq<17)
if(freq<17 && freq != 0)
freq=17;
samples=S_RATE*duration/1000;
if(samples<=S_RATE/freq*2)
samples=S_RATE/freq*2;
if(samples > S_RATE/freq*2) {
if(freq) {
if(samples<=S_RATE/freq*2)
samples=S_RATE/freq*2;
}
if(freq==0 || samples > S_RATE/freq*2) {
int sample_len;
makewave(freq,wave,S_RATE*15/2,shape);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment