[xiph-cvs] cvs commit: ao-python/src aomodule.c aomodule.h

Andrew Catham Master of Python andrew at xiph.org
Thu Aug 30 19:02:11 PDT 2001



andrew      01/08/30 19:02:11

  Modified:    .        ChangeLog test.py
               src      aomodule.c aomodule.h
  Log:
  2001-08-30  Andrew H. Chatham <andrew.chatham at duke.edu>
          * aomodule.[ch] Updated to work with the updated ao API.
            Not entirely sure the way of dealing with files is perfect,
            but it should be serviceable.
  
          * aomodule.[ch] Changed some of the function names so that they
            look more like their C counterparts (getting rid of get_
            in function names, for example)

Revision  Changes    Path
1.4       +9 -0      ao-python/ChangeLog

Index: ChangeLog
===================================================================
RCS file: /usr/local/cvsroot/ao-python/ChangeLog,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ChangeLog	2001/02/18 22:52:19	1.3
+++ ChangeLog	2001/08/31 02:02:10	1.4
@@ -1,3 +1,12 @@
+2001-08-30  Andrew H. Chatham <andrew.chatham at duke.edu>
+	* aomodule.[ch] Updated to work with the updated ao API.
+	  Not entirely sure the way of dealing with files is perfect,
+	  but it should be serviceable.
+
+	* aomodule.[ch] Changed some of the function names so that they
+	  look more like their C counterparts (getting rid of get_ 
+	  in function names, for example)
+	
 2001-02-18  Andrew H. Chatham <andrew.chatham at duke.edu>
         * README: Fixed incorrect config instructions
         

1.2       +2 -2      ao-python/test.py

Index: test.py
===================================================================
RCS file: /usr/local/cvsroot/ao-python/test.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- test.py	2001/01/18 00:10:15	1.1
+++ test.py	2001/08/31 02:02:10	1.2
@@ -2,8 +2,8 @@
 
 import ao
 
-id = ao.get_driver_id('oss')
-dev = ao.AudioDevice(id)
+dev = ao.AudioDevice('wav', bits=16)
 f = open('test.wav', 'r')
 data = f.read()
 dev.play(data, len(data))
+

1.3       +92 -33    ao-python/src/aomodule.c

Index: aomodule.c
===================================================================
RCS file: /usr/local/cvsroot/ao-python/src/aomodule.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- aomodule.c	2001/01/21 22:11:55	1.2
+++ aomodule.c	2001/08/31 02:02:11	1.3
@@ -1,11 +1,12 @@
 #include "aomodule.h"
+#include <assert.h>
 
-static ao_option_t *
+static ao_option *
 dict_to_options(PyObject *dict)
 {
   int pos = 0;
   PyObject *key, *val;
-  ao_option_t *head = NULL;
+  ao_option *head = NULL;
   int ret;
   
 
@@ -33,37 +34,91 @@
   return NULL;
 }
 
