Files
lmms/plugins/BitInvader/BitInvader.cpp
saker b2f2fc4ad1 Revisit the initialization for local variables (#7143)
* clang-tidy: Apply cppcoreguidelines-init-variables everywhere (treating NaNs as zeros)

* Initialize msec and tick outside switch

* Update plugins/Vestige/Vestige.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update plugins/Vestige/Vestige.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update plugins/Vestige/Vestige.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update plugins/VstEffect/VstEffectControls.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update plugins/VstEffect/VstEffectControls.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update plugins/VstEffect/VstEffectControls.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Use initialization with =

* Use tabs

* Use static_cast

* Update DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Do not use tabs for alignment in src/core/DrumSynth.cpp

Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com>

* Move x variable inside loop

* Use ternary operator for b variable

* Revert "Use tabs"

This reverts commit 07afd8a83f58b539c3673310b2aad4b63c9198a0.

* Remove unnecessary variables in XpressiveView

* Simplify initialization in Plugin

* Combine declaration and initialization in EqCurve

* Combine declaration and initialization in Song

* Combine declaration and initialization in AudioAlsa

* Combine declaration and initialization in EqCurve (again)

* Missed some

* Undo changes made to non-LMMS files

* Undo indentation changes in SidInstrument.cpp

* Combine declaration with assignment in IoHelper

* Combine declaration with assignment using auto in Carla

* Combine declaration with assignment

* Combine declaration with assignment in BasicFilters

* Simplify assignments in AudioFileProcessorWaveView::zoom

* Simplify out sample variable in BitInvader

* Remove sampleLength variable in DelayEffect

* Move gain variable in DynamicsProcessor

* Combine peak variable declaration with assignment in EqSpectrumView

* Move left/right lfo variables in for loop in FlangerEffect

* Use ternary operator for group variable in LadspaControlDialog

* Combine declaration with assignment in Lb302

* Combine declaration with assignment in MidiExport

* Combine declaration with assignment in MidiFile

* Combine declaration with assignment in MidiImport

* Use ternary operator for vel_adjusted variable in OpulenZ

* Move tmpL and dcblkL variables in for loop in ReverbSC

* Combine declaration with initialization in SlicerT

* Combine declaration with assignment in SaSpectrumView

* Combine declaration with assignment in SaWaterfallView

* Combine declaration with assignment in StereoEnhancerEffect

* Combine declaration with assignment in VibratingString

* Combine declaration with assignment in VstEffectControls

* Combine declaration with assignment in Xpressive

* Combine declaration with assignment in AutomatableModel

* Combine declaration with assignment in AutomationClip

* Move sample variable in for loop in BandLimitedWave

* Combine declaration with assignment in DataFile

* Combine declaration with assignment in DrumSynth

* Combine declaration with assignment in Effect

* Remove redundant assignment to nphsLeft in InstrumentPlayHandle

* Combine declaration with assignment in LadspaManager

* Combine declaration with assignment in LinkedModelGroups

* Combine declaration with assignment in MemoryHelper

* Combine declaration with assignment in AudioAlsa

* Combine declaration with assignment in AudioFileOgg

* Combine declaration with assignment in AudioPortAudio

* Combine declaration with assignment in AudioSoundIo

* Combine declaration with assignment in Lv2Evbuf

* Combine declaration with assignment in Lv2Proc

* Combine declaration with assignment in main

* Combine declaration with assignment in MidiAlsaRaw

* Combine declaration with assignment in MidiAlsaSeq

* Combine declaration with assignment in MidiController

* Combine declaration with assignment in MidiJack

* Combine declaration with assignment in MidiSndio

* Combine declaration with assignment in ControlLayout

* Combine declaration with assignment in MainWindow

* Combine declaration with assignment in ProjectNotes

* Use ternary operator for nextValue variable in AutomationClipView

* Combine declaration with assignment in AutomationEditor

* Move length variable in for-loop in PianoRoll

* Combine declaration with assignment in ControllerConnectionDialog

* Combine declaration with assignment in Graph

* Combine declaration with assignment in LcdFloatSpinBox

* Combine declaration with assignment in TimeDisplayWidget

* Remove currentNote variable in InstrumentTrack

* Combine declaration with assignment in DrumSynth (again)

* Use ternary operator for factor variable in BitInvader

* Use ternary operator for highestBandwich variable in EqCurve

Bandwich?

* Move sum variable into for loop in Graph

* Fix format in MidiSndio

* Fixup a few more

* Cleanup error variables

* Use ternary operators and combine declaration with initialization

* Combine declaration with initialization

* Update plugins/LadspaEffect/LadspaControlDialog.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update plugins/OpulenZ/OpulenZ.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update plugins/SpectrumAnalyzer/SaProcessor.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/midi/MidiAlsaRaw.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/gui/MainWindow.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/gui/clips/AutomationClipView.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/gui/editors/AutomationEditor.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/gui/widgets/Fader.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Move static_cast conversion into separate variable

* Use real index when interpolating

* Remove empty line

* Make helpBtn a private member

* Move controller type into separate variable

* Fix format of DrumSynth::waveform function

* Use tabs and static_cast

* Remove redundant if branch

* Refactor using static_cast/reinterpret_cast

* Add std namespace prefix

* Store repeated conditional into boolean variable

* Cast to int before assigning to m_currentLength

* Rename note_frames to noteFrames

* Update src/core/Controller.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/core/DrumSynth.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Update src/gui/widgets/Graph.cpp

Co-authored-by: Kevin Zander <veratil@gmail.com>

* Revert changes that initialized variables redudantly

For situations where the initialization is
more complex or passed into a function
by a pointer, we dont need to do
initialization ourselves since it is
already done for us, just in a different way.

* Remove redundant err variable

* Remove explicit check of err variable

* Clean up changes and address review

* Do not initialize to 0/nullptr when not needed

* Wrap condition in parentheses for readability

---------

Co-authored-by: Kevin Zander <veratil@gmail.com>
Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com>
2024-03-28 17:21:31 -04:00

561 lines
13 KiB
C++

/*
* BitInvader.cpp - instrument which uses a usereditable wavetable
*
* Copyright (c) 2006-2008 Andreas Brandmaier <andy/at/brandmaier/dot/de>
*
* This file is part of LMMS - https://lmms.io
*
* 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 <QDomElement>
#include "BitInvader.h"
#include "AudioEngine.h"
#include "base64.h"
#include "Engine.h"
#include "Graph.h"
#include "InstrumentTrack.h"
#include "Knob.h"
#include "LedCheckBox.h"
#include "NotePlayHandle.h"
#include "PixmapButton.h"
#include "Song.h"
#include "interpolation.h"
#include "embed.h"
#include "plugin_export.h"
namespace lmms
{
static const int wavetableSize = 200;
static const float defaultNormalizationFactor = 1.0f;
extern "C"
{
Plugin::Descriptor PLUGIN_EXPORT bitinvader_plugin_descriptor =
{
LMMS_STRINGIFY( PLUGIN_NAME ),
"BitInvader",
QT_TRANSLATE_NOOP( "PluginBrowser",
"Customizable wavetable synthesizer" ),
"Andreas Brandmaier <andreas/at/brandmaier/dot/de>",
0x0100,
Plugin::Type::Instrument,
new PluginPixmapLoader( "logo" ),
nullptr,
nullptr,
} ;
}
BSynth::BSynth( float * _shape, NotePlayHandle * _nph, bool _interpolation,
float _factor, const sample_rate_t _sample_rate ) :
sample_index( 0 ),
sample_realindex( 0 ),
nph( _nph ),
sample_rate( _sample_rate ),
interpolation( _interpolation)
{
sample_shape = new float[wavetableSize];
for (int i=0; i < wavetableSize; ++i)
{
float buf = _shape[i] * _factor;
/* Double check that normalization has been performed correctly,
i.e., the absolute value of all samples is <= 1.0 if _factor
is different to the default normalization factor. If there is
a value > 1.0, clip the sample to 1.0 to limit the range. */
if ((_factor != defaultNormalizationFactor) && (fabsf(buf) > 1.0f))
{
buf = (buf < 0) ? -1.0f : 1.0f;
}
sample_shape[i] = buf;
}
}
BSynth::~BSynth()
{
delete[] sample_shape;
}
sample_t BSynth::nextStringSample( float sample_length )
{
auto sample_step = static_cast<float>(sample_length / (sample_rate / nph->frequency()));
// check overflow
while (sample_realindex >= sample_length) {
sample_realindex -= sample_length;
}
const auto currentRealIndex = sample_realindex;
const auto currentIndex = static_cast<int>(sample_realindex);
sample_realindex += sample_step;
if (!interpolation)
{
sample_index = currentIndex;
return sample_shape[sample_index];
}
const auto nextIndex = currentIndex < sample_length - 1 ? currentIndex + 1 : 0;
return linearInterpolate(sample_shape[currentIndex], sample_shape[nextIndex], fraction(currentRealIndex));
}
/***********************************************************************
*
* class BitInvader
*
* lmms - plugin
*
***********************************************************************/
BitInvader::BitInvader( InstrumentTrack * _instrument_track ) :
Instrument( _instrument_track, &bitinvader_plugin_descriptor ),
m_sampleLength(wavetableSize, 4, wavetableSize, 1, this, tr("Sample length")),
m_graph(-1.0f, 1.0f, wavetableSize, this),
m_interpolation(false, this, tr("Interpolation")),
m_normalize(false, this, tr("Normalize"))
{
m_graph.setWaveToSine();
lengthChanged();
connect( &m_sampleLength, SIGNAL( dataChanged() ),
this, SLOT( lengthChanged() ), Qt::DirectConnection );
connect( &m_graph, SIGNAL( samplesChanged( int, int ) ),
this, SLOT( samplesChanged( int, int ) ) );
}
void BitInvader::saveSettings( QDomDocument & _doc, QDomElement & _this )
{
// Save plugin version
_this.setAttribute( "version", "0.1" );
// Save sample length
m_sampleLength.saveSettings( _doc, _this, "sampleLength" );
// Save sample shape base64-encoded
QString sampleString;
base64::encode((const char *)m_graph.samples(),
wavetableSize * sizeof(float), sampleString);
_this.setAttribute( "sampleShape", sampleString );
// save LED normalize
m_interpolation.saveSettings( _doc, _this, "interpolation" );
// save LED
m_normalize.saveSettings( _doc, _this, "normalize" );
}
void BitInvader::loadSettings( const QDomElement & _this )
{
// Clear wavetable before loading a new
m_graph.clear();
// Load sample length
m_sampleLength.loadSettings( _this, "sampleLength" );
int sampleLength = (int)m_sampleLength.value();
// Load sample shape
int size = 0;
char * dst = 0;
base64::decode( _this.attribute( "sampleShape"), &dst, &size );
m_graph.setLength(size / sizeof(float));
m_graph.setSamples(reinterpret_cast<float*>(dst));
m_graph.setLength(sampleLength);
delete[] dst;
// Load LED normalize
m_interpolation.loadSettings( _this, "interpolation" );
// Load LED
m_normalize.loadSettings( _this, "normalize" );
}
void BitInvader::lengthChanged()
{
m_graph.setLength( (int) m_sampleLength.value() );
normalize();
}
void BitInvader::samplesChanged( int _begin, int _end )
{
normalize();
//engine::getSongEditor()->setModified();
}
void BitInvader::normalize()
{
// analyze
float max = std::numeric_limits<float>::epsilon();
const float* samples = m_graph.samples();
for(int i=0; i < m_graph.length(); i++)
{
const float f = fabsf( samples[i] );
if (f > max) { max = f; }
}
m_normalizeFactor = 1.0 / max;
}
QString BitInvader::nodeName() const
{
return( bitinvader_plugin_descriptor.name );
}
void BitInvader::playNote( NotePlayHandle * _n,
sampleFrame * _working_buffer )
{
if (!_n->m_pluginData)
{
float factor = !m_normalize.value() ? defaultNormalizationFactor : m_normalizeFactor;
_n->m_pluginData = new BSynth(
const_cast<float*>( m_graph.samples() ),
_n,
m_interpolation.value(), factor,
Engine::audioEngine()->processingSampleRate() );
}
const fpp_t frames = _n->framesLeftForCurrentPeriod();
const f_cnt_t offset = _n->noteOffset();
auto ps = static_cast<BSynth*>(_n->m_pluginData);
for( fpp_t frame = offset; frame < frames + offset; ++frame )
{
const sample_t cur = ps->nextStringSample( m_graph.length() );
for( ch_cnt_t chnl = 0; chnl < DEFAULT_CHANNELS; ++chnl )
{
_working_buffer[frame][chnl] = cur;
}
}
applyRelease( _working_buffer, _n );
}
void BitInvader::deleteNotePluginData( NotePlayHandle * _n )
{
delete static_cast<BSynth *>( _n->m_pluginData );
}
gui::PluginView * BitInvader::instantiateView( QWidget * _parent )
{
return( new gui::BitInvaderView( this, _parent ) );
}
namespace gui
{
BitInvaderView::BitInvaderView( Instrument * _instrument,
QWidget * _parent ) :
InstrumentViewFixedSize( _instrument, _parent )
{
setAutoFillBackground( true );
QPalette pal;
pal.setBrush( backgroundRole(), PLUGIN_NAME::getIconPixmap(
"artwork" ) );
setPalette( pal );
m_sampleLengthKnob = new Knob( KnobType::Dark28, this );
m_sampleLengthKnob->move( 6, 201 );
m_sampleLengthKnob->setHintText( tr( "Sample length" ), "" );
m_graph = new Graph( this, Graph::Style::Nearest, 204, 134 );
m_graph->move(23,59); // 55,120 - 2px border
m_graph->setAutoFillBackground( true );
m_graph->setGraphColor( QColor( 255, 255, 255 ) );
m_graph->setToolTip(tr("Draw your own waveform here "
"by dragging your mouse on this graph."
));
pal = QPalette();
pal.setBrush( backgroundRole(),
PLUGIN_NAME::getIconPixmap("wavegraph") );
m_graph->setPalette( pal );
m_sinWaveBtn = new PixmapButton( this, tr( "Sine wave" ) );
m_sinWaveBtn->move( 131, 205 );
m_sinWaveBtn->setActiveGraphic( embed::getIconPixmap(
"sin_wave_active" ) );
m_sinWaveBtn->setInactiveGraphic( embed::getIconPixmap(
"sin_wave_inactive" ) );
m_sinWaveBtn->setToolTip(
tr( "Sine wave" ) );
m_triangleWaveBtn = new PixmapButton( this, tr( "Triangle wave" ) );
m_triangleWaveBtn->move( 131 + 14, 205 );
m_triangleWaveBtn->setActiveGraphic(
embed::getIconPixmap( "triangle_wave_active" ) );
m_triangleWaveBtn->setInactiveGraphic(
embed::getIconPixmap( "triangle_wave_inactive" ) );
m_triangleWaveBtn->setToolTip(
tr( "Triangle wave" ) );
m_sawWaveBtn = new PixmapButton( this, tr( "Saw wave" ) );
m_sawWaveBtn->move( 131 + 14*2, 205 );
m_sawWaveBtn->setActiveGraphic( embed::getIconPixmap(
"saw_wave_active" ) );
m_sawWaveBtn->setInactiveGraphic( embed::getIconPixmap(
"saw_wave_inactive" ) );
m_sawWaveBtn->setToolTip(
tr( "Saw wave" ) );
m_sqrWaveBtn = new PixmapButton( this, tr( "Square wave" ) );
m_sqrWaveBtn->move( 131 + 14*3, 205 );
m_sqrWaveBtn->setActiveGraphic( embed::getIconPixmap(
"square_wave_active" ) );
m_sqrWaveBtn->setInactiveGraphic( embed::getIconPixmap(
"square_wave_inactive" ) );
m_sqrWaveBtn->setToolTip(
tr( "Square wave" ) );
m_whiteNoiseWaveBtn = new PixmapButton( this,
tr( "White noise" ) );
m_whiteNoiseWaveBtn->move( 131 + 14*4, 205 );
m_whiteNoiseWaveBtn->setActiveGraphic(
embed::getIconPixmap( "white_noise_wave_active" ) );
m_whiteNoiseWaveBtn->setInactiveGraphic(
embed::getIconPixmap( "white_noise_wave_inactive" ) );
m_whiteNoiseWaveBtn->setToolTip(
tr( "White noise" ) );
m_usrWaveBtn = new PixmapButton( this, tr( "User-defined wave" ) );
m_usrWaveBtn->move( 131 + 14*5, 205 );
m_usrWaveBtn->setActiveGraphic( embed::getIconPixmap(
"usr_wave_active" ) );
m_usrWaveBtn->setInactiveGraphic( embed::getIconPixmap(
"usr_wave_inactive" ) );
m_usrWaveBtn->setToolTip(
tr( "User-defined wave" ) );
m_smoothBtn = new PixmapButton( this, tr( "Smooth waveform" ) );
m_smoothBtn->move( 131 + 14*6, 205 );
m_smoothBtn->setActiveGraphic( PLUGIN_NAME::getIconPixmap(
"smooth_active" ) );
m_smoothBtn->setInactiveGraphic( PLUGIN_NAME::getIconPixmap(
"smooth_inactive" ) );
m_smoothBtn->setToolTip(
tr( "Smooth waveform" ) );
m_interpolationToggle = new LedCheckBox( "Interpolation", this,
tr( "Interpolation" ), LedCheckBox::LedColor::Yellow );
m_interpolationToggle->move( 131, 221 );
m_normalizeToggle = new LedCheckBox( "Normalize", this,
tr( "Normalize" ), LedCheckBox::LedColor::Green );
m_normalizeToggle->move( 131, 236 );
connect( m_sinWaveBtn, SIGNAL (clicked () ),
this, SLOT ( sinWaveClicked() ) );
connect( m_triangleWaveBtn, SIGNAL ( clicked () ),
this, SLOT ( triangleWaveClicked() ) );
connect( m_sawWaveBtn, SIGNAL (clicked () ),
this, SLOT ( sawWaveClicked() ) );
connect( m_sqrWaveBtn, SIGNAL ( clicked () ),
this, SLOT ( sqrWaveClicked() ) );
connect( m_whiteNoiseWaveBtn, SIGNAL ( clicked () ),
this, SLOT ( noiseWaveClicked() ) );
connect( m_usrWaveBtn, SIGNAL ( clicked () ),
this, SLOT ( usrWaveClicked() ) );
connect( m_smoothBtn, SIGNAL ( clicked () ),
this, SLOT ( smoothClicked() ) );
connect( m_interpolationToggle, SIGNAL( toggled( bool ) ),
this, SLOT ( interpolationToggled( bool ) ) );
connect( m_normalizeToggle, SIGNAL( toggled( bool ) ),
this, SLOT ( normalizeToggled( bool ) ) );
}
void BitInvaderView::modelChanged()
{
auto b = castModel<BitInvader>();
m_graph->setModel( &b->m_graph );
m_sampleLengthKnob->setModel( &b->m_sampleLength );
m_interpolationToggle->setModel( &b->m_interpolation );
m_normalizeToggle->setModel( &b->m_normalize );
}
void BitInvaderView::sinWaveClicked()
{
m_graph->model()->clearInvisible();
m_graph->model()->setWaveToSine();
Engine::getSong()->setModified();
}
void BitInvaderView::triangleWaveClicked()
{
m_graph->model()->clearInvisible();
m_graph->model()->setWaveToTriangle();
Engine::getSong()->setModified();
}
void BitInvaderView::sawWaveClicked()
{
m_graph->model()->clearInvisible();
m_graph->model()->setWaveToSaw();
Engine::getSong()->setModified();
}
void BitInvaderView::sqrWaveClicked()
{
m_graph->model()->clearInvisible();
m_graph->model()->setWaveToSquare();
Engine::getSong()->setModified();
}
void BitInvaderView::noiseWaveClicked()
{
m_graph->model()->clearInvisible();
m_graph->model()->setWaveToNoise();
Engine::getSong()->setModified();
}
void BitInvaderView::usrWaveClicked()
{
QString fileName = m_graph->model()->setWaveToUser();
if (!fileName.isEmpty())
{
m_usrWaveBtn->setToolTip(fileName);
m_graph->model()->clearInvisible();
Engine::getSong()->setModified();
}
}
void BitInvaderView::smoothClicked()
{
m_graph->model()->smooth();
Engine::getSong()->setModified();
}
void BitInvaderView::interpolationToggled( bool value )
{
m_graph->setGraphStyle( value ? Graph::Style::Linear : Graph::Style::Nearest);
Engine::getSong()->setModified();
}
void BitInvaderView::normalizeToggled( bool value )
{
Engine::getSong()->setModified();
}
} // namespace gui
extern "C"
{
// necessary for getting instance out of shared lib
PLUGIN_EXPORT Plugin * lmms_plugin_main( Model *m, void * )
{
return( new BitInvader( static_cast<InstrumentTrack *>( m ) ) );
}
}
} // namespace lmms