Commits

Lenard Lindstrom committed eefcb56

Add __array_interface__ property to pygame.mixer.Sound

Passes mixer module units tests form Python 2.7 and Python 3.3 on Linux Mint.

Comments (0)

Files changed (2)

     return cobj;
 }
 
+static PyObject*
+snd_get_arrayinterface (PyObject* self, void* closure)
+{
+    Py_buffer view;
+    PyObject* dict;
+
+    if (PyObject_GetBuffer (self, &view, PyBUF_RECORDS)) {
+        return 0;
+    }
+    dict = PgBuffer_AsArrayInterface (&view);
+    PyBuffer_Release (&view);
+    return dict;
+}
+
 static PyObject *
 snd_get_samples_address(PyObject *self, PyObject *closure)
 {
 static PyGetSetDef sound_getset[] =
 {
     {"__array_struct__", snd_get_arraystruct, NULL, "Version 3", NULL},
+    {"__array_interface__", snd_get_arrayinterface, NULL, "Version 3", NULL},
     {"_samples_address", (getter)snd_get_samples_address,
      NULL, "samples buffer address (readonly)", NULL },
     {NULL, NULL, NULL, NULL, NULL}

test/mixer_test.py

                         # Some formats (e.g. -8) may not be supported.
                         continue
                     for c, a in a_lists[format]:
-                        self._test_array_interface(format, a, c == channels)
+                        self._test_array_argument(format, a, c == channels)
                 finally:
                     mixer.quit()
 
-    def _test_array_interface(self, format, a, test_pass):
+    def _test_array_argument(self, format, a, test_pass):
         from numpy import array, all as all_
 
         try:
     def _test_array_interface_fail(self, a):
         self.assertRaises(ValueError, mixer.Sound, array=a)
  
+    def test_array_interface(self):
+        mixer.init(22050, -16, 1)
+        try:
+            snd = mixer.Sound(as_bytes('\x00\x7f') * 20)
+            d = snd.__array_interface__
+            self.assertTrue(isinstance(d, dict))
+            if pygame.get_sdl_byteorder() == pygame.LIL_ENDIAN:
+                typestr = '<i2'
+            else:
+                typestr = '>i2'
+            self.assertEqual(d['typestr'], typestr)
+            self.assertEqual(d['shape'], (20,))
+            self.assertEqual(d['strides'], (2,))
+            self.assertEqual(d['data'], (snd._samples_address, False))
+        finally:
+            mixer.quit()
+
     def test_get_raw(self):
         from ctypes import pythonapi, c_void_p, py_object