sound library in Python Mode

edited December 2017 in Python Mode

Hi All,

Apologies right off the bat for what may be a stupid question - but I'm working in Python mode and would like to be able to use the sound library. Specifically, I have an FFT visualization script from Java mode that I'd like to be able to adapt for something I'm working on in Python mode. I'm not sure how to go about translating the code that references the library, though (I'm pasting some for example here). Are there any resources out there that might help with this? I've looked for documentation on the sound library within Python mode but haven't found anything yet. Any other suggestions? Thanks!

// Create an Input stream which is routed into the Amplitude analyzer fft = new FFT(this, bands); in = new AudioIn(this, 0);

// start the Audio Input in.start();

// patch the AudioIn fft.input(in);

Answers

  • I'm sorry I didn't see this until now. Here's the sound library's example sketch "FFTSpectrum" ported to Python Mode. I hope it helps.

    add_library('sound')
    
    scale = 5
    
    bands = 128
    
    sum = [0.0] * bands
    
    smooth_factor = 0.2
    
    def setup():
        size(640, 360)
        background(255)
    
        global device
        device = AudioDevice(this, 44000, bands)
    
        global r_width
        r_width = width / float(bands)
    
        sample = SoundFile(this, "beat.aiff")
        sample.loop()
    
        global fft
        fft = FFT(this, bands)
        fft.input(sample)
    
    def draw():
        background(125, 255, 125)
        fill(255, 0, 150)
        noStroke()
    
        fft.analyze()
    
        for i, v in enumerate(sum):
            sum[i] += (fft.spectrum[i] - v) * smooth_factor
            rect(i * r_width, height, r_width, -sum[i] * height * scale)
    
  • Hi Jonathan,

    Thanks for this, this is great and very helpful! However... I've been trying to incorporate it into something I'm working on, but I keep getting "java platform SE binary has stopped working" error messages. It doesn't happen consistently, but about 50% of the time that I run a script whenever I have imported the sound library (I never get it otherwise). I'm running windows 10, have searched for this issue but haven't found any documentation that seems to apply. I've also tried uninstalling / reinstalling Java, but that did not help. Any suggestions on how to troubleshoot this further?

    One other question - I know python mode is new, but I'm wondering if the use of the sound library within Python mode documented somewhere? I think I might not be totally understanding how the translation between the library, which I think is in Java, to the scripting, which is happening in Python, occurs. I'd be interested in understanding this more clearly if there's anything I can read about it.

    Thanks again!

  • The error is vexing, and I have no insight into what might be causing it, other than to suggest that you try to do what this rticle suggests:

    https://www.java.com/en/download/help/java_crash_video_driver.xml
    

    There's basically no Python-specific documentation for Processing libraries, unfortunately. The best I can tell you is "read the documentation for the library, then do it in Python"! The code I posted above is an example of that idea, but I don't know how to explain it any better. To make certain libraries easier to use, Python Mode does expose a special variable this, which acts just like the Java this for the Python sketch.

  • Ok, I'll check out the link, and thanks for the answer to my other question as well!

Sign In or Register to comment.