/* * Oscillator.cpp - implementation of powerful oscillator-class * * Copyright (c) 2004-2009 Tobias Doerffel * * This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program (see COPYING); if not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA. * */ #include "Oscillator.h" #include "engine.h" #include "mixer.h" #include "AutomatableModel.h" Oscillator::Oscillator( const IntModel * _wave_shape_model, const IntModel * _mod_algo_model, const float & _freq, const float & _detuning, const float & _phase_offset, const float & _volume, Oscillator * _sub_osc ) : m_waveShapeModel( _wave_shape_model ), m_modulationAlgoModel( _mod_algo_model ), m_freq( _freq ), m_detuning( _detuning ), m_volume( _volume ), m_ext_phaseOffset( _phase_offset ), m_subOsc( _sub_osc ), m_phaseOffset( _phase_offset ), m_phase( _phase_offset ), m_userWave( NULL ) { } void Oscillator::update( sampleFrame * _ab, const fpp_t _frames, const ch_cnt_t _chnl ) { if( m_freq >= engine::getMixer()->processingSampleRate() / 2 ) { mixer::clearAudioBuffer( _ab, _frames ); return; } if( m_subOsc != NULL ) { switch( m_modulationAlgoModel->value() ) { case PhaseModulation: updatePM( _ab, _frames, _chnl ); break; case AmplitudeModulation: updateAM( _ab, _frames, _chnl ); break; case SignalMix: updateMix( _ab, _frames, _chnl ); break; case SynchronizedBySubOsc: updateSync( _ab, _frames, _chnl ); break; case FrequencyModulation: updateFM( _ab, _frames, _chnl ); } } else { updateNoSub( _ab, _frames, _chnl ); } } void Oscillator::updateNoSub( sampleFrame * _ab, const fpp_t _frames, const ch_cnt_t _chnl ) { switch( m_waveShapeModel->value() ) { case SineWave: default: updateNoSub( _ab, _frames, _chnl ); break; case TriangleWave: updateNoSub( _ab, _frames, _chnl ); break; case SawWave: updateNoSub( _ab, _frames, _chnl ); break; case SquareWave: updateNoSub( _ab, _frames, _chnl ); break; case MoogSawWave: updateNoSub( _ab, _frames, _chnl ); break; case ExponentialWave: updateNoSub( _ab, _frames, _chnl ); break; case WhiteNoise: updateNoSub( _ab, _frames, _chnl ); break; case UserDefinedWave: updateNoSub( _ab, _frames, _chnl ); break; } } void Oscillator::updatePM( sampleFrame * _ab, const fpp_t _frames, const ch_cnt_t _chnl ) { switch( m_waveShapeModel->value() ) { case SineWave: default: updatePM( _ab, _frames, _chnl ); break; case TriangleWave: updatePM( _ab, _frames, _chnl ); break; case SawWave: updatePM( _ab, _frames, _chnl ); break; case SquareWave: updatePM( _ab, _frames, _chnl ); break; case MoogSawWave: updatePM( _ab, _frames, _chnl ); break; case ExponentialWave: updatePM( _ab, _frames, _chnl ); break; case WhiteNoise: updatePM( _ab, _frames, _chnl ); break; case UserDefinedWave: updatePM( _ab, _frames, _chnl ); break; } } void Oscillator::updateAM( sampleFrame * _ab, const fpp_t _frames, const ch_cnt_t _chnl ) { switch( m_waveShapeModel->value() ) { case SineWave: default: updateAM( _ab, _frames, _chnl ); break; case TriangleWave: updateAM( _ab, _frames, _chnl ); break; case SawWave: updateAM( _ab, _frames, _chnl ); break; case SquareWave: updateAM( _ab, _frames, _chnl ); break; case MoogSawWave: updateAM( _ab, _frames, _chnl ); break; case ExponentialWave: updateAM( _ab, _frames, _chnl ); break; case WhiteNoise: updateAM( _ab, _frames, _chnl ); break; case UserDefinedWave: updateAM( _ab, _frames, _chnl ); break; } } void Oscillator::updateMix( sampleFrame * _ab, const fpp_t _frames, const ch_cnt_t _chnl ) { switch( m_waveShapeModel->value() ) { case SineWave: default: updateMix( _ab, _frames, _chnl ); break; case TriangleWave: updateMix( _ab, _frames, _chnl ); break; case SawWave: updateMix( _ab, _frames, _chnl ); break; case SquareWave: updateMix( _ab, _frames, _chnl ); break; case MoogSawWave: updateMix( _ab, _frames, _chnl ); break; case ExponentialWave: updateMix( _ab, _frames, _chnl ); break; case WhiteNoise: updateMix( _ab, _frames, _chnl ); break; case UserDefinedWave: updateMix( _ab, _frames, _chnl ); break; } } void Oscillator::updateSync( sampleFrame * _ab, const fpp_t _frames, const ch_cnt_t _chnl ) { switch( m_waveShapeModel->value() ) { case SineWave: default: updateSync( _ab, _frames, _chnl ); break; case TriangleWave: updateSync( _ab, _frames, _chnl ); break; case SawWave: updateSync( _ab, _frames, _chnl ); break; case SquareWave: updateSync( _ab, _frames, _chnl ); break; case MoogSawWave: updateSync( _ab, _frames, _chnl ); break; case ExponentialWave: updateSync( _ab, _frames, _chnl ); break; case WhiteNoise: updateSync( _ab, _frames, _chnl ); break; case UserDefinedWave: updateSync( _ab, _frames, _chnl ); break; } } void Oscillator::updateFM( sampleFrame * _ab, const fpp_t _frames, const ch_cnt_t _chnl ) { switch( m_waveShapeModel->value() ) { case SineWave: default: updateFM( _ab, _frames, _chnl ); break; case TriangleWave: updateFM( _ab, _frames, _chnl ); break; case SawWave: updateFM( _ab, _frames, _chnl ); break; case SquareWave: updateFM( _ab, _frames, _chnl ); break; case MoogSawWave: updateFM( _ab, _frames, _chnl ); break; case ExponentialWave: updateFM( _ab, _frames, _chnl ); break; case WhiteNoise: updateFM( _ab, _frames, _chnl ); break; case UserDefinedWave: updateFM( _ab, _frames, _chnl ); break; } } // should be called every time phase-offset is changed... inline void Oscillator::recalcPhase() { if( !typeInfo::isEqual( m_phaseOffset, m_ext_phaseOffset ) ) { m_phase -= m_phaseOffset; m_phaseOffset = m_ext_phaseOffset; m_phase += m_phaseOffset; } m_phase = absFraction( m_phase )+2; // make sure we're not running // negative when doing PM } inline bool Oscillator::syncOk( float _osc_coeff ) { const float v1 = m_phase; m_phase += _osc_coeff; // check whether m_phase is in next period return( floorf( m_phase ) > floorf( v1 ) ); } float Oscillator::syncInit( sampleFrame * _ab, const fpp_t _frames, const ch_cnt_t _chnl ) { if( m_subOsc != NULL ) { m_subOsc->update( _ab, _frames, _chnl ); } recalcPhase(); return( m_freq * m_detuning ); } // if we have no sub-osc, we can't do any modulation... just get our samples template void Oscillator::updateNoSub( sampleFrame * _ab, const fpp_t _frames, const ch_cnt_t _chnl ) { recalcPhase(); const float osc_coeff = m_freq * m_detuning; for( fpp_t frame = 0; frame < _frames; ++frame ) { _ab[frame][_chnl] = getSample( m_phase ) * m_volume; m_phase += osc_coeff; } } // do pm by using sub-osc as modulator template void Oscillator::updatePM( sampleFrame * _ab, const fpp_t _frames, const ch_cnt_t _chnl ) { m_subOsc->update( _ab, _frames, _chnl ); recalcPhase(); const float osc_coeff = m_freq * m_detuning; for( fpp_t frame = 0; frame < _frames; ++frame ) { _ab[frame][_chnl] = getSample( m_phase + _ab[frame][_chnl] ) * m_volume; m_phase += osc_coeff; } } // do am by using sub-osc as modulator template void Oscillator::updateAM( sampleFrame * _ab, const fpp_t _frames, const ch_cnt_t _chnl ) { m_subOsc->update( _ab, _frames, _chnl ); recalcPhase(); const float osc_coeff = m_freq * m_detuning; for( fpp_t frame = 0; frame < _frames; ++frame ) { _ab[frame][_chnl] *= getSample( m_phase ) * m_volume; m_phase += osc_coeff; } } // do mix by using sub-osc as mix-sample template void Oscillator::updateMix( sampleFrame * _ab, const fpp_t _frames, const ch_cnt_t _chnl ) { m_subOsc->update( _ab, _frames, _chnl ); recalcPhase(); const float osc_coeff = m_freq * m_detuning; for( fpp_t frame = 0; frame < _frames; ++frame ) { _ab[frame][_chnl] += getSample( m_phase ) * m_volume; m_phase += osc_coeff; } } // sync with sub-osc (every time sub-osc starts new period, we also start new // period) template void Oscillator::updateSync( sampleFrame * _ab, const fpp_t _frames, const ch_cnt_t _chnl ) { const float sub_osc_coeff = m_subOsc->syncInit( _ab, _frames, _chnl ); recalcPhase(); const float osc_coeff = m_freq * m_detuning; for( fpp_t frame = 0; frame < _frames ; ++frame ) { if( m_subOsc->syncOk( sub_osc_coeff ) ) { m_phase = m_phaseOffset; } _ab[frame][_chnl] = getSample( m_phase ) * m_volume; m_phase += osc_coeff; } } // do fm by using sub-osc as modulator template void Oscillator::updateFM( sampleFrame * _ab, const fpp_t _frames, const ch_cnt_t _chnl ) { m_subOsc->update( _ab, _frames, _chnl ); recalcPhase(); const float osc_coeff = m_freq * m_detuning; const float sampleRateCorrection = 44100.0f / engine::getMixer()->processingSampleRate(); for( fpp_t frame = 0; frame < _frames; ++frame ) { m_phase += _ab[frame][_chnl] * sampleRateCorrection; _ab[frame][_chnl] = getSample( m_phase ) * m_volume; m_phase += osc_coeff; } } template<> inline sample_t Oscillator::getSample( const float _sample ) { return( sinSample( _sample ) ); } template<> inline sample_t Oscillator::getSample( const float _sample ) { return( triangleSample( _sample ) ); } template<> inline sample_t Oscillator::getSample( const float _sample ) { return( sawSample( _sample ) ); } template<> inline sample_t Oscillator::getSample( const float _sample ) { return( squareSample( _sample ) ); } template<> inline sample_t Oscillator::getSample( const float _sample ) { return( moogSawSample( _sample ) ); } template<> inline sample_t Oscillator::getSample( const float _sample ) { return( expSample( _sample ) ); } template<> inline sample_t Oscillator::getSample( const float _sample ) { return( noiseSample( _sample ) ); } template<> inline sample_t Oscillator::getSample( const float _sample ) { return( userWaveSample( _sample ) ); }