SyntaxError: Non-ASCII character

ldompeling at casema.nl ldompeling at casema.nl
Sun Jul 17 12:27:21 EDT 2016


Op zondag 17 juli 2016 11:19:32 UTC+2 schreef ldomp... at casema.nl:
> I copy this script from the magpi but when I run this script I get this error: 
> SyntaxError: Non-ASCII character '\xe2' in file sound.py on line 32, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
> 
> Below is the eamplescript. What is wrong with this script.
> 
> 
> # 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(“test.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()

When I change input=True instead than I get a lot of errors:
Traceback (most recent call last):
  File "sound.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

I don't think we can solve those errors
Anyway thanks for helping me out so far.




More information about the Python-list mailing list