Skip to content

Commit

Permalink
Move towards python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
larsimmisch committed May 8, 2015
1 parent 08b8011 commit 7b9d982
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 29 deletions.
10 changes: 6 additions & 4 deletions isine.py
Expand Up @@ -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
Expand All @@ -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)))

Expand Down Expand Up @@ -55,7 +57,7 @@ def change(self, frequency):
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)

Expand All @@ -64,7 +66,7 @@ def run(self):
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:
Expand Down
15 changes: 8 additions & 7 deletions mixertest.py
Expand Up @@ -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
Expand All @@ -25,19 +26,19 @@
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]))
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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__':
Expand Down
6 changes: 2 additions & 4 deletions playbacktest.py
Expand Up @@ -13,17 +13,15 @@
## 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
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__':
Expand Down
11 changes: 4 additions & 7 deletions playwav.py
Expand Up @@ -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
Expand All @@ -13,9 +11,8 @@

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())
Expand Down Expand Up @@ -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__':
Expand Down
7 changes: 3 additions & 4 deletions recordtest.py
Expand Up @@ -12,18 +12,17 @@
## 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
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__':
Expand Down
6 changes: 3 additions & 3 deletions 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
Expand Down

0 comments on commit 7b9d982

Please sign in to comment.