Python SoundObject

Using Python with the External SoundObject versus the Python SoundObject
Usage

The Python SoundObject is a Python-only scripting SoundObject which uses the Jython Python interpreter (more information about this all-Java Python interpreter is available at http://www.jython.org). Jython is included with blue, and being so, it means that if you are using the Python SoundObject in blue, you do not need Python installed on your computer. The Python SoundObject is made in such a way that it in no way uses any Python installations you may or may not have on your computer, but instead only uses what is a part of Jython. If you want to add extra packages and libraries to use with the Python SoundObject, you may copy them into the lib/PythonLib directory or in the userhome/.blue/pythonLib folder. As an example of adding your own library, you can see that already included with blue in the lib/PythonLib directory is the pmask library by Maurizio Umberto Puxeddu (currently, I do not know where this library exists anymore on the internet; if you know where it can be downloaded from, please let me know and I will update this part of the documentation to better give credit to Maurizio).

So, to clarify, having a Python interpreter installed on your computer does not in any way affect the Python SoundObject within blue; they are two completely different entities. also, if you have a library installed for use with your Python interpreter installed on your computer, it will *NOT* be accessible to the Python SoundObject. to use it, you will have to copy it into the lib/PythonLib directory of blue.

Note: what you could do, also, is put the individual libraries you install in the lib/PythonLib directory as part of your PythonPATH environment variable, such that all of the libraries will be both accessible to your Python interpreter on your computer as well as the one in blue. It is not recommended to put the entire PythonLib directory as part of your PythonPath, however, as the PythonLib has many of the same files from the standard Python installation and may cause issues with locating standard library files. However, I find that just copying the library twice into both the PythonLib and my standard Python install's directory to be sufficient and easier to manage.

A Simple Example

Let's say you're writing a simple Python script:

 score = "i1 0 2 3 4 5\n"
  1. the first thing blue does is clear the

    score

    variable in the interpreter and assign the variable

    blueDuration

    the value of the duration of the SoundObject on the timeline. For this example, it does not affect the outcome of the score generated

  2. Next, blue runs the script. in this case, the only thing that happens is that the

    score

    variable is being assigned the text string of a single note, along with a newline.

  3. At the end of the script's execution, the

    score

    variable is read. the score variable may or may not have anything assigned to it, but the script within the PythonObject is still run. this allows for the possibility of code that needs to be run but doesn't necessary need to generate score text at that time. (as mentioned above in the section called “The Case for Using the Python SoundObject”)

  4. blue parses the score text, making Note objects for blue to use, applies scaling and translation of time to make the genreated notes start at the time of the PythonObject and last the subjective duration, then applies any noteProcessors.

And that's it!

A More Complex Example

For the following example, I will make a very simple score generator that produces as many notes as I give as an argument. the entire code for the script is:

 
def generateNotes(numOfNotes):
    scoreText = ""
    
    for i in range(numOfNotes):
        scoreText += "i1 " + str(i) + " 2 3 4 5\n"
    
    return scoreText

score = generateNotes(10)

The function I made,

generateNotes(numOfNotes)

, takes in the number of notes I want to generate. for the above, I wanted it to generate 10 notes, and if I printed out the above

generateNotes(10)

, I would have gotten the result:

 
i1 0 2 3 4 5 
i1 1 2 3 4 5
i1 2 2 3 4 5
i1 3 2 3 4 5
i1 4 2 3 4 5
i1 5 2 3 4 5
i1 6 2 3 4 5
i1 7 2 3 4 5
i1 8 2 3 4 5
i1 9 2 3 4 5

This text string above is returned by the

generateNotes()

function and assigned to the

score

variable. blue then grabs that text from the

score

variable and parses it and the proceeds with compiling out the .CSD file.

Note: this is a very basic example of a note generator. As you work with Python more for score generation, you'll probably either create a slew of text-handling classes that use lots of regular expressions or a more object-based system that interacts amongst itself before generating score text output. I hope to address these types of design decisions in the near future in another tutorial which will hopefully help those new to scripting and programming learn how to go about analyzing music goals in terms of programming and then designing and implementing their solutions.