diff --git a/isine.py b/isine.py index f71611e..10a63b7 100644 --- a/isine.py +++ b/isine.py @@ -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: diff --git a/mixertest.py b/mixertest.py index 28fe958..553ad24 100755 --- a/mixertest.py +++ b/mixertest.py @@ -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 ] ' \ - '[ [,]]\n') + print('usage: mixertest.py [-c ] [ [,]]', + file=sys.stderr) sys.exit(2) if __name__ == '__main__': diff --git a/playbacktest.py b/playbacktest.py index 22cec26..8d1f012 100755 --- a/playbacktest.py +++ b/playbacktest.py @@ -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 ] \n') + print('usage: playbacktest.py [-c ] ', file=sys.stderr) sys.exit(2) if __name__ == '__main__': diff --git a/playwav.py b/playwav.py index bbec930..b456494 100755 --- a/playwav.py +++ b/playwav.py @@ -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 ] \n') + print('usage: playwav.py [-c ] ', file=sys.stderr) sys.exit(2) if __name__ == '__main__': diff --git a/recordtest.py b/recordtest.py index 3967860..e12d6cf 100755 --- a/recordtest.py +++ b/recordtest.py @@ -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 ] \n') + print('usage: recordtest.py [-c ] ', file=sys.stderr) sys.exit(2) if __name__ == '__main__': diff --git a/test.py b/test.py index bc8f19a..48cfcf4 100755 --- a/test.py +++ b/test.py @@ -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