6

I am trying to get my Raspberry Pi to read some audio input through a basic USB souncard and play it back in real time for 10 seconds, and then print the output with Matplotlib after it's finished. I am using PyAudio in callback mode. Audio recording and playback works fine in Audacity.

import pyaudio
import time
import numpy as np
from matplotlib import pyplot as plt
import scipy.signal as signal



CHANNELS = 1
RATE = 44100

p = pyaudio.PyAudio()
fulldata = np.array([])
dry_data = np.array([])

def main():
stream = p.open(format=pyaudio.paFloat32,
                channels=CHANNELS,
                rate=RATE,
                output=True,
                input=True,
                stream_callback=callback)

stream.start_stream()

while stream.is_active():
    time.sleep(10)
    stream.stop_stream()
stream.close()

numpydata = np.hstack(fulldata)
plt.plot(numpydata)
plt.title("Wet")
plt.show()


numpydata = np.hstack(dry_data)
plt.plot(numpydata)
plt.title("Dry")
plt.show()


p.terminate()

def callback(in_data, frame_count, time_info, flag):
    global b,a,fulldata,dry_data,frames 
    audio_data = np.fromstring(in_data, dtype=np.float32)
    dry_data = np.append(dry_data,audio_data)
    #do processing here
    fulldata = np.append(fulldata,audio_data)
    return (audio_data, pyaudio.paContinue)

main()

After I run my code I can see the the plotted output signal, but it only has very few samples. I can't hear anything through the speakers either while I'm playing. Running the same code on my Windows machine works fine, using the same Python and PyAudio versions. Audio Output

I have set my USB audio card as default in etc/modprobe.d/alsa-base.conf.

Any help would be much appreciated !

  • Your plot shows values from -1 to +1. When you sent them to playback, did you raise the amplitude? I don't hear anything till the amplitude gets around 100, and is good at 5000 for int16 format data. – Alan McDonley Aug 22 '16 at 12:08
4

I do not know why your audio is not working. Perhaps you need to explicitly state the output device index.

This is what I did to get audio out through the RPi audio jack.

#!/usr/bin/python
#
# tone.py   play a tone on raspberry pi 
#

import myPyLib   # get control-C handler

import time
import math
import pyaudio
from numpy import linspace,sin,pi,int16

pa = None;
s  = None;

def init_audio(rate=8000):
  global pa,s
  print "init_audio: Create PyAudio object"
  pa = pyaudio.PyAudio()
  print "init_audio: Open stream"
  s = pa.open(output=True,
            channels=1,
            rate=rate,
            format=pyaudio.paInt16,
            output_device_index=0)
  print "init_audio: audio stream initialized"

def close_audio():
  global pa,s
  print "close_audio: Closing stream"
  s.close()
  print "close_audio: Terminating PyAudio Object"
  pa.terminate()


def note(freq, len, amp=5000, rate=8000):
 t = linspace(0,len,len*rate)
 data = sin(2*pi*freq*t)*amp
 return data.astype(int16) # two byte integers

def tone(freq=440.0, tonelen=0.5, amplitude=5000, rate=8000):
  global s
  # generate sound
  tone = note(freq, tonelen, amplitude, rate)

  # play it    
  #print "tone.main(): start playing tone"
  s.write(tone)


# ##### MAIN ######
def main():
  myPyLib.set_cntl_c_handler(close_audio)  # Set CNTL-C handler 

  # open audio channel
  init_audio()

  # play tones forever    
  print "tone.main(): start playing tones"
  while True:
    print "tone.main: tone() 440"
    tone()
    time.sleep(3)
    print "tone.main: tone(261)"
    tone(261,1)
    time.sleep(3)
    print "tone.main: tone(880)"
    tone(880)
    time.sleep(3)


if __name__ == "__main__":
    main()

There is a 20 second delay opening the stream, but then there is no delay.

pi@raspberrypi:~/RWP $ ./tone.py
init_audio: Create PyAudio object
ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused

ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused

Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
init_audio: Open stream
init_audio: audio stream initialized
tone.main(): start playing tones
tone.main: tone() 440
tone.main: tone(261)
tone.main: tone(880)
tone.main: tone() 440
tone.main: tone(261)
tone.main: tone(880)
^C
** Control-C Detected
close_audio: Closing stream
close_audio: Terminating PyAudio Object
pi@raspberrypi:~/RWP $ 
  • 2
    Your answer is supposed to help the OP get their code working. – Dmitry Grigoryev Aug 22 '16 at 7:39

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.