Coder Social home page Coder Social logo

gesellkammer / csoundengine Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 0.0 10.54 MB

A python library to control a csound process

License: GNU General Public License v3.0

Python 20.59% Jupyter Notebook 79.41%
csound computer-music dsp realtime-audio spectral-analysis

csoundengine's Introduction

csoundengine

sh-month

This package implements an intuitive interface to run and control a csound process

Documentation

https://csoundengine.readthedocs.io/en/latest/index.html


Introduction

The core of this package is the Engine class, which wraps a csound process and allows transparent control over all parameters, while providing sane defaults. It uses the csound API to communicate with a running csound instance. All audio processing is run in a separate performance thread.

from csoundengine import *
# create an Engine with default options for the platform.
engine = Engine()

# Define an instrument
engine.compile(r'''
  instr synth
    ; pfields of the instrument
    kmidinote = p4
    kamp = p5
    kcutoff = p6
    kdetune = p7

    kfreq = mtof:k(kmidinote)
    ; A filtered sawtooth
    asig  = vco2:a(kamp*0.7, kfreq)
    asig += vco2:a(kamp*0.7, kfreq + kdetune)
    asig = moogladder2(asig, kcutoff, 0.9)
    ; Attack / Release
    aenv = linsegr:a(0, 0.01, 1, 0.2, 0)
    asig *= aenv
    outs asig, asig
  endin
''')

# Start a synth with indefinite duration. This returns the eventid (p1)
# of the running instrument, which can be used to further control it
event = engine.sched("synth", args=[48, 0.2, 3000, 4])

# Change midinote. setp means: set p-field. This sets kmidinote (p4) to 50
engine.setp(event, 4, 50)

# Modify cutoff
engine.setp(event, 6, 1000, delay=4)

# Create a graphic interface to interact with this event. If running within a jupyter notebook
# a html gui is generated, otherwise a native gui is used:
engine.eventUI(event, p4=(0, 127), p5=(0, 1), kcutoff=(100, 5000))

Session - high level interface

Each engine can have an associated Session. A Session provides a higher level interface, allowing to:

  • Define instrument templates (an Instr), which can be instantiated at any order of evaluation, allowing to implement processing chains of any complexity
  • An Instr can have named parameters which can be used to control the event.
  • A Session provides a series of built-in Instr's to perform some common tasks, like playing samples from memory or from disk, perform audio analysis, etc.

from csoundengine import *

# Create an Engine and a corresponding Session using default options
session = Engine().session()

# create a master audio bus
masterbus = session.assignBus()

# define instruments
session.defInstr("synth", r'''
  |ibus, kmidi=60, kamp=0.1, ktransp=0, ifade=0.5|
  ; a simple sawtooth
  asig vco2 kamp, mtof:k(kmidi+ktransp)
  asig *= linsegr:a(0, ifade, 1, ifade, 0)
  ; output is routed to a bus
  busout(ibus, asig)
''')

session.defInstr("filter", r'''
  |ibus, imasterbus, kcutoff=1000, kresonance=0.9|
  asig = busin(ibus)
  asig = moogladder2(asig, kcutoff, kresonance)
  busmix(imasterbus, asig)
''')

session.defInstr("master", r'''
  imasterbus = p4
  asig = busin(imasterbus)
  asig compress2 asig, asig, -120, -40, -12, 3, 0.1, 0.01, 0.05
  outch 1, asig
''')

# Start a master instance at the end of the evaluation chain
master = session.sched("master", imasterbus=masterbus, priority=10)

# Launch some notes
for i, midinote in enumerate(range(60, 72, 2)):
    # for each synth, we create a bus to plug it to an effect, in this case a filter
    # The bus will be collected once all clients are finished
    bus = session.assignBus()

    # start time for synth and effect
    start = i * 1

    # Schedule a synth
    synth = session.sched("synth", delay=start, dur=5, kmidi=midinote, ibus=bus)

    # Automate pitch transposition so that it descends 2 semitones over the
    # duration of the event
    synth.automatep('ktransp', [0, 0, dur, -2], delay=start)

    # Schedule the filter for this synth, with a priority higher than the
    # synth, so that it is evaluated later in the chain
    filt = session.sched("filter", 
                         delay=start, 
                         dur=synth.dur, 
                         priority=synth.priority+1,
                         kcutoff=2000, 
                         kresonance=0.92, 
                         ibus=bus, 
                         imasterbus=masterbus)

    # Automate the cutoff freq. of the filter
    filt.automatep('kcutoff', [0, 2000, dur*0.8, 500, dur, 6000], delay=start) 

