Changeset 1192

Show
Ignore:
Timestamp:
08/04/08 06:29:40 (4 months ago)
Author:
cdavid
Message:

Add ability to play arrays of short for alsa backend.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/audiolab/scikits/audiolab/soundio/_alsa.pyx

    r1190 r1192  
    6060 
    6161        ctypedef struct snd_pcm_t 
     62        # XXX: how to make sure the typedef is OK ? 
     63        ctypedef unsigned long snd_pcm_uframes_t 
    6264 
    6365        int snd_pcm_open(snd_pcm_t **, char*, int, int) 
    6466        int snd_pcm_close(snd_pcm_t *) 
     67        int snd_pcm_drain(snd_pcm_t *) 
    6568 
    6669        int snd_pcm_set_params(snd_pcm_t *, snd_pcm_format_t, 
    6770                        snd_pcm_access_t, unsigned int, 
    6871                        unsigned int, int, unsigned int) 
     72 
     73        int snd_pcm_writei(snd_pcm_t *, void*, snd_pcm_uframes_t) 
    6974 
    7075        char* snd_strerror(int error) 
     
    7378        int snd_card_get_name(int icard, char** name) 
    7479        char* snd_asoundlib_version() 
     80 
     81cdef extern from "numpy/arrayobject.h": 
     82        ctypedef int intp 
     83        ctypedef extern class numpy.ndarray [object PyArrayObject]: 
     84                cdef char *data 
     85                cdef int nd 
     86                cdef intp *dimensions 
     87                cdef intp *strides 
     88                cdef int flags 
    7589 
    7690cdef extern from "stdlib.h": 
     
    8195        size_t strlen(char *s) 
    8296        char *strcpy(char *dest, char *src) 
     97 
     98cdef extern from "stdint.h": 
     99        ctypedef unsigned short int16_t 
    83100 
    84101cdef extern from "Python.h": 
     
    117134        return cardname 
    118135 
    119 cdef class PCM: 
     136cdef class _PCM: 
    120137        cdef snd_pcm_t* pcmhdl 
    121138        cdef public char* name 
     
    136153 
    137154cdef class Device: 
    138         cdef PCM pcm 
     155        cdef _PCM pcm 
    139156        cdef unsigned int samplerate 
    140         cdef unsigned int channels 
     157        # XXX: set property instead 
     158        cdef public unsigned int channels 
     159        cdef snd_pcm_format_t format 
     160        cdef snd_pcm_access_t access 
    141161        def __init__(self, samplerate = 48000, channels = 1, 
    142162                     format = SND_PCM_FORMAT_S16, 
    143163                     access = SND_PCM_ACCESS_RW_INTERLEAVED): 
    144                 self.pcm = PCM() 
     164                self.pcm = _PCM() 
    145165 
    146166                self.samplerate = samplerate 
    147167                self.channels = channels 
     168 
     169                self.access = access 
     170                self.format = format 
    148171 
    149172                st = snd_pcm_set_params(self.pcm.pcmhdl, format, access, channels, 
     
    152175                        raise AlsaException() 
    153176 
     177        def play_short(self, ndarray input): 
     178                cdef int16_t* p = <int16_t*>input.data 
     179                cdef int16_t* buff = NULL 
     180                #cdef int n = input.dimensions[0] 
     181                cdef int n = input.dimensions[0] 
     182                cdef int nb = n / 1024 / 16 
     183                cdef snd_pcm_uframes_t bufsz = 1024 * 16 
     184 
     185                assert self.format == SND_PCM_FORMAT_S16 
     186                assert self.access == SND_PCM_ACCESS_RW_INTERLEAVED 
     187 
     188                for i from 0 <= i  < nb: 
     189                        st =  snd_pcm_writei(self.pcm.pcmhdl, &p[i * bufsz], bufsz) 
     190                        if not st == bufsz: 
     191                                print "Error while writing to device", st 
     192                                print snd_strerror(st) 
     193 
     194                snd_pcm_drain(self.pcm.pcmhdl) 
     195 
    154196        def _get_name(self): 
    155197                return self.pcm.name 
  • trunk/audiolab/scikits/audiolab/soundio/setup.py

    r1184 r1192  
    22from distutils.extension import Extension 
    33from Cython.Distutils import build_ext 
     4from numpy.distutils.misc_util import get_numpy_include_dirs 
    45 
    56setup(name = "PyrexGuide", 
    6       ext_modules=[Extension("_alsa", ["_alsa.pyx"], libraries = ['asound'])], 
     7      ext_modules=[  
     8          Extension("_alsa", ["_alsa.pyx"],  
     9                    libraries = ['asound'],  
     10                    include_dirs = get_numpy_include_dirs())], 
    711      cmdclass = {'build_ext': build_ext}) 
    812