Skip to content

Commit

Permalink
Huge structural changes
Browse files Browse the repository at this point in the history
Well, this commit got a bit out of hand, what with 26 files changed. Oh well.

Basically, we're using the buffermanager to dispense temporary buffers for playhandles and audioports to use.
This allows us to change the way playhandles work. Earlier, playhandles of the same track were waiting in line
to push their output to the audioport. This was of course inefficient, so now they just register themselves to the port,
then the port handles mixing the buffers.

Caveat: this is still a work in progress, the vol/pan knobs on instruments are temporarily non-functional - will be fixed in
the next commit, but I have to get some sleep now.
  • Loading branch information
diizy committed Aug 28, 2014
1 parent 38a83b2 commit 722235b
Show file tree
Hide file tree
Showing 26 changed files with 296 additions and 205 deletions.
52 changes: 15 additions & 37 deletions include/AudioPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "Mixer.h"
#include "MemoryManager.h"
#include "PlayHandle.h"

class EffectChain;

Expand All @@ -41,38 +42,21 @@ class AudioPort : public ThreadableJob
AudioPort( const QString & _name, bool _has_effect_chain = true );
virtual ~AudioPort();

inline sampleFrame * firstBuffer()
inline sampleFrame * buffer()
{
return m_firstBuffer;
return m_portBuffer;
}

inline sampleFrame * secondBuffer()
inline void lockBuffer()
{
return m_secondBuffer;
m_portBufferLock.lock();
}

inline void lockFirstBuffer()
inline void unlockBuffer()
{
m_firstBufferLock.lock();
m_portBufferLock.unlock();
}

inline void lockSecondBuffer()
{
m_secondBufferLock.lock();
}

inline void unlockFirstBuffer()
{
m_firstBufferLock.unlock();
}

inline void unlockSecondBuffer()
{
m_secondBufferLock.unlock();
}

void nextPeriod();


// indicate whether JACK & Co should provide output-buffer at ext. port
inline bool extOutputEnabled() const
Expand Down Expand Up @@ -112,28 +96,20 @@ class AudioPort : public ThreadableJob
bool processEffects();

// ThreadableJob stuff
virtual void doProcessing( sampleFrame * );
virtual void doProcessing();
virtual bool requiresProcessing() const
{
return true;
}


enum bufferUsages
{
NoUsage,
FirstBuffer,
BothBuffers
} ;

void addPlayHandle( PlayHandle * handle );
void removePlayHandle( PlayHandle * handle );

private:
volatile bufferUsages m_bufferUsage;
volatile bool m_bufferUsage;

sampleFrame * m_firstBuffer;
sampleFrame * m_secondBuffer;
QMutex m_firstBufferLock;
QMutex m_secondBufferLock;
sampleFrame * m_portBuffer;
QMutex m_portBufferLock;

bool m_extOutputEnabled;
fx_ch_t m_nextFxChannel;
Expand All @@ -142,6 +118,8 @@ class AudioPort : public ThreadableJob

EffectChain * m_effects;

PlayHandleList m_playHandles;
QMutex m_playHandleLock;

friend class Mixer;
friend class MixerWorkerThread;
Expand Down
12 changes: 8 additions & 4 deletions include/BufferManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,25 @@
#include <QtCore/QReadWriteLock>


const int BM_INITIAL_BUFFERS = 256;
const int BM_INCREMENT = 16;
const int BM_INITIAL_BUFFERS = 512;
//const int BM_INCREMENT = 64;

class BufferManager
{
public:
static void init();
static sampleFrame * acquire();
static void release( sampleFrame * buf );
static void extend( int c );
static void refresh();
// static void extend( int c );

private:
static sampleFrame ** s_available;
static QAtomicInt s_availableIndex;
static QReadWriteLock s_mutex;

static sampleFrame ** s_released;
static QAtomicInt s_releasedIndex;
// static QReadWriteLock s_mutex;
static int s_size;
};

Expand Down
2 changes: 1 addition & 1 deletion include/FxMixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class FxChannel : public ThreadableJob
virtual bool requiresProcessing() const { return true; }

private:
virtual void doProcessing( sampleFrame * _working_buffer );
virtual void doProcessing();
};