Installation

https://csoundengine.readthedocs.io/en/latest/Installation.html

Dependencies

pip install csoundengine

csoundengine also needs many csound plugins (https://github.com/csound-plugins/csound-plugins/releases), but these are installed automatically if needed.

Documentation

https://csoundengine.readthedocs.io


Usage in other projects

  • csoundengine is used as the audio engine in maelzel

csoundengine's People

Contributors

gesellkammer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

csoundengine's Issues

Engine().session() occasionally crashes

Hi Eduardo,

About one out of 100 times that I run csoundengine my program crashes and gives me this error:

Traceback (most recent call last):
  File "/Users/hallenj/Documents/csound/aya/aya.py", line 1922, in <module>
    main()
  File "/Users/hallenj/Documents/csound/aya/aya.py", line 1903, in main
    window = MainWindow()
             ^^^^^^^^^^^^
  File "/Users/hallenj/Documents/csound/aya/aya.py", line 63, in __init__
    self.initialize_csound()
    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hallenj/Documents/csound/aya/aya.py", line 126, in initialize_csound
    self.cs = CsoundThread()
              ^^^^^^^^^^^^^^
  File "/Users/hallenj/Documents/csound/aya/csoundthread.py", line 24, in __init__
    self.session = ce.Engine(nchnls=1, backend = "auhal", ksmps=64, buffersize=256, numbuffers=4, outdev="dac0", globalcode = half_sine_table).session()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hallenj/miniconda3/envs/csound/lib/python3.11/site-packages/csoundengine/engine.py", line 1829, in session
    self._session = Session(self.name)
                    ^^^^^^^^^^^^^^^^^^
  File "/Users/hallenj/miniconda3/envs/csound/lib/python3.11/site-packages/csoundengine/session.py", line 436, in __init__
    bucketSizes = [int(size) for size in bucketSizeCurve.map(numpriorities)]
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hallenj/miniconda3/envs/csound/lib/python3.11/site-packages/csoundengine/session.py", line 436, in <listcomp>
    bucketSizes = [int(size) for size in bucketSizeCurve.map(numpriorities)]
                   ^^^^^^^^^
ValueError: cannot convert float NaN to integer
Exiting python, closing all active engines
... stopping engine0

Any idea why this is happening?

Thanks,
Jason

Error opening necessary plugin libraries

Hello,

I was able to install csoundengine on my MacBook Pro (macOS 13.5.1) today and successfully import it in Python. However, when I create an Engine() I get the following error:

>>> from csoundengine import *
>>> engine = Engine()
Asked for backend sr, but backend pa_cb, does nothave a fixed sr. Using sr=44100

error: syntax error, unexpected T_IDENT  (token "dict_new")
 line 14:
 >>> gi__soundfontIndexes dict_new <<<
Unexpected untyped word gi__soundfontIndexes when expecting a variable
Parsing failed due to invalid input!
Stopping on parser failure
Error compiling base orchestra. A copy of the orchestra has been saved to /var/folders/lr/q0m4k0hx34q16lqv0x138d980000gn/T/csoundengine-lshfzgse.orc

When I run csound -z in the command line I don't see dict_new in the list. At the bottom of the opcodes list printout I see these warnings:

WARNING: could not open library '/Users/hallenj/Library/csound/6.0/plugins64/libjsfx.dylib' (-1)
WARNING: could not open library '/Users/hallenj/Library/csound/6.0/plugins64/libpathtools.dylib' (-1)
WARNING: could not open library '/Users/hallenj/Library/csound/6.0/plugins64/libelse.dylib' (-1)
WARNING: could not open library '/Users/hallenj/Library/csound/6.0/plugins64/libbeosc.dylib' (-1)
WARNING: could not open library '/Users/hallenj/Library/csound/6.0/plugins64/libcsound_webserver.dylib' (-1)
WARNING: could not open library '/Users/hallenj/Library/csound/6.0/plugins64/librisset.dylib' (-1)
WARNING: could not open library '/Users/hallenj/Library/csound/6.0/plugins64/libklib.dylib' (-1)
WARNING: could not open library '/Users/hallenj/Library/csound/6.0/plugins64/libsndmeta.dylib' (-1)
WARNING: could not open library '/Users/hallenj/Library/csound/6.0/plugins64/libpoly.dylib' (-1)

Csound clearly knows to look in this directory and load the libraries, but apparently it can't open them. Is this a permissions issue? Do you have any suggestions for what I could try?

Thanks!
Jason

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.