Csound API  6.18
CsoundThreaded Class Reference

This class provides a multi-threaded C++ interface to the "C" Csound API. More...

#include <csound_threaded.hpp>

+ Inheritance diagram for CsoundThreaded:

Public Member Functions

 CsoundThreaded ()
 
 CsoundThreaded (CSOUND *csound_)
 
 CsoundThreaded (void *host_data)
 
virtual ~CsoundThreaded ()
 
virtual void SetKperiodCallback (void(*kperiod_callback_)(CSOUND *, void *), void *kperiod_callback_user_data_)
 
virtual int PerformRoutine ()
 
virtual int PerformAndResetRoutine ()
 
virtual int Perform ()
 Overrides Csound::Perform to run in a separate thread of execution. More...
 
virtual int PerformAndReset ()
 Like Perform, but calls Cleanup() and Reset() at the conclusion of the performance, so that this is done in the performance thread. More...
 
virtual int ScoreEvent (char opcode, const MYFLT *pfields, long pfield_count)
 Enqueues a low-level score event with raw pfields for dispatch from the performance thread routine. More...
 
virtual void InputMessage (const char *message)
 Enqueues a textual score event or events for dispatch from the performance thread routine. More...
 
virtual int ReadScore (const char *score)
 Enqueues a textual score event, score fragment, or entire score for dispatch from the performance thread routine. More...
 
virtual void Stop ()
 Signals the performance thread routine to stop and return. More...
 
virtual void Join ()
 Causes the calling thread to wait for the end of the performance thread routine. More...
 
virtual bool IsPlaying () const
 Returns whether or not the performance thread routine is running. More...
 

Protected Member Functions

void ClearQueue ()
 

Protected Attributes

std::thread performance_thread
 
std::atomic< bool > keep_running
 
void(* kperiod_callback )(CSOUND *, void *)
 
void * kperiod_callback_user_data
 
concurrent_queue< CsoundEvent * > input_queue
 

Detailed Description

This class provides a multi-threaded C++ interface to the "C" Csound API.

The interface is identical to the C++ interface of the Csound class in csound.hpp; however, the ::Perform() function runs in a separate thread of execution that is fed by a thread-safe FIFO of messages from ::ScoreEvent, ::InputMessage, and ::ReadScore.

This is a header-file-only facility. The multi-threaded features of this class are minimalistic, but seem sufficient for most purposes. There are no external dependences apart from Csound and the standard C++ library.

Constructor & Destructor Documentation

◆ CsoundThreaded() [1/3]

CsoundThreaded::CsoundThreaded ( )
inline

◆ CsoundThreaded() [2/3]

CsoundThreaded::CsoundThreaded ( CSOUND csound_)
inline

◆ CsoundThreaded() [3/3]

CsoundThreaded::CsoundThreaded ( void *  host_data)
inline

◆ ~CsoundThreaded()

virtual CsoundThreaded::~CsoundThreaded ( )
inlinevirtual

Member Function Documentation

◆ ClearQueue()

void CsoundThreaded::ClearQueue ( )
inlineprotected

◆ InputMessage()

virtual void CsoundThreaded::InputMessage ( const char *  message)
inlinevirtual

Enqueues a textual score event or events for dispatch from the performance thread routine.

References concurrent_queue< Data >::push().

◆ IsPlaying()

virtual bool CsoundThreaded::IsPlaying ( ) const
inlinevirtual

Returns whether or not the performance thread routine is running.

◆ Join()

virtual void CsoundThreaded::Join ( )
inlinevirtual

Causes the calling thread to wait for the end of the performance thread routine.

◆ Perform()

virtual int CsoundThreaded::Perform ( )
inlinevirtual

Overrides Csound::Perform to run in a separate thread of execution.

The granularity of time is one kperiod. If a kperiod callback has been set, it is called with the CSOUND object and any user data on every kperiod.

References PerformRoutine().

◆ PerformAndReset()

virtual int CsoundThreaded::PerformAndReset ( )
inlinevirtual

Like Perform, but calls Cleanup() and Reset() at the conclusion of the performance, so that this is done in the performance thread.

References PerformAndResetRoutine().

◆ PerformAndResetRoutine()

virtual int CsoundThreaded::PerformAndResetRoutine ( )
inlinevirtual

◆ PerformRoutine()

virtual int CsoundThreaded::PerformRoutine ( )
inlinevirtual

References concurrent_queue< Data >::try_pop().

Referenced by Perform().

◆ ReadScore()

virtual int CsoundThreaded::ReadScore ( const char *  score)
inlinevirtual

Enqueues a textual score event, score fragment, or entire score for dispatch from the performance thread routine.

References concurrent_queue< Data >::push().

◆ ScoreEvent()

virtual int CsoundThreaded::ScoreEvent ( char  opcode,
const MYFLT *  pfields,
long  pfield_count 
)
inlinevirtual

Enqueues a low-level score event with raw pfields for dispatch from the performance thread routine.

References concurrent_queue< Data >::push().

◆ SetKperiodCallback()

virtual void CsoundThreaded::SetKperiodCallback ( void(*)(CSOUND *, void *)  kperiod_callback_,
void *  kperiod_callback_user_data_ 
)
inlinevirtual

◆ Stop()

virtual void CsoundThreaded::Stop ( )
inlinevirtual

Signals the performance thread routine to stop and return.

Field Documentation

◆ input_queue

concurrent_queue<CsoundEvent *> CsoundThreaded::input_queue
protected

◆ keep_running

std::atomic<bool> CsoundThreaded::keep_running
protected

◆ kperiod_callback

void(* CsoundThreaded::kperiod_callback) (CSOUND *, void *)
protected

◆ kperiod_callback_user_data

void* CsoundThreaded::kperiod_callback_user_data
protected

◆ performance_thread

std::thread CsoundThreaded::performance_thread
protected