Play a sound? (python mode)

edited February 2018 in Python Mode

Wrapping up the semester in intro programming, students are happily using processing's python mode. Trying to figure out how to let them play sounds in python mode. I've tried a few things to no avail, but I'm hoping someone here can just make it easy for me and post a snippet of python mode that plays a sound from a sound file. Thanks, Albert


  • edited April 2016
    • Most examples you're gonna find is in standard Java Mode.
    • Therefore knowing how to convert them to Python Mode is very important.
    • However I've found out there are 2 examples in Python Mode relying on Beads library.
    • After installing that contributed library, now under Python Mode, hit CTRL+SHIFT+O.
    • Then follow "Contributed Libraries in Python" -> "beads". Those 2 examples are there.
  • edited April 2016

    Thanks, didn't really know about the whole library system. Beads was way more than I was looking for, but the "sound" library from the processing folks is great. After adding the sound library using the "Sketch -> Import Library... -> Add Library..." dialog (I did this in Java mode, not sure if that matters), the following little snippet works just fine in Python mode (provide your own sound file and put it in the data folder of the sketch):

    def setup():
        global sf
        # Load sound file, you provide, put in data/
        sf = SoundFile(this,"pop.wav")
    def draw():
    def mouseClicked():
        # Play sound when mouse clicked on canvas.
  • (I did this in Java mode, not sure if that matters),

    It should not! But who cares? :)) Glad it's finally worked for ya! :-bd

  • On a side note, I'm curious about the "this" argument in the SoundFile() function. What's it doing? It has to be there, but I don't know why.

  • edited April 2016
    • this is a Java keyword which holds the current instance reference of the class it's scoped written:
    • In Processing, when this is outside any class, it refers to the sketch's PApplet instance.
    • Of course Python doesn't have any keyword called this.
    • What happens is that the author of Python Mode created a system global variable called this.
    • Very similar to Java Mode, it also holds the reference for the sketch's instance.
    • But since it's a global variable and not a keyword, even inside classes, it still represents the sketch.
    • In Java this changes its datatype according to the class it belongs to.
  • edited April 2016

    What's it doing? It has to be there, but I don't know why.

    • Many foreign top classes, which belong to libraries we import, need to access our sketch's canvas and its other states.
    • That's why most of them request our sketch's reference to be passed to their constructor.
    • In the particular case for SoundFile class, it's logical it doesn't need to access the canvas.
    • Yet it needs to hook itself up to the sketch's dispose() method via registerMethod().
    • So when sketch closes, SoundFile can adequately shutdown the sound hw resources it's grabbed.
Sign In or Register to comment.