Overview
Csound’s architecture follows a traditional compiler-interpreter model, separating the compilation of orchestra code from the performance (execution) phase. The system is built around several interconnected subsystems that work together to process audio in real-time.Core components
Public API layer
The public API is defined ininclude/csound.h and provides the interface for host applications:
- CSOUND structure: Opaque handle to the Csound instance
- Lifecycle management: Create, compile, perform, destroy
- Parameter control: Set/get configuration options
- Real-time interaction: Score events, channels, callbacks
include/csound.h:155
Engine layer
TheEngine/ directory contains the core compilation and orchestration logic:
Compiler
- Lexer/Parser:
csound_orc.lex,csound_orc.y - Type system:
csound_type_system.c - Semantic analysis:
csound_orc_semantics.c - Code generation:
csound_orc_compile.c - Optimization:
csound_orc_optimize.c
Runtime
- Performance control:
musmon.c - Event scheduling:
linevent.c - Opcode management:
insert.c,entry.c - User-defined opcodes:
udo.c - Parallel execution:
cs_par_base.c
Engine/README.md:
- auxfd.c: Auxiliary resource management for opcodes
- cfgvar.c: Configuration variable functions
- corfiles.c: Core file processing for Csound programs and scores
- fgens.c: Function table generators
- memalloc.c: Memory resources management
- symbtab.c: Compiler symbol table
Core data structures
The internal engine structures are defined ininclude/csoundCore.h:
Instrument definition (INSTRTXT)
include/csoundCore.h:133
Instrument instance (INSDS)
include/csoundCore.h:427
Opcode entry (OENTRY)
include/csoundCore.h:90
Opcode system
Opcodes are implemented in two directories:- OOps/: Internal (built-in) opcodes - core functionality
- Opcodes/: External (plugin) opcodes - extended functionality
OOps/README.md:
- Audio I/O, signal generators, filters
- FFT routines, phase vocoder operations
- MIDI operations, control flow
- Scheduling, string manipulation
Execution model
Initialization phase
- Instance creation:
csoundCreate()allocates CSOUND structure - Orchestra compilation:
csoundCompileOrc()parses and compiles instrument definitions - Score compilation:
csoundReadScore()processes score events - Audio setup:
initialise_io()configures audio backend - Opcode initialization: All i-rate opcodes execute
Performance cycle
The main performance loop executes in k-rate cycles:Engine/musmon.c:64
- Event sensing: Check for new score events, MIDI, real-time events
- Instance activation: Start new instrument instances
- Performance: Execute k-rate and a-rate opcodes for all active instances
- Audio output: Write audio buffer to output
- Deactivation: Turn off completed instrument instances
The
ksmps parameter determines the number of audio samples per control period, balancing control rate precision against computational efficiency.Parallel execution
Csound 6+ includes parallel execution capabilities incs_par_base.c:
- Dependency analysis: Automatically detect opcode dependencies
- Thread pools: Distribute independent opcodes across CPU cores
- Lock-free queues: Manage parallel task dispatch
- NUMA awareness: Optimize memory access patterns
Engine/README.md:
- cs_new_dispatch.c: Task dependency management and dispatching
- cs_par_orc_semantic_analysis.c: Parallel semantics analysis
Memory management
Csound uses several memory management strategies:AUXCH system
include/csoundCore.h:194
AUXCH for:
- Delay line buffers
- FFT scratch space
- State variables
- Large working arrays
Memory pools
- String pool: Deduplicated string storage
- Variable pool: Per-instrument variable allocation
- Opcode pool: Reusable opcode instance memory
Type system
Csound 6+ introduced a sophisticated type system (csound_type_system.c):
- Primitive types:
i,k,a,S(i-rate, k-rate, a-rate, string) - Array types: Multi-dimensional arrays of any type
- User-defined types: Struct-like composite types
- Polymorphic opcodes: Overloading based on type signatures
- Parser: Infer types from context
- Semantic analysis: Check type compatibility
- Code generation: Generate type-specific code
- Runtime: Type metadata for dynamic operations
Modularity and extensibility
Csound’s architecture enables extension through:Plugin opcodes
Developers can add opcodes via dynamic libraries:Engine/entry.c:78
User-defined opcodes (UDOs)
Users can define opcodes in Csound language itself:Engine/udo.c).
Related topics
Audio engine
Deep dive into audio processing and I/O
Opcode system
How opcodes work and how to create them
Instruments
Orchestra and instrument concepts
API reference
Complete API documentation