0

I am using the latest Raspbian I just downloaded this morning.

I bought this USB audio adapter, https://www.sossolutions.nl/usb-audio-adapter-works-with-raspberry-pi. And then I followed tutorial here, https://learn.adafruit.com/usb-audio-cards-with-a-raspberry-pi/instructions.

The tutorial basically just to add ~/.asoundrc into this.

pcm.!default {
    type hw card 1    
}
ctl.!default {
    type hw card 1
}

I checked alsamixer, it detects both my microphone and earphone. I use a phone earphone that I usually use to answer call and listen to music with my phone. I attached this mic-earphone into the mic slot of the USB audio adapter.

Then I change sudo nano /usr/share/alsa/alsa.conf lines into these.

defaults.ctl.card 1
defaults.pcm.card 1

However, changing sudo nano /usr/share/alsa/alsa.conf does not listed as something to do in the Adafruit tutorial I mentioned before.

I have a code to detect pitch and volume with PyAudio from microphone. I tried to run that, but it gives me error of.

OSError: [Errrno -9996] Invalid input device (no default output device)

The error is in this line.

self.mic = pA.open(format=self.FORMAT, channels=self.CHANNELS, rate=self.SAMPLE_RATE, input=True,
    frames_per_buffer=self.PERIOD_SIZE_IN_FRAME)

My goal is to have pitch and volume detection using PyAudio via microphone. How can I solve this problem?

EDIT - 1, I have checked via https*//www*onlinemictest*com, my microphone does not work. But it is detected in alsamixer as "mic".

1

I'm updating this answer to include the discussion from the comments and flow more like a troubleshooting guide for future readers.

  1. Make sure your USB device is compatible with your microphone. Some devices might not work with 2-in-1 headset/microphones. To eliminate this source of potential problems, use a dedicated micrphone.

  2. Make sure these lines are in /etc/asound.conf:

    pcm.!default {
        type hw card 1    
    }
    ctl.!default {
        type hw card 1
    }
    
  3. Use the tutorial's method to test the speaker output and microphone input using software which is known to be good.

    You can use this to test microphone input:

    arecord --device=hw:1,0 --format S16_LE --rate 44100 -c1 test.wav
    

    And do this to test speaker output:

    speaker-test -c2 --test=wav -w /usr/share/sounds/alsa/Front_Center.wav
    

    Then try playing the sound you recorded a moment ago:

    aplay --device=plughw:1,0 test.wav
    
  4. Assuming those work, run this python script to check which index number you should be using (source: https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=71062):

    import pyaudio
    p = pyaudio.PyAudio()
    for i in range(p.get_device_count()):
        dev = p.get_device_info_by_index(i)
        print((i,dev['name'],dev['maxInputChannels']))
    
  5. Then try this snippet of python code to open the microphone:

    self.mic = pA.open(
        format = self.FORMAT,
        channels = self.CHANNELS, 
        rate = self.SAMPLE_RATE,
        frames_per_buffer = self.PERIOD_SIZE_IN_FRAME,
        input_device_index = self.INPUT_DEVICE_INDEX,
        input = True)
    

    Remember define self.INPUT_DEVICE_INDEX to be whatever number you discovered when running the previous snippet of python code, or just put the number in its place.

  6. If you run into overflow problems and don't want to use exception handling (although it's probably best if you do) you can set exception_on_overflow = False in your function call to read the audio data:

    data = stream.read(chunk, exception_on_overflow = False)
    

Best of luck!

  • My /etc/asound.conf and ~/.asoundrc are the same. – notalentgeek Jan 4 '17 at 15:09
  • Okay, have you tested your configuration using arecord specifically? You introduce other potential complications by using a web site to test your microphone. the arecord tool is the one indicated by the tutorial, so it's the best one to use to test your ability to record. Then use speaker-test as described in my answer to test that it plays. We're trying to use process of elimination to determine the failure. – FlippingBinary Jan 4 '17 at 15:13
  • My /etc/asound.conf and ~/.asoundrc are the same. speaker-test -c2 is fine, I can listen through the USB earphone. arecord --device=hw:1,0 --format S16_LE --rate 44100 -c1 test.wav has no error and it generates test.wav. But, speaker-test -c2 --test=wav -w test.wav returns Sample rate does not match (44100) for /home/pi/test.wav – notalentgeek Jan 4 '17 at 15:16
  • the first comment was incomplete, I was not aware I cannot do line break. – notalentgeek Jan 4 '17 at 15:17
  • I'm sorry, playback command from tutorial is aplay --device=plughw:1,0 test.wav I'll update my answer. – FlippingBinary Jan 4 '17 at 15:23
0

Configure your .asoundrc to

pcm.!default {
  type asym
  capture.pcm "mic"
  playback.pcm "speaker"
}
pcm.mic {
  type plug
  slave {
    pcm "hw:<card number>,<device number>"
  }
}
pcm.speaker {
  type plug
  slave {
    pcm "hw:<card number>,<device number>"
  }
}

You may want to refer to this:

https://developers.google.com/assistant/sdk/prototype/getting-started-pi-python/configure-audio

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.