Move towards python 3

This commit is contained in:
Lars Immisch
2015-05-08 11:26:39 +02:00
parent 08b80110b0
commit 7b9d9828e6
6 changed files with 26 additions and 29 deletions

View File

@@ -4,8 +4,10 @@
>>> change(880)
'''
from __future__ import print_function
from threading import Thread
from Queue import Queue, Empty
from queue import Queue, Empty
from math import pi, sin
import struct
import alsaaudio
@@ -23,7 +25,7 @@ def digitize(s):
def generate(frequency):
# generate a buffer with a sine wave of frequency
size = int(sampling_rate / frequency)
buffer = ''
buffer = bytes()
for i in range(size):
buffer = buffer + digitize(sin(i/(2 * pi)))
@@ -55,7 +57,7 @@ class SinePlayer(Thread):
factor = 1
buf = generate(frequency) * factor
print 'factor: %d, frames: %d' % (factor, len(buf) / framesize)
print('factor: %d, frames: %d' % (factor, len(buf) / framesize))
self.queue.put( buf)
@@ -64,7 +66,7 @@ class SinePlayer(Thread):
while True:
try:
buffer = self.queue.get(False)
self.device.setperiodsize(len(buffer) / framesize)
self.device.setperiodsize(int(len(buffer) / framesize))
self.device.write(buffer)
except Empty:
if buffer:

View File

@@ -17,6 +17,7 @@
## python mixertest.py Master 0,[un]mute # [un]mute channel 0
## python mixertest.py Capture 0,[un]rec # [dis/en]able capture on channel 0
from __future__ import print_function
import sys
import getopt
@@ -25,19 +26,19 @@ import alsaaudio
def list_mixers(kwargs):
print("Available mixer controls:")
for m in alsaaudio.mixers(**kwargs):
print(" '%s'\n" % m)
print(" '%s'" % m)
def show_mixer(name, kwargs):
# Demonstrates how mixer settings are queried.
try:
mixer = alsaaudio.Mixer(name, **kwargs)
except alsaaudio.ALSAAudioError:
sys.stderr.write("No such mixer\n")
print("No such mixer", file=sys.stderr)
sys.exit(1)
print("Mixer name: '%s'" % mixer.mixer())
print("Capabilities: %s %s" % (' '.join(mixer.volumecap()),
' '.join(mixer.switchcap())))
' '.join(mixer.switchcap())))
volumes = mixer.getvolume()
for i in range(len(volumes)):
print("Channel %i volume: %i%%" % (i,volumes[i]))
@@ -65,7 +66,7 @@ def set_mixer(name, args, kwargs):
try:
mixer = alsaaudio.Mixer(name, **kwargs)
except alsaaudio.ALSAAudioError:
sys.stderr.write("No such mixer")
print("No such mixer", file=sys.stderr)
sys.exit(1)
if args.find(',') != -1:
@@ -96,8 +97,8 @@ def set_mixer(name, args, kwargs):
mixer.setvolume(volume, channel)
def usage():
sys.stderr.write('usage: mixertest.py [-c <card>] ' \
'[ <control>[,<value>]]\n')
print('usage: mixertest.py [-c <card>] [ <control>[,<value>]]',
file=sys.stderr)
sys.exit(2)
if __name__ == '__main__':

View File

@@ -13,9 +13,7 @@
## python playbacktest.py out.raw
# Footnote: I'd normally use print instead of sys.std(out|err).write,
# but we're in the middle of the conversion between python 2 and 3
# and this code runs on both versions without conversion
from __future__ import print_function
import sys
import time
@@ -23,7 +21,7 @@ import getopt
import alsaaudio
def usage():
sys.stderr.write('usage: playbacktest.py [-c <card>] <file>\n')
print('usage: playbacktest.py [-c <card>] <file>', file=sys.stderr)
sys.exit(2)
if __name__ == '__main__':

View File

@@ -2,9 +2,7 @@
# Simple test script that plays (some) wav files
# Footnote: I'd normally use print instead of sys.std(out|err).write,
# but this version runs on python 2 and python 3 without conversion
from __future__ import print_function
import sys
import wave
@@ -13,9 +11,8 @@ import alsaaudio
def play(device, f):
sys.stdout.write('%d channels, %d sampling rate\n' % (f.getnchannels(),
f.getframerate()))
print('%d channels, %d sampling rate\n' % (f.getnchannels(),
f.getframerate()))
# Set attributes
device.setchannels(f.getnchannels())
device.setrate(f.getframerate())
@@ -43,7 +40,7 @@ def play(device, f):
def usage():
sys.stderr.write('usage: playwav.py [-c <card>] <file>\n')
print('usage: playwav.py [-c <card>] <file>', file=sys.stderr)
sys.exit(2)
if __name__ == '__main__':

View File

@@ -12,10 +12,9 @@
## python recordtest.py out.raw # talk to the microphone
## aplay -r 8000 -f S16_LE -c 1 out.raw
#!/usr/bin/env python
# Footnote: I'd normally use print instead of sys.std(out|err).write,
# but we're in the middle of the conversion between python 2 and 3
# and this code runs on both versions without conversion
from __future__ import print_function
import sys
import time
@@ -23,7 +22,7 @@ import getopt
import alsaaudio
def usage():
sys.stderr.write('usage: recordtest.py [-c <card>] <file>\n')
print('usage: recordtest.py [-c <card>] <file>', file=sys.stderr)
sys.exit(2)
if __name__ == '__main__':

View File

@@ -1,11 +1,11 @@
#!/usr/bin/env python
# These are internal test. They shouldn't fail, but they don't cover all
# of the ALSA API. Most of all PCM.read and PCM.write are missing.
# These are internal tests. They shouldn't fail, but they don't cover all
# of the ALSA API. Most importantly PCM.read and PCM.write are missing.
# These need to be tested by playbacktest.py and recordtest.py
# In case of a problem, run these tests. If they fail, file a bug report on
# http://sourceforge.net/projects/pyalsaaudio
# http://github.com/larsimmisch/pyalsaaudio/issues
import unittest
import alsaaudio