#!/usr/bin/env python
#==============================================================================
#
#  $Id: esdtest,v 1.2 2000/08/03 01:18:11 mike Exp $
#
"""
   ESD test script: creates a sample, caches it, and plays it.
"""
#
#  Copyright (C) 1999 Michael A. Muller
#
#  Permission is granted to use, modify and redistribute this code,
#  providing that the following conditions are met:
#
#  1) This copyright/licensing notice must remain intact.
#  2) If the code is modified and redistributed, the modifications must 
#  include documentation indicating that the code has been modified.
#  3) The author(s) of this code must be indemnified and held harmless
#  against any damage resulting from the use of this code.
#
#  This code comes with ABSOLUTELY NO WARRANTEE, not even the implied 
#  warrantee of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
#
#  $Log: esdtest,v $
#  Revision 1.2  2000/08/03 01:18:11  mike
#  Documented Recorder and added caveats about the hostname parameter.
#
#  Revision 1.1.1.1  1999/07/23 16:07:44  mike
#  Python EsounD wrapper
#
#
#==============================================================================

from math import pi, sin
from array import array
import os, time

def makebell():

   str = array('B')
   for i in range(0, 32000):
      x = pi * i / 10.0
      byte = int(sin(x) * 120 * (32000 - i) / 32000 + 120)
      str.append(byte)
   return str.tostring()

import esd

# define format and rate info for all subsequent calls
fmt = esd.ESD_PLAY | esd.ESD_MONO | esd.ESD_BITS8
rate = esd.ESD_DEFAULT_RATE / 4

# generate a pleasant bell sound
bellSound = makebell()

print 'generating bell sample'
bellwave = esd.Sample(fmt, rate, bellSound)

print 'connecting to daemon'
s = esd.ServerSession()

print 'caching bell sample in server'
sampleId = s.cacheSample(bellwave, 'pyesd bell sample')

print 'playing sample'
s.playSample(sampleId)

print 'sleeping for 3 secs'
time.sleep(3)

print 'freeing sample'
s.freeSample(sampleId)

print 'destroying server'
del s

print 'creating recorder'
r = esd.Recorder(fmt, rate, '', 'bogus')

print 'recording 1K'
data = r.read(1024)

print 'destroying recorder'
del r

print 'creating player'
p = esd.Player(fmt, rate, '', 'bogus')

print 'playing back recorded sample'
p.write(data)

print 'playing back bell sound'
p.write(bellSound)

print 'destroying player'
del p

print 'done!'


