The csd Package

csd

Parses Unified Csound CSD code.

csd.get_base64(csd)

Not implemented.

csd.get_csound(csd)

Not implemented.

csd.get_license(csd)

Not implemented.

csd.get_midi_base64(csd)

Not implemented.

csd.get_options(csd)

Not implemented.

csd.get_orchestra(csd)

Returns the orchestra from a Csound CSD.

csd.get_sample_base64(csd)

Not implemented.

csd.get_score(csd)

Returns the score from a Csound CSD.

csd.get_version(csd)

Not implemented.

csd.overwrite_score(csd, sco)

Returns a new csd string with a new score.

sco

These methods operate on multiple score events at a time. Generally speaking, these methods specifically deal with columns, where csd.sco.event deals with rows.

csd.sco.map_(score, pattern, pfield_index_list, pfunction, *args)

Performs a pfunction on multiple pfields in a score, returning a new score.

Example:

>>> def add(x, y):
...     return x + y
... 
>>> sco.map_("""
... i 1 0 4 1.0 440
... i 1 4 4 0.5 880""", {0: 'i'}, 5, add, 777)
'\ni 1 0 4 1.0 1217\ni 1 4 4 0.5 1657'

Example using lambda function:

>>> sco.map_("""
... i 1 0 4 1.0 440
... i 1 4 4 0.5 880""", {0: 'i'}, 5, lambda x: x * 3)
'\ni 1 0 4 1.0 1320\ni 1 4 4 0.5 2640'

See pattern, pfield_index_list, pfunction, score

csd.sco.merge(score, selection)

Merges a selection back into a score string, overwriting the existing parts of the score.

The follow example merges a score string with a selection, replacing the second line (index 1) with a new event.

Example:

>>> sco.merge(
... """i 1 0 4 1.0 440
... i 1 4 4 0.5 880""",
... {1: 'i 1 4 4 0.5 1138'})
'i 1 0 4 1.0 440\ni 1 4 4 0.5 1138'

See selection, score

csd.sco.select(score, pattern)

Returns a selection from a score.

Example:

>>> sco.select(
... """f 1 0 8192 10 1
... i 1 0 4 1.0 440
... i 1 4 4 0.5 880"""
... , {0: 'i'})
{1: 'i 1 0 4 1.0 440', 2: 'i 1 4 4 0.5 880'}

See pattern, score, selection

csd.sco.select_all(score)

Returns a selection of all events in a score.

Example:

>>> sco.select_all(
... """f 1 0 8192 10 1
... i 1 0 4 1.0 440
... i 1 4 4 0.5 880""")
{0: 'f 1 0 8192 10 1', 1: 'i 1 0 4 1.0 440', 2: 'i 1 4 4 0.5 880'}

See score, selection

element

Module for handling Csound score elements.

csd.sco.element.is_valid(element)

Returns a boolean value indicating if the element if valid.

An example of an invalid element is a compound element, consiting of two or more elements. Continuous space is valid.

Example:

>>> element.is_valid('440')
True
>>> element.is_valid(' ')
True
>>> element.is_valid('i 1')
False

See element

csd.sco.element.is_valid_pfield(element)

Returns a boolean value indicating if element is a pfield data type.

Example:

>>> element.is_valid_pfield('440')
True
>>> element.is_valid_pfield(' ')
False
>>> element.is_valid_pfield('i 1')
False

See element

csd.sco.element.str_to_numeric(numeric)

Converts a str to numeric type int or float.

Example:

>>> element.str_to_numeric('440')
440
>>> element.str_to_numeric('440.0')
440.0

See numeric

csd.sco.element.token_type(element)

Returns the Csound score token type of an element.

Example:

>>> score.token_type('[~ * 440 + 440]') == score.EXPRESSION
True
>>> score.token_type('i') == score.EXPRESSION
False

Note

The mechanisms handling tokens will change in the future. For the mean time, tokens are treated as faux- enums, and should be compared directly with the token constants.

See element

event

This module operates on individual Csound score events.

csd.sco.event.get(event, pfield_index)

Returns a pfield element for an event.

The pfield maybe a number, string, expression, or a statement. Comments are right out.

Example:

>>> event.get('i 1 0 4 1.0 440  ; A440', 5)
'440'

See event, pfield

csd.sco.event.get_pfield_list(event)

Returns a list of all the pfield elements in a list.

Example:

>>> event.get_pfield_list('i 1 0 4 1.0 440  ; A440')
['i', '1', '0', '4', '1.0', '440']

See event

csd.sco.event.get_trailing_comment(event)

Returns a string of a trailing inline comment for an event.

Returns an empty string if no trailing comment is found.

Example:

>>> event.get_trailing_comment('i1 0 4 1.0 440  ; comment')
'; comment'

See event

csd.sco.event.insert(event, pfield_index, fill='.')

Returns a new event with an inserted pfield element.

The fill attribute is the character or string which will be inserted into the event.

Example:

>>> event.insert('i 1 0 4 1.0 440  ; A440', 5, '1138')
'i 1 0 4 1.0 1138 440  ; A440'

See event, pfield

csd.sco.event.match(event, pattern)

Returns a boolean determined whether an event matches the requirements of a pattern.

Example:

>>> event.match('i 1 0 4 1.0 440', {0: 'i'})
True
>>> event.match('i 1 0 4 1.0 440', {0: 'i', 1: 1})
True
>>> event.match('i 1 0 4 1.0 440', {0: 'i', 1: 2})
False

See event, pattern.

csd.sco.event.number_of_pfields(event)

