h8windozeh8
Posts: 7
Joined: Tue Sep 03, 2013 9:07 pm

Pyaudio in Python 3

Tue Sep 03, 2013 9:22 pm

I have searched the forum but cant find anything specific, I am a total noob so be kind to me.
I am having trouble getting pyaudio to work in python 3.
I need it to work in Python 3 as my pi-face python code in in Python 3.
I would like to record a small sound sample to a wave file. I can record using my usb mic and arecord,
but when I record in Python 3 after installing pyaudio I get "importerror: no module name pyaudio".
Any help would be greatly appreciated.

Code: Select all

"""
PyAudio example: Record a few seconds of audio and save to a WAVE
file.
"""

import pyaudio
import wave
import sys

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

if sys.platform == 'darwin':
    CHANNELS = 1

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

print("* recording")

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pyaudio in Python 3

Wed Sep 04, 2013 5:16 pm

How did you install pyaudio? Was it following the procedure here (debian) which seems to only have distributions for x86 machines or with 'sudo apt-get install python-pyaudio'? There look to be python2 and python3 packages, does your app work ok with python2?

You might have to do something as outlined here but at the end 'sudo python3 setup.py install'. Just a guess though!
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

h8windozeh8
Posts: 7
Joined: Tue Sep 03, 2013 9:07 pm

Re: Pyaudio in Python 3

Wed Sep 04, 2013 10:53 pm

Thank you for your reply - I installed Pyaudio as per instructions Here I used all the methods on the post including the last one.

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pyaudio in Python 3

Thu Sep 05, 2013 7:17 am

Hi, just tried this and it works ok, so my comments are not now pure guesswork!

I did basically as I said in my last comment i.e. change the 'python setup.py install' to 'python3 setup.py install' this puts the pyaudio files into /usr/local/lib/python3.2/dist-packages/ which is where python3 looks for packages. If you use the first version it will install it into /usr/local/lib/python2.7/dist-packages/ Yes, I know, this is just one of those really annoying things about python!

Code: Select all

sudo apt-get install git
sudo git clone http://people.csail.mit.edu/hubert/git/pyaudio.git
sudo apt-get install libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev
sudo apt-get python3-dev
cd pyaudio
sudo python3 setup.py install
NB I already had already installed git and python3-dev so didn't actually run these lines. It's important that lines 4 and 6 use python3 rather than python

There is a similar issue if you want to install using 'pip' which you should google for if and when you run up against it.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

h8windozeh8
Posts: 7
Joined: Tue Sep 03, 2013 9:07 pm

Re: Pyaudio in Python 3

Thu Sep 05, 2013 11:31 pm

Thanks for your reply - I have followed your instructions and on line 4 I get an error:
E: Invalid operation python3-dev

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pyaudio in Python 3

Fri Sep 06, 2013 6:41 am

Sorry, as I said, I had already installed python3-dev and git so didn't actually run those lines. Obviously what I should have typed was

Code: Select all

sudo apt-get install python3-dev
at least now you will recognise the error apt-get sends you when you forget to type install!!
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

h8windozeh8
Posts: 7
Joined: Tue Sep 03, 2013 9:07 pm

Re: Pyaudio in Python 3

Sun Sep 08, 2013 3:18 am

That's it - sorry to be such a noob, working well now - thanks for all your help.

Return to “Python”