Skip to main content

Cython bindings for the Csound audio synthesis library

Project description

cycsound

Cython bindings for the Csound 6.x API.

Overview

cycsound is a Cython wrapper for the Csound audio synthesis library, providing Python bindings for the csound.h C API. It offers an alternative to the default ctypes-based ctcsound.py wrapper included with Csound.

Features

  • 98% API coverage of the Csound C API (216/219 functions)
  • Context manager support for automatic resource cleanup
  • GIL release for all blocking operations (enables true multi-threading)
  • Static linking support on macOS (standalone wheels without Csound installation)
  • Platform support: macOS, Linux, Windows
  • CLI for common operations (play, render, check, info, eval)

Installation

pip install cycsound

Building From Source

Prerequisites:

  • Python >= 3.9
  • Csound 6.x installed:
    • macOS: CsoundLib64.framework in /Library/Frameworks/
    • Linux: libcsound64-dev package
    • Windows: Csound6_x64 in C:/Program Files/
# Clone the repository
git clone https://github.com/yourusername/cycsound.git
cd cycsound

# Install in development mode
make sync

# Or install directly
pip install .

Building Wheels

# Dynamic linking (requires Csound at runtime)
make wheel

# Static linking - standalone wheel (macOS only)
make wheel-static

Command-Line Interface

cycsound includes a CLI for common Csound operations:

# Play a CSD file
cycsound play myscore.csd

# Render to audio file
cycsound render myscore.csd                    # Creates myscore.wav
cycsound render -o output.wav myscore.csd      # Specify output file
cycsound render -f flac -r 48000 myscore.csd   # FLAC format, 48kHz

# Validate CSD syntax
cycsound check myscore.csd

# Show Csound version info
cycsound info
cycsound info -v    # Verbose output

# Get help
cycsound --help
cycsound play --help

CLI Commands

Command Description
play <file> Play CSD file in real-time
render <file> Render CSD to audio file
check <file> Validate CSD syntax
info Show Csound version
eval <code> Evaluate orchestra code

Common Options

Option Description
-q, --quiet Suppress output
-o, --output Output file/device
-h, --help Show help

Quick Start

import cycsound

# Using context manager (recommended)
with cycsound.Csound() as cs:
    cs.set_option("-odac")  # Real-time audio output
    cs.set_option("-d")     # Suppress displays
    cs.compile_orc("""
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1

instr 1
    asig oscil 0.5, 440
    outs asig, asig
endin
""")
    cs.read_score("i1 0 2")
    cs.run()  # Performs entire score
# cleanup() called automatically on exit

# Or with manual control
with cycsound.Csound() as cs:
    cs.compile_csd("myscore.csd")
    cs.start()
    while not cs.perform_ksmps():
        # Process each k-cycle
        pass

API Reference

Module-level Functions

Function Description
get_version() Returns Csound version (e.g., 6180 for 6.18.0)
get_api_version() Returns API version
get_size_of_myflt() Returns size of MYFLT type (4 or 8)
initialize(flags=0) Initialize Csound library (rarely needed)
set_opcode_dir(path) Set opcode directory override

Csound Class

Core methods for audio synthesis:

# Context manager support
with cycsound.Csound() as cs:
    # ... use cs ...
# cleanup() called automatically

# Compilation
cs.compile_orc(orc_string)      # Compile orchestra code
cs.compile_csd(filename)        # Compile CSD file
cs.compile_csd_text(csd_string) # Compile CSD from string
cs.read_score(score_string)     # Read score events

# Performance
cs.run()             # Perform entire score (convenience method)
cs.start()           # Prepare for performance
cs.perform()         # Perform entire score (blocks)
cs.perform_ksmps()   # Perform one control period
cs.perform_buffer()  # Perform one buffer
cs.stop()            # Stop performance
cs.cleanup()         # Close audio/MIDI devices
cs.reset()           # Reset for new performance

# Configuration
cs.set_option(option)           # Set command-line option
cs.get_sr() / cs.get_kr()       # Get sample/control rate
cs.get_ksmps() / cs.get_nchnls() # Get ksmps/channels

# Channels
cs.set_control_channel(name, value)
cs.get_control_channel(name)
cs.set_string_channel(name, value)
cs.get_string_channel(name)

Enums

# Status codes (return values from Csound operations)
cycsound.Status.SUCCESS        # 0 - Operation succeeded
cycsound.Status.ERROR          # -1 - General error
cycsound.Status.INITIALIZATION # -2 - Initialization error
cycsound.Status.PERFORMANCE    # -3 - Performance error
cycsound.Status.MEMORY         # -4 - Memory allocation error
cycsound.Status.SIGNAL         # -5 - Signal received