Returns an int of the number of pfields that exists in a given event.

The statement (pfield 0) is also counted. Comments and whitespace are omitted from the tally. The following examples counts ‘i’, ‘1’, ‘0’, ‘4’, ‘1.0’ and ‘440’, and does not include ‘; A440’ as part of the returned figure:

>>> event.number_of_pfields('i 1 0 4 1.0 440  ; A440')
6

See event

csd.sco.event.pop(event)

Removes the last pfield element from an event and returns a tuple containing a new event and the removed element.

This function preserves whitespace.

Example:

>>> event.pop('i 1 0 4 1.0 440  ; A440')
('i 1 0 4 1.0   ; A440', '440')

See event

csd.sco.event.push(event, fill='.')

Appends a pfield element to the last pfield and returns the new event.

The fill attribute is the character or string which will be inserted into the event.

Example:

>>> event.push('i 1 0 4 1.0 440  ; A440', '1138')
'i 1 0 4 1.0 440 1138  ; A440'

See event

csd.sco.event.remove(event, pfield_index)

Removes a pfield and returns a tuple containing the new event and the removed element.

This function preserves whitespace.

Example:

>>> event.remove('i 1 0 4 1.0 440  ; A440', 4)
('i 1 0 4  440  ; A440', '1.0')

See event, pfield_index

csd.sco.event.sanitize(event)

Returns a copy of the event with extra whitespace and comments removed.

A single whitespace separates each pfield in the return.

Example:

>>> event.sanitize('i1 0 4    1.0 440  ; A440')
'i 1 0 4 1.0 440'

See event

csd.sco.event.set(event, pfield_index, value)

Returns a new event string with the specified pfield set with the new value.

Example:

>>> event.set('i 1 0 4 1.0 440  ; A440', 5, 1138)
'i 1 0 4 1.0 1138  ; A440'

See event, pfield_index

csd.sco.event.split(event)

Returns a list that includes all event pfield and comment elements.

Example:

>>> event.split('i 1 0 4 1.0 440  ; A440')
['i', '1', '0', '4', '1.0', '440', '; A440']

See event

csd.sco.event.statement_spacer(event, spacer=1)

Returns a new string with the whitespace between a statement and the following element altered.

The spacer attribute is the number of whitespace characters to place between the statement and the following pfield.

Example:

>>> event.statement_spacer('i1 0 4 1.0 440')
'i 1 0 4 1.0 440'

See event

csd.sco.event.swap(event, pfield_index_a, pfield_index_b)

Returns a new event string after swapping two pfield elements.

Example:

>>> event.swap('i 1 0 4 1.0 440 ; A440', 4, 5)
'i 1 0 4 440 1.0 ; A440'

Note

This currently will not swap pfield 0. Not sure if it should, though throwing an error might be in order.

See event, pfield_index

csd.sco.event.tokenize(event)

Returns a list of all elements in an event.

Elements include pfield data, comments and whitespace.

Example:

>>> event.tokenize('i 1 0 4 1.0 440  ; A440')
['i', ' ', '1', ' ', '0', ' ', '4', ' ', '1.0', ' ', '440', '  ', '; A440']

Note

This function will attempt to tokenize invalid elements. Be sure that the event you provide is syntactically correct. Though this module should provide an is_valid() that does this for you.

See event

preprocessor

Preprocessor functions.

csd.sco.preprocessor.carry_to_value(score)

Not implemented.

csd.sco.preprocessor.value_to_carry(score)

Replaces subsequent repeated values with a carry (.)

Identical expressions do no carry, as a carry only copies the first value output from an expression. This breaks the form when multiple random evaluations are part of the score.

Macros do no carry as they may contain expressions.

No-carries are not carried.

selection

These methods operate on selections.

csd.sco.selection.operate_numeric(selection, pfield_index_list, pfunction, *args)

Processes a matrix of pfields and events using the supplied pfunction and any optional arguments.

In cases where the original numeric pfield was an int, but processed with floats, the int will be output as a float in the score, even if the output contains no fractional parts.

Example:

>>> def multiply(pf, m): return pf * m
... 
>>> sco.operate_numeric({0: 'i 1 0 4 1.0 440', 1: 'i 1 4 4 0.5 880'},
...                     5, multiply, 3)
{0: 'i 1 0 4 1.0 1320', 1: 'i 1 4 4 0.5 2640'}

A lambda function can specified as the pfunction argument:

# Invert pfield
operate_numeric(score, pf, lambda x: 1.0 / x)

See pfield_index_list, pfunction, selection

csd.sco.selection.replace(selection, pfield_index_list, pgenerator, *args)

Replaces pfield values in selection using a supplied pgenerator, function or method.

This will overwrite and existing value, numeric or not, in a pfield, including elements such as carry statements and expressions.

Use this function instead of operate_numeric() when you want to create new data, instead of altering existing pfield data. This works wells with python objects that have a persistant state.

Example:

>>> def return_something(x):
...     return x
... 
>>> selection.replace({0: 'i 1 0 4 1.0 440'}, 5, return_something, '$foo')
{0: 'i 1 0 4 1.0 $foo'}

See pfield_index_list, pgenerator, selection

csd.sco.selection.swap(selection, pfield_index_a, pfield_index_b)

Returns a copy of selection with swapped pfield columns.

Example:

>>> selection.swap({0: 'i 1 0 4 440 1.0', 1: 'i 1 4 4 880 0.5'}, 4, 5)
{0: 'i 1 0 4 1.0 440', 1: 'i 1 4 4 0.5 880'}

See selection, pfield_index