diff --git a/isine.py b/isine.py index e5eb702..782c789 100644 --- a/isine.py +++ b/isine.py @@ -56,10 +56,7 @@ class SinePlayer(Thread): def __init__(self, frequency = 440.0): Thread.__init__(self) self.setDaemon(True) - self.device = alsaaudio.PCM() - self.device.setchannels(channels) - self.device.setformat(format) - self.device.setrate(sampling_rate) + self.device = alsaaudio.PCM(channels=channels, format=format, rate=sampling_rate) self.queue = Queue() self.change(frequency) diff --git a/playbacktest.py b/playbacktest.py index 592ee92..874415b 100755 --- a/playbacktest.py +++ b/playbacktest.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 +# -*- mode: python; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- ## playbacktest.py ## @@ -38,18 +39,11 @@ if __name__ == '__main__': f = open(args[0], 'rb') - # Open the device in playback mode. - out = alsaaudio.PCM(alsaaudio.PCM_PLAYBACK, device=device) - - # Set attributes: Mono, 44100 Hz, 16 bit little endian frames - out.setchannels(1) - out.setrate(44100) - out.setformat(alsaaudio.PCM_FORMAT_S16_LE) - + # Open the device in playback mode in Mono, 44100 Hz, 16 bit little endian frames # The period size controls the internal number of frames per period. # The significance of this parameter is documented in the ALSA api. - out.setperiodsize(160) + out = alsaaudio.PCM(alsaaudio.PCM_PLAYBACK, channels=1, rate=44100, format=alsaaudio.PCM_FORMAT_S16_LE, periodsize=160, device=device) # Read data from stdin data = f.read(320) while data: diff --git a/playwav.py b/playwav.py index 5567440..ffa778d 100755 --- a/playwav.py +++ b/playwav.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 +# -*- mode: python; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- # Simple test script that plays (some) wav files @@ -9,57 +10,52 @@ import wave import getopt import alsaaudio -def play(device, f): +def play(device, f): - print('%d channels, %d sampling rate\n' % (f.getnchannels(), - f.getframerate())) - # Set attributes - device.setchannels(f.getnchannels()) - device.setrate(f.getframerate()) + format = None - # 8bit is unsigned in wav files - if f.getsampwidth() == 1: - device.setformat(alsaaudio.PCM_FORMAT_U8) - # Otherwise we assume signed data, little endian - elif f.getsampwidth() == 2: - device.setformat(alsaaudio.PCM_FORMAT_S16_LE) - elif f.getsampwidth() == 3: - device.setformat(alsaaudio.PCM_FORMAT_S24_3LE) - elif f.getsampwidth() == 4: - device.setformat(alsaaudio.PCM_FORMAT_S32_LE) - else: - raise ValueError('Unsupported format') + # 8bit is unsigned in wav files + if f.getsampwidth() == 1: + format = alsaaudio.PCM_FORMAT_U8 + # Otherwise we assume signed data, little endian + elif f.getsampwidth() == 2: + format = alsaaudio.PCM_FORMAT_S16_LE + elif f.getsampwidth() == 3: + format = alsaaudio.PCM_FORMAT_S24_3LE + elif f.getsampwidth() == 4: + format = alsaaudio.PCM_FORMAT_S32_LE + else: + raise ValueError('Unsupported format') - periodsize = f.getframerate() // 8 + print('%d channels, %d sampling rate\n' % (f.getnchannels(), + f.getframerate())) - device.setperiodsize(periodsize) - - data = f.readframes(periodsize) - while data: - # Read data from stdin - device.write(data) - data = f.readframes(periodsize) + periodsize = f.getframerate() // 8 + + device = alsaaudio.PCM(channels=f.getnchannels(), rate=f.getframerate(), format=format, periodsize=periodsize, device=device) + + data = f.readframes(periodsize) + while data: + # Read data from stdin + device.write(data) + data = f.readframes(periodsize) def usage(): - print('usage: playwav.py [-d ] ', file=sys.stderr) - sys.exit(2) + print('usage: playwav.py [-d ] ', file=sys.stderr) + sys.exit(2) if __name__ == '__main__': - device = 'default' + device = 'default' - opts, args = getopt.getopt(sys.argv[1:], 'd:') - for o, a in opts: - if o == '-d': - device = a + opts, args = getopt.getopt(sys.argv[1:], 'd:') + for o, a in opts: + if o == '-d': + device = a - if not args: - usage() - - f = wave.open(args[0], 'rb') - device = alsaaudio.PCM(device=device) - - play(device, f) - - f.close() + if not args: + usage() + + with wave.open(args[0], 'rb') as f: + play(device, f) diff --git a/recordtest.py b/recordtest.py index a4ed94f..c77646b 100755 --- a/recordtest.py +++ b/recordtest.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 +# -*- mode: python; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- ## recordtest.py ## @@ -22,48 +23,42 @@ import getopt import alsaaudio def usage(): - print('usage: recordtest.py [-d ] ', file=sys.stderr) - sys.exit(2) + print('usage: recordtest.py [-d ] ', file=sys.stderr) + sys.exit(2) if __name__ == '__main__': - device = 'default' + device = 'default' - opts, args = getopt.getopt(sys.argv[1:], 'd:') - for o, a in opts: - if o == '-d': - device = a + opts, args = getopt.getopt(sys.argv[1:], 'd:') + for o, a in opts: + if o == '-d': + device = a - if not args: - usage() + if not args: + usage() - f = open(args[0], 'wb') + f = open(args[0], 'wb') - # Open the device in nonblocking capture mode. The last argument could - # just as well have been zero for blocking mode. Then we could have - # left out the sleep call in the bottom of the loop - inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK, device=device) + # Open the device in nonblocking capture mode in mono, with a sampling rate of 44100 Hz + # and 16 bit little endian samples + # The period size controls the internal number of frames per period. + # The significance of this parameter is documented in the ALSA api. + # For our purposes, it is suficcient to know that reads from the device + # will return this many frames. Each frame being 2 bytes long. + # This means that the reads below will return either 320 bytes of data + # or 0 bytes of data. The latter is possible because we are in nonblocking + # mode. + inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK, + channels=1, rate=44100, format=alsaaudio.PCM_FORMAT_S16_LE, + periodsize=160, device=device) - # Set attributes: Mono, 44100 Hz, 16 bit little endian samples - inp.setchannels(1) - inp.setrate(44100) - inp.setformat(alsaaudio.PCM_FORMAT_S16_LE) - - # The period size controls the internal number of frames per period. - # The significance of this parameter is documented in the ALSA api. - # For our purposes, it is suficcient to know that reads from the device - # will return this many frames. Each frame being 2 bytes long. - # This means that the reads below will return either 320 bytes of data - # or 0 bytes of data. The latter is possible because we are in nonblocking - # mode. - inp.setperiodsize(160) - - loops = 1000000 - while loops > 0: - loops -= 1 - # Read data from device - l, data = inp.read() - - if l: - f.write(data) - time.sleep(.001) + loops = 1000000 + while loops > 0: + loops -= 1 + # Read data from device + l, data = inp.read() + + if l: + f.write(data) + time.sleep(.001) diff --git a/setup.py b/setup.py index cd683c3..2b86ead 100755 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ from setuptools import setup from setuptools.extension import Extension from sys import version -pyalsa_version = '0.8.6' +pyalsa_version = '0.9.0' if __name__ == '__main__': setup(