Posts tagged ‘sound’

Python ticking clock – Periodically play a ticking sound

The following python script will periodically play a ticking sound after a given pause. This is can be useful for timing things when you are unable to start a timer – for example if you are practicing balancing a walking-stick on your nose on your own.

#!/usr/bin/env python
import ao
import ogg.vorbis
import os
import sys
from time import sleep, time

def switch_off_buffering():
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
switch_off_buffering()

PERIOD = 2
PAUSE = 5
SOUND = "/usr/share/sounds/gnome/default/alerts/sonar.ogg"

filename = 'test.ogg'

class OggSample(object):
    def __init__(self, filename):
        id = ao.driver_id('esd')
        self.vf = ogg.vorbis.VorbisFile(filename)
        self.device = ao.AudioDevice(id)

    def play(self):
        while 1:
            buff, bytes, _ = self.vf.read(4096)
            if bytes != 0:
                self.device.play(buff, bytes)
            else:
                self.vf.time_seek(0)
                return

def repeat(pause, period):
    sample = OggSample(SOUND)

    sleep(pause)
    tick_time = time()
    while True:
        sys.stdout.write(".")
        sample.play()
        tick_time += period # avoid drift
        sleep(tick_time - time())


DEFAULT_DELAY = 5
DEFAULT_PERIOD = 2

def usage(stream):
    print >>stream, "ticking-count [DELAY seconds] [PERIOD seconds]"
    print >>stream, "Default delay: %s seconds" % DEFAULT_DELAY
    print >>stream, "Default period: %s seconds" % DEFAULT_PERIOD

if __name__ == '__main__':
    args = sys.argv[1:]
    if '-h' in args or '--help' in args:
        usage(sys.stdout)

    if len(args) == 0:
        delay, period = DEFAULT_DELAY, DEFAULT_PERIOD
    elif len(args) == 1:
        delay, = args
        period = DEFAULT_PERIOD
    elif len(args) == 2:
        delay, period = args
    else:
        usage(sys.stderr)
    repeat(delay, period)

March 11, 2010 at 11:17 pm Leave a comment

Playing an ogg file on linux using python

The following code allows one to repeatedly play a sound sample using python on linux.

This code depends on python-ogg, python-pyvorbis and python-pyao.

import ogg.vorbis
import ao

filename = 'test.ogg'

class OggSample(object):
	def __init__(self, filename):
		id = ao.driver_id('esd')
		self.vf = ogg.vorbis.VorbisFile(filename)
		self.device = ao.AudioDevice(id)

	def play(self):
		while 1:
			buff, bytes, _ = self.vf.read(4096)
			if bytes != 0:
				self.device.play(buff, bytes)
			else:
				self.vf.time_seek(0)
				return

sample = OggSample(filename)
sample.play()
sample.play()

There may well be better ways of doing this : in particular, I’m not sure how much this will cause one to talk to the disk.

March 11, 2010 at 11:02 pm Leave a comment

Flash broken by upgrade to ubuntu intrepid

Upgrading to ubuntu intrepid broke the sound in flash. Videos would play but there was no sound.

Attempting to install the apt non-free flash player failed complaining about the md5 checksum not matching (presumably becase the md5 sum is hard-code and the flash player that was being downloaded had changed). [aside: running apt-get clean and repeating made this work. ]

However downloading and installing the latest version of flash worked.

February 24, 2009 at 11:04 pm Leave a comment


May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031