Expand Down
7 changes: 2 additions & 5 deletions include/InstrumentPlayHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@
class InstrumentPlayHandle : public PlayHandle
{
public:
InstrumentPlayHandle( Instrument* instrument ) :
PlayHandle( TypeInstrumentPlayHandle ),
m_instrument( instrument )
{
}
InstrumentPlayHandle( Instrument * instrument, InstrumentTrack* instrumentTrack );

virtual ~InstrumentPlayHandle()
{
Expand Down Expand Up @@ -88,6 +84,7 @@ class InstrumentPlayHandle : public PlayHandle

private:
Instrument* m_instrument;
InstrumentTrack * m_instrumentTrack;

} ;

Expand Down
5 changes: 0 additions & 5 deletions include/Mixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,6 @@ class EXPORT Mixer : public QObject
}

// audio-buffer-mgm
void bufferToPort( const sampleFrame * _buf,
const fpp_t _frames,
stereoVolumeVector _volume_vector,
AudioPort * _port );

static void clearAudioBuffer( sampleFrame * _ab,
const f_cnt_t _frames,
const f_cnt_t _offset = 0 );
Expand Down
7 changes: 3 additions & 4 deletions include/MixerWorkerThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
*
*/

#ifndef _MIXER_WORKER_THREAD_H
#define _MIXER_WORKER_THREAD_H
#ifndef MIXER_WORKER_THREAD_H
#define MIXER_WORKER_THREAD_H

#include <QtCore/QAtomicPointer>
#include <QtCore/QThread>
Expand Down Expand Up @@ -57,7 +57,7 @@ class MixerWorkerThread : public QThread

void addJob( ThreadableJob * _job );

void run( sampleFrame * _buffer );
void run();
void wait();

private:
Expand Down Expand Up @@ -109,7 +109,6 @@ class MixerWorkerThread : public QThread
static QWaitCondition * queueReadyWaitCond;
static QList<MixerWorkerThread *> workerThreads;

sampleFrame * m_workingBuf;
volatile bool m_quit;

} ;
Expand Down
49 changes: 34 additions & 15 deletions include/PlayHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "lmms_basics.h"

class track;

class AudioPort;

class PlayHandle : public ThreadableJob
{
Expand All @@ -48,12 +48,7 @@ class PlayHandle : public ThreadableJob
} ;
typedef Types Type;

PlayHandle( const Type type, f_cnt_t offset = 0 ) :
m_type( type ),
m_offset( offset ),
m_affinity( QThread::currentThread() )
{
}
PlayHandle( const Type type, f_cnt_t offset = 0 );

PlayHandle & operator = ( PlayHandle & p )
{
Expand All @@ -63,9 +58,7 @@ class PlayHandle : public ThreadableJob
return *this;
}

virtual ~PlayHandle()
{
}
virtual ~PlayHandle();

virtual bool affinityMatters() const
{
Expand All @@ -83,10 +76,7 @@ class PlayHandle : public ThreadableJob
}

// required for ThreadableJob
virtual void doProcessing( sampleFrame* buffer )
{
play( buffer );
}
virtual void doProcessing();

virtual bool requiresProcessing() const
{
Expand All @@ -106,7 +96,7 @@ class PlayHandle : public ThreadableJob
return m_processingLock.tryLock();
}
virtual void play( sampleFrame* buffer ) = 0;
virtual bool isFinished( void ) const = 0;
virtual bool isFinished() const = 0;

// returns the frameoffset at the start of the playhandle,
// ie. how many empty frames should be inserted at the start of the first period
Expand All @@ -123,12 +113,41 @@ class PlayHandle : public ThreadableJob

virtual bool isFromTrack( const track * _track ) const = 0;

bool usesBuffer() const
{
return m_usesBuffer;
}

void setUsesBuffer( const bool b )
{
m_usesBuffer = b;
}

AudioPort * audioPort()
{
return m_audioPort;
}

void setAudioPort( AudioPort * port )
{
m_audioPort = port;
}

void releaseBuffer();

sampleFrame * buffer()
{
return m_playHandleBuffer;
}

private:
Type m_type;
f_cnt_t m_offset;
QThread* m_affinity;
QMutex m_processingLock;
sampleFrame * m_playHandleBuffer;
bool m_usesBuffer;
AudioPort * m_audioPort;

} ;

