Synthesizer based on the Unzen / Nucleo F746ZG
Dependencies: amakusa mbed-dsp mbed shimabara ukifune unzen_nucleo_f746
Fork of skeleton_unzen_nucleo_f746 by
vfo.cpp
00001 #include "signal_processing.h" 00002 00003 00004 VFO::VFO( int32_t block_size ) 00005 { 00006 // initial parameter setting. 00007 this->block_size = block_size; 00008 00009 this->form = triangle; 00010 this->Fs = 48000; 00011 this->frequency = 440; 00012 this->duty_cycle = 0.5; 00013 00014 this->update_parameters(); 00015 } // End of constructor() 00016 00017 VFO::~VFO( void ) 00018 { 00019 // do nothing 00020 } 00021 00022 00023 00024 void VFO::run( 00025 float out_buffer[] // vfo output buffer 00026 ) 00027 { 00028 00029 // place the signal processing coce here 00030 for ( int i= 0; i< this->block_size; i++ ) 00031 { 00032 // 1 : if phase < half_way; 0 : others. 00033 if ( this->form == square ) 00034 { 00035 if ( this->current_phase < this->half_way ) 00036 out_buffer[i] = 1.0; 00037 else 00038 out_buffer[i] = 0.0; 00039 } 00040 else // form == triangle 00041 { 00042 if ( this->current_phase < this->half_way ) 00043 out_buffer[i] = this->rising_rate * this->current_phase; 00044 else 00045 out_buffer[i] = 1 + this->falling_rate * ( this->current_phase - this->half_way ); 00046 } 00047 00048 // update phase 00049 this->current_phase += this->frequency; 00050 // limit the range of the phase. 00051 if ( this->current_phase >= this->Fs ) 00052 this->current_phase -= this->Fs; 00053 } 00054 } // End of run() 00055 00056 00057 void VFO::set_Fs( int Fs ) 00058 { 00059 // regulate the Fs. 00060 if ( Fs != 32000 && Fs != 44100 && Fs != 96000 && Fs != 48000 ) 00061 Fs = 48000; 00062 this->Fs = Fs; 00063 00064 this->update_parameters(); 00065 } 00066 00067 void VFO::set_frequency( float freq ) 00068 { 00069 if ( freq > this->Fs / 4 ) 00070 freq = Fs / 4; 00071 this->frequency = freq; 00072 00073 this->update_parameters(); 00074 } 00075 00076 void VFO::set_duty_cycle( float duty ) 00077 { 00078 if ( duty > 0.5f ) // high limit 00079 duty = 0.5f; 00080 if ( duty < 0.0f ) // low limit 00081 duty = 0.0f; 00082 this->duty_cycle = duty; 00083 00084 this->update_parameters(); 00085 } 00086 00087 void VFO::set_wave_form( wave_form form ) 00088 { 00089 this->form = form; 00090 } 00091 00092 00093 // update the internal parameter by given parameters 00094 void VFO::update_parameters(void) 00095 { 00096 // calc the half_way; 00097 this-> half_way = this->Fs * this-> duty_cycle; 00098 00099 // make it integer multiple of frequency 00100 this->half_way = (int)(this->half_way/this->frequency); 00101 this->half_way *= this->frequency; 00102 00103 // forbid to be zero. 00104 if ( this-> half_way < this->frequency ) 00105 this->half_way = this->frequency; 00106 00107 // for triangle wave; 00108 this->rising_rate = 1.0 / this->half_way; 00109 00110 this->falling_rate = - 1.0 / ( this->Fs - this->half_way ); 00111 } 00112
Generated on Wed Jul 13 2022 23:24:36 by 1.7.2