-static char *new_kwlist[] = {"driver_id", "bits", "rate", 
-			     "channels", "options"};
+/* 
+   Helper function to parse everything out of the argument list.
 
+   Returns 0 on failure.
+*/
+static int
+parse_args(PyObject *args, PyObject *kwargs, 
+	   ao_sample_format * format, PyObject **py_options,
+	   const char **filename, 
+	   uint_32 *driver_id,
+	   uint_32 *overwrite)
+{
+  static char *driver_id_kwlist[] = {"driver_id", "bits", "rate", 
+				     "channels", "byte_format",
+				     "options", "filename", 
+				     "overwrite", NULL};
+  static char *driver_name_kwlist[] = {"driver_name", "bits", "rate", 
+				       "channels", "byte_format",
+				       "options", "filename", 
+				       "overwrite", NULL};
+
+  const char *driver_name = NULL;
+
+  assert(py_options != NULL);
+  assert(format != NULL);
+  assert(filename != NULL);
+  assert(driver_id != NULL);
+  assert(overwrite != NULL);
+
+  /* Set the default values */
+  format->bits = 16;
+  format->rate = 44100;
+  format->channels = 2;
+  format->byte_format = 1; /* What should this be by default? Anyone? */
+  
+  overwrite = 0;
+
+  if(PyArg_ParseTupleAndKeywords(args, kwargs, "s|llllO!sl", 
+				 driver_name_kwlist,
+				 &driver_name, 
+				 &format->bits, 
+				 &format->rate, 
+				 &format->channels, 
+				 &format->byte_format,
+				 &PyDict_Type, py_options,
+				 filename, 
+				 overwrite)) {
+    *driver_id = ao_driver_id(driver_name);
+  } else {
+    PyErr_Clear();
+    if(!(PyArg_ParseTupleAndKeywords(args, kwargs, "i|llllO!sl",
+				     driver_id_kwlist,
+				     driver_id, 
+				     &format->bits, 
+				     &format->rate, 
+				     &format->channels, 
+				     &format->byte_format,
+				     &PyDict_Type, py_options,
+				     filename, 
+				     overwrite)))
+      return 0;
+  }  
+
+  return 1;
+}
+
+/*
+  Actually create a new AudioDevice object
+*/
 static PyObject*
 py_ao_new(PyObject *self, PyObject *args, PyObject *kwargs)
 {
-  uint_32 driver_id, bits, rate, channels;
-  char * driver_name;
+  uint_32 overwrite, driver_id;
+  const char * filename = NULL;
   PyObject *py_options = NULL;
-  ao_option_t *c_options;
-  ao_device_t *dev;
+  ao_option *c_options = NULL;
+  ao_device *dev;
+  ao_sample_format sample_format;
   ao_Object *retobj;
-
-  bits = 16;
-  rate = 44100;
-  channels = 2;
-  c_options = NULL;
-
-  if(PyArg_ParseTupleAndKeywords(args, kwargs, "s|lllO!", new_kwlist,
-				 &driver_name, &bits, &rate, &channels, 
-				 &PyDict_Type, &py_options)) {
-    driver_id = ao_get_driver_id(driver_name);
-  } else {
 
-    PyErr_Clear();
-    if(!(PyArg_ParseTupleAndKeywords(args, kwargs, "i|lllO!", new_kwlist,
-				     &driver_id, &bits, &rate, &channels, 
-				     &PyDict_Type, &py_options)))
-      return NULL;
+  if (!parse_args(args, kwargs, 
+		  &sample_format, &py_options,
+		  &filename, &driver_id, &overwrite))
+    return NULL;
 
-  }  
   if (py_options && PyDict_Size(py_options) > 0) {
     /* dict_to_options returns NULL on error, so you can't pass
        an empty dictionary. We can skip this then anyway. */
@@ -78,7 +133,11 @@
     Py_DECREF(py_options);
   }
 
-  dev = ao_open(driver_id, bits, rate, channels, c_options);
+  if (filename == NULL)
+    dev = ao_open_live(driver_id, &sample_format, c_options);
+  else
+    dev = ao_open_file(driver_id, filename, overwrite, 
+		       &sample_format, c_options);
   ao_free_options(c_options);
 
   if (dev == NULL) {
@@ -99,7 +158,7 @@
 }
 
 static PyObject *
-py_ao_get_driver_id(PyObject *self, PyObject *args)
+py_ao_driver_id(PyObject *self, PyObject *args)
 {
   int driver_id;
   char *str = NULL;
@@ -107,7 +166,7 @@
   if (!PyArg_ParseTuple(args, "|s", &str))
     return NULL;
 
-  driver_id = ao_get_driver_id(str); /* akes NULL for default */
+  driver_id = ao_driver_id(str); /* takes NULL for default */
 
   if (driver_id == -1) {
     PyErr_SetString(Py_aoError, "No such driver");
@@ -118,25 +177,25 @@
 }
 
 static PyObject *
-py_ao_get_driver_info(PyObject *self, PyObject *args)
+py_ao_driver_info(PyObject *self, PyObject *args)
 {
   int driver_id = 0;
   char *driver_name;
-  ao_info_t *info; 
+  ao_info *info; 
   PyObject *retdict;
 
   if (self != NULL) {
 
     /* It's a method */
     ao_Object *ao_self = (ao_Object *) self;
-    info = ao_self->dev->funcs->get_driver_info();
+    info = ao_driver_info(ao_self->dev->driver_id);
 
   } else {
 
     /* Maybe it's a string */
     if ((PyArg_ParseTuple(args, "s", &driver_name))) {
 
-      driver_id = ao_get_driver_id(driver_name);
+      driver_id = ao_driver_id(driver_name);
       if (driver_id == -1) {
         PyErr_SetString(Py_aoError, "Invalid driver name");
       }
@@ -149,7 +208,7 @@
         return NULL;
     }
     
-    info = ao_get_driver_info(driver_id);
+    info = ao_driver_info(driver_id);
 
   }
   if (!info) {

1.4       +21 -15    ao-python/src/aomodule.h

Index: aomodule.h
===================================================================
RCS file: /usr/local/cvsroot/ao-python/src/aomodule.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- aomodule.h	2001/05/14 15:29:53	1.3
+++ aomodule.h	2001/08/31 02:02:11	1.4
@@ -8,14 +8,15 @@
 
 typedef struct {
   PyObject_HEAD
-  ao_device_t *dev;
+  ao_device *dev;
 } ao_Object;
 
 static PyObject *Py_aoError;
 
-static ao_option_t * dict_to_options(PyObject *);
+static ao_option * dict_to_options(PyObject *);
 
 static PyObject *py_ao_new(PyObject *, PyObject *, PyObject *);
+
 static void py_ao_dealloc(ao_Object *);
 static PyObject* py_ao_getattr(PyObject *, char *);
 
@@ -28,29 +29,34 @@
 n : Number of bytes to play (defaults to len(buff))";
 static PyObject *py_ao_play(PyObject *, PyObject *);
 
-static char py_ao_get_driver_id_doc[] = 
+static char py_ao_driver_id_doc[] = 
 "Return the integer identifier for the driver with the given name (or object).";
-static PyObject *py_ao_get_driver_id(PyObject *, PyObject *);
+static PyObject *py_ao_driver_id(PyObject *, PyObject *);
 
-static char py_ao_get_driver_info_doc[] =
+static char py_ao_driver_info_doc[] =
 "Return a dictionary of information about a driver.\n\
 \n\
 It can either be called as a member function of an AudioDevice object:\n\
    x.get_driver_info()\n\
 or as a standalone function which takes the integer id of the driver:\n\
    get_driver_info(1)";
-static PyObject *py_ao_get_driver_info(PyObject *, PyObject *);
+static PyObject *py_ao_driver_info(PyObject *, PyObject *);
 
 static char py_ao_is_big_endian_doc[] =
 "Returns the endianness of the current host.";
 static PyObject *py_ao_is_big_endian(PyObject *, PyObject *);
 
 static char py_ao_doc[] = 
-"AudioDevice(id=default)\n\
+"
+AudioDevice(driverid, bits=16, rate=44100, channels=2, byte_format=1, options=[], filename='', overwrite=0)\n\
+OR
+AudioDevice(drivername, bits=16, rate=44100, channels=2, byte_format=1, options=[], filename='', overwrite=0)\n\
 \n\
 An AudioDevice object is an interface to a sound device. You can either pass\n\
-an id of a specific type of device or if you don't supply one it will\n\
-choose the library default for your system.";
+an id of a specific type of device or the name of that device type.\n\
+If filename is passed, the module will try to open an output file as the\n\
+audio device. In this case, overwrite indicates whether to overwrite an\n\
+existing file\n";
 
 static PyTypeObject ao_Type = {
   PyObject_HEAD_INIT(&PyType_Type)
@@ -82,8 +88,8 @@
 
 
 struct PyMethodDef ao_Object_methods[] = {
-  {"get_driver_info", py_ao_get_driver_info, 
-   METH_VARARGS, py_ao_get_driver_info_doc},
+  {"get_driver_info", py_ao_driver_info, 
+   METH_VARARGS, py_ao_driver_info_doc},
   {"play", py_ao_play, 
    METH_VARARGS, py_ao_play_doc},
   {NULL, NULL},
@@ -92,10 +98,10 @@
 struct PyMethodDef ao_methods[] = {
   {"AudioDevice", (PyCFunction) py_ao_new, 
    METH_VARARGS|METH_KEYWORDS, py_ao_doc},
-  {"get_driver_id", py_ao_get_driver_id, 
-   METH_VARARGS, py_ao_get_driver_id_doc},
-  {"get_driver_info", py_ao_get_driver_info, 
-   METH_VARARGS, py_ao_get_driver_info_doc},
+  {"driver_id", py_ao_driver_id, 
+   METH_VARARGS, py_ao_driver_id_doc},
+  {"driver_info", py_ao_driver_info, 
+   METH_VARARGS, py_ao_driver_info_doc},
   {"is_big_endian", py_ao_is_big_endian, 
    METH_VARARGS, py_ao_is_big_endian_doc},
   {NULL, NULL}

--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list