1

I'm trying to build a simple python program that will capture audio from 2 different audio inputs and then play audio from 1 of them. I bought 2 USB sound cards so I could do this.

I started with an even simpler program just to capture and play audio from one of the sound cards:

#!/usr/bin/evn python

import alsaaudio, sys
import time

channels = 1
sample_size = 1
frame_size = channels * sample_size
frame_rate = 44100
byte_rate = frame_rate * frame_size
period_size = 1024

inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL)
inp.setchannels(channels)
inp.setrate(frame_rate)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(period_size)

out = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL)
out.setchannels(channels)
out.setrate(frame_rate)
out.setformat(alsaaudio.PCM_FORMAT_S16_LE)
out.setperiodsize(period_size)

def main():
  while True:
    l, data = inp.read()
    if l:
        out.write(data)
    time.sleep(.001)
return

if __name__ == '__main__':
  print("something is happening")
main()

Originally, this worked great, but when I added a 2nd USB and pointed the output to that via 'hw:1,0' in the out = line, the sound quality nosedived to pretty much just a solid beeping/humming/staticking noise that my ears are still recovering from.

I went down some rabbit holes trying to troubleshoot this, and things have gotten worse. Now even the original program from just 1 USB outputs terrible audio. I know it's not a problem with the sound card because it's happening on either USB device and NOT when I play a local file on the RPi. I also know it's not a problem with my Aux cables.

Some of the things I've done on the way down the rabbit hole:

  • blacklisted RPi's native bcm2835 sound card
  • set aliases for the 2 USB sound cards in ./asoundrc
  • deleted Pulse Audio

But I think the most likely culprit is that when changing the ~/.asoundrc file, I deleted a default slave term that I can't remember. Right now my .asoundrc file only has the alias declarations for both USB cards... nothing else. I don't understand the master vs. slave stuff very well, so I'm not sure if a formatting error there can cause these kinds of audio problems. I'm hoping they can, since it seems like it would be an easy fix.

  • I have a feeling this is what I need to be doing with my .asoundrc file: gist.github.com/Solonarv/d28e8b50c8597778dd1e - though I'm not sure how to implement it to my program. Adding to my .asoundrc file as is and running comes up with an error that "int is not defined for inp." – carleverett Apr 17 '17 at 7:11
  • What's connected to the inputs of your USB devices while you're capturing audio? – goobering Apr 17 '17 at 7:51
  • Does the RPi's crappy USB controller even have enough bandwidth to handle three USB audio streams at the same time? – CL. Apr 17 '17 at 8:07
  • @goobering - I've been using either a phone or laptop playing music through an aux cord. – carleverett Apr 17 '17 at 14:49
  • @CL - it had enough to handle 2 streams well at one point at least. The fact that there was a sudden drop-off in quality tells me something else is wrong though. – carleverett Apr 17 '17 at 14:51

Your Answer

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

Browse other questions tagged or ask your own question.