# File types
cycsound.FileType.WAVE         # WAV audio
cycsound.FileType.FLAC         # FLAC audio
cycsound.FileType.OGG          # OGG Vorbis audio
cycsound.FileType.UNIFIED_CSD  # Csound CSD file
cycsound.FileType.STD_MIDI     # Standard MIDI file
# ... and 60+ more file type constants

# Message types
cycsound.Msg.DEFAULT   # Standard message
cycsound.Msg.ERROR     # Error message
cycsound.Msg.WARNING   # Warning message

# Message colors
cycsound.Color.FG_RED  # Foreground colors
cycsound.Color.FG_GREEN
cycsound.Color.FG_BOLD # Text attributes

Development

Setup

# Initial setup
make sync

# Rebuild after code changes
make build

# Run tests
make test          # All tests
make test-cli      # CLI tests only

Available Make Targets

Target Description
make / make build Build/rebuild the extension
make sync Initial environment setup
make test Run test suite
make wheel Build wheel (dynamic linking)
make wheel-static Build standalone wheel (macOS)
make release Build static wheels for all Python versions
make check Check distribution with twine
make publish Publish to PyPI
make publish-test Publish to TestPyPI
make clean Remove build artifacts
make help Show all targets

Static Build Requirements (macOS)

For make wheel-static, install dependencies via Homebrew:

brew install libsndfile flac opus libvorbis libogg mpg123 lame

Rationale

This project provides an alternative to the default ctypes-based wrapper. Cython offers several advantages over ctypes:

  • Better performance for tight loops
  • Direct C-level access to Csound internals
  • Ability to release the GIL for true multi-threading
  • Static linking for self-contained distribution

For more details, see this comparison of wrapping approaches.

Status

  • Wrap enough to play arbitrary CSD files
  • Build static variant as wheel (macOS only)
  • 98% coverage of csound.h API (216/219 functions)
  • GIL release for all blocking operations
  • scikit-build-core based build system
  • Context manager support
  • Test suite (36 tests: core API, CLI, channels, threading, enums)
  • CI/CD with cibuildwheel (GitHub Actions)
  • Complete wrapping of csound.h (remaining: 3 va_list callbacks)
  • Include plugin support in wheel
  • Feature parity with ctcsound.py

Related Projects

  • ctcsound - Official ctypes-based Python wrapper
  • Csound - The Csound audio synthesis system

License

See LICENSE file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

cycsound-0.1.0-cp314-cp314-macosx_15_0_arm64.whl (2.1 MB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

cycsound-0.1.0-cp313-cp313-macosx_15_0_arm64.whl (2.1 MB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

cycsound-0.1.0-cp312-cp312-macosx_15_0_arm64.whl (2.1 MB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

cycsound-0.1.0-cp311-cp311-macosx_15_0_arm64.whl (2.1 MB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

cycsound-0.1.0-cp310-cp310-macosx_15_0_arm64.whl (2.1 MB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

Details for the file cycsound-0.1.0-cp314-cp314-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for cycsound-0.1.0-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 eb203e5a2486894cbd7dffef2f706e271411f20ab5095960745d32f9f308b0ec
MD5 0c91588c91f3cd48e67995f5f08ee9e0
BLAKE2b-256 c91efbdea684dc99f18a90c5af2cb2061eaf478d4f6f148beab1c8522076a76d

See more details on using hashes here.

File details

Details for the file cycsound-0.1.0-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for cycsound-0.1.0-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 70d1fead85acf9bb975a731327c846da0f3899c5ef4a342a7a524dbbdb8e0f12
MD5 3ea7be1d755c3122f45818d403d9ccba
BLAKE2b-256 c4cbb5e14ba75af79156611944d12cce61e16686c282555d4096be9c0193ce9f

See more details on using hashes here.

File details

Details for the file cycsound-0.1.0-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for cycsound-0.1.0-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 5e17bcdbcf65d1fbc28ea5b7ce53617c0248a916d294b81f0a67b401b1c078e3
MD5 c2451b52f5e413d7fc7548afcc5b8733
BLAKE2b-256 1388ff321d15ca67fde8993e1c4fe78d237322e385b6a59f33a9fde476c94411

See more details on using hashes here.

File details

Details for the file cycsound-0.1.0-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for cycsound-0.1.0-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 795d365f4bebdf2f6fef619b2a0d6072a0a151e8b3dd74bb65a90435f489a1d0
MD5 024b44df8a33a2da77f518bdf4140f1b
BLAKE2b-256 d4fcc55436d89891a3429e0e3f66717ed42b2f163c4746a606a319eed00ffca3

See more details on using hashes here.

File details

Details for the file cycsound-0.1.0-cp310-cp310-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for cycsound-0.1.0-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 7b6b60e28fda399c4289ae49a70cb14d57a057fbb73ea1d99d337102b330c719
MD5 dd5a92f622a3ad712857220c606376e3
BLAKE2b-256 92987a7ca291aea5698c3fedec7b8678520e930817d884cd5c58a3452776d490

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page