doc/python_module.rst: add demo_source_simple.py
authorPaul Brossier <piem@piem.org>
Fri, 26 Aug 2016 22:39:56 +0000 (00:39 +0200)
committerPaul Brossier <piem@piem.org>
Fri, 26 Aug 2016 22:39:56 +0000 (00:39 +0200)
doc/python_module.rst
python/demos/demo_source_simple.py [new file with mode: 0644]

index 3568ba8..17819b3 100644 (file)
@@ -1,9 +1,15 @@
+.. _python:
+
 Python module
 =============
 
+The aubio extension for Python is available for Python 2.7 and Python 3.
+
 Installing aubio with pip
 -------------------------
 
+aubio can now be installed using ``pip`` and ``easy_install``.
+
 .. code-block:: bash
 
     $ pip install aubio
@@ -25,31 +31,25 @@ Using aubio in python
 Once you have python-aubio installed, you should be able to run ``python -c
 "import aubio"``.
 
-A very simple example
-.....................
+A simple example
+................
 
-Here is a very simple script
-to read all the samples from a media file:
+Here is a :download:`simple script <../python/demos/demo_source_simple.py>`
+that reads all the samples from a media file:
 
-.. code-block:: python
-
-        #! /usr/bin/env python
-        import aubio
-
-        s = aubio.source(sys.argv[1], 0, 256)
-        while True:
-          samples, read = s()
-          #print(samples)
-          if read < 256: break
+.. literalinclude:: ../python/demos/demo_source_simple.py
+   :language: python
 
 Filtering an input sound file
 .............................
 
-Here is a more complete example, `demo_filter.py`_. This files executes the following:
+Here is a more complete example, :download:`demo_filter.py
+<../python/demos/demo_filter.py>`. This files executes the following:
 
 * read an input media file (``aubio.source``)
 
-* filter it using an A-weighting filter (``aubio.digital_filter``)
+* filter it using an `A-weighting <https://en.wikipedia.org/wiki/A-weighting>`_
+  filter (``aubio.digital_filter``)
 
 * write result to a new file (``aubio.sink``)
 
@@ -67,7 +67,7 @@ Python tests
 A number of `python tests`_ are provided. To run them, use
 ``python/tests/run_all_tests``.
 
-.. _python tests folder: https://github.com/aubio/aubio/blob/master/python/tests
 .. _python demos folder: https://github.com/aubio/aubio/blob/master/python/demos
 .. _demo_filter.py: https://github.com/aubio/aubio/blob/master/python/demos/demo_filter.py
+.. _python tests: https://github.com/aubio/aubio/blob/master/python/tests
 
diff --git a/python/demos/demo_source_simple.py b/python/demos/demo_source_simple.py
new file mode 100644 (file)
index 0000000..e966032
--- /dev/null
@@ -0,0 +1,16 @@
+#! /usr/bin/env python
+import sys, aubio
+
+samplerate = 0  # use original source samplerate
+hop_size = 256 # number of frames to read in one block
+s = aubio.source(sys.argv[1], samplerate, hop_size)
+total_frames = 0
+
+while True: # reading loop
+  samples, read = s()
+  total_frames += read
+  if read < hop_size: break # end of file reached
+
+fmt_string = "read {:d} frames at {:d}Hz from {:s}"
+print (fmt_string.format(total_frames, s.samplerate, sys.argv[1]))
+