input
Posts: 132
Joined: Sun Nov 02, 2014 9:18 am

raspberry pi magazine pythonscript

Wed Jul 27, 2016 9:24 am

I copy this script from a raspberry pi magazine 2015.
But I found two typo's in it.One: input=TRUE must be input=True: Second:while data != ‘’: Must be while data != '':
Third: what is rb meaning: wf = wave.open(“filename.wav”, “rb”)

Can somone help me out with this script.

Code: Select all

# You need to import the pyaudio module
import pyaudio
# First, we will listen
# We need to set some parameters
# Buffer chunk size in bytes
CHUNK = 1024
# The audio format
FORMAT = pyaudio.paInt16
# The number of channels to record on
CHANNELS = 2
# The sample rate, 44.1KHz
RATE = 44100
# The number of seconds to record for
RECORD_SECS = 5
# Next, we create a PyAudio object
p = pyaudio.PyAudio()
# We need a stream to record from
stream = p.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=TRUE, frames_per_buffer=CHUNK)
# We can now record into a temporary buffer
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECS)):
	data = stream.read(CHUNK)
	frames.append(data)
# We can now shut everything down
stream.stop_stream()
stream.close()
p.terminate()
# If we want to play a wave file, we will need the wave module
import wave
# We can open it, give a filename
wf = wave.open(“filename.wav”, “rb”)
# We need a new PyAudio object
p = pyaudio.PyAudio()
# We will open a stream, using the settings from the wave file
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(), rate=wf.getframerate(),
output=True)
# We can now read from the file and play it out
data = wf.readframes(CHUNK)
while data != ‘’:
	stream.write(data)
	data = wf.readframes(CHUNK)
# Don’t forget to shut everything down again
stream.stop_stream()
stream.close()
p.terminate()








User avatar
davidcoton
Posts: 3653
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: raspberry pi magazine pythonscript

Wed Jul 27, 2016 9:53 am

I think your two corrections are correct. TRUE may have worked on some versions, but AFAICT True is better.
The quotation marks are a common typesetting problem -- the software thinks it is correcting a human error, without understanding that in the context of code the correction of '' to `' is wrong.
The comment tells you exactly what the wave.open line does. You will need to have a file "filename.wav" for it to read. "rb" means read only, binary.

Does it run with your two corrections?
Signature retired

input
Posts: 132
Joined: Sun Nov 02, 2014 9:18 am

Re: raspberry pi magazine pythonscript

Wed Jul 27, 2016 10:14 am

davidcoton wrote:I think your two corrections are correct. TRUE may have worked on some versions, but AFAICT True is better.
The quotation marks are a common typesetting problem -- the software thinks it is correcting a human error, without understanding that in the context of code the correction of '' to `' is wrong.
The comment tells you exactly what the wave.open line does. You will need to have a file "filename.wav" for it to read. "rb" means read only, binary.

Does it run with your two corrections?
Thanks for the reply. But where did you find "to ' is wrong.

input
Posts: 132
Joined: Sun Nov 02, 2014 9:18 am

Re: raspberry pi magazine pythonscript

Wed Jul 27, 2016 10:31 am

No it is notworking:

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

connect(2) call to /tmp/jack-0/default/jack_0 failed (err=No such file or directory)
attempt to connect to server failed
Expression 'parameters->channelCount <= maxChans' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1514
Expression 'ValidateParameters( inputParameters, hostApi, StreamDirection_In )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2818
Traceback (most recent call last):
File "mag01.py", line 19, in <module>
rate=RATE, input=True, frames_per_buffer=CHUNK)
File "/usr/lib/python3/dist-packages/pyaudio.py", line 747, in open
stream = Stream(self, *args, **kwargs)
File "/usr/lib/python3/dist-packages/pyaudio.py", line 442, in __init__
self._stream = pa.open(**arguments)
OSError: [Errno Invalid number of channels] -9998

User avatar
davidcoton
Posts: 3653
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: raspberry pi magazine pythonscript

Wed Jul 27, 2016 11:12 am

Knowing about the quotation marks is just experience.

Now the next error -- there is an extra line break in the middle of the "stream =" command. The line should not end until the ")"
There's another one the same further on.
Signature retired

input
Posts: 132
Joined: Sun Nov 02, 2014 9:18 am

Re: raspberry pi magazine pythonscript

Wed Jul 27, 2016 6:31 pm

I thought that my thread was deleted by the moderator.
But anyway I am step further thanks to the idle off python.
The script is running now, but only I get the message that the jackserver could not be found.
So I have to google on that.

Thanks to all that helpt me so far.

This is the error:
connect(2) call to /tmp/jack-0/default/jack_0 failed (err=No such file or directory)
attempt to connect to server failed

Return to “The MagPi”