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空挺団の「シンセサイザー」カテゴリーを参照してください。初回は「ドッグフードを食べる」です。
signal_processing.h@23:58626090fce3, 2017-02-05 (annotated)
- Committer:
- shorie
- Date:
- Sun Feb 05 06:25:03 2017 +0000
- Revision:
- 23:58626090fce3
- Parent:
- 22:dc2cbe8db9d9
- Child:
- 25:d15dd7b9101c
Fixed bug of the f_factor in SVF
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shorie | 6:486b1cb03e61 | 1 | #ifndef _SIGNAL_PROCESSING_H_ |
shorie | 6:486b1cb03e61 | 2 | #define _SIGNAL_PROCESSING_H_ |
shorie | 6:486b1cb03e61 | 3 | |
shorie | 6:486b1cb03e61 | 4 | #include "amakusa.h" |
shorie | 6:486b1cb03e61 | 5 | |
shorie | 18:b9b1116f8768 | 6 | #define SAMPLING_FREQUENCY 48000 |
shorie | 14:cec63d8da48c | 7 | |
shorie | 18:b9b1116f8768 | 8 | enum wave_form { triangle, square }; |
shorie | 20:53021b2c424d | 9 | enum svf_mode { lpf, hpf, bpf }; |
shorie | 18:b9b1116f8768 | 10 | |
shorie | 18:b9b1116f8768 | 11 | // Variable Frequency Oscillator. Only square and triangle |
shorie | 14:cec63d8da48c | 12 | class VFO { |
shorie | 14:cec63d8da48c | 13 | public: |
shorie | 14:cec63d8da48c | 14 | VFO( void ); |
shorie | 18:b9b1116f8768 | 15 | virtual ~VFO(void); |
shorie | 14:cec63d8da48c | 16 | void run( |
shorie | 18:b9b1116f8768 | 17 | float out_buffer[], // place to write the right output samples |
shorie | 22:dc2cbe8db9d9 | 18 | unsigned int block_size // block size [sample] |
shorie | 14:cec63d8da48c | 19 | ); |
shorie | 14:cec63d8da48c | 20 | |
shorie | 14:cec63d8da48c | 21 | // parameter settings |
shorie | 18:b9b1116f8768 | 22 | void set_frequency( int freq ); // unit is Hz. |
shorie | 18:b9b1116f8768 | 23 | void set_Fs( int Fs ); // unit is Hz. |
shorie | 14:cec63d8da48c | 24 | void set_duty_cycle( float duty ); // 0 ... 0.5 |
shorie | 18:b9b1116f8768 | 25 | void set_wave_form( wave_form form ); |
shorie | 14:cec63d8da48c | 26 | private: |
shorie | 14:cec63d8da48c | 27 | |
shorie | 16:d4ea3e6a0bce | 28 | // control variables. |
shorie | 14:cec63d8da48c | 29 | int frequency; // VFO frequency [Hz] |
shorie | 14:cec63d8da48c | 30 | int Fs; // sampling Frequency [Hz] |
shorie | 14:cec63d8da48c | 31 | float duty_cycle; // VFO duty cycle. 0 ... 0.5 |
shorie | 18:b9b1116f8768 | 32 | wave_form form; // form of the wave form. |
shorie | 15:de22b9d147e0 | 33 | |
shorie | 16:d4ea3e6a0bce | 34 | // internal variable. |
shorie | 19:f5e785fe50b1 | 35 | int current_phase; // internal variable of VFO. |
shorie | 16:d4ea3e6a0bce | 36 | int half_way; // change point by duty cycle. ( period * duty_cycle ). |
shorie | 16:d4ea3e6a0bce | 37 | float rising_rate; |
shorie | 16:d4ea3e6a0bce | 38 | float falling_rate; |
shorie | 16:d4ea3e6a0bce | 39 | |
shorie | 16:d4ea3e6a0bce | 40 | void update_parameters(void); // call one of the parameter is changed. |
shorie | 14:cec63d8da48c | 41 | }; |
shorie | 14:cec63d8da48c | 42 | |
shorie | 19:f5e785fe50b1 | 43 | // Blocking DC |
shorie | 19:f5e785fe50b1 | 44 | class DCBlocker : public amakusa::AbstractFilter { |
shorie | 19:f5e785fe50b1 | 45 | public: |
shorie | 19:f5e785fe50b1 | 46 | DCBlocker( uint32_t blockSize ); |
shorie | 19:f5e785fe50b1 | 47 | virtual void run( float32_t *pSrc, float32_t *pDst, uint32_t blockSize = 0 ); |
shorie | 19:f5e785fe50b1 | 48 | private: |
shorie | 19:f5e785fe50b1 | 49 | float x_last; |
shorie | 19:f5e785fe50b1 | 50 | float y_last; |
shorie | 19:f5e785fe50b1 | 51 | }; |
shorie | 19:f5e785fe50b1 | 52 | |
shorie | 20:53021b2c424d | 53 | // State Variable Filter |
shorie | 20:53021b2c424d | 54 | // The f * f_factor must be < Fs/6. |
shorie | 20:53021b2c424d | 55 | class SVFilter : public amakusa::AbstractFilter { |
shorie | 20:53021b2c424d | 56 | public: |
shorie | 20:53021b2c424d | 57 | SVFilter( uint32_t blockSize ); |
shorie | 20:53021b2c424d | 58 | virtual void run( float32_t *pSrc, float32_t *pDst, uint32_t blockSize = 0 ); |
shorie | 20:53021b2c424d | 59 | void set_Q( float32_t Q ); // real Q factor [ 0.5 ... inf ] |
shorie | 20:53021b2c424d | 60 | void set_Fs( int new_Fs ); // Hz |
shorie | 20:53021b2c424d | 61 | void set_fc( int new_fc ); // Hz |
shorie | 20:53021b2c424d | 62 | void set_f_factor( float32_t new_f_factor ); |
shorie | 20:53021b2c424d | 63 | void set_mode( svf_mode new_mode ); |
shorie | 20:53021b2c424d | 64 | private: |
shorie | 20:53021b2c424d | 65 | // internal variable |
shorie | 20:53021b2c424d | 66 | float32_t d1, d2; // delay 1, delay 2; |
shorie | 20:53021b2c424d | 67 | float32_t q, f; // q = 1/Q, f = 2 * sin( fc*f_factor*pi/Fs ); |
shorie | 20:53021b2c424d | 68 | // parameter set by method |
shorie | 23:58626090fce3 | 69 | int Fs, fc; // sampling frequency and control frequency |
shorie | 23:58626090fce3 | 70 | float32_t f_factor; |
shorie | 20:53021b2c424d | 71 | svf_mode mode; // lpf, hpf, bpf |
shorie | 20:53021b2c424d | 72 | void update_parameters( void ); |
shorie | 20:53021b2c424d | 73 | }; |
shorie | 20:53021b2c424d | 74 | |
shorie | 18:b9b1116f8768 | 75 | // Monophonic synthsizer class |
shorie | 18:b9b1116f8768 | 76 | class Monophonic { |
shorie | 18:b9b1116f8768 | 77 | public: |
shorie | 18:b9b1116f8768 | 78 | Monophonic( unsigned int block_size ); |
shorie | 18:b9b1116f8768 | 79 | virtual ~Monophonic(void); |
shorie | 18:b9b1116f8768 | 80 | void run( |
shorie | 18:b9b1116f8768 | 81 | float out_buffer[], // place to write the right output samples |
shorie | 20:53021b2c424d | 82 | unsigned int block_size = 0 // block size [sample] |
shorie | 18:b9b1116f8768 | 83 | ); |
shorie | 18:b9b1116f8768 | 84 | void set_Fs( int Fs ); // unit is Hz. |
shorie | 18:b9b1116f8768 | 85 | void set_vfo_frequency( int freq ); // unit is Hz. |
shorie | 18:b9b1116f8768 | 86 | void set_vfo_duty_cycle( float duty ); // 0 ... 0.5 |
shorie | 18:b9b1116f8768 | 87 | void set_vfo_wave_form( wave_form form ); |
shorie | 22:dc2cbe8db9d9 | 88 | void set_filter_mode( svf_mode mode ); |
shorie | 22:dc2cbe8db9d9 | 89 | void set_filter_Q( float Q ); // 0.5 .. inf |
shorie | 22:dc2cbe8db9d9 | 90 | void set_filter_f_factor( float f_factor ); // 0.0..1.0 |
shorie | 18:b9b1116f8768 | 91 | private: |
shorie | 18:b9b1116f8768 | 92 | VFO *vfo; |
shorie | 20:53021b2c424d | 93 | DCBlocker *dc_blocker; |
shorie | 20:53021b2c424d | 94 | SVFilter *sv_filter; |
shorie | 20:53021b2c424d | 95 | float32_t *work_buf_a, *work_buf_b; |
shorie | 18:b9b1116f8768 | 96 | }; |
shorie | 18:b9b1116f8768 | 97 | |
shorie | 14:cec63d8da48c | 98 | |
shorie | 6:486b1cb03e61 | 99 | // User Signal processing Class |
shorie | 6:486b1cb03e61 | 100 | class SignalProcessing { |
shorie | 6:486b1cb03e61 | 101 | public: |
shorie | 6:486b1cb03e61 | 102 | // essential members. Do not touch |
shorie | 6:486b1cb03e61 | 103 | SignalProcessing( unsigned int block_size ); |
shorie | 6:486b1cb03e61 | 104 | void run( |
shorie | 6:486b1cb03e61 | 105 | float rx_left_buffer[], // array of the left input samples |
shorie | 6:486b1cb03e61 | 106 | float rx_right_buffer[], // array of the right input samples |
shorie | 6:486b1cb03e61 | 107 | float tx_left_buffer[], // place to write the left output samples |
shorie | 13:b33cb5925113 | 108 | float tx_right_buffer[], // place to write the right output samples |
shorie | 6:486b1cb03e61 | 109 | unsigned int block_size // block size [sample] |
shorie | 6:486b1cb03e61 | 110 | ); |
shorie | 6:486b1cb03e61 | 111 | |
shorie | 6:486b1cb03e61 | 112 | // project depenedent members. |
shorie | 6:486b1cb03e61 | 113 | void set_volume( float vol ); |
shorie | 22:dc2cbe8db9d9 | 114 | void set_Fs( int Fs ); // unit is Hz. |
shorie | 22:dc2cbe8db9d9 | 115 | void set_vfo_frequency( int freq ); // unit is Hz. |
shorie | 22:dc2cbe8db9d9 | 116 | void set_vfo_duty_cycle( float duty ); // 0 ... 1.0 |
shorie | 18:b9b1116f8768 | 117 | void set_vfo_wave_form( wave_form form ); |
shorie | 22:dc2cbe8db9d9 | 118 | void set_filter_mode( svf_mode mode ); |
shorie | 22:dc2cbe8db9d9 | 119 | void set_filter_Q( float Q ); // 0.0..1.0 |
shorie | 22:dc2cbe8db9d9 | 120 | void set_filter_f_factor( float f_factor ); // 0.0..1.0 |
shorie | 6:486b1cb03e61 | 121 | private: |
shorie | 6:486b1cb03e61 | 122 | // essential members. Do not touch. |
shorie | 6:486b1cb03e61 | 123 | void enter_critical_section(void); |
shorie | 6:486b1cb03e61 | 124 | void leave_critical_section(void); |
shorie | 6:486b1cb03e61 | 125 | |
shorie | 6:486b1cb03e61 | 126 | // project dependent members. |
shorie | 14:cec63d8da48c | 127 | float volume_level; // 0 ... 1.0 |
shorie | 18:b9b1116f8768 | 128 | Monophonic * note; |
shorie | 6:486b1cb03e61 | 129 | }; |
shorie | 6:486b1cb03e61 | 130 | |
shorie | 6:486b1cb03e61 | 131 | #endif |