|
| 1 | +#include <Python.h> |
| 2 | +#include <alsa/asoundlib.h> |
| 3 | + |
| 4 | +/******************************************************** |
| 5 | + Audio Tools, a module and set of tools for manipulating audio data |
| 6 | + Copyright (C) 2007-2008 Brian Langenberger |
| 7 | +
|
| 8 | + This program is free software; you can redistribute it and/or modify |
| 9 | + it under the terms of the GNU General Public License as published by |
| 10 | + the Free Software Foundation; either version 2 of the License, or |
| 11 | + (at your option) any later version. |
| 12 | +
|
| 13 | + This program is distributed in the hope that it will be useful, |
| 14 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + GNU General Public License for more details. |
| 17 | +
|
| 18 | + You should have received a copy of the GNU General Public License |
| 19 | + along with this program; if not, write to the Free Software |
| 20 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | + *******************************************************/ |
| 22 | + |
| 23 | +/*alsa.Output definition*/ |
| 24 | +typedef struct { |
| 25 | + PyObject_HEAD |
| 26 | + snd_pcm_t *playback; |
| 27 | + snd_pcm_hw_params_t *params; |
| 28 | +} alsa_Output; |
| 29 | + |
| 30 | +PyMODINIT_FUNC initalsa(void); |
| 31 | +PyObject *ALSAOutput_new(PyTypeObject *type, |
| 32 | + PyObject *args, PyObject *kwds); |
| 33 | +int ALSAOutput_init(alsa_Output *self, |
| 34 | + PyObject *args, PyObject *kwds); |
| 35 | +void ALSAOutput_dealloc(alsa_Output* self); |
| 36 | +PyObject *ALSAOutput_close(alsa_Output* self); |
| 37 | +PyObject *ALSAOutput_setparams(alsa_Output* self, |
| 38 | + PyObject *args); |
| 39 | +PyObject *ALSAOutput_write(alsa_Output* self, |
| 40 | + PyObject *args); |
| 41 | + |
| 42 | +PyMethodDef module_methods[] = { |
| 43 | + {NULL} |
| 44 | +}; |
| 45 | + |
| 46 | +PyMethodDef ALSAOutput_methods[] = { |
| 47 | + {"close", (PyCFunction)ALSAOutput_close, |
| 48 | + METH_NOARGS,"Closes the ALSA output stream."}, |
| 49 | + {"setparams", (PyCFunction)ALSAOutput_setparams, |
| 50 | + METH_VARARGS,"Sets the PCM stream parameters."}, |
| 51 | + {"write", (PyCFunction)ALSAOutput_write, |
| 52 | + METH_VARARGS,"Writes PCM data to output stream."}, |
| 53 | + {NULL} |
| 54 | +}; |
| 55 | + |
| 56 | +PyTypeObject alsa_OutputType = { |
| 57 | + PyObject_HEAD_INIT(NULL) |
| 58 | + 0, /*ob_size*/ |
| 59 | + "alsa.Output", /*tp_name*/ |
| 60 | + sizeof(alsa_Output), /*tp_basicsize*/ |
| 61 | + 0, /*tp_itemsize*/ |
| 62 | + (destructor)ALSAOutput_dealloc, /*tp_dealloc*/ |
| 63 | + 0, /*tp_print*/ |
| 64 | + 0, /*tp_getattr*/ |
| 65 | + 0, /*tp_setattr*/ |
| 66 | + 0, /*tp_compare*/ |
| 67 | + 0, /*tp_repr*/ |
| 68 | + 0, /*tp_as_number*/ |
| 69 | + 0, /*tp_as_sequence*/ |
| 70 | + 0, /*tp_as_mapping*/ |
| 71 | + 0, /*tp_hash */ |
| 72 | + 0, /*tp_call*/ |
| 73 | + 0, /*tp_str*/ |
| 74 | + 0, /*tp_getattro*/ |
| 75 | + 0, /*tp_setattro*/ |
| 76 | + 0, /*tp_as_buffer*/ |
| 77 | + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
| 78 | + "Output objects", /* tp_doc */ |
| 79 | + 0, /* tp_traverse */ |
| 80 | + 0, /* tp_clear */ |
| 81 | + 0, /* tp_richcompare */ |
| 82 | + 0, /* tp_weaklistoffset */ |
| 83 | + 0, /* tp_iter */ |
| 84 | + 0, /* tp_iternext */ |
| 85 | + ALSAOutput_methods, /* tp_methods */ |
| 86 | + 0, /* tp_members */ |
| 87 | + 0, /* tp_getset */ |
| 88 | + 0, /* tp_base */ |
| 89 | + 0, /* tp_dict */ |
| 90 | + 0, /* tp_descr_get */ |
| 91 | + 0, /* tp_descr_set */ |
| 92 | + 0, /* tp_dictoffset */ |
| 93 | + (initproc)ALSAOutput_init, /* tp_init */ |
| 94 | + 0, /* tp_alloc */ |
| 95 | + ALSAOutput_new, /* tp_new */ |
| 96 | +}; |
| 97 | + |
| 98 | + |
| 99 | +PyMODINIT_FUNC initalsa(void) { |
| 100 | + PyObject* m; |
| 101 | + |
| 102 | + alsa_OutputType.tp_new = PyType_GenericNew; |
| 103 | + if (PyType_Ready(&alsa_OutputType) < 0) |
| 104 | + return; |
| 105 | + |
| 106 | + m = Py_InitModule3("alsa", module_methods, |
| 107 | + "An output-only ALSA interface module."); |
| 108 | + |
| 109 | + Py_INCREF(&alsa_OutputType); |
| 110 | + PyModule_AddObject(m, "Output", |
| 111 | + (PyObject *)&alsa_OutputType); |
| 112 | +} |
| 113 | + |
| 114 | +PyObject *ALSAOutput_new(PyTypeObject *type, |
| 115 | + PyObject *args, PyObject *kwds) { |
| 116 | + alsa_Output *self; |
| 117 | + |
| 118 | + self = (alsa_Output *)type->tp_alloc(type, 0); |
| 119 | + self->playback = NULL; |
| 120 | + self->params = NULL; |
| 121 | + |
| 122 | + return (PyObject *)self; |
| 123 | +} |
| 124 | + |
| 125 | +int ALSAOutput_init(alsa_Output *self, |
| 126 | + PyObject *args, PyObject *kwds) { |
| 127 | + |
| 128 | + int err; |
| 129 | + char *device; |
| 130 | + |
| 131 | + if (!PyArg_ParseTuple(args, "s", &device)) |
| 132 | + return -1; |
| 133 | + |
| 134 | + err = snd_pcm_open(&(self->playback), device, SND_PCM_STREAM_PLAYBACK, 0); |
| 135 | + if (err < 0) { |
| 136 | + PyErr_SetString(PyExc_IOError, snd_strerror(err)); |
| 137 | + return -1; |
| 138 | + } |
| 139 | + |
| 140 | + err = snd_pcm_hw_params_malloc(&(self->params)); |
| 141 | + if (err < 0) { |
| 142 | + PyErr_SetString(PyExc_IOError, snd_strerror(err)); |
| 143 | + return -1; |
| 144 | + } |
| 145 | + |
| 146 | + err = snd_pcm_hw_params_any(self->playback, self->params); |
| 147 | + if (err < 0) { |
| 148 | + PyErr_SetString(PyExc_IOError, snd_strerror(err)); |
| 149 | + return -1; |
| 150 | + } |
| 151 | + |
| 152 | + err = snd_pcm_hw_params_set_access(self->playback, self->params, |
| 153 | + SND_PCM_ACCESS_RW_INTERLEAVED); |
| 154 | + if (err < 0) { |
| 155 | + PyErr_SetString(PyExc_IOError, snd_strerror(err)); |
| 156 | + return -1; |
| 157 | + } |
| 158 | + |
| 159 | + return 0; |
| 160 | +} |
| 161 | + |
| 162 | +PyObject *ALSAOutput_close(alsa_Output* self) { |
| 163 | + if (self->params != NULL) { |
| 164 | + snd_pcm_hw_params_free(self->params); |
| 165 | + self->params = NULL; |
| 166 | + } |
| 167 | + |
| 168 | + if (self->playback != NULL) { |
| 169 | + snd_pcm_close(self->playback); |
| 170 | + self->playback = NULL; |
| 171 | + } |
| 172 | + |
| 173 | + Py_INCREF(Py_None); |
| 174 | + return Py_None; |
| 175 | +} |
| 176 | + |
| 177 | +PyObject *ALSAOutput_setparams(alsa_Output* self, |
| 178 | + PyObject *args) { |
| 179 | + unsigned int sample_rate; |
| 180 | + unsigned int channels; |
| 181 | + int bits_per_sample; |
| 182 | + snd_pcm_format_t pcm_format = SND_PCM_FORMAT_S16_LE; |
| 183 | + int err; |
| 184 | + int dir = 0; |
| 185 | + |
| 186 | + if (!PyArg_ParseTuple(args, "IIi", &sample_rate, &channels, &bits_per_sample)) |
| 187 | + return NULL; |
| 188 | + |
| 189 | + if ((self->playback == NULL) || (self->params == NULL)) { |
| 190 | + PyErr_SetString(PyExc_IOError, |
| 191 | + "ALSA stream is closed"); |
| 192 | + return NULL; |
| 193 | + } |
| 194 | + |
| 195 | + if ((bits_per_sample != 8) && |
| 196 | + (bits_per_sample != 16) && |
| 197 | + (bits_per_sample != 24)) { |
| 198 | + PyErr_SetString(PyExc_ValueError, |
| 199 | + "bits per sample must be 8, 16 or 24"); |
| 200 | + return NULL; |
| 201 | + } |
| 202 | + |
| 203 | + |
| 204 | + /*test/set rate*/ |
| 205 | + err = snd_pcm_hw_params_test_rate(self->playback, self->params, |
| 206 | + sample_rate, dir); |
| 207 | + if (err < 0) { |
| 208 | + PyErr_SetString(PyExc_ValueError, snd_strerror(err)); |
| 209 | + return NULL; |
| 210 | + } |
| 211 | + |
| 212 | + err = snd_pcm_hw_params_set_rate_near(self->playback, self->params, |
| 213 | + &sample_rate, &dir); |
| 214 | + if (err < 0) { |
| 215 | + PyErr_SetString(PyExc_ValueError, snd_strerror(err)); |
| 216 | + return NULL; |
| 217 | + } |
| 218 | + |
| 219 | + /*test/set channels*/ |
| 220 | + err = snd_pcm_hw_params_test_channels(self->playback, self->params, |
| 221 | + channels); |
| 222 | + if (err < 0) { |
| 223 | + PyErr_SetString(PyExc_ValueError, snd_strerror(err)); |
| 224 | + return NULL; |
| 225 | + } |
| 226 | + |
| 227 | + err = snd_pcm_hw_params_set_channels(self->playback, self->params, |
| 228 | + channels); |
| 229 | + if (err < 0) { |
| 230 | + PyErr_SetString(PyExc_ValueError, snd_strerror(err)); |
| 231 | + return NULL; |
| 232 | + } |
| 233 | + |
| 234 | + |
| 235 | + /*test/set bits-per-sample*/ |
| 236 | + switch (bits_per_sample) { |
| 237 | + case 8: pcm_format = SND_PCM_FORMAT_U8; break; |
| 238 | + case 16: pcm_format = SND_PCM_FORMAT_S16_LE; break; |
| 239 | + case 24: pcm_format = SND_PCM_FORMAT_S24_LE; break; |
| 240 | + } |
| 241 | + |
| 242 | + err = snd_pcm_hw_params_test_format(self->playback, self->params, |
| 243 | + pcm_format); |
| 244 | + if (err < 0) { |
| 245 | + PyErr_SetString(PyExc_ValueError, snd_strerror(err)); |
| 246 | + return NULL; |
| 247 | + } |
| 248 | + |
| 249 | + err = snd_pcm_hw_params_set_format(self->playback, self->params, |
| 250 | + pcm_format); |
| 251 | + if (err < 0) { |
| 252 | + PyErr_SetString(PyExc_ValueError, snd_strerror(err)); |
| 253 | + return NULL; |
| 254 | + } |
| 255 | + |
| 256 | + |
| 257 | + err = snd_pcm_hw_params(self->playback, self->params); |
| 258 | + if (err < 0) { |
| 259 | + PyErr_SetString(PyExc_ValueError, snd_strerror(err)); |
| 260 | + return NULL; |
| 261 | + } |
| 262 | + |
| 263 | + err = snd_pcm_prepare(self->playback); |
| 264 | + if (err < 0) { |
| 265 | + PyErr_SetString(PyExc_ValueError, snd_strerror(err)); |
| 266 | + return NULL; |
| 267 | + } |
| 268 | + |
| 269 | + |
| 270 | + |
| 271 | + Py_INCREF(Py_None); |
| 272 | + return Py_None; |
| 273 | +} |
| 274 | + |
| 275 | +PyObject *ALSAOutput_write(alsa_Output* self, |
| 276 | + PyObject *args) { |
| 277 | + char *pcm_data; |
| 278 | + Py_ssize_t pcm_data_length; |
| 279 | + |
| 280 | + if (!PyArg_ParseTuple(args,"s#",&pcm_data,&pcm_data_length)) |
| 281 | + return NULL; |
| 282 | + |
| 283 | + if ((self->playback == NULL) || (self->params == NULL)) { |
| 284 | + PyErr_SetString(PyExc_IOError, |
| 285 | + "ALSA stream is closed"); |
| 286 | + return NULL; |
| 287 | + } |
| 288 | + |
| 289 | + Py_BEGIN_ALLOW_THREADS |
| 290 | + snd_pcm_writei(self->playback, pcm_data, |
| 291 | + (snd_pcm_uframes_t)pcm_data_length); |
| 292 | + Py_END_ALLOW_THREADS |
| 293 | + |
| 294 | + Py_INCREF(Py_None); |
| 295 | + return Py_None; |
| 296 | +} |
| 297 | + |
| 298 | +void ALSAOutput_dealloc(alsa_Output* self) |
| 299 | +{ |
| 300 | + if (self->params != NULL) { |
| 301 | + snd_pcm_hw_params_free(self->params); |
| 302 | + } |
| 303 | + |
| 304 | + if (self->playback != NULL) { |
| 305 | + snd_pcm_close(self->playback); |
| 306 | + } |
| 307 | + |
| 308 | + self->ob_type->tp_free((PyObject*)self); |
| 309 | +} |
0 commit comments