Using Sound Input From Mic

Hi. I am trying to create a drawing tool that then takes the input from the microphone and applies a visual effect to the drawing in relation to the amplitude of the input.

However I can’t get the microphone to work. I am using processing.py 3 and the Sound library. I am opening with this code but getting 0.0 response in the console when I am playing music…

add_library('sound')

mic = None
amp = None

def setup():
  global mic, amp
  size(440, 440)
  background(0)
  
  mic = AudioIn(this, 2)
  mic.start()
  
  amp = Amplitude(this)
  amp.input(mic)

def draw():
    print(amp.analyze())

Is anyone able to point out where I am going wrong here? Or could you please point me in the right direction for some processing.py sound input examples? Thank you!! :slight_smile:
Joe

2 Likes

Welcome, @joebrailsford!

Your code works fine on my system.

Have you tried different channel arguments? Like:
mic = AudioIn(this, 0)
or
mic = AudioIn(this, 1)

What output do you get if you run this line?
print(Sound.list())

1 Like

This is my output with the list and sound on. It seems to be recognising something. But when I try to run the example code from the Getting Started with Processing.py book, I get nada.

device id 0: Default Audio Device
max inputs : 1 (default)
max outputs: 2 (default)
device id 1: BenQ EW3270U
max inputs : 0
max outputs: 2
device id 2: Bose USB Audio
max inputs : 0
max outputs: 6
device id 3: External Headphones
max inputs : 0
max outputs: 2
device id 4: MacBook Pro Microphone
max inputs : 1
max outputs: 0
device id 5: MacBook Pro Speakers
max inputs : 0
max outputs: 2
device id 6: BoomAudio
max inputs : 2
max outputs: 2
device id 7: Port BenQ EW3270U
max inputs : 0
max outputs: 0
device id 8: Port Bose USB Audio
max inputs : 0
max outputs: 0
device id 9: Port External Headphones
max inputs : 0
max outputs: 0
device id 10: Port MacBook Pro Microphone
max inputs : 0
max outputs: 0
device id 11: Port MacBook Pro Speakers
max inputs : 0
max outputs: 0
device id 12: Port BoomAudio
max inputs : 0
max outputs: 0

Code example from the book that isn’t picking up the mic…

add_library('sound')

mic = None
amp = None

def setup():
  global mic, amp
  size(440, 440)
  background(0)
  # Create an audio input and start it
  mic = AudioIn(this, 0)
  mic.start()
  # Create a new amplitude analyzer and patch into input
  amp = Amplitude(this)
  amp.input(mic)

def draw():
  # Draw a background that fades to black
  noStroke()
  fill(26, 76, 102, 10)
  rect(0, 0, width, height)
  # The analyze() method returns values between 0 and 1, 
  # so map() is used to convert the values to larger numbers
  diameter = map(amp.analyze(), 0, 1, 10, width)
  # Draw the circle based on the volume
  fill(255)
  ellipse(width/2, height/2, diameter, diameter)

And thanks! Great to be here :slight_smile: I’m excited to embark on this audio visual journey!

Hmmm sadly that didn’t make the circles start dancing…

Okay – this worked fine on my Linux machine.

However, on Windows, it wouldn’t work initially. I removed the channel argument to default to zero – AudioIn(this) – then saved the sketch. I restarted Processing, then loaded the sketch and it worked fine.

Maybe you could try this, too.

2 Likes

Hey.

I have tried closing, saving, changing input, reverting to default and nada…

Very strange.

Okay –

I tried this on a MacBook running High Sierra; it worked fine. Then I tried an iMac running Catalina, and it didn’t work. Are you running Catalina?

After a quick search, I found this:

1 Like