Csound API 6.09
Loading...
Searching...
No Matches
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.
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.
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.
virtual void InputMessage (const char *message)
 Enqueues a textual score event or events for dispatch from the performance thread routine.
virtual int ReadScore (const char *score)
 Enqueues a textual score event, score fragment, or entire score for dispatch from the performance thread routine.
virtual void Stop ()
 Signals the performance thread routine to stop and return.
virtual void Join ()
 Causes the calling thread to wait for the end of the performance thread routine.
virtual bool IsPlaying () const
 Returns whether or not the performance thread routine is running.

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

References ClearQueue(), Join(), and Stop().

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 input_queue.

◆ IsPlaying()

virtual bool CsoundThreaded::IsPlaying ( ) const
inlinevirtual

Returns whether or not the performance thread routine is running.

References keep_running.

◆ Join()

virtual void CsoundThreaded::Join ( )
inlinevirtual

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

References performance_thread.

Referenced by ~CsoundThreaded().

◆ 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 performance_thread, and 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 performance_thread, and PerformAndResetRoutine().

◆ PerformAndResetRoutine()

virtual int CsoundThreaded::PerformAndResetRoutine ( )
inlinevirtual

◆ PerformRoutine()

virtual int CsoundThreaded::PerformRoutine ( )
inlinevirtual

◆ 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 input_queue.

◆ 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 input_queue.

◆ SetKperiodCallback()

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

◆ Stop()

virtual void CsoundThreaded::Stop ( )
inlinevirtual

Signals the performance thread routine to stop and return.

References keep_running.

Referenced by ~CsoundThreaded().

Field Documentation

◆ input_queue

◆ 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

Referenced by Join(), Perform(), and PerformAndReset().