Synthesizer based on the Unzen / Nucleo F746ZG

Dependencies:   amakusa mbed-dsp mbed shimabara ukifune unzen_nucleo_f746

Fork of skeleton_unzen_nucleo_f746 by seiichi horie

雲仙フレームワークのテストとして作っているプロジェクトです。中身はどんどん変っていきます。 説明はDSP空挺団の「シンセサイザー」カテゴリーを参照してください。初回は「ドッグフードを食べる」です。

Committer:
shorie
Date:
Sat Feb 18 04:37:19 2017 +0000
Revision:
29:8ee84bda128c
Parent:
28:547f19ed6f67
Limiter function is confirmed. SV filter ignored bug was fixed.

Who changed what in which revision?

UserRevisionLine numberNew 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 26:e99f71165e19 10 enum eg_state { release, attack, decay };
shorie 18:b9b1116f8768 11
shorie 18:b9b1116f8768 12 // Variable Frequency Oscillator. Only square and triangle
shorie 14:cec63d8da48c 13 class VFO {
shorie 14:cec63d8da48c 14 public:
shorie 26:e99f71165e19 15 VFO( int32_t block_size );
shorie 26:e99f71165e19 16 virtual ~VFO();
shorie 14:cec63d8da48c 17 void run(
shorie 26:e99f71165e19 18 float out_buffer[] // place to write the right output samples
shorie 14:cec63d8da48c 19 );
shorie 14:cec63d8da48c 20
shorie 14:cec63d8da48c 21 // parameter settings
shorie 28:547f19ed6f67 22 void set_frequency( float 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 26:e99f71165e19 27 int32_t block_size;
shorie 14:cec63d8da48c 28
shorie 16:d4ea3e6a0bce 29 // control variables.
shorie 28:547f19ed6f67 30 float frequency; // VFO frequency [Hz]
shorie 14:cec63d8da48c 31 int Fs; // sampling Frequency [Hz]
shorie 14:cec63d8da48c 32 float duty_cycle; // VFO duty cycle. 0 ... 0.5
shorie 18:b9b1116f8768 33 wave_form form; // form of the wave form.
shorie 15:de22b9d147e0 34
shorie 16:d4ea3e6a0bce 35 // internal variable.
shorie 28:547f19ed6f67 36 float current_phase; // internal variable of VFO.
shorie 28:547f19ed6f67 37 float half_way; // change point by duty cycle. ( period * duty_cycle ).
shorie 16:d4ea3e6a0bce 38 float rising_rate;
shorie 16:d4ea3e6a0bce 39 float falling_rate;
shorie 16:d4ea3e6a0bce 40
shorie 16:d4ea3e6a0bce 41 void update_parameters(void); // call one of the parameter is changed.
shorie 14:cec63d8da48c 42 };
shorie 14:cec63d8da48c 43
shorie 19:f5e785fe50b1 44 // Blocking DC
shorie 19:f5e785fe50b1 45 class DCBlocker : public amakusa::AbstractFilter {
shorie 19:f5e785fe50b1 46 public:
shorie 19:f5e785fe50b1 47 DCBlocker( uint32_t blockSize );
shorie 28:547f19ed6f67 48 virtual void run( float *pSrc, float *pDst );
shorie 19:f5e785fe50b1 49 private:
shorie 19:f5e785fe50b1 50 float x_last;
shorie 19:f5e785fe50b1 51 float y_last;
shorie 19:f5e785fe50b1 52 };
shorie 19:f5e785fe50b1 53
shorie 20:53021b2c424d 54 // State Variable Filter
shorie 20:53021b2c424d 55 // The f * f_factor must be < Fs/6.
shorie 20:53021b2c424d 56 class SVFilter : public amakusa::AbstractFilter {
shorie 20:53021b2c424d 57 public:
shorie 27:fcb1f1da2ad7 58 SVFilter( uint32_t a_block_size );
shorie 28:547f19ed6f67 59 virtual void run( float *pSrc, float *pDst);
shorie 28:547f19ed6f67 60 void set_Q( float Q ); // real Q factor [ 0.5 ... inf ]
shorie 20:53021b2c424d 61 void set_Fs( int new_Fs ); // Hz
shorie 20:53021b2c424d 62 void set_fc( int new_fc ); // Hz
shorie 28:547f19ed6f67 63 void set_f_factor( float new_f_factor );
shorie 20:53021b2c424d 64 void set_mode( svf_mode new_mode );
shorie 20:53021b2c424d 65 private:
shorie 20:53021b2c424d 66 // internal variable
shorie 28:547f19ed6f67 67 float d1, d2; // delay 1, delay 2;
shorie 28:547f19ed6f67 68 float q, f; // q = 1/Q, f = 2 * sin( fc*f_factor*pi/Fs );
shorie 20:53021b2c424d 69 // parameter set by method
shorie 23:58626090fce3 70 int Fs, fc; // sampling frequency and control frequency
shorie 28:547f19ed6f67 71 float f_factor;
shorie 20:53021b2c424d 72 svf_mode mode; // lpf, hpf, bpf
shorie 20:53021b2c424d 73 void update_parameters( void );
shorie 20:53021b2c424d 74 };
shorie 20:53021b2c424d 75
shorie 26:e99f71165e19 76 class EG {
shorie 26:e99f71165e19 77 public:
shorie 27:fcb1f1da2ad7 78 EG ( int32_t a_block_size );
shorie 28:547f19ed6f67 79 virtual void run( float *pEnvelope );
shorie 26:e99f71165e19 80 void on(void);
shorie 26:e99f71165e19 81 void off(void);
shorie 28:547f19ed6f67 82 void set_attack( float attack ); // [0,1.0]
shorie 28:547f19ed6f67 83 void set_decay( float attack ); // [0,1.0]
shorie 28:547f19ed6f67 84 void set_sustain( float sustain ); // [0,1.0]
shorie 28:547f19ed6f67 85 void set_release ( float attack ); // [0,1.0]
shorie 26:e99f71165e19 86 private:
shorie 28:547f19ed6f67 87 float current_level, sustain_level;
shorie 28:547f19ed6f67 88 float attack_time_constant, decay_time_constant, release_time_constant;
shorie 26:e99f71165e19 89 eg_state state;
shorie 26:e99f71165e19 90 int32_t block_size;
shorie 26:e99f71165e19 91 };
shorie 26:e99f71165e19 92
shorie 18:b9b1116f8768 93 // Monophonic synthsizer class
shorie 18:b9b1116f8768 94 class Monophonic {
shorie 18:b9b1116f8768 95 public:
shorie 18:b9b1116f8768 96 Monophonic( unsigned int block_size );
shorie 18:b9b1116f8768 97 virtual ~Monophonic(void);
shorie 18:b9b1116f8768 98 void run(
shorie 26:e99f71165e19 99 float out_buffer[] // place to write the right output samples
shorie 18:b9b1116f8768 100 );
shorie 18:b9b1116f8768 101 void set_Fs( int Fs ); // unit is Hz.
shorie 28:547f19ed6f67 102 void set_vfo_frequency( float freq ); // unit is Hz.
shorie 18:b9b1116f8768 103 void set_vfo_duty_cycle( float duty ); // 0 ... 0.5
shorie 18:b9b1116f8768 104 void set_vfo_wave_form( wave_form form );
shorie 22:dc2cbe8db9d9 105 void set_filter_mode( svf_mode mode );
shorie 22:dc2cbe8db9d9 106 void set_filter_Q( float Q ); // 0.5 .. inf
shorie 22:dc2cbe8db9d9 107 void set_filter_f_factor( float f_factor ); // 0.0..1.0
shorie 26:e99f71165e19 108 void eg_on(void);
shorie 26:e99f71165e19 109 void eg_off(void);
shorie 28:547f19ed6f67 110 void set_eg_attack( float attack ); // [0,1.0]
shorie 28:547f19ed6f67 111 void set_eg_decay( float decay ); // [0,1.0]
shorie 28:547f19ed6f67 112 void set_eg_sustain( float sustain ); // [0,1.0]
shorie 28:547f19ed6f67 113 void set_eg_release ( float release ); // [0,1.0]
shorie 18:b9b1116f8768 114 private:
shorie 18:b9b1116f8768 115 VFO *vfo;
shorie 20:53021b2c424d 116 DCBlocker *dc_blocker;
shorie 20:53021b2c424d 117 SVFilter *sv_filter;
shorie 26:e99f71165e19 118 EG *eg;
shorie 29:8ee84bda128c 119 amakusa::LimitterLinAtan *limitter;
shorie 28:547f19ed6f67 120 float *work_buf_a, *work_buf_b;
shorie 26:e99f71165e19 121 int32_t block_size;
shorie 18:b9b1116f8768 122 };
shorie 18:b9b1116f8768 123
shorie 14:cec63d8da48c 124
shorie 6:486b1cb03e61 125 // User Signal processing Class
shorie 6:486b1cb03e61 126 class SignalProcessing {
shorie 6:486b1cb03e61 127 public:
shorie 6:486b1cb03e61 128 // essential members. Do not touch
shorie 6:486b1cb03e61 129 SignalProcessing( unsigned int block_size );
shorie 6:486b1cb03e61 130 void run(
shorie 6:486b1cb03e61 131 float rx_left_buffer[], // array of the left input samples
shorie 6:486b1cb03e61 132 float rx_right_buffer[], // array of the right input samples
shorie 6:486b1cb03e61 133 float tx_left_buffer[], // place to write the left output samples
shorie 13:b33cb5925113 134 float tx_right_buffer[], // place to write the right output samples
shorie 6:486b1cb03e61 135 unsigned int block_size // block size [sample]
shorie 6:486b1cb03e61 136 );
shorie 6:486b1cb03e61 137
shorie 6:486b1cb03e61 138 // project depenedent members.
shorie 6:486b1cb03e61 139 void set_volume( float vol );
shorie 22:dc2cbe8db9d9 140 void set_Fs( int Fs ); // unit is Hz.
shorie 28:547f19ed6f67 141 void set_vfo_frequency( float freq ); // unit is Hz.
shorie 22:dc2cbe8db9d9 142 void set_vfo_duty_cycle( float duty ); // 0 ... 1.0
shorie 18:b9b1116f8768 143 void set_vfo_wave_form( wave_form form );
shorie 22:dc2cbe8db9d9 144 void set_filter_mode( svf_mode mode );
shorie 22:dc2cbe8db9d9 145 void set_filter_Q( float Q ); // 0.0..1.0
shorie 22:dc2cbe8db9d9 146 void set_filter_f_factor( float f_factor ); // 0.0..1.0
shorie 26:e99f71165e19 147 void eg_on(void);
shorie 26:e99f71165e19 148 void eg_off(void);
shorie 28:547f19ed6f67 149 void set_eg_attack( float attack ); // [0,1.0]
shorie 28:547f19ed6f67 150 void set_eg_decay( float decay ); // [0,1.0]
shorie 28:547f19ed6f67 151 void set_eg_sustain( float sustain ); // [0,1.0]
shorie 28:547f19ed6f67 152 void set_eg_release ( float release ); // [0,1.0]
shorie 6:486b1cb03e61 153 private:
shorie 6:486b1cb03e61 154 // essential members. Do not touch.
shorie 6:486b1cb03e61 155 void enter_critical_section(void);
shorie 6:486b1cb03e61 156 void leave_critical_section(void);
shorie 6:486b1cb03e61 157
shorie 6:486b1cb03e61 158 // project dependent members.
shorie 14:cec63d8da48c 159 float volume_level; // 0 ... 1.0
shorie 18:b9b1116f8768 160 Monophonic * note;
shorie 6:486b1cb03e61 161 };
shorie 6:486b1cb03e61 162
shorie 6:486b1cb03e61 163 #endif