Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mpg123.h>
- #include <ao.h>
- #include <Python.h>
- #define BITS 8
- static PyObject *mpg123_play(PyObject *self, PyObject *args) {
- const char *command;
- int sts;
- if (!PyArg_ParseTuple(args, "s", &command))
- return NULL;
- sts = play(command);
- return PyBuildValue("i", sts);
- }
- int play(char *filename)
- {
- mpg123_handle *mh;
- unsigned char *buffer;
- size_t buffer_size;
- size_t done;
- int err;
- int driver;
- ao_device *dev;
- ao_sample_format format;
- int channels, encoding;
- long rate;
- /* initializations */
- ao_initialize();
- driver = ao_default_driver_id();
- mpg123_init();
- mh = mpg123_new(NULL, &err);
- buffer_size = mpg123_outblock(mh);
- buffer = (unsigned char*) malloc(buffer_size * sizeof(unsigned char));
- /* open the file and get the decoding format */
- mpg123_open(mh, filename);
- mpg123_getformat(mh, &rate, &channels, &encoding);
- /* set the output format and open the output device */
- format.bits = mpg123_encsize(encoding) * BITS;
- format.rate = rate;
- format.channels = channels;
- format.byte_format = AO_FMT_NATIVE;
- format.matrix = 0;
- dev = ao_open_live(driver, &format, NULL);
- /* decode and play */
- while (mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK)
- ao_play(dev, buffer, done);
- /* clean up */
- free(buffer);
- ao_close(dev);
- mpg123_close(mh);
- mpg123_delete(mh);
- mpg123_exit();
- ao_shutdown();
- }
- static PyMethodDef SpamMethods[] = {
- {"play", mpg123_play, METH_VARARGS,
- "play an mp3 file using libmpg123"},
- {NULL, NULL, 0, NULL} /* Sentinel */
- };
- PyMODINIT_FUNC
- initmodule(void)
- {
- (void) Py_InitModule("mpg123", SpamMethods);
- }
- int main(int argc, char **argv) {
- /* Pass argv[0] to the Python interpreter */
- Py_SetProgramName(argv[0]);
- /* Initialize the Python interpreter. Required. */
- Py_Initialize();
- initmodule();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment