Index: python/ext/py-source.c
===================================================================
--- python/ext/py-source.c	(revision 16e2bb090f9f805afefcd58e73f21b20c9be822e)
+++ python/ext/py-source.c	(revision 58eb25022baf1c2c9d1aeda0833368778ca106f5)
@@ -17,66 +17,314 @@
 
 static char Py_source_doc[] = ""
-"   __new__(path, samplerate = 0, hop_size = 512, channels = 1)\n"
-"\n"
-"       Create a new source, opening the given path for reading.\n"
-"\n"
-"       Examples\n"
-"       --------\n"
-"\n"
-"       Create a new source, using the original samplerate, with hop_size = 512:\n"
-"\n"
-"       >>> source('/tmp/t.wav')\n"
-"\n"
-"       Create a new source, resampling the original to 8000Hz:\n"
-"\n"
-"       >>> source('/tmp/t.wav', samplerate = 8000)\n"
-"\n"
-"       Create a new source, resampling it at 32000Hz, hop_size = 32:\n"
-"\n"
-"       >>> source('/tmp/t.wav', samplerate = 32000, hop_size = 32)\n"
-"\n"
-"       Create a new source, using its original samplerate:\n"
-"\n"
-"       >>> source('/tmp/t.wav', samplerate = 0)\n"
-"\n"
-"   __call__()\n"
-"       vec, read = x() <==> vec, read = x.do()\n"
-"\n"
-"       Read vector from source.\n"
-"\n"
-"       See also\n"
-"       --------\n"
-"       aubio.source.do\n"
-"\n";
+"source(path, samplerate=0, hop_size=512, channels=0)\n"
+"\n"
+"Create a new source, opening the given pathname for reading.\n"
+"\n"
+"`source` open the file specified in `path` and creates a callable\n"
+"returning `hop_size` new audio samples at each invocation.\n"
+"\n"
+"If `samplerate=0` (default), the original sampling rate of `path`\n"
+"will be used. Otherwise, the output audio samples will be\n"
+"resampled at the desired sampling-rate.\n"
+"\n"
+"If `channels=0` (default), the original number of channels\n"
+"in `path` will be used. Otherwise, the output audio samples\n"
+"will be down-mixed or up-mixed to the desired number of\n"
+"channels.\n"
+"\n"
+"If `path` is a URL, a remote connection will be attempted to\n"
+"open the resource and stream data from it.\n"
+"\n"
+"The parameter `hop_size` determines how many samples should be\n"
+"read at each consecutive calls.\n"
+"\n"
+"Parameters\n"
+"----------\n"
+"path : str\n"
+"   pathname (or URL) of the file to be opened for reading\n"
+"samplerate : int, optional\n"
+"   sampling rate of the file\n"
+"hop_size : int, optional\n"
+"   number of samples to be read per iteration\n"
+"channels : int, optional\n"
+"   number of channels of the file\n"
+"\n"
+"Examples\n"
+"--------\n"
+"By default, when only `path` is given, the file will be opened\n"
+"with its original sampling rate and channel:\n"
+"\n"
+">>> src = aubio.source('stereo.wav')\n"
+">>> src.uri, src.samplerate, src.channels, src.duration\n"
+"('stereo.wav', 48000, 2, 86833)\n"
+"\n"
+"A typical loop to read all samples from a local file could\n"
+"look like this:\n"
+"\n"
+">>> src = aubio.source('stereo.wav')\n"
+">>> total_read = 0\n"
+">>> while True:\n"
+"...     samples, read = src()\n"
+"...     # do something with samples\n"
+"...     total_read += read\n"
+"...     if read < src.hop_size:\n"
+"...         break\n"
+"...\n"
+"\n"
+"In a more Pythonic way, it can also look like this:\n"
+"\n"
+">>> total_read = 0\n"
+">>> with aubio.source('stereo.wav') as src:\n"
+"...     for frames in src:\n"
+"...         total_read += samples.shape[-1]\n"
+"...\n"
+"\n"
+".. rubric:: Basic interface\n"
+"\n"
+"`source` is a **callable**; its :meth:`__call__` method\n"
+"returns a tuple containing:\n"
+"\n"
+"- a vector of shape `(hop_size,)`, filled with the `read` next\n"
+"  samples available, zero-padded if `read < hop_size`\n"
+"- `read`, an integer indicating the number of samples read\n"
+"\n"
+"To read the first `hop_size` samples from the source, simply call\n"
+"the instance itself, with no argument:\n"
+"\n"
+">>> src = aubio.source('song.ogg')\n"
+">>> samples, read = src()\n"
+">>> samples.shape, read, src.hop_size\n"
+"((512,), 512, 512)\n"
+"\n"
+"The first call returned the slice of samples `[0 : hop_size]`.\n"
+"The next call will return samples `[hop_size: 2*hop_size]`.\n"
+"\n"
+"After several invocations of :meth:`__call__`, when reaching the end\n"
+"of the opened stream, `read` might become less than `hop_size`:\n"
+"\n"
+">>> samples, read = src()\n"
+">>> samples.shape, read\n"
+"((512,), 354)\n"
+"\n"
+"The end of the vector `samples` is filled with zeros.\n"
+"\n"
+"After the end of the stream, `read` will be `0` since no more\n"
+"samples are available:\n"
+"\n"
+">>> samples, read = src()\n"
+">>> samples.shape, read\n"
+"((512,), 0)\n"
+"\n"
+"**Note**: when the source has more than one channels, they\n"
+"are be down-mixed to mono when invoking :meth:`__call__`.\n"
+"To read from each individual channel, see :meth:`__next__`.\n"
+"\n"
+".. rubric:: ``for`` statements\n"
+"\n"
+"The `source` objects are **iterables**. This allows using them\n"
+"directly in a ``for`` loop, which calls :meth:`__next__` until\n"
+"the end of the stream is reached:\n"
+"\n"
+">>> src = aubio.source('stereo.wav')\n"
+">>> for frames in src:\n"
+">>>     print (frames.shape)\n"
+"...\n"
+"(2, 512)\n"
+"(2, 512)\n"
+"(2, 230)\n"
+"\n"
+"**Note**: When `next(self)` is called on a source with multiple\n"
+"channels, an array of shape `(channels, read)` is returned,\n"
+"unlike with :meth:`__call__` which always returns the down-mixed\n"
+"channels.\n"
+"\n"
+"If the file is opened with a single channel, `next(self)` returns\n"
+"an array of shape `(read,)`:\n"
+"\n"
+">>> src = aubio.source('stereo.wav', channels=1)\n"
+">>> next(src).shape\n"
+"(512,)\n"
+"\n"
+".. rubric:: ``with`` statements\n"
+"\n"
+"The `source` objects are **context managers**, which allows using\n"
+"them in ``with`` statements:\n"
+"\n"
+">>> with aubio.source('audiotrack.wav') as source:\n"
+"...     n_frames=0\n"
+"...     for samples in source:\n"
+"...         n_frames += len(samples)\n"
+"...     print('read', n_frames, 'samples in', samples.shape[0], 'channels',\n"
+"...         'from file \"\%s\"' \% source.uri)\n"
+"...\n"
+"read 239334 samples in 2 channels from file \"audiotrack.wav\"\n"
+"\n"
+"The file will be closed before exiting the statement.\n"
+"\n"
+"See also the methods implementing the context manager,\n"
+":meth:`__enter__` and :meth:`__exit__`.\n"
+"\n"
+".. rubric:: Seeking and closing\n"
+"\n"
+"At any time, :meth:`seek` can be used to move to any position in\n"
+"the file. For instance, to rewind to the start of the stream:\n"
+"\n"
+">>> src.seek(0)\n"
+"\n"
+"The opened file will be automatically closed when the object falls\n"
+"out of scope and is scheduled for garbage collection.\n"
+"\n"
+"In some cases, it is useful to manually :meth:`close` a given source,\n"
+"for instance to limit the number of simultaneously opened files:\n"
+"\n"
+">>> src.close()\n"
+"\n"
+".. rubric:: Input formats\n"
+"\n"
+"Depending on how aubio was compiled, :class:`source` may or may not\n"
+"open certain **files format**. Below are some examples that assume\n"
+"support for compressed files and remote urls was compiled in:\n"
+"\n"
+"- open a local file using its original sampling rate and channels,\n"
+"  and with the default hop size:\n"
+"\n"
+">>> s = aubio.source('sample.wav')\n"
+">>> s.uri, s.samplerate, s.channels, s.hop_size\n"
+"('sample.wav', 44100, 2, 512)\n"
+"\n"
+"- open a local compressed audio file, resampling to 32000Hz if needed:\n"
+"\n"
+">>> s = aubio.source('song.mp3', samplerate=32000)\n"
+">>> s.uri, s.samplerate, s.channels, s.hop_size\n"
+"('song.mp3', 32000, 2, 512)\n"
+"\n"
+"- open a local video file, down-mixing and resampling it to 16kHz:\n"
+"\n"
+">>> s = aubio.source('movie.mp4', samplerate=16000, channels=1)\n"
+">>> s.uri, s.samplerate, s.channels, s.hop_size\n"
+"('movie.mp4', 16000, 1, 512)\n"
+"\n"
+"- open a remote resource, with hop_size = 1024:\n"
+"\n"
+">>> s = aubio.source('https://aubio.org/drum.ogg', hop_size=1024)\n"
+">>> s.uri, s.samplerate, s.channels, s.hop_size\n"
+"('https://aubio.org/drum.ogg', 48000, 2, 1024)\n"
+"\n"
+"See Also\n"
+"--------\n"
+"sink: write audio samples to a file.\n"
+"";
 
 static char Py_source_get_samplerate_doc[] = ""
-"x.get_samplerate() -> source samplerate\n"
-"\n"
-"Get samplerate of source.";
+"get_samplerate()\n"
+"\n"
+"Get sampling rate of source.\n"
+"\n"
+"Returns\n"
+"-------\n"
+"int\n"
+"    Sampling rate, in Hz.\n"
+"";
 
 static char Py_source_get_channels_doc[] = ""
-"x.get_channels() -> number of channels\n"
-"\n"
-"Get number of channels in source.";
+"get_channels()\n"
+"\n"
+"Get number of channels in source.\n"
+"\n"
+"Returns\n"
+"-------\n"
+"int\n"
+"    Number of channels.\n"
+"";
 
 static char Py_source_do_doc[] = ""
-"vec, read = x.do() <==> vec, read = x()\n"
-"\n"
-"Read monophonic vector from source.";
+"source.do()\n"
+"\n"
+"Read vector of audio samples.\n"
+"\n"
+"If the audio stream in the source has more than one channel,\n"
+"the channels will be down-mixed.\n"
+"\n"
+"Returns\n"
+"-------\n"
+"samples : numpy.ndarray, shape `(hop_size,)`, dtype aubio.float_type\n"
+"    `fvec` of size `hop_size` containing the new samples.\n"
+"read : int\n"
+"    Number of samples read from the source, equals to `hop_size`\n"
+"    before the end-of-file is reached, less when it is reached,\n"
+"    and `0` after.\n"
+"\n"
+"See Also\n"
+"--------\n"
+"do_multi\n"
+"\n"
+"Examples\n"
+"--------\n"
+">>> src = aubio.source('sample.wav', hop_size=1024)\n"
+">>> src.do()\n"
+"(array([-0.00123001, -0.00036685,  0.00097106, ..., -0.2031033 ,\n"
+"       -0.2025854 , -0.20221856], dtype=" AUBIO_NPY_SMPL_STR "), 1024)\n"
+"";
 
 static char Py_source_do_multi_doc[] = ""
