Synthesizer based on the Unzen / Nucleo F746ZG
Dependencies: amakusa mbed-dsp mbed shimabara ukifune unzen_nucleo_f746
Fork of skeleton_unzen_nucleo_f746 by
雲仙フレームワークのテストとして作っているプロジェクトです。中身はどんどん変っていきます。 説明はDSP空挺団の「シンセサイザー」カテゴリーを参照してください。初回は「ドッグフードを食べる」です。
vfo.cpp@22:dc2cbe8db9d9, 2017-02-05 (annotated)
- Committer:
- shorie
- Date:
- Sun Feb 05 06:16:49 2017 +0000
- Revision:
- 22:dc2cbe8db9d9
- Parent:
- 18:b9b1116f8768
- Child:
- 24:c77331e72374
Integrated filter
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shorie | 14:cec63d8da48c | 1 | #include "signal_processing.h" |
shorie | 14:cec63d8da48c | 2 | |
shorie | 18:b9b1116f8768 | 3 | |
shorie | 14:cec63d8da48c | 4 | VFO::VFO( void ) |
shorie | 14:cec63d8da48c | 5 | { |
shorie | 14:cec63d8da48c | 6 | // initial parameter setting. |
shorie | 18:b9b1116f8768 | 7 | this->form = triangle; |
shorie | 14:cec63d8da48c | 8 | this->Fs = 48000; |
shorie | 14:cec63d8da48c | 9 | this->frequency = 440; |
shorie | 14:cec63d8da48c | 10 | this->duty_cycle = 0.5; |
shorie | 16:d4ea3e6a0bce | 11 | |
shorie | 16:d4ea3e6a0bce | 12 | this->update_parameters(); |
shorie | 14:cec63d8da48c | 13 | } // End of constructor() |
shorie | 18:b9b1116f8768 | 14 | |
shorie | 18:b9b1116f8768 | 15 | VFO::~VFO( void ) |
shorie | 18:b9b1116f8768 | 16 | { |
shorie | 18:b9b1116f8768 | 17 | // do nothing |
shorie | 18:b9b1116f8768 | 18 | } |
shorie | 14:cec63d8da48c | 19 | |
shorie | 14:cec63d8da48c | 20 | |
shorie | 18:b9b1116f8768 | 21 | |
shorie | 14:cec63d8da48c | 22 | void VFO::run( |
shorie | 14:cec63d8da48c | 23 | float out_buffer[], // vfo output buffer |
shorie | 14:cec63d8da48c | 24 | unsigned int block_size // block size [sample] |
shorie | 14:cec63d8da48c | 25 | ) |
shorie | 14:cec63d8da48c | 26 | { |
shorie | 22:dc2cbe8db9d9 | 27 | |
shorie | 14:cec63d8da48c | 28 | // place the signal processing coce here |
shorie | 14:cec63d8da48c | 29 | for ( int i= 0; i< block_size; i++ ) |
shorie | 14:cec63d8da48c | 30 | { |
shorie | 15:de22b9d147e0 | 31 | // 1 : if phase < half_way; 0 : others. |
shorie | 18:b9b1116f8768 | 32 | if ( this->form == square ) |
shorie | 15:de22b9d147e0 | 33 | { |
shorie | 15:de22b9d147e0 | 34 | if ( this->current_phase < this->half_way ) |
shorie | 16:d4ea3e6a0bce | 35 | out_buffer[i] = 1.0; |
shorie | 15:de22b9d147e0 | 36 | else |
shorie | 15:de22b9d147e0 | 37 | out_buffer[i] = 0.0; |
shorie | 15:de22b9d147e0 | 38 | } |
shorie | 18:b9b1116f8768 | 39 | else // form == triangle |
shorie | 16:d4ea3e6a0bce | 40 | { |
shorie | 16:d4ea3e6a0bce | 41 | if ( this->current_phase < this->half_way ) |
shorie | 16:d4ea3e6a0bce | 42 | out_buffer[i] = this->rising_rate * this->current_phase; |
shorie | 16:d4ea3e6a0bce | 43 | else |
shorie | 16:d4ea3e6a0bce | 44 | out_buffer[i] = 1 + this->falling_rate * ( this->current_phase - this->half_way ); |
shorie | 16:d4ea3e6a0bce | 45 | } |
shorie | 15:de22b9d147e0 | 46 | |
shorie | 15:de22b9d147e0 | 47 | // update phase |
shorie | 15:de22b9d147e0 | 48 | this->current_phase += this->frequency; |
shorie | 15:de22b9d147e0 | 49 | // limit the range of the phase. |
shorie | 15:de22b9d147e0 | 50 | if ( this->current_phase >= this->Fs ) |
shorie | 15:de22b9d147e0 | 51 | this->current_phase -= this->Fs; |
shorie | 14:cec63d8da48c | 52 | } |
shorie | 14:cec63d8da48c | 53 | } // End of run() |
shorie | 14:cec63d8da48c | 54 | |
shorie | 14:cec63d8da48c | 55 | |
shorie | 14:cec63d8da48c | 56 | void VFO::set_Fs( int Fs ) |
shorie | 14:cec63d8da48c | 57 | { |
shorie | 14:cec63d8da48c | 58 | // regulate the Fs. |
shorie | 14:cec63d8da48c | 59 | if ( Fs != 32000 && Fs != 44100 && Fs != 96000 && Fs != 48000 ) |
shorie | 14:cec63d8da48c | 60 | Fs = 48000; |
shorie | 14:cec63d8da48c | 61 | this->Fs = Fs; |
shorie | 16:d4ea3e6a0bce | 62 | |
shorie | 16:d4ea3e6a0bce | 63 | this->update_parameters(); |
shorie | 14:cec63d8da48c | 64 | } |
shorie | 14:cec63d8da48c | 65 | |
shorie | 14:cec63d8da48c | 66 | void VFO::set_frequency( int freq ) |
shorie | 14:cec63d8da48c | 67 | { |
shorie | 14:cec63d8da48c | 68 | if ( freq > this->Fs / 4 ) |
shorie | 16:d4ea3e6a0bce | 69 | freq = Fs / 4; |
shorie | 14:cec63d8da48c | 70 | this->frequency = freq; |
shorie | 16:d4ea3e6a0bce | 71 | |
shorie | 16:d4ea3e6a0bce | 72 | this->update_parameters(); |
shorie | 14:cec63d8da48c | 73 | } |
shorie | 14:cec63d8da48c | 74 | |
shorie | 14:cec63d8da48c | 75 | void VFO::set_duty_cycle( float duty ) |
shorie | 14:cec63d8da48c | 76 | { |
shorie | 14:cec63d8da48c | 77 | if ( duty > 0.5f ) // high limit |
shorie | 14:cec63d8da48c | 78 | duty = 0.5f; |
shorie | 17:728ffc633179 | 79 | if ( duty < 0.0f ) // low limit |
shorie | 17:728ffc633179 | 80 | duty = 0.0f; |
shorie | 14:cec63d8da48c | 81 | this->duty_cycle = duty; |
shorie | 16:d4ea3e6a0bce | 82 | |
shorie | 16:d4ea3e6a0bce | 83 | this->update_parameters(); |
shorie | 14:cec63d8da48c | 84 | } |
shorie | 14:cec63d8da48c | 85 | |
shorie | 18:b9b1116f8768 | 86 | void VFO::set_wave_form( wave_form form ) |
shorie | 14:cec63d8da48c | 87 | { |
shorie | 18:b9b1116f8768 | 88 | this->form = form; |
shorie | 14:cec63d8da48c | 89 | } |
shorie | 14:cec63d8da48c | 90 | |
shorie | 14:cec63d8da48c | 91 | |
shorie | 16:d4ea3e6a0bce | 92 | // update the internal parameter by given parameters |
shorie | 16:d4ea3e6a0bce | 93 | void VFO::update_parameters(void) |
shorie | 16:d4ea3e6a0bce | 94 | { |
shorie | 16:d4ea3e6a0bce | 95 | // calc the half_way; |
shorie | 17:728ffc633179 | 96 | this-> half_way = this->Fs * this-> duty_cycle; |
shorie | 16:d4ea3e6a0bce | 97 | |
shorie | 16:d4ea3e6a0bce | 98 | // forbid to be zero. |
shorie | 16:d4ea3e6a0bce | 99 | if ( this-> half_way < this->frequency ) |
shorie | 16:d4ea3e6a0bce | 100 | half_way = this->frequency; |
shorie | 16:d4ea3e6a0bce | 101 | |
shorie | 16:d4ea3e6a0bce | 102 | // for triangle wave; |
shorie | 16:d4ea3e6a0bce | 103 | this->rising_rate = 1.0 / this->half_way; |
shorie | 16:d4ea3e6a0bce | 104 | |
shorie | 16:d4ea3e6a0bce | 105 | this->falling_rate = - 1.0 / ( this->Fs - this->half_way ); |
shorie | 16:d4ea3e6a0bce | 106 | } |
shorie | 16:d4ea3e6a0bce | 107 |