Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SampleBuffer -> Samplerate - Fix two sample rate issues: #4991

Merged
merged 3 commits into from
Jun 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@


#include "export.h"
#include "lmms_basics.h"

class BBTrackContainer;
class DummyTrackContainer;
Expand Down Expand Up @@ -100,6 +101,9 @@ class EXPORT LmmsCore : public QObject
{
return s_framesPerTick;
}

static float framesPerTick(sample_rate_t sample_rate);

static void updateFramesPerTick();

static inline LmmsCore * inst()
Expand Down
2 changes: 2 additions & 0 deletions include/SampleBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ public slots:
void sampleRateChanged();

private:
static sample_rate_t mixerSampleRate();

void update( bool _keep_settings = false );

void convertIntToFloat ( int_sample_t * & _ibuf, f_cnt_t _frames, int _channels);
Expand Down
6 changes: 6 additions & 0 deletions src/core/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ void LmmsCore::destroy()
delete ConfigManager::inst();
}

float LmmsCore::framesPerTick(sample_rate_t sample_rate)
{
return sample_rate * 60.0f * 4 /
DefaultTicksPerTact / s_song->getTempo();
}




Expand Down
17 changes: 11 additions & 6 deletions src/core/SampleBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ SampleBuffer::SampleBuffer( const QString & _audio_file,
m_amplification( 1.0f ),
m_reversed( false ),
m_frequency( BaseFreq ),
m_sampleRate( Engine::mixer()->baseSampleRate() )
m_sampleRate( mixerSampleRate () )
{
if( _is_base64_data == true )
{
Expand All @@ -99,7 +99,7 @@ SampleBuffer::SampleBuffer( const sampleFrame * _data, const f_cnt_t _frames ) :
m_amplification( 1.0f ),
m_reversed( false ),
m_frequency( BaseFreq ),
m_sampleRate( Engine::mixer()->baseSampleRate() )
m_sampleRate( mixerSampleRate () )
{
if( _frames > 0 )
{
Expand Down Expand Up @@ -127,7 +127,7 @@ SampleBuffer::SampleBuffer( const f_cnt_t _frames ) :
m_amplification( 1.0f ),
m_reversed( false ),
m_frequency( BaseFreq ),
m_sampleRate( Engine::mixer()->baseSampleRate() )
m_sampleRate( mixerSampleRate () )
{
if( _frames > 0 )
{
Expand Down Expand Up @@ -155,6 +155,11 @@ void SampleBuffer::sampleRateChanged()
update( true );
}

sample_rate_t SampleBuffer::mixerSampleRate()
{
return Engine::mixer()->processingSampleRate();
}


void SampleBuffer::update( bool _keep_settings )
{
Expand Down Expand Up @@ -190,7 +195,7 @@ void SampleBuffer::update( bool _keep_settings )
int_sample_t * buf = NULL;
sample_t * fbuf = NULL;
ch_cnt_t channels = DEFAULT_CHANNELS;
sample_rate_t samplerate = Engine::mixer()->baseSampleRate();
sample_rate_t samplerate = mixerSampleRate();
m_frames = 0;

const QFileInfo fileInfo( file );
Expand Down Expand Up @@ -379,10 +384,10 @@ void SampleBuffer::normalizeSampleRate( const sample_rate_t _src_sr,
bool _keep_settings )
{
// do samplerate-conversion to our default-samplerate
if( _src_sr != Engine::mixer()->baseSampleRate() )
if( _src_sr != mixerSampleRate() )
{
SampleBuffer * resampled = resample( _src_sr,
Engine::mixer()->baseSampleRate() );
mixerSampleRate() );
MM_FREE( m_data );
m_frames = resampled->frames();
m_data = MM_ALLOC( sampleFrame, m_frames );
Expand Down
2 changes: 1 addition & 1 deletion src/core/SamplePlayHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ bool SamplePlayHandle::isFromTrack( const Track * _track ) const

f_cnt_t SamplePlayHandle::totalFrames() const
{
return ( m_sampleBuffer->endFrame() - m_sampleBuffer->startFrame() ) * ( Engine::mixer()->processingSampleRate() / Engine::mixer()->baseSampleRate() );
return ( m_sampleBuffer->endFrame() - m_sampleBuffer->startFrame() ) * ( Engine::mixer()->processingSampleRate() / m_sampleBuffer->sampleRate() );
}


Expand Down
15 changes: 12 additions & 3 deletions src/tracks/SampleTrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ void SampleTCO::saveSettings( QDomDocument & _doc, QDomElement & _this )
QString s;
_this.setAttribute( "data", m_sampleBuffer->toBase64( s ) );
}

_this.setAttribute ("sample_rate", m_sampleBuffer->sampleRate());
// TODO: start- and end-frame
}

Expand All @@ -264,6 +266,10 @@ void SampleTCO::loadSettings( const QDomElement & _this )
}
changeLength( _this.attribute( "len" ).toInt() );
setMuted( _this.attribute( "muted" ).toInt() );

if (_this.hasAttribute("sample_rate")) {
m_sampleBuffer->setSampleRate(_this.attribute("sample_rate").toInt());
}
}


Expand Down Expand Up @@ -598,13 +604,16 @@ bool SampleTrack::play( const MidiTime & _start, const fpp_t _frames,
{
TrackContentObject * tco = getTCO( i );
SampleTCO * sTco = dynamic_cast<SampleTCO*>( tco );
float framesPerTick = Engine::framesPerTick();

if( _start >= sTco->startPosition() && _start < sTco->endPosition() )
{
if( sTco->isPlaying() == false )
{
f_cnt_t sampleStart = framesPerTick * ( _start - sTco->startPosition() );
f_cnt_t tcoFrameLength = framesPerTick * ( sTco->endPosition() - sTco->startPosition() );
auto bufferFramesPerTick = Engine::framesPerTick (sTco->sampleBuffer ()->sampleRate ());
f_cnt_t sampleStart = bufferFramesPerTick * ( _start - sTco->startPosition() );

f_cnt_t tcoFrameLength = bufferFramesPerTick * ( sTco->endPosition() - sTco->startPosition() );

f_cnt_t sampleBufferLength = sTco->sampleBuffer()->frames();
//if the Tco smaller than the sample length we play only until Tco end
//else we play the sample to the end but nothing more
Expand Down