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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers signal_processing.h Source File

signal_processing.h

00001 #ifndef _SIGNAL_PROCESSING_H_
00002 #define _SIGNAL_PROCESSING_H_
00003 
00004 #include "amakusa.h"
00005 
00006 #define SAMPLING_FREQUENCY 48000
00007 
00008 enum wave_form { triangle, square };
00009 enum svf_mode { lpf, hpf, bpf };
00010 enum eg_state { release, attack, decay };
00011 
00012     // Variable Frequency Oscillator. Only square and triangle
00013 class VFO {
00014 public:
00015     VFO( int32_t block_size );
00016     virtual ~VFO();
00017     void run(           
00018         float out_buffer[]         // place to write the right output samples
00019         );
00020            
00021         // parameter settings
00022     void set_frequency( float freq );     // unit is Hz.
00023     void set_Fs( int Fs );              // unit is Hz.
00024     void set_duty_cycle( float duty );  // 0 ... 0.5
00025     void set_wave_form( wave_form form );
00026 private:
00027     int32_t block_size;
00028 
00029         // control variables.
00030     float frequency;          // VFO frequency [Hz]
00031     int Fs;                 // sampling Frequency [Hz]
00032     float duty_cycle;       // VFO duty cycle. 0 ... 0.5
00033     wave_form form;         // form of the wave form.
00034     
00035         // internal variable.
00036     float current_phase;      // internal variable of VFO.
00037     float half_way;           // change point by duty cycle. ( period * duty_cycle ).
00038     float rising_rate;
00039     float falling_rate;
00040     
00041     void update_parameters(void);    // call one of the parameter is changed.
00042 };
00043 
00044     // Blocking DC 
00045 class DCBlocker : public amakusa::AbstractFilter {
00046 public:
00047     DCBlocker( uint32_t blockSize );
00048     virtual void run( float *pSrc, float *pDst );
00049 private:    
00050     float x_last;
00051     float y_last;
00052 };
00053 
00054     // State Variable Filter
00055     // The f * f_factor must be < Fs/6.
00056 class SVFilter : public amakusa::AbstractFilter {
00057 public:
00058     SVFilter( uint32_t a_block_size );
00059     virtual void run( float *pSrc, float *pDst);
00060     void set_Q( float Q );              // real Q factor [ 0.5 ... inf ]
00061     void set_Fs( int new_Fs );              // Hz
00062     void set_fc( int new_fc );                // Hz
00063     void set_f_factor( float new_f_factor );   
00064     void set_mode( svf_mode new_mode );
00065 private:    
00066         // internal variable
00067     float d1, d2;                       // delay 1, delay 2;
00068     float q, f;                         // q = 1/Q, f = 2 * sin( fc*f_factor*pi/Fs );
00069         // parameter set by method
00070     int Fs, fc;                             // sampling frequency and control frequency
00071     float f_factor;
00072     svf_mode mode;                          // lpf, hpf, bpf
00073     void update_parameters( void );
00074 };
00075 
00076 class EG {
00077 public:
00078     EG ( int32_t a_block_size );
00079     virtual void run( float *pEnvelope );
00080     void on(void);
00081     void off(void);
00082     void set_attack( float attack );                // [0,1.0]
00083     void set_decay( float attack );                 // [0,1.0]
00084     void set_sustain( float sustain );              // [0,1.0]
00085     void set_release ( float attack );              // [0,1.0]
00086 private:
00087     float current_level, sustain_level;
00088     float attack_time_constant, decay_time_constant, release_time_constant;
00089     eg_state  state;
00090     int32_t block_size;
00091 };
00092 
00093     // Monophonic synthsizer class
00094 class Monophonic {
00095 public:
00096     Monophonic( unsigned int  block_size );
00097     virtual ~Monophonic(void);
00098     void run(           
00099         float out_buffer[]         // place to write the right output samples
00100         );
00101     void set_Fs( int Fs );                  // unit is Hz.
00102     void set_vfo_frequency( float freq );     // unit is Hz.
00103     void set_vfo_duty_cycle( float duty );  // 0 ... 0.5
00104     void set_vfo_wave_form( wave_form form );
00105     void set_filter_mode( svf_mode mode );
00106     void set_filter_Q( float Q );           // 0.5 .. inf
00107     void set_filter_f_factor( float f_factor ); // 0.0..1.0
00108     void eg_on(void);
00109     void eg_off(void);
00110     void set_eg_attack( float attack );                // [0,1.0]
00111     void set_eg_decay( float decay  );                 // [0,1.0]
00112     void set_eg_sustain( float sustain );              // [0,1.0]
00113     void set_eg_release ( float release );             // [0,1.0]
00114 private:
00115     VFO *vfo;
00116     DCBlocker *dc_blocker;
00117     SVFilter *sv_filter;
00118     EG *eg;
00119     amakusa::LimitterLinAtan *limitter;
00120     float *work_buf_a, *work_buf_b;
00121     int32_t block_size;
00122 };
00123 
00124 
00125     // User Signal processing Class 
00126 class SignalProcessing {
00127 public:
00128         // essential members. Do not touch
00129     SignalProcessing( unsigned int  block_size );
00130     void run(           
00131         float rx_left_buffer[],     // array of the left input samples
00132         float rx_right_buffer[],    // array of the right input samples
00133         float tx_left_buffer[],     // place to write the left output samples
00134         float tx_right_buffer[],    // place to write the right output samples
00135         unsigned int block_size     // block size [sample]
00136         );
00137            
00138         // project depenedent members.
00139     void set_volume( float vol );
00140     void set_Fs( int Fs );                      // unit is Hz.
00141     void set_vfo_frequency( float freq );         // unit is Hz.
00142     void set_vfo_duty_cycle( float duty );      // 0 ... 1.0
00143     void set_vfo_wave_form( wave_form form );
00144     void set_filter_mode( svf_mode mode );
00145     void set_filter_Q( float Q );               // 0.0..1.0
00146     void set_filter_f_factor( float f_factor ); // 0.0..1.0
00147     void eg_on(void);
00148     void eg_off(void);
00149     void set_eg_attack( float attack );                // [0,1.0]
00150     void set_eg_decay( float decay );                  // [0,1.0]
00151     void set_eg_sustain( float sustain );              // [0,1.0]
00152     void set_eg_release ( float release );             // [0,1.0]
00153 private:
00154         // essential members. Do not touch.
00155     void enter_critical_section(void);
00156     void leave_critical_section(void);
00157 
00158         // project dependent members.
00159     float volume_level;     // 0 ... 1.0
00160     Monophonic * note;
00161 };
00162 
00163 #endif