Python Sound Library

Hey, I am just starting to look into using sound in processing more seriously, and it seems that the best way is with the Processing Foundation Sound library. But I am running into an issue with it. I run my code, and it will not give me any errors or information, it will just run and not do anything. Right now my code is this:

add_library("sound")
def setup():
    global input
    global loudness
    size(1080, 1080)
    input = AudioIn(this, 0)
    input.start()
    loudness = Amplitude(this)
    loudness.input(input)
def draw():
    global input
    global loudness
    inputLevel = map(mouseY, 0, height, 1.0, 0.0);
    input.amp(inputLevel)
    volume = loudness.analyze()
    sizen = int(map(volume, 0, 0.5, 1, 350))
    circle(width/2, height/2, sizen)

But when I run it I get a circle with a size of 1 (presumably), and when I print the volume variable, it just returns a constant 0.0. If I print the sizen variable, it returns a constant 1. I don’t know what I am doing wrong or if I’m even doing anything wrong at all. All I do know is that this is running on Windows 10 and processing 3.5.4. If anyone can help that would be wonderful. If there’s any information that you need I’ll try to get it for you.

I copy-pasted this into Processing 3.5.4 for Windows and Linux – the sketch works fine for me. Are you certain your mic is working correctly? Have you tested it in another application (to see if it’s picking up any sound)?

2 Likes

I have tested it in Java mode, using the example code for the sound library and it worked exactly as I expected, so processing is picking up on the audio for sure. This code is just my copy of the code over to python to test.

Sometimes restarting Processing helps with odd things like this – like, switch to Python Mode, quit Processing, restart (Python Mode is now default) and try run the code again. There’s always the ol’ Windows turn-it-off-and-on-again thing, too …

Sorry, that’s all I can suggest.

After not running the file for a while, it just suddenly started working properly, thank you!

Actually, after more testing, it runs properly the first time, then any time after that I have to relaunch processing :frowning:

Hardly an ideal solution, but thanks @The_Math_Maniac1 for noting how you got it working.