Expand Down
4 changes: 2 additions & 2 deletions include/PresetPreviewPlayHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*
*/

#ifndef _PRESET_PREVIEW_PLAY_HANDLE_H
#define _PRESET_PREVIEW_PLAY_HANDLE_H
#ifndef PRESET_PREVIEW_PLAY_HANDLE_H
#define PRESET_PREVIEW_PLAY_HANDLE_H

#include "NotePlayHandle.h"

Expand Down
1 change: 0 additions & 1 deletion include/SamplePlayHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ class SamplePlayHandle : public PlayHandle
f_cnt_t m_frame;
SampleBuffer::handleState m_state;

AudioPort * m_audioPort;
const bool m_ownAudioPort;

FloatModel m_defaultVolumeModel;
Expand Down
10 changes: 5 additions & 5 deletions include/ThreadableJob.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
*
*/

#ifndef _THREADABLE_JOB_H
#define _THREADABLE_JOB_H
#ifndef THREADABLE_JOB_H
#define THREADABLE_JOB_H

#include <QtCore/QAtomicInt>

Expand Down Expand Up @@ -62,11 +62,11 @@ class ThreadableJob
m_state = Queued;
}

void process( sampleFrame* workingBuffer = NULL )
void process()
{
if( m_state.testAndSetOrdered( Queued, InProgress ) )
{
doProcessing( workingBuffer );
doProcessing();
m_state = Done;
}
}
Expand All @@ -75,7 +75,7 @@ class ThreadableJob


protected:
virtual void doProcessing( sampleFrame* workingBuffer) = 0;
virtual void doProcessing() = 0;

QAtomicInt m_state;

Expand Down
2 changes: 1 addition & 1 deletion plugins/lb302/lb302.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ lb302Synth::lb302Synth( InstrumentTrack * _instrumentTrack ) :

filterChanged();

InstrumentPlayHandle * iph = new InstrumentPlayHandle( this );
InstrumentPlayHandle * iph = new InstrumentPlayHandle( this, _instrumentTrack );
engine::mixer()->addPlayHandle( iph );
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/opl2/opl2instrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ opl2instrument::opl2instrument( InstrumentTrack * _instrument_track ) :
trem_depth_mdl(false, this, tr( "Tremolo Depth" ) )
{
// Connect the plugin to the mixer...
InstrumentPlayHandle * iph = new InstrumentPlayHandle( this );
InstrumentPlayHandle * iph = new InstrumentPlayHandle( this, _instrument_track );
engine::mixer()->addPlayHandle( iph );

// Voices are laid out in a funny way...
Expand Down
2 changes: 1 addition & 1 deletion plugins/sf2_player/sf2_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ sf2Instrument::sf2Instrument( InstrumentTrack * _instrument_track ) :
// everytime we load a new soundfont.
m_synth = new_fluid_synth( m_settings );

InstrumentPlayHandle * iph = new InstrumentPlayHandle( this );
InstrumentPlayHandle * iph = new InstrumentPlayHandle( this, _instrument_track );
engine::mixer()->addPlayHandle( iph );

loadFile( configManager::inst()->defaultSoundfont() );
Expand Down
2 changes: 1 addition & 1 deletion plugins/vestige/vestige.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ vestigeInstrument::vestigeInstrument( InstrumentTrack * _instrument_track ) :
p_subWindow( NULL )
{
// now we need a play-handle which cares for calling play()
InstrumentPlayHandle * iph = new InstrumentPlayHandle( this );
InstrumentPlayHandle * iph = new InstrumentPlayHandle( this, _instrument_track );
engine::mixer()->addPlayHandle( iph );
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/zynaddsubfx/ZynAddSubFx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ ZynAddSubFxInstrument::ZynAddSubFxInstrument(
connect( &m_resBandwidthModel, SIGNAL( dataChanged() ), this, SLOT( updateResBandwidth() ) );

// now we need a play-handle which cares for calling play()
InstrumentPlayHandle * iph = new InstrumentPlayHandle( this );
InstrumentPlayHandle * iph = new InstrumentPlayHandle( this, _instrumentTrack );
engine::mixer()->addPlayHandle( iph );

connect( engine::mixer(), SIGNAL( sampleRateChanged() ),
Expand Down
Loading

0 comments on commit 722235b

Please sign in to comment.