-"mat, read = x.do_multi()\n"
-"\n"
-"Read polyphonic vector from source.";
+"do_multi()\n"
+"\n"
+"Read multiple channels of audio samples.\n"
+"\n"
+"If the source was opened with the same number of channels\n"
+"found in the stream, each channel will be read individually.\n"
+"\n"
+"If the source was opened with less channels than the number\n"
+"of channels in the stream, only the first channels will be read.\n"
+"\n"
+"If the source was opened with more channels than the number\n"
+"of channel in the original stream, the first channels will\n"
+"be duplicated on the additional output channel.\n"
+"\n"
+"Returns\n"
+"-------\n"
+"samples : np.ndarray([hop_size, channels], dtype=aubio.float_type)\n"
+"    NumPy array of shape `(hop_size, channels)` containing the new\n"
+"    audio samples.\n"
+"read : int\n"
+"    Number of samples read from the source, equals to `hop_size`\n"
+"    before the end-of-file is reached, less when it is reached,\n"
+"    and `0` after.\n"
+"\n"
+"See Also\n"
+"--------\n"
+"do\n"
+"\n"
+"Examples\n"
+"--------\n"
+">>> src = aubio.source('sample.wav')\n"
+">>> src.do_multi()\n"
+"(array([[ 0.00668335,  0.0067749 ,  0.00714111, ..., -0.05737305,\n"
+"        -0.05856323, -0.06018066],\n"
+"       [-0.00842285, -0.0072937 , -0.00576782, ..., -0.09405518,\n"
+"        -0.09558105, -0.09725952]], dtype=" AUBIO_NPY_SMPL_STR "), 512)\n"
+"";
 
 static char Py_source_close_doc[] = ""
