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.