/* make arbitrary audio data here */ void makeaud(UC*p,const UL rate,UL z){ float freq=500; UC amp=120; while(z--){ *p++=sinewave(rate,freq,amp,z); } }
TEST(DiodeRectifierSingle, TestTick) { float in[1000]; sinewave(in, 1000, 1000.0, 0, 1.0, 44100); for (unsigned th_idx = 0; th_idx < 11; ++th_idx) { DiodeRectifier * diode = DiodeRectifierInit(FORWARD_BIAS, thresholds[th_idx]); for (unsigned i = 0; i < 1000; ++i) { ASSERT_GE(DiodeRectifierTick(diode, in[i]), 0.0); } DiodeRectifierFree(diode); } }
TEST(DiodeRectifierSingle, TestProcessReverse) { float in[1000]; float out[1000]; sinewave(in, 1000, 1000.0, 0, 1.0, 44100); for (unsigned th_idx = 0; th_idx < 11; ++th_idx) { DiodeRectifier * diode = DiodeRectifierInit(REVERSE_BIAS, thresholds[th_idx]); DiodeRectifierProcess(diode, out, in, 1000); DiodeRectifierFree(diode); for (unsigned i = 0; i < 1000; ++i) { ASSERT_LE(out[i], 0.0); } } }
int main(void) { pthread_t serial_th; state sinestate = {.phase=0, .w=500*TAU, .vib_w=5*TAU, .vib_phase=0, .s =0}; int err; unsigned int i; snd_pcm_t *handle; snd_pcm_sframes_t frames; snd_pcm_hw_params_t *hwparams; pthread_create(&serial_th, NULL, (void* (*)(void *))serialreader, &sv); snd_pcm_hw_params_alloca(&hwparams); if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) { printf("Playback open error: %s\n", snd_strerror(err)); exit(EXIT_FAILURE); } if (set_hwparams(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) { return 1; } for (;;) { sinewave(&sinestate, buffer, PERIOD_SIZE, makeangular(sv.range), makelux(sv.lux)); frames = snd_pcm_writei(handle, buffer, PERIOD_SIZE); // printf("%d\n", frames); if (frames < 0) frames = snd_pcm_recover(handle, frames, 0); if (frames < 0) { printf("snd_pcm_writei failed: %s\n", snd_strerror(err)); // break; } if (frames > 0 && frames < PERIOD_SIZE) printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames); } snd_pcm_close(handle); return 0; }
float sinewaveandcircle(const Vec2f& pt) { return min(sinewave(pt), circle(pt, solid_bounds_centre, 0.1f)); }
unsigned char frequencyrange(void) { unsigned char m;// represent adc convert value unsigned int f;// represent frequency char msg1[16] = " START NOW? ";//LCD LINE 1 message char msg2[16] = " PRESS ANYKEY ";// LCD line 2 message LCD_Reset();// initiliaze LCD sprintf(msg1," Please choose ");// chose frequency message LCD_Line1(msg1); sprintf(msg2,"Frequency"); LCD_Line2(msg2); Delay10KTCYx(250);// delay 0.25s Delay10KTCYx(250);//delay 0.25s Delay10KTCYx(250);// delay0.25s Delay10KTCYx(250);//delay0.25s LCD_Reset();// initialize lCD switch(bo) { case 1: do { sprintf(msg1,"Range:11~2277 Hz"); LCD_Line1(msg1); m=poten(8);// ADC convert value if (m==0) m=1;// let the least ADC value is 1 f=(unsigned int)1/(1/12600+m*0.00035+0.000089);;// caculate sine wave frequency if ((m<=60)&&(m>=30))f=f-1;// slightly adjust frequency if ((m<=20)&&(m>=10))f=f-4;// slightly adjust frequency if ((m>5)&&(m<10) )f=f-5;// slightly adjust frequency if(m==5) f=f-6;// slightly adjust frequency if(m==4) f=f-7;// slightly adjust frequency if(m==3) f=f-7;// slightly adjust frequency if (m==2) f=f+13;// slightly adjust frequency sprintf(msg2,"%10d Hz",f); LCD_Line2(msg2);// show frequency if (SWITCH_1==0) { switch_release();//wait for RA5 release return 2;// if RA5 pressed goto wavetype function } } while(SWITCH_0 == 1); switch_release();//wait for RB0 release sinewave(m);//output sine wave switch_release();//wait for RA5 release return 3;// if RA5 pressed goto frequency range function break; case 2: do { sprintf(msg1,"Range:6~581 Hz"); LCD_Line1(msg1); m=poten(8);// ADC convert value if (m==0) m=1;// let the least ADC value is 1 f=(unsigned int) 1/ (1/847 + m*0.00057+0.001230); //caculate and slightly adjust triangle wave frequency if (m==1) f=f+26;//caculate and slightly adjust triangle wave frequency if (m==2) f=f+20;//caculate and slightly adjust triangle wave frequency if (m==3) f=f+12;//caculate and slightly adjust triangle wave frequency if (m==4) f=f+10;//caculate and slightly adjust triangle wave frequency if (m==5) f=f+7;//caculate and slightly adjust triangle wave frequency if ((m>=6)&&(m<=8)) f=f+5;//caculate and slightly adjust triangle wave frequency if (m==9) f=f+4;//caculate and slightly adjust triangle wave frequency if ((m>=10)&& (m<=13)) f=f+3;//caculate and slightly adjust triangle wave frequency if ((m>=13)&&(m<=17)) f=f+2;//caculate and slightly adjust triangle wave frequency if ((m>=18)&&(m<=50)) f=f+1;//caculate and slightly adjust triangle wave frequency sprintf(msg2,"%10d Hz",f); LCD_Line2(msg2);// show frequency if (SWITCH_1==0) { switch_release();//wait for RA5 release return 2;// if RA5 pressed goto wavetype function } } while(SWITCH_0 == 1); switch_release();//wait for RB0 release triwave(m);// output triangle wave switch_release();//wait for RA5 release return 3;// if RA5 pressed goto frequency range function break; case 3: do { sprintf(msg1,"Range:5~676 Hz"); LCD_Line1(msg1); m=poten(8);// ADC convert value if (m<=1) m=2;// let the least ADC value is 2 f=(unsigned int) 1/ (1/15600 + m*0.000709+0.00007);//caculate sawtooth wave frequency if ((m==2)||(m==3)) f=f+4;//caculate and slightly adjust sawtooth wave frequency if (m==4) f=f+1;//caculate and slightly adjust sawtooth wave frequency sprintf(msg2,"%10d Hz",f); LCD_Line2(msg2);// show frequency if (SWITCH_1==0) { switch_release();//wait for RA5 release return 2;// if RA5 pressed goto wavetype function } } while(SWITCH_0 == 1); switch_release();//wait for RB0 release sawtooth(m);//output sawtooth wave switch_release();//wait for RA5 release return 3;// if RA5 pressed goto frequency range function break; case 4: do { sprintf(msg1,"Range:102~7690Hz"); LCD_Line1(msg1); m=poten(8);// adc convert value if (m==0) m=1;// let the least ADC value is 1 if ((m>=140)&&(m<=255)) f=(unsigned int) 1/ (1/11400 + m*0.0000376+0.000075)-0.5;//caculate and slightly adjust square wave frequency if ((m>=101)&&(m<=139)) f=(unsigned int) 1/ (1/11400 + m*0.0000376+0.000075)-1.5;//caculate and slightly adjust square wave frequency if ((m>=70)&&(m<=100)) f=(unsigned int) 1/ (1/11400 + m*0.0000376+0.000075)-2.5;//caculate and slightly adjust square wave frequency if ((m>=64)&&(m<=69)) f=(unsigned int) 1/ (1/11400 + m*0.0000376+0.000075)-3.5;//caculate and slightly adjust square wave frequency if ((m>=55)&&(m<=63)) f=(unsigned int) 1/ (1/11400 + m*0.0000376+0.000075)-4.5;//caculate and slightly adjust square wave frequency if ((m>=46)&&(m<=54)) f=(unsigned int) 1/ (1/11400 + m*0.0000376+0.000075)-5.5;//caculate and slightly adjust square wave frequency if ((m>=45)&&(m<=43)) f=(unsigned int) 1/ (1/11400 + m*0.0000376+0.000075)-6.5;//caculate and slightly adjust square wave frequency if ((m>=40)&&(m<=42)) f=(unsigned int) 1/ (1/11400 + m*0.0000376+0.000075)-7.5;//caculate and slightly adjust square wave frequency if ((m>=30)&&(m<=40)) f=(unsigned int) 1/ (1/11400 + m*0.0000376+0.000075)-9.5;//caculate and slightly adjust square wave frequency if ((m>=20)&&(m<=29)) f=(unsigned int) 1/ (1/11400 + m*0.0000376+0.000075)-11.5;//caculate and slightly adjust square wave frequency if ((m>=10)&&(m<=19)) f=(unsigned int) 1/ (1/11400 + m*0.0000376+0.000075)-2.5*m;//caculate and slightly adjust square wave frequency if (m==10) f=2131;//caculate and slightly adjust square wave frequency if (m==9) f=2313;//caculate and slightly adjust square wave frequency if (m==8) f=2540;//caculate and slightly adjust square wave frequency if (m==7) f=2810;//caculate and slightly adjust square wave frequency if (m==6) f=3140;//caculate and slightly adjust square wave frequency if (m==5) f=3570;//caculate and slightly adjust square wave frequency if (m==4) f=4130;//caculate and slightly adjust square wave frequency if (m==3) f=4900;//caculate and slightly adjust square wave frequency if (m==2) f=6020;//caculate and slightly adjust square wave frequency if (m==1) f=7690; //caculate and slightly adjust square wave frequency sprintf(msg2,"%10d Hz",f); LCD_Line2(msg2);// show frequency if (SWITCH_1==0) { switch_release();//wait for RA5 release return 2;// if RA5 pressed goto wavetype function } } while(SWITCH_0 == 1); switch_release();//wait for RB0 release squarewave(m);//output square wave switch_release();//wait for RA5 release return 3;// if RA5 pressed goto frequency range function break; default: break; } }
void Haptuator::renderVibration(double t,float freq,float mag){ double temp = sinewave(t,freq,mag); _acc_set[1] = (temp - _acc_set[0]) / _deltaT; _acc_set[0] = temp; }
void Haptuator::renderVibration(double t,float freq,float mag){ _acc_render = sinewave(t,freq,mag); }