test_linuxaudiodev.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Lib » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Lib » test » test_linuxaudiodev.py
from test_support import verbose,findfile,TestFailed,TestSkipped

import errno
import fcntl
import linuxaudiodev
import os
import sys
import select
import sunaudio
import time
import audioop

SND_FORMAT_MULAW_8 = 1

def play_sound_file(path):
    fp = open(path, 'r')
    size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
    data = fp.read()
    fp.close()

    if enc != SND_FORMAT_MULAW_8:
        print "Expect .au file with 8-bit mu-law samples"
        return

    try:
        a = linuxaudiodev.open('w')
    except linuxaudiodev.error, msg:
        if msg[0] in (errno.EACCES, errno.ENODEV):
            raise TestSkipped, msg
        raise TestFailed, msg

    # convert the data to 16-bit signed
    data = audioop.ulaw2lin(data, 2)

    # set the data format
    if sys.byteorder == 'little':
        fmt = linuxaudiodev.AFMT_S16_LE
    else:
        fmt = linuxaudiodev.AFMT_S16_BE

    # at least check that these methods can be invoked
    a.bufsize()
    a.obufcount()
    a.obuffree()
    a.getptr()
    a.fileno()

    # set parameters based on .au file headers
    a.setparameters(rate, 16, nchannels, fmt)
    a.write(data)
    a.flush()
    a.close()

def test_errors():
    a = linuxaudiodev.open("w")
    size = 8
    fmt = linuxaudiodev.AFMT_U8
    rate = 8000
    nchannels = 1
    try:
        a.setparameters(-1, size, nchannels, fmt)
    except ValueError, msg:
        print msg
    try:
        a.setparameters(rate, -2, nchannels, fmt)
    except ValueError, msg:
        print msg
    try:
        a.setparameters(rate, size, 3, fmt)
    except ValueError, msg:
        print msg
    try:
        a.setparameters(rate, size, nchannels, 177)
    except ValueError, msg:
        print msg
    try:
        a.setparameters(rate, size, nchannels, linuxaudiodev.AFMT_U16_LE)
    except ValueError, msg:
        print msg
    try:
        a.setparameters(rate, 16, nchannels, fmt)
    except ValueError, msg:
        print msg

def test():
    play_sound_file(findfile('audiotest.au'))
    test_errors()

test()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.