csound_rtaudio.h header defines the interface for implementing real-time audio I/O modules in Csound. It provides structures and callbacks for audio backends.
Overview
This header is used by:- Audio backend module developers
- Custom audio I/O implementations
- Host applications bypassing default I/O
Key structures
Real-time audio parameters
devName- Device identifier string (backend-specific)devNum- Numeric device ID; 1024 means use default devicebufSamp_SW- Software buffer size in sample frames (set by-b)bufSamp_HW- Hardware buffer size in sample frames (set by-B)nChannels- Number of audio channelssampleFormat- Audio sample format constantsampleRate- Sampling rate in Hertzksmps- Number of samples per control period
Callback function types
Device open callback
- 0 on success
- Non-zero error code on failure
Audio output callback
csound- Csound instancebuffer- Output buffer (MYFLT samples)nframes- Number of sample frames to write
Audio input callback
csound- Csound instancebuffer- Input buffer to fillnframes- Number of sample frames to read
- 0 on success
- Non-zero on error
Device list callback
csound- Csound instancelist- Array to fill with device info (NULL to get count)isOutput- 1 for output devices, 0 for input devices
- Number of devices available
API functions
User data access
Register callbacks
Set playback open callback
Set playback callback
Set recording open callback
Set recording callback
Set close callback
Set device list callback
Host audio I/O
csoundCreate() and before performance to use custom I/O.
When this is called:
- Host must handle audio I/O directly
- Access audio via
csoundGetSpin()andcsoundGetSpout() - Call
csoundPerformKsmps()to process audio blocks
Set audio module
module- Module name (e.g., “portaudio”, “jack”, “alsa”)
Sample formats
ThesampleFormat field uses constants defined in Csound:
Buffer size parameters
Software buffer (bufSamp_SW)
- Set by
-bcommand-line option - Csound’s internal processing buffer
- Typically smaller than hardware buffer
- Should be multiple of ksmps for efficiency
Hardware buffer (bufSamp_HW)
- Set by
-Bcommand-line option - Audio driver’s buffer size
- Larger values: more latency, more stable
- Smaller values: less latency, more CPU
bufSamp_HW >= bufSamp_SW * 2
Implementing an audio backend
Basic structure
Device enumeration
Usage by host applications
Using default audio backend
Custom audio I/O
Thread safety
Audio callbacks may be called from:- Real-time audio threads
- High-priority system threads
- Different threads than main Csound thread
- Keep callbacks fast and non-blocking
- Avoid memory allocation in callbacks
- Use lock-free techniques when possible
- Don’t call blocking Csound API functions from callbacks
See also
- csound.h - Main API with audio I/O functions
- csound_rtmidi.h - MIDI I/O interface