diff options
| author | Matthias Geier <Matthias.Geier@gmail.com> | 2019-09-17 20:11:40 +0200 |
|---|---|---|
| committer | Matthias Geier <Matthias.Geier@gmail.com> | 2019-09-18 13:10:25 +0200 |
| commit | e8253a615d6f70f7a1f6bc662e703a857fd2e8c4 (patch) | |
| tree | 788e9cc9efbf15cd2656afb7573cd0c68143d249 /doc | |
| parent | f5d8fd58924e12fac69c83cd9e3c6250ed8ad868 (diff) | |
DOC: Documentation overhaul
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/README | 2 | ||||
| -rw-r--r-- | doc/usage.rst | 111 | ||||
| -rw-r--r-- | doc/version-history.rst | 2 |
3 files changed, 78 insertions, 37 deletions
diff --git a/doc/README b/doc/README new file mode 100644 index 0000000..ceb9350 --- /dev/null +++ b/doc/README @@ -0,0 +1,2 @@ +This directory contains the source files for the user documentation. +The rendered result can be found at https://python-sounddevice.readthedocs.io/ diff --git a/doc/usage.rst b/doc/usage.rst index 37e49a6..53b33d0 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -1,3 +1,5 @@ +.. currentmodule:: sounddevice + Usage ===== @@ -12,21 +14,28 @@ Playback Assuming you have a NumPy array named ``myarray`` holding audio data with a sampling frequency of ``fs`` (in the most cases this will be 44100 or 48000 -frames per second), you can play it back with `sounddevice.play()`: +frames per second), you can play it back with `play()`: .. code:: python sd.play(myarray, fs) This function returns immediately but continues playing the audio signal in the -background. You can stop playback with `sounddevice.stop()`: +background. You can stop playback with `stop()`: .. code:: python sd.stop() +If you want to block the Python interpreter until playback is finished, +you can use `wait()`: + +.. code:: python + + sd.wait() + If you know that you will use the same sampling frequency for a while, you can -set it as default using `sounddevice.default.samplerate`: +set it as default using `default.samplerate`: .. code:: python @@ -38,18 +47,23 @@ After that, you can drop the *samplerate* argument: sd.play(myarray) +.. note:: + + If you don't specify the correct sampling frequency, + the sound might be played back too slow or too fast! + Recording --------- -To record audio data from your sound device into a NumPy array, use -`sounddevice.rec()`: +To record audio data from your sound device into a NumPy array, +you can use `rec()`: .. code:: python duration = 10.5 # seconds myrecording = sd.rec(int(duration * fs), samplerate=fs, channels=2) -Again, for repeated use you can set defaults using `sounddevice.default`: +Again, for repeated use you can set defaults using `default`: .. code:: python @@ -62,41 +76,35 @@ After that, you can drop the additional arguments: myrecording = sd.rec(int(duration * fs)) -This function also returns immediately but continues recording in the -background. In the meantime, you can run other commands. If you want to check -if the recording is finished, you should use `sounddevice.wait()`: +This function also returns immediately but continues recording in the background. +In the meantime, you can run other commands. +If you want to check if the recording is finished, you should use `wait()`: .. code:: python sd.wait() -If the recording was already finished, this returns immediately; if not, it -waits and returns as soon as the recording is finished. +If the recording was already finished, this returns immediately; +if not, it waits and returns as soon as the recording is finished. -Alternatively, you could have used the *blocking* argument in the first place: +By default, the recorded array has the data type ``'float32'`` +(see `default.dtype`), but this can be changed with the *dtype* argument: .. code:: python - myrecording = sd.rec(duration * fs, blocking=True) - -By default, the recorded array has the data type ``'float32'`` (see -`sounddevice.default.dtype`), but this can be changed with the *dtype* argument: - -.. code:: python - - myrecording = sd.rec(duration * fs, dtype='float64') + myrecording = sd.rec(int(duration * fs), dtype='float64') Simultaneous Playback and Recording ----------------------------------- -To play back an array and record at the same time, use `sounddevice.playrec()`: +To play back an array and record at the same time, you can use `playrec()`: .. code:: python myrecording = sd.playrec(myarray, fs, channels=2) -The number of output channels is obtained from ``myarray``, but the number of -input channels still has to be specified. +The number of output channels is obtained from ``myarray``, +but the number of input channels still has to be specified. Again, default values can be used: @@ -107,26 +115,27 @@ Again, default values can be used: myrecording = sd.playrec(myarray) In this case the number of output channels is still taken from ``myarray`` -(which may or may not have 2 channels), but the number of input channels is -taken from `sounddevice.default.channels`. +(which may or may not have 2 channels), +but the number of input channels is taken from `default.channels`. Device Selection ---------------- In many cases, the default input/output device(s) will be the one(s) you want, but it is of course possible to choose a different device. -Use `sounddevice.query_devices()` to get a list of supported devices. +Use `query_devices()` to get a list of supported devices. The same list can be obtained from a terminal by typing the command :: python3 -m sounddevice You can use the corresponding device ID to select a desired device by assigning -to `sounddevice.default.device` or by passing it as *device* argument to -`sounddevice.play()`, `sounddevice.Stream()` etc. +to `default.device` or by passing it as *device* argument to +`play()`, `Stream()` etc. Instead of the numerical device ID, you can also use a space-separated list of -case-insensitive substrings of the device name (and the host API name, if -needed). See `sounddevice.default.device` for details. +case-insensitive substrings of the device name +(and the host API name, if needed). +See `default.device` for details. .. code:: python @@ -138,7 +147,25 @@ needed). See `sounddevice.default.device` for details. Callback Streams ---------------- -Callback "wire" with `sounddevice.Stream`: +The aforementioned convenience functions `play()`, `rec()` and `playrec()` +(as well as the related functions `wait()`, `stop()`, `get_status()` and +`get_stream()`) are designed for small scripts and interactive use +(e.g. in a Jupyter_ notebook). +They are supposed to be simple and convenient, +but their use cases are quite limited. + +If you need more control (e.g. continuous recording, realtime processing, ...), +you should use the lower-level "stream" classes +(e.g. `Stream`, `InputStream`, `RawInputStream`), +either with the "non-blocking" callback interface or with the "blocking" +`Stream.read()` and `Stream.write()` methods, see `Blocking Read/Write Streams`_. + +As an example for the "non-blocking" interface, +the following code creates a `Stream` with a callback function +that obtains audio data from the input channels +and simply forwards everything to the output channels +(be careful with the output volume, because this might cause acoustic feedback +if your microphone is close to your loudspeakers): .. code:: python @@ -153,7 +180,8 @@ Callback "wire" with `sounddevice.Stream`: with sd.Stream(channels=2, callback=callback): sd.sleep(int(duration * 1000)) -Same thing with `sounddevice.RawStream`: +The same thing can be done with `RawStream` +(NumPy_ doesn't have to be installed): .. code:: python @@ -171,11 +199,20 @@ Same thing with `sounddevice.RawStream`: .. note:: We are using 24-bit samples here for no particular reason (just because we can). +You can of course extend the callback functions +to do arbitrarily more complicated stuff. +You can also use streams without inputs (e.g. `OutputStream`) +or streams without outputs (e.g. `InputStream`). + +See :doc:`examples` for more examples. + Blocking Read/Write Streams --------------------------- -Instead of using a callback function, you can also use the blocking methods -`sounddevice.Stream.read()` and `sounddevice.Stream.write()` (and of course the -corresponding methods in `sounddevice.InputStream`, `sounddevice.OutputStream`, -`sounddevice.RawStream`, `sounddevice.RawInputStream` and -`sounddevice.RawOutputStream`). +Instead of using a callback function, +you can also use the "blocking" methods `Stream.read()` and `Stream.write()` +(and of course the corresponding methods in `InputStream`, `OutputStream`, +`RawStream`, `RawInputStream` and `RawOutputStream`). + +.. _Jupyter: https://jupyter.org/ +.. _NumPy: https://numpy.org/ diff --git a/doc/version-history.rst b/doc/version-history.rst index 04c6953..f8d8b7c 100644 --- a/doc/version-history.rst +++ b/doc/version-history.rst @@ -1,6 +1,8 @@ Version History =============== +.. currentmodule:: sounddevice + .. default-role:: py:obj .. include:: ../NEWS.rst |
