2015-11-16 19:59:27 +00:00
|
|
|
//( { file_desc: "This is the kernel of a real-time audio processing engine." kw:[audio rt] }
|
2013-03-25 23:00:39 +00:00
|
|
|
//
|
|
|
|
// The audio system is composed a collection of independent sub-systems.
|
|
|
|
// Each sub-system maintains a thread which runs asynchrounsly
|
|
|
|
// from the application, the MIDI devices, and the audio devices.
|
|
|
|
// To faciliate communication between these components each sub-system maintains
|
|
|
|
// two thread-safe data buffers one for control information and a second
|
|
|
|
// for audio data.
|
|
|
|
//
|
|
|
|
// The audio devices are the primary driver for the system.
|
|
|
|
// Callbacks from the audio devices (See #cmApCallbackPtr_t)
|
|
|
|
// inserts incoming audio samples into the audio
|
|
|
|
// record buffers and extracts samples from the playback buffer.
|
|
|
|
// When sufficient incoming samples and outgoing empty buffer space exists
|
|
|
|
// a sub-system thread is waken up by the callback. This triggers a DSP audio
|
|
|
|
// processing cycle which empties/fills the audio buffers. During a DSP
|
|
|
|
// processing cycle control messages from the application and MIDI are blocked and
|
|
|
|
// buffered. Upon completetion of the DSP cycle a control message
|
|
|
|
// transfer cycles occurs - buffered incoming messages are passed to
|
|
|
|
// the DSP system and messages originating in the DSP system are
|
|
|
|
// buffered by the audio system for later pickup by the application
|
|
|
|
// or MIDI system.
|
|
|
|
//
|
|
|
|
// Note that control messages that arrive when the DSP cycle is not
|
|
|
|
// occurring can pass directly through to the DSP system.
|
|
|
|
//
|
|
|
|
// The DSP system sends messages back to the host by calling
|
|
|
|
// cmAsDspToHostFunc_t provided by cmAudioSysCtx_t. These
|
|
|
|
// calls are always made from within an audio system call to
|
|
|
|
// audio or control update within cmAsCallback_t. cmAsDspToHostFunc_t
|
|
|
|
// simply stores the message in a message buffer. The host picks
|
|
|
|
// up the message at some later time when it notices that messages
|
|
|
|
// are waiting via polling cmAudioSysIsMsgWaiting().
|
|
|
|
//
|
|
|
|
// Implementation: \n
|
|
|
|
// The audio sub-systems work by maintaining an internal thread
|
|
|
|
// which blocks on a mutex condition variable.
|
|
|
|
// While the thread is blocked the mutex is unlocked allowing messages
|
|
|
|
// to pass directly through to the DSP procedure via cmAsCallback().
|
|
|
|
//
|
|
|
|
// Periodic calls from running audio devices update the audio buffer.
|
|
|
|
// When the audio buffer has input samples waiting and output space
|
|
|
|
// available the condition variable is signaled, the mutex is
|
|
|
|
// then automatically locked by the system, and the DSP execution
|
|
|
|
// procedure is called via cmAsCallback().
|
|
|
|
//
|
|
|
|
// Messages arriving while the mutex is locked are queued and
|
|
|
|
// delivered to the DSP procedure at the end of the DSP execution
|
|
|
|
// procedure.
|
|
|
|
//
|
2015-11-16 19:59:27 +00:00
|
|
|
//)
|
2012-10-30 03:52:39 +00:00
|
|
|
|
|
|
|
#ifndef cmAudioSys_h
|
|
|
|
#define cmAudioSys_h
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2015-11-16 19:59:27 +00:00
|
|
|
|
|
|
|
//(
|
2013-03-25 23:00:39 +00:00
|
|
|
// Audio system result codes
|
2012-10-30 03:52:39 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
kOkAsRC = cmOkRC,
|
|
|
|
kThreadErrAsRC,
|
|
|
|
kMutexErrAsRC,
|
|
|
|
kTsQueueErrAsRC,
|
|
|
|
kMsgEnqueueFailAsRC,
|
|
|
|
kAudioDevSetupErrAsRC,
|
|
|
|
kAudioBufSetupErrAsRC,
|
|
|
|
kAudioDevStartFailAsRC,
|
|
|
|
kAudioDevStopFailAsRC,
|
|
|
|
kBufTooSmallAsRC,
|
|
|
|
kNoMsgWaitingAsRC,
|
|
|
|
kMidiSysFailAsRC,
|
|
|
|
kMsgSerializeFailAsRC,
|
|
|
|
kStateBufFailAsRC,
|
|
|
|
kInvalidArgAsRC,
|
|
|
|
kNotInitAsRC
|
|
|
|
};
|
|
|
|
|
2013-03-29 06:13:58 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
kAsDfltMsgQueueByteCnt = 0xffff,
|
|
|
|
kAsDfltDevFramesPerCycle = 512,
|
|
|
|
kAsDfltDspFramesPerCycle = 64,
|
|
|
|
kAsDfltBufCnt = 3,
|
|
|
|
kAsDfltSrate = 44100,
|
|
|
|
kAsDfltSyncToInputFl = 1,
|
2013-04-09 06:02:37 +00:00
|
|
|
kAsDfltMinMeterMs = 10,
|
|
|
|
kAsDfltMeterMs = 50,
|
|
|
|
kAsDfltMaxMeterMs = 1000
|
2013-03-29 06:13:58 +00:00
|
|
|
};
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
typedef cmHandle_t cmAudioSysH_t; //< Audio system handle type
|
|
|
|
typedef unsigned cmAsRC_t; //< Audio system result code
|
2012-10-30 03:52:39 +00:00
|
|
|
|
|
|
|
struct cmAudioSysCtx_str;
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
//
|
|
|
|
// DSP system callback function.
|
|
|
|
//
|
|
|
|
// This is the sole point of entry into the DSP system while the audio system is running.
|
|
|
|
//
|
|
|
|
// ctxPtr is pointer to a cmAudioSysCtx_t record.
|
|
|
|
//
|
|
|
|
// This function is called under two circumstances:
|
|
|
|
//
|
|
|
|
// 1) To notify the DSP system that the audio input/output buffers need to be serviced.
|
|
|
|
// This is a perioidic request which the DSP system uses as its execution trigger.
|
|
|
|
// cmAudioSysCtx_t.audioRateFl is set to true to indicate this type of callback.
|
|
|
|
//
|
|
|
|
// 2) To pass messages from the host application to the DSP system.
|
|
|
|
// The DSP system is asyncronous with the host because it executes in the
|
|
|
|
// audio system thread rather than the host thread. The cmAudioSysDeliverMsg()
|
|
|
|
// function synchronizes incoming messages with the internal audio system
|
|
|
|
// thread to prevent thread collisions.
|
|
|
|
//
|
|
|
|
// Notes:
|
|
|
|
// This callback is always made with the internal audio system mutex locked.
|
|
|
|
//
|
|
|
|
// The signal time covered by the callback is from
|
|
|
|
// ctx->begSmpIdx to ctx->begSmpIdx+cfg->dspFramesPerCycle.
|
|
|
|
//
|
|
|
|
// The return value is currently not used.
|
2012-10-30 03:52:39 +00:00
|
|
|
typedef cmRC_t (*cmAsCallback_t)(void* ctxPtr, unsigned msgByteCnt, const void* msgDataPtr );
|
|
|
|
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Audio device sub-sytem configuration record
|
|
|
|
typedef struct cmAudioSysArgs_str
|
2012-10-30 03:52:39 +00:00
|
|
|
{
|
2013-03-25 23:00:39 +00:00
|
|
|
cmRpt_t* rpt; // system console object
|
|
|
|
unsigned inDevIdx; // input audio device
|
|
|
|
unsigned outDevIdx; // output audio device
|
|
|
|
bool syncInputFl; // true/false sync the DSP update callbacks with audio input/output
|
|
|
|
unsigned msgQueueByteCnt; // Size of the internal msg queue used to buffer msgs arriving via cmAudioSysDeliverMsg().
|
|
|
|
unsigned devFramesPerCycle; // (512) Audio device samples per channel per device update buffer.
|
|
|
|
unsigned dspFramesPerCycle; // (64) Audio samples per channel per DSP cycle.
|
|
|
|
unsigned audioBufCnt; // (3) Audio device buffers.
|
|
|
|
double srate; // Audio sample rate.
|
2012-10-30 03:52:39 +00:00
|
|
|
} cmAudioSysArgs_t;
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Audio sub-system configuration record.
|
|
|
|
// This record is provided by the host to configure the audio system
|
|
|
|
// via cmAudioSystemAllocate() or cmAudioSystemInitialize().
|
2012-10-30 03:52:39 +00:00
|
|
|
typedef struct cmAudioSysSubSys_str
|
|
|
|
{
|
2013-03-25 23:00:39 +00:00
|
|
|
cmAudioSysArgs_t args; // Audio device configuration
|
|
|
|
cmAsCallback_t cbFunc; // DSP system entry point function.
|
|
|
|
void* cbDataPtr; // Host provided data for the DSP system callback.
|
2012-10-30 03:52:39 +00:00
|
|
|
} cmAudioSysSubSys_t;
|
|
|
|
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Signature of a callback function provided by the audio system to receive messages
|
|
|
|
// from the DSP system for later dispatch to the host application.
|
|
|
|
// This declaration is used by the DSP system implementation and the audio system.
|
|
|
|
// Note that this function is intended to convey one message broken into multiple parts.
|
|
|
|
// See cmTsQueueEnqueueSegMsg() for the equivalent interface.
|
2012-10-30 03:52:39 +00:00
|
|
|
typedef cmAsRC_t (*cmAsDspToHostFunc_t)(struct cmAudioSysCtx_str* p, const void* msgDataPtrArray[], unsigned msgByteCntArray[], unsigned msgSegCnt);
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Record passed with each call to the DSP callback function cmAsCallback_t
|
2012-10-30 03:52:39 +00:00
|
|
|
typedef struct cmAudioSysCtx_str
|
|
|
|
{
|
2013-03-25 23:00:39 +00:00
|
|
|
void* reserved; // used internally by the audio system
|
2012-10-30 03:52:39 +00:00
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
bool audioRateFl; // true if this is an audio update callback
|
2012-10-30 03:52:39 +00:00
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
unsigned srcNetNodeId; // Source net node if this is a msg callback originating from a remote network node.
|
|
|
|
unsigned asSubIdx; // index of the sub-system this DSP process is serving
|
2012-10-30 03:52:39 +00:00
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
cmAudioSysSubSys_t* ss; // ptr to a copy of the cfg recd used to initialize the audio system
|
|
|
|
unsigned begSmpIdx; // gives signal time as a sample count
|
2012-10-30 03:52:39 +00:00
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
cmAsDspToHostFunc_t dspToHostFunc; // Callback used by the DSP process to send messages to the host
|
|
|
|
// via the audio system. Returns a cmAsRC_t result code.
|
2012-10-30 03:52:39 +00:00
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// output (playback) buffers
|
|
|
|
cmSample_t** oChArray; // each ele is a ptr to buffer with cfg.dspFramesPerCycle samples
|
|
|
|
unsigned oChCnt; // count of output channels (ele's in oChArray[])
|
2013-12-15 23:44:35 +00:00
|
|
|
cmTimeSpec_t oTimeStamp;
|
2012-10-30 03:52:39 +00:00
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// input (recording) buffers
|
|
|
|
cmSample_t** iChArray; // each ele is a ptr to buffer with cfg.dspFramesPerCycle samples
|
|
|
|
unsigned iChCnt; // count of input channels (ele's in iChArray[])
|
2013-12-15 23:44:35 +00:00
|
|
|
cmTimeSpec_t iTimeStamp;
|
2012-10-30 03:52:39 +00:00
|
|
|
|
|
|
|
} cmAudioSysCtx_t;
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
|
|
|
|
// Audio system configuration record used by cmAudioSysAllocate().
|
2012-10-30 03:52:39 +00:00
|
|
|
typedef struct cmAudioSysCfg_str
|
|
|
|
{
|
2013-03-25 23:00:39 +00:00
|
|
|
cmAudioSysSubSys_t* ssArray; // sub-system cfg record array
|
|
|
|
unsigned ssCnt; // count of sub-systems
|
|
|
|
unsigned meterMs; // Meter sample period in milliseconds
|
|
|
|
void* clientCbData; // User arg. for clientCbFunc().
|
|
|
|
cmTsQueueCb_t clientCbFunc; // Called by cmAudioSysReceiveMsg() to deliver internally generated msg's to the host.
|
|
|
|
// Set to NULL if msg's will be directly returned by buffers passed to cmAudioSysReceiveMsg().
|
2012-10-30 03:52:39 +00:00
|
|
|
cmUdpNetH_t netH;
|
|
|
|
} cmAudioSysCfg_t;
|
|
|
|
|
|
|
|
extern cmAudioSysH_t cmAudioSysNullHandle;
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Allocate and initialize an audio system as a collection of 'cfgCnt' sub-systems.
|
|
|
|
// Prior to call this function the audio audio ports system must be initalized
|
|
|
|
// (via cmApInitialize()) and the MIDI port system must be initialized
|
|
|
|
// (via cmMpInitialize()). Note also that cmApFinalize() and cmMpFinalize()
|
|
|
|
// cannot be called prior to cmAudioSysFree().
|
|
|
|
// See cmAudioSystemTest() for a complete example.
|
2012-10-30 03:52:39 +00:00
|
|
|
cmAsRC_t cmAudioSysAllocate( cmAudioSysH_t* hp, cmRpt_t* rpt, const cmAudioSysCfg_t* cfg );
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Finalize and release any resources held by the audio system.
|
2012-10-30 03:52:39 +00:00
|
|
|
cmAsRC_t cmAudioSysFree( cmAudioSysH_t* hp );
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Returns true if 'h' is a handle which was successfully allocated by
|
|
|
|
// cmAudioSysAllocate().
|
2012-10-30 03:52:39 +00:00
|
|
|
bool cmAudioSysHandleIsValid( cmAudioSysH_t h );
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Reinitialize a previously allocated audio system. This function
|
|
|
|
// begins with a call to cmAudioSysFinalize().
|
|
|
|
// Use cmAudioSysEnable(h,true) to begin processing audio following this call.
|
2012-10-30 03:52:39 +00:00
|
|
|
cmAsRC_t cmAudioSysInitialize( cmAudioSysH_t h, const cmAudioSysCfg_t* cfg );
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Complements cmAudioSysInitialize(). In general there is no need to call this function
|
|
|
|
// since calls to cmAudioSysInitialize() and cmAudioSysFree() automaticatically call it.
|
2012-10-30 03:52:39 +00:00
|
|
|
cmAsRC_t cmAudioSysFinalize( cmAudioSysH_t h );
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Returns true if the audio system has been successfully initialized.
|
2012-10-30 03:52:39 +00:00
|
|
|
bool cmAudioSysIsInitialized( cmAudioSysH_t );
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Returns true if the audio system is enabled.
|
2012-10-30 03:52:39 +00:00
|
|
|
bool cmAudioSysIsEnabled( cmAudioSysH_t h );
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Enable/disable the audio system. Enabling the starts audio stream
|
|
|
|
// in/out of the system.
|
2012-10-30 03:52:39 +00:00
|
|
|
cmAsRC_t cmAudioSysEnable( cmAudioSysH_t h, bool enableFl );
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
//
|
|
|
|
// Host to DSP delivery functions
|
|
|
|
//
|
|
|
|
|
|
|
|
// Deliver a message from the host application to the DSP process. (host -> DSP);
|
|
|
|
// The message is formed as a concatenation of the bytes in each of the segments
|
|
|
|
// pointed to by 'msgDataPtrArrary[segCnt][msgByteCntArray[segCnt]'.
|
|
|
|
// This is the canonical msg delivery function in so far as the other host->DSP
|
|
|
|
// msg delivery function are written in terms of this function.
|
|
|
|
// The first 4 bytes in the first segment must contain the index of the audio sub-system
|
|
|
|
// which is to receive the message.
|
2012-10-30 03:52:39 +00:00
|
|
|
cmAsRC_t cmAudioSysDeliverSegMsg( cmAudioSysH_t h, const void* msgDataPtrArray[], unsigned msgByteCntArray[], unsigned msgSegCnt, unsigned srcNetNodeId );
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Deliver a single message from the host to the DSP system.
|
2012-10-30 03:52:39 +00:00
|
|
|
cmAsRC_t cmAudioSysDeliverMsg( cmAudioSysH_t h, const void* msgPtr, unsigned msgByteCnt, unsigned srcNetNodeId );
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Deliver a single message from the host to the DSP system.
|
|
|
|
// Prior to delivery the 'id' is prepended to the message.
|
2012-10-30 03:52:39 +00:00
|
|
|
cmAsRC_t cmAudioSysDeliverIdMsg( cmAudioSysH_t h, unsigned asSubIdx, unsigned id, const void* msgPtr, unsigned msgByteCnt, unsigned srcNetNodeId );
|
|
|
|
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
//
|
|
|
|
// DSP to Host message functions
|
|
|
|
//
|
|
|
|
|
|
|
|
// Is a msg from the DSP waiting to be picked up by the host? (host <- DSP)
|
|
|
|
// 0 = no msgs are waiting or the msg queue is locked by the DSP process.
|
|
|
|
// >0 = the size of the buffer required to hold the next msg returned via
|
|
|
|
// cmAudioSysReceiveMsg().
|
2012-10-30 03:52:39 +00:00
|
|
|
unsigned cmAudioSysIsMsgWaiting( cmAudioSysH_t h );
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Copy the next available msg sent from the DSP process to the host into the host supplied msg buffer
|
|
|
|
// pointed to by 'msgBufPtr'. Set 'msgDataPtr' to NULL to receive msg by callback from cmAudioSysCfg_t.clientCbFunc.
|
|
|
|
// Returns kBufTooSmallAsRC if msgDataPtr[msgByteCnt] is too small to hold the msg.
|
|
|
|
// Returns kNoMsgWaitingAsRC if no messages are waiting for delivery or the msg queue is locked by the DSP process.
|
|
|
|
// Returns kOkAsRC if a msg was delivered.
|
|
|
|
// Call cmAudioSysIsMsgWaiting() prior to calling this function to get
|
|
|
|
// the size of the data buffer required to hold the next message.
|
2012-10-30 03:52:39 +00:00
|
|
|
cmAsRC_t cmAudioSysReceiveMsg( cmAudioSysH_t h, void* msgDataPtr, unsigned msgByteCnt );
|
|
|
|
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Fill an audio system status record.
|
2012-10-30 03:52:39 +00:00
|
|
|
void cmAudioSysStatus( cmAudioSysH_t h, unsigned asSubIdx, cmAudioSysStatus_t* statusPtr );
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Enable cmAudioSysStatus_t notifications to be sent periodically to the host.
|
|
|
|
// Set asSubIdx to cmInvalidIdx to enable/disable all sub-systems.
|
|
|
|
// The notifications occur approximately every cmAudioSysCfg_t.meterMs milliseconds.
|
2012-10-30 03:52:39 +00:00
|
|
|
void cmAudioSysStatusNotifyEnable( cmAudioSysH_t, unsigned asSubIdx, bool enableFl );
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Return a pointer the context record associated with a sub-system
|
2012-10-30 03:52:39 +00:00
|
|
|
cmAudioSysCtx_t* cmAudioSysContext( cmAudioSysH_t h, unsigned asSubIdx );
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Return the count of audio sub-systems.
|
|
|
|
// This is the same as the count of cfg recds passed to cmAudioSystemInitialize().
|
2012-10-30 03:52:39 +00:00
|
|
|
unsigned cmAudioSysSubSystemCount( cmAudioSysH_t h );
|
|
|
|
|
2013-03-25 23:00:39 +00:00
|
|
|
// Audio system test and example function.
|
2012-10-30 03:52:39 +00:00
|
|
|
void cmAudioSysTest( cmRpt_t* rpt, int argc, const char* argv[] );
|
|
|
|
|
2015-11-16 19:59:27 +00:00
|
|
|
//)
|
|
|
|
|
2012-10-30 03:52:39 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|