-"x.close()\n"
-"\n"
-"Close this source now.";
+"close()\n"
+"\n"
+"Close this source now.\n"
+"\n"
+".. note:: Closing twice a source will **not** raise any exception.\n"
+"";
 
 static char Py_source_seek_doc[] = ""
-"x.seek(position)\n"
-"\n"
-"Seek to resampled frame position.";
+"seek(position)\n"
+"\n"
+"Seek to position in file.\n"
+"\n"
+"If the source was not opened with its original sampling-rate,\n"
+"`position` corresponds to the position in the re-sampled file.\n"
+"\n"
+"Parameters\n"
+"----------\n"
+"position : str\n"
+"   position to seek to, in samples\n"
+"";
 
 static PyObject *
@@ -218,13 +466,27 @@
 static PyMemberDef Py_source_members[] = {
   {"uri", T_STRING, offsetof (Py_source, uri), READONLY,
-    "path at which the source was created"},
+    "str (read-only): pathname or URL"},
   {"samplerate", T_INT, offsetof (Py_source, samplerate), READONLY,
-    "samplerate at which the source is viewed"},
+    "int (read-only): sampling rate"},
   {"channels", T_INT, offsetof (Py_source, channels), READONLY,
-    "number of channels found in the source"},
+    "int (read-only): number of channels"},
   {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY,
-    "number of consecutive frames that will be read at each do or do_multi call"},
+    "int (read-only): number of samples read per iteration"},
   {"duration", T_INT, offsetof (Py_source, duration), READONLY,
-    "total number of frames in the source (estimated)"},
+    "int (read-only): total number of frames in the source\n"
+    "\n"
+    "Can be estimated, for instance if the opened stream is\n"
+    "a compressed media or a remote resource.\n"
+    "\n"
+    "Example\n"
+    "-------\n"
+    ">>> n = 0\n"
+    ">>> src = aubio.source('track1.mp3')\n"
+    ">>> for samples in src:\n"
+    "...     n += samples.shape[-1]\n"
+    "...\n"
+    ">>> n, src.duration\n"
+    "(9638784, 9616561)\n"
+    ""},
   { NULL } // sentinel
 };
