[xiph-cvs] cvs commit: vorbis-python/src pyvorbisfile.c pyvorbisinfo.c

Andrew Chatham Master of Python andrew at xiph.org
Thu Dec 18 23:54:41 PST 2003



andrew      03/12/19 02:54:40

  Modified:    .        ChangeLog
               src      pyvorbisfile.c pyvorbisinfo.c
  Log:
  2003-12-18  Andrew H. Chatham <pyogg at andrewchatham.com>
          * pyvorbisinfo.c: Incoporate John Morton's explanations about what
          all those arguments mean. Now everything that takes a logical
          stream index says so.
  
          * pyvorbisinfo.c: Provide str() methods for VorbisComment and
          VorbisInfo objects. Now they print out all pretty and stuff. Now
          VorbisFile could still use one.

Revision  Changes    Path
1.25      +9 -0      vorbis-python/ChangeLog

Index: ChangeLog
===================================================================
RCS file: /usr/local/cvsroot/vorbis-python/ChangeLog,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -r1.24 -r1.25
--- ChangeLog	9 Sep 2003 03:40:47 -0000	1.24
+++ ChangeLog	19 Dec 2003 07:54:33 -0000	1.25
@@ -1,3 +1,12 @@
+2003-12-18  Andrew H. Chatham <pyogg at andrewchatham.com>
+	* pyvorbisinfo.c: Incoporate John Morton's explanations about what
+	all those arguments mean. Now everything that takes a logical
+	stream index says so.
+
+	* pyvorbisinfo.c: Provide str() methods for VorbisComment and
+	VorbisInfo objects. Now they print out all pretty and stuff. Now
+	VorbisFile could still use one.
+	
 2003-09-08  Andrew H. Chatham <pyogg at andrewchatham.com>
         * pyvorbisfile.c, pyvorbisinfo.c: Fix an exception leak (the
         exceptions would show up later, even though things worked OK)

<p><p>1.14      +37 -33    vorbis-python/src/pyvorbisfile.c

Index: pyvorbisfile.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis-python/src/pyvorbisfile.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- pyvorbisfile.c	9 Sep 2003 03:40:48 -0000	1.13
+++ pyvorbisfile.c	19 Dec 2003 07:54:40 -0000	1.14
@@ -37,27 +37,30 @@
 
 static PyObject *py_ov_read(PyObject *, PyObject *, PyObject *);
 
-FDEF(ov_streams) "Returns the streams value for this VorbisFile";
+FDEF(ov_streams) "Returns the number of logical streams in this VorbisFile";
 FDEF(ov_seekable) "Returns whether this VorbisFile is seekable.";
-FDEF(ov_bitrate) "Returns the bitrate of this VorbisFile.";
+FDEF(ov_bitrate) \
+"x.bitrate(stream_idx=-1):\n\n\
+Returns the bitrate of this VorbisFile";
 
 FDEF(ov_serialnumber) \
-     "x.serialnumber(link):\n\n\
-Returns the serialnumber of this VorbisFile. What's link do?";
+"x.serialnumber(stream_idx=-1):\n\n\
+Returns the serialnumber of this VorbisFile.";
 
 FDEF(ov_bitrate_instant) \
      "Returns the bitrate_instant value for this VorbisFile.";
 
-     FDEF(ov_raw_total) "x.raw_total(i):\n\n\
-Returns the raw_total value for this VorbisFile. What's i?";
+FDEF(ov_raw_total) \
+"x.raw_total(stream_idx=-1):\n\n\
+Returns the raw_total value for this VorbisFile.";
 
 FDEF(ov_pcm_total) \
-     "x.pcm_total(i):\n\n\
-Returns the pcm_total value for this VorbisFile. What's i?";
+"x.pcm_total(stream_idx=-1):\n\n\
+Returns the pcm_total value for this VorbisFile.";
 
 FDEF(ov_time_total) \
-     "x.time_total(i):\n\n\
-Returns the time_total value for this VorbisFile. What's i?";
+"x.time_total(stream_idx=-1):\n\n\
+Returns the time_total value for this VorbisFile.";
 
 FDEF(ov_raw_seek) "x.raw_seek(pos):\n\nSeeks to raw position pos.";
 FDEF(ov_pcm_seek) "x.pcm_seek(pos):\n\nSeeks to pcm position pos.";
@@ -70,7 +73,8 @@
 FDEF(ov_info) "Return an info object for this file";
 
 FDEF(ov_comment) \
-     "Returns a dictionary of lists for the comments in this file.\n\
+"x.comment(stream_idx=-1)\n\n\
+Returns a dictionary of lists for the comments in this file.\n\
 All values are stored in uppercase, since values should be case-insensitive.";
 
 static PyObject *py_ov_file_getattr(PyObject *, char *name);
@@ -131,7 +135,7 @@
    METH_VARARGS, py_ov_serialnumber_doc},
   {"bitrate_instant",  py_ov_bitrate_instant,  
    METH_VARARGS, py_ov_bitrate_instant_doc},
