| 2 | | from _alsa import Device |
|---|
| | 4 | from _alsa import Device, AlsaException |
|---|
| | 5 | |
|---|
| | 6 | def play(input, samplerate = 48000): |
|---|
| | 7 | if input.ndim == 1: |
|---|
| | 8 | n = input.size |
|---|
| | 9 | nc = 1 |
|---|
| | 10 | elif input.ndim == 2: |
|---|
| | 11 | n, nc = input.shape |
|---|
| | 12 | else: |
|---|
| | 13 | raise ValueError("Only ndim 1 or 2 supported") |
|---|
| | 14 | |
|---|
| | 15 | try: |
|---|
| | 16 | dev = Device(samplerate = samplerate, channels = nc) |
|---|
| | 17 | |
|---|
| | 18 | assert nc == dev.channels |
|---|
| | 19 | assert input.dtype == np.float32 or input.dtype == np.float64 |
|---|
| | 20 | |
|---|
| | 21 | dev.play_short((16384 * input).astype(np.int16)) |
|---|
| | 22 | except AlsaException, e: |
|---|
| | 23 | raise IOError(str(e)) |
|---|