Skip to content

Commit

Permalink
Bitinvader - Fix saving with automation and division by 0 (LMMS#5805)
Browse files Browse the repository at this point in the history
* Prevent division by 0 in bitInvader::normalize().
* Save and load whole wavetable on save/load and also clear wavetable
before loading a new one.

Co-authored-by: Dominic Clark <mrdomclark@gmail.com>
Co-authored-by: Spekular <Spekularr@gmail.com>
Co-authored-by: thmueller64 <64359888+thmueller64@users.noreply.github.com>
  • Loading branch information
4 people authored Nov 27, 2020
1 parent fd934a9 commit 3607ba6
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions plugins/bit_invader/bit_invader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

#include "plugin_export.h"

static const int wavetableSize = 200;

extern "C"
{

Expand Down Expand Up @@ -72,8 +74,8 @@ bSynth::bSynth( float * _shape, NotePlayHandle * _nph, bool _interpolation,
sample_rate( _sample_rate ),
interpolation( _interpolation)
{
sample_shape = new float[200];
for (int i=0; i < 200; ++i)
sample_shape = new float[wavetableSize];
for (int i=0; i < wavetableSize; ++i)
{
sample_shape[i] = _shape[i] * _factor;
}
Expand Down Expand Up @@ -138,12 +140,12 @@ sample_t bSynth::nextStringSample( float sample_length )

bitInvader::bitInvader( InstrumentTrack * _instrument_track ) :
Instrument( _instrument_track, &bitinvader_plugin_descriptor ),
m_sampleLength( 128, 4, 200, 1, this, tr( "Sample length" ) ),
m_graph( -1.0f, 1.0f, 200, this ),
m_sampleLength(128, 4, wavetableSize, 1, this, tr("Sample length")),
m_graph(-1.0f, 1.0f, wavetableSize, this),
m_interpolation( false, this ),
m_normalize( false, this )
{

lengthChanged();

m_graph.setWaveToSine();
Expand Down Expand Up @@ -177,8 +179,8 @@ void bitInvader::saveSettings( QDomDocument & _doc, QDomElement & _this )

// Save sample shape base64-encoded
QString sampleString;
base64::encode( (const char *)m_graph.samples(),
m_graph.length() * sizeof(float), sampleString );
base64::encode((const char *)m_graph.samples(),
wavetableSize * sizeof(float), sampleString);
_this.setAttribute( "sampleShape", sampleString );


Expand All @@ -194,6 +196,9 @@ void bitInvader::saveSettings( QDomDocument & _doc, QDomElement & _this )

void bitInvader::loadSettings( const QDomElement & _this )
{
// Clear wavetable before loading a new
m_graph.clear();

// Load sample length
m_sampleLength.loadSettings( _this, "sampleLength" );

Expand All @@ -204,8 +209,9 @@ void bitInvader::loadSettings( const QDomElement & _this )
char * dst = 0;
base64::decode( _this.attribute( "sampleShape"), &dst, &size );

m_graph.setLength( sampleLength );
m_graph.setSamples( (float*) dst );
m_graph.setLength(size / sizeof(float));
m_graph.setSamples(reinterpret_cast<float*>(dst));
m_graph.setLength(sampleLength);
delete[] dst;

// Load LED normalize
Expand Down Expand Up @@ -240,7 +246,7 @@ void bitInvader::samplesChanged( int _begin, int _end )
void bitInvader::normalize()
{
// analyze
float max = 0;
float max = std::numeric_limits<float>::epsilon();
const float* samples = m_graph.samples();
for(int i=0; i < m_graph.length(); i++)
{
Expand Down

0 comments on commit 3607ba6

Please sign in to comment.