-  {"raw_total",  py_ov_raw_total,  
+  {"raw_total",  py_ov_raw_total,
    METH_VARARGS, py_ov_raw_total_doc},
   {"pcm_total",  py_ov_pcm_total,  
    METH_VARARGS, py_ov_pcm_total_doc},
@@ -363,12 +367,12 @@
 {
   py_vorbisfile * ov_self = (py_vorbisfile *) self;
   long val;
-  int i;
+  int stream_idx = -1;
   
-  if (!PyArg_ParseTuple(args, "i", &i)) 
+  if (!PyArg_ParseTuple(args, "|i", &stream_idx))
     return NULL;
 
-  val = ov_bitrate(ov_self->ovf, i);
+  val = ov_bitrate(ov_self->ovf, stream_idx);
   if (val < 0)
     return v_error_from_code(val, "Error getting bitrate: ");
   return PyInt_FromLong(val);
@@ -379,12 +383,12 @@
 {
   py_vorbisfile * ov_self = (py_vorbisfile *) self;
   long val;
-  int i;
+  int stream_idx = -1;
   
-  if (!PyArg_ParseTuple(args, "i", &i)) 
+  if (!PyArg_ParseTuple(args, "|i", &stream_idx)) 
     return NULL;
 
-  val = ov_serialnumber(ov_self->ovf, i);
+  val = ov_serialnumber(ov_self->ovf, stream_idx);
   return PyInt_FromLong(val);
 }
 
@@ -408,12 +412,12 @@
 {
   py_vorbisfile * ov_self = (py_vorbisfile *) self;
   ogg_int64_t val;
-  int i;
+  int stream_idx = -1;
 
-  if(!PyArg_ParseTuple(args, "i", &i)) 
+  if (!PyArg_ParseTuple(args, "|i", &stream_idx)) 
     return NULL;
 
-  val = ov_raw_total(ov_self->ovf, i);
+  val = ov_raw_total(ov_self->ovf, stream_idx);
   if (val < 0)
     return v_error_from_code(val, "Error in ov_raw_total: ");
   return PyLong_FromLongLong(val);
@@ -424,12 +428,12 @@
 {
   py_vorbisfile * ov_self = (py_vorbisfile *) self;
   ogg_int64_t val;
-  int i;
+  int stream_idx = -1;
 
-  if (!PyArg_ParseTuple(args, "i", &i)) 
+  if (!PyArg_ParseTuple(args, "|i", &stream_idx)) 
     return NULL;
 
-  val = ov_pcm_total(ov_self->ovf, i);
+  val = ov_pcm_total(ov_self->ovf, stream_idx);
   if (val < 0)
     return v_error_from_code(val, "Error in ov_pcm_total: ");
   return PyLong_FromLongLong(val);
@@ -440,12 +444,12 @@
 {
   py_vorbisfile * ov_self = (py_vorbisfile *) self;
   double val;
-  int i;
+  int stream_idx = -1;
 
-  if (!PyArg_ParseTuple(args, "i", &i)) 
+  if (!PyArg_ParseTuple(args, "|i", &stream_idx))
     return NULL;
 
-  val = ov_time_total(ov_self->ovf, i);
+  val = ov_time_total(ov_self->ovf, stream_idx);
   if (val < 0)
     return v_error_from_code(val, "Error in ov_time_total: ");
   return PyFloat_FromDouble(val);
@@ -572,13 +576,13 @@
 py_ov_info(PyObject *self, PyObject *args)
 {
   py_vorbisfile *ov_self = (py_vorbisfile *) self;
-  int link = -1;
+  int stream_idx = -1;
   vorbis_info *vi;
 
-  if (!PyArg_ParseTuple(args, "|i", &link))
+  if (!PyArg_ParseTuple(args, "|i", &stream_idx))
     return NULL;
 
-  vi = ov_info(ov_self->ovf, link);
+  vi = ov_info(ov_self->ovf, stream_idx);
   if (!vi) {
     PyErr_SetString(PyExc_RuntimeError, "Couldn't get info for VorbisFile.");
     return NULL;
@@ -594,12 +598,12 @@
   py_vorbisfile *ov_self = (py_vorbisfile *) self;
   vorbis_comment *comments;
 
-  int link = -1;
+  int stream_idx = -1;
   
-  if (!PyArg_ParseTuple(args, "|i", &link))
+  if (!PyArg_ParseTuple(args, "|i", &stream_idx))
     return NULL;
 
-  comments = ov_comment(ov_self->ovf, link);
+  comments = ov_comment(ov_self->ovf, stream_idx);
   if (!comments) {
     PyErr_SetString(PyExc_RuntimeError, "Couldn't get comments");
     return NULL;

<p><p>1.19      +95 -4     vorbis-python/src/pyvorbisinfo.c

Index: pyvorbisinfo.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis-python/src/pyvorbisinfo.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- pyvorbisinfo.c	9 Sep 2003 03:40:48 -0000	1.18
+++ pyvorbisinfo.c	19 Dec 2003 07:54:40 -0000	1.19
@@ -36,6 +36,8 @@
 "A VorbisInfo object stores information about a Vorbis stream.\n\
 Information is stored as attributes.";
 
+static PyObject *py_ov_info_str(PyObject *);
+
 PyTypeObject py_vinfo_type = {
   PyObject_HEAD_INIT(NULL)
   0,
@@ -57,7 +59,7 @@
   0, /* as mapping */
   0, /* hash */
   0, /* binary */
-  0, /* repr */
+  &py_ov_info_str, /* repr */
   0, /* getattro */
   0, /* setattro */
   0, /* as buffer */
@@ -169,6 +171,7 @@
     CMP_RET(bitrate_upper);
     CMP_RET(bitrate_nominal);
     CMP_RET(bitrate_lower);
+    CMP_RET(bitrate_window);
     break;
   case 'c':
     CMP_RET(channels);
@@ -186,6 +189,38 @@
   return NULL;
 }
 
+#define ADD_FIELD(field) \
+  { \
+    int added = snprintf(cur, buf_left, "  %s: %d\n", #field, vi->field); \
+    cur += added; \
+    buf_left -= added; \
+  }
+
+static PyObject *
+py_ov_info_str(PyObject *self) 
+{
+  PyObject *ret = NULL;
+  char buf[1000];
+  char *cur = &buf[0];
+  int buf_left = sizeof(buf) - 1;
+  vorbis_info *vi = PY_VINFO(self);
+
+  int added = snprintf(cur, buf_left, "<VorbisInfo>\n");
+  cur += added;
+  buf_left -= added;
+
+  ADD_FIELD(version);
+  ADD_FIELD(channels);
+  ADD_FIELD(rate);
+  ADD_FIELD(bitrate_upper);
+  ADD_FIELD(bitrate_nominal);
+  ADD_FIELD(bitrate_lower);
+  ADD_FIELD(bitrate_window);
+  
+  ret = PyString_FromString(buf);
+  return ret;
+}
+
 static PyObject *
 py_vorbis_analysis_init(PyObject *self, PyObject *args)
 {
@@ -280,8 +315,10 @@
 A comment object also has the keys() items() and values() functions that\n\
 dictionaries have, the difference being that the lists in items() and\n\
 values() are flattened. So if there are two 'Artist' entries, there will\n\
-be two separate tuples in items() for 'Artist' and two strings for 'Artist'\n\
-in value().";
+be two separate tuples in items() for 'Artist' and two strings for \n\
+'Artist' in value().";
+
+static PyObject *py_vcomment_str(PyObject *);
 
 PyTypeObject py_vcomment_type = {
   PyObject_HEAD_INIT(NULL)
@@ -304,7 +341,7 @@
   &py_vcomment_Mapping_Methods,
   0, /* hash */
   0, /* binary */
-  0, /* repr */
+  &py_vcomment_str, /* repr */
   0, /* getattro */
   0, /* setattro */
   0, /* as buffer */
@@ -384,6 +421,60 @@
   PyMem_DEL(self);
 }
 
+
+static PyObject*
+py_vcomment_str(PyObject *self) 
+{
+#if PY_UNICODE
+  py_vcomment *vc_self = (py_vcomment *) self;
+  int k, buf_len = 0;
+  char *buf, *cur;
+  PyObject *ret = NULL;
+
+  static const char *message = "<VorbisComment>\n";
+  int message_len = strlen(message);
+  static const char *prefix = "  "; /* goes before each line */
+  int prefix_len = strlen(prefix);
+  static const char *suffix = "\n"; /* after each line */
+  int suffix_len = strlen(suffix);
+
+  /* first figure out how much space we need */
+  for (k = 0; k < vc_self->vc->comments; ++k) {
+    buf_len += vc_self->vc->comment_lengths[k];
+  }
+
+  /* add space for prefix/suffix and a trailing \0 */
+  buf_len += vc_self->vc->comments * (prefix_len + suffix_len) + 1; 
+  buf_len += message_len;
+  buf = malloc(buf_len);
+
+  strcpy(buf, message);
+  cur = buf + message_len;
+
+  /* now copy the comments in */
+  for (k = 0; k < vc_self->vc->comments; ++k) {
+    int comment_len = vc_self->vc->comment_lengths[k];
+    
+    strncpy(cur, prefix, prefix_len);
+    cur += prefix_len;
+
+    strncpy(cur, vc_self->vc->user_comments[k], comment_len);
+    cur += comment_len;
+
+    strncpy(cur, suffix, suffix_len);
+    cur += suffix_len;
+  }
+  buf[buf_len - 1] = '\0';
+
+  ret = PyUnicode_DecodeUTF8(buf, buf_len, NULL);
+  free(buf);
+  return ret;
+#else
+  /* We can't do much without unicode */
+  return PyString_FromString("<VorbisComment>");
+#endif
+}
+
 static PyObject*
 py_vorbis_comment_getattr(PyObject *self, char *name)
 {

<p><p>--- >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