|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
-
-
- #include <Arduino.h>
- #include "synth_tonesweep.h"
- #include "arm_math.h"
-
-
-
-
-
-
-
-
-
-
- boolean AudioSynthToneSweep::play(float t_amp,int t_lo,int t_hi,float t_time)
- {
- float tone_tmp;
-
- if(0) {
- Serial.print("AudioSynthToneSweep.begin(tone_amp = ");
- Serial.print(t_amp);
- Serial.print(", tone_lo = ");
- Serial.print(t_lo);
- Serial.print(", tone_hi = ");
- Serial.print(t_hi);
- Serial.print(", tone_time = ");
- Serial.print(t_time,1);
- Serial.println(")");
- }
- tone_amp = 0;
- if(t_amp < 0)return false;
- if(t_amp > 1)return false;
- if(t_lo < 1)return false;
- if(t_hi < 1)return false;
- if(t_hi >= (int) AUDIO_SAMPLE_RATE_EXACT / 2)return false;
- if(t_lo >= (int) AUDIO_SAMPLE_RATE_EXACT / 2)return false;
- if(t_time <= 0)return false;
- tone_lo = t_lo;
- tone_hi = t_hi;
- tone_phase = 0;
-
- tone_amp = t_amp * 32767.0;
-
- tone_freq = tone_lo*0x100000000LL;
- if (tone_hi >= tone_lo) {
- tone_tmp = tone_hi - tone_lo;
- tone_sign = 1;
- } else {
- tone_sign = -1;
- tone_tmp = tone_lo - tone_hi;
- }
- tone_tmp = tone_tmp / t_time / AUDIO_SAMPLE_RATE_EXACT;
- tone_incr = (tone_tmp * 0x100000000LL);
- sweep_busy = 1;
- return(true);
- }
-
-
-
- unsigned char AudioSynthToneSweep::isPlaying(void)
- {
- return(sweep_busy);
- }
-
-
- void AudioSynthToneSweep::update(void)
- {
- audio_block_t *block;
- short *bp;
- int i;
-
- if(!sweep_busy)return;
-
-
- block = allocate();
- if(block) {
- bp = block->data;
- uint32_t tmp = tone_freq >> 32;
- uint64_t tone_tmp = (tone_freq << 14) / (int) AUDIO_SAMPLE_RATE_EXACT;
- uint64_t incr = (tone_incr << 14) / (int) AUDIO_SAMPLE_RATE_EXACT;
-
- for(i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
- *bp++ = (short)(( (short)(arm_sin_q31((uint32_t)((tone_phase >> 15)&0x7fffffff))>>16) *tone_amp) >> 15);
-
- tone_phase += tone_tmp;
- tone_tmp += incr ;
-
- if(tone_sign > 0) {
- if(tmp > tone_hi) {
- sweep_busy = 0;
- break;
- }
- tone_freq += tone_incr;
- } else {
- if(tmp < tone_hi || tone_freq < tone_incr) {
- sweep_busy = 0;
-
- break;
- }
- tone_freq -= tone_incr;
- }
- }
- while(i < AUDIO_BLOCK_SAMPLES) {
- *bp++ = 0;
- i++;
- }
-
- transmit(block,0);
- release(block);
- }
- }
|