TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/csound/csound/llms.txt
Use this file to discover all available pages before exploring further.
csound_compiler.h header provides low-level access to Csound’s parser and compiler. It allows programmatic manipulation of orchestra code as abstract syntax trees (AST).
Overview
This header enables:- Parsing orchestra code into AST structures
- Compiling AST nodes into executable code
- Dynamic code generation and manipulation
- Advanced metaprogramming techniques
Key structures
Orchestra token
Abstract syntax tree node
- Binary tree with
leftandrightchildren - Siblings linked via
nextpointer - Annotated with rate information for type checking
- Markup field used by semantic checker (transitional)
API functions
Parse orchestra code
csound- Csound instancestr- Orchestra code as ASCII string
- Pointer to root TREE node on success
- NULL on parse error
Compile AST to structs
csound- Csound instanceroot- Root node of AST to compileasync- 0 for synchronous, 1 for asynchronous compilation
CSOUND_SUCCESS(0) on success- Negative error code on failure
Free AST resources
csound- Csound instancetree- Root node of tree to delete
csoundParseOrc() is no longer needed and can be deallocated.
Usage:
Usage patterns
Parse, compile, and cleanup
Dynamic code generation
Asynchronous compilation
Compilation modes
Synchronous (async = 0)
- Blocks until compilation completes
- Safe for immediate use of compiled code
- Recommended for most use cases
Asynchronous (async = 1)
- Returns immediately, compilation in background
- Useful for non-blocking UI
- Must ensure compilation completes before use
- Thread-safe with Csound’s internal locking
Tree node types
Thetype field in TREE nodes represents different syntactic elements:
- Instrument definitions
- Opcode statements
- Expressions (arithmetic, logical)
- Variable references
- Constants
- Control structures (if/else, loops)
Rate annotations
Therate field indicates signal rate:
- i-rate: Initialization time only
- k-rate: Control rate (per k-period)
- a-rate: Audio rate (per sample)
- Mixed: Determined by context
Memory management
Important considerations:- Always free trees: Call
csoundDeleteTree()for everycsoundParseOrc() - Timing: Free after successful compilation
- Error handling: Free even on compilation errors
- Ownership: Tree memory owned by Csound instance
Error handling
Performance considerations
- Parsing is relatively fast
- Compilation can be expensive for large instruments
- Use asynchronous mode for large code blocks
- Cache parsed trees if recompiling frequently
- Tree traversal is depth-first
Use cases
Code generation tools
Generate Csound code from:- Visual programming interfaces
- Algorithmic composition systems
- Domain-specific languages
- Templates with parameters
Live coding
Enable dynamic orchestra modification:- Parse and compile on-the-fly
- Add instruments during performance
- Modify existing definitions
- Interactive development
Metaprogramming
Create code that generates code:- Macro expansion
- Code transformations
- Optimization passes
- Analysis tools
Testing and validation
Build development tools:- Syntax validators
- Static analyzers
- Code formatters
- Documentation generators
Limitations
- Markup field is temporary/transitional
- Tree modification not officially supported
- Limited introspection of node types
- No direct access to symbol tables
Thread safety
Parsing and compilation are protected by Csound’s internal locks. Multiple threads can safely:- Parse different code strings
- Compile to the same Csound instance
- Mix synchronous and asynchronous calls
- Modifying trees from multiple threads
- Freeing trees during compilation
See also
- csound.h - Main API for compilation
- csoundCore.h - Internal structures