annotate doc/command_line_usage.rst @ 423:88e99cfd9c4c

Refactor io
author Amine Sehili <amine.sehili@gmail.com>
date Sat, 19 Oct 2024 14:13:28 +0200
parents df2a320e10d5
children 81bc2375354f
rev   line source
amine@379 1 ``auditok`` can also be used from the command-line. For more information about
amine@379 2 parameters and their description type:
amine@379 3
amine@379 4
amine@379 5 .. code:: bash
amine@379 6
amine@379 7 auditok -h
amine@379 8
amine@379 9 In the following we'll a few examples that covers most use-cases.
amine@379 10
amine@379 11
amine@379 12 Read and split audio data online
amine@379 13 --------------------------------
amine@379 14
amine@379 15 To try ``auditok`` from the command line with you voice, you should either
amine@379 16 install `pyaudio <https://people.csail.mit.edu/hubert/pyaudio>`_ so that ``auditok``
amine@379 17 can directly read data from the microphone, or record data with an external program
amine@379 18 (e.g., `sox`) and redirect its output to ``auditok``.
amine@379 19
amine@379 20 Read data from the microphone (`pyaudio` installed):
amine@379 21
amine@379 22 .. code:: bash
amine@379 23
amine@379 24 auditok
amine@379 25
amine@379 26 This will print the *id*, *start time* and *end time* of each detected audio
amine@379 27 event. Note that we didn't pass any additional arguments to the previous command,
amine@379 28 so ``auditok`` will use default values. The most important arguments are:
amine@379 29
amine@379 30
amine@379 31 - ``-n``, ``--min-duration`` : minimum duration of a valid audio event in seconds, default: 0.2
amine@379 32 - ``-m``, ``--max-duration`` : maximum duration of a valid audio event in seconds, default: 5
amine@379 33 - ``-s``, ``--max-silence`` : maximum duration of a consecutive silence within a valid audio event in seconds, default: 0.3
amine@379 34 - ``-e``, ``--energy-threshold`` : energy threshold for detection, default: 50
amine@379 35
amine@379 36
amine@379 37 Read audio data with an external program
amine@379 38 ----------------------------------------
amine@379 39
amine@379 40 If you don't have `pyaudio`, you can use `sox` for data acquisition
amine@379 41 (`sudo apt-get install sox`) and make ``auditok`` read data from standard input:
amine@379 42
amine@379 43 .. code:: bash
amine@379 44
amine@379 45 rec -q -t raw -r 16000 -c 1 -b 16 -e signed - | auditok - -r 16000 -w 2 -c 1
amine@379 46
amine@379 47 Note that when data is read from standard input, the same audio parameters must
amine@379 48 be used for both `sox` (or any other data generation/acquisition tool) and
amine@379 49 ``auditok``. The following table summarizes audio parameters.
amine@379 50
amine@379 51
amine@379 52 +-----------------+------------+------------------+-----------------------+
amine@379 53 | Audio parameter | sox option | `auditok` option | `auditok` default |
amine@379 54 +=================+============+==================+=======================+
amine@379 55 | Sampling rate | -r | -r | 16000 |
amine@379 56 +-----------------+------------+------------------+-----------------------+
amine@379 57 | Sample width | -b (bits) | -w (bytes) | 2 |
amine@379 58 +-----------------+------------+------------------+-----------------------+
amine@379 59 | Channels | -c | -c | 1 |
amine@379 60 +-----------------+------------+------------------+-----------------------+
amine@379 61 | Encoding | -e | NA | always a signed int |
amine@379 62 +-----------------+------------+------------------+-----------------------+
amine@379 63
amine@379 64 According to this table, the previous command can be run with the default
amine@379 65 parameters as:
amine@379 66
amine@379 67 .. code:: bash
amine@379 68
amine@379 69 rec -q -t raw -r 16000 -c 1 -b 16 -e signed - | auditok -i -
amine@379 70
amine@379 71 Play back audio detections
amine@379 72 --------------------------
amine@379 73
amine@379 74 Use the ``-E`` option (for echo):
amine@379 75
amine@379 76 .. code:: bash
amine@379 77
amine@379 78 auditok -E
amine@379 79 # or
amine@379 80 rec -q -t raw -r 16000 -c 1 -b 16 -e signed - | auditok - -E
amine@379 81
amine@379 82 The second command works without further argument because data is recorded with
amine@379 83 ``auditok``'s default audio parameters . If one of the parameters is not at the
amine@379 84 default value you should specify it alongside ``-E``.
amine@379 85
amine@379 86
amine@379 87
amine@379 88 Using ``-E`` requires `pyaudio`, if it's not installed you can use the ``-C``
amine@379 89 (used to run an external command with detected audio event as argument):
amine@379 90
amine@379 91 .. code:: bash
amine@379 92
amine@379 93 rec -q -t raw -r 16000 -c 1 -b 16 -e signed - | auditok - -C "play -q {file}"
amine@379 94
amine@379 95 Using the ``-C`` option, ``auditok`` will save a detected event to a temporary wav
amine@379 96 file, fill the ``{file}`` placeholder with the temporary name and run the
amine@379 97 command. In the above example we used ``-C`` to play audio data with an external
amine@379 98 program but you can use it to run any other command.
amine@379 99
amine@379 100
amine@379 101 Print out detection information
amine@379 102 -------------------------------
amine@379 103
amine@379 104 By default ``auditok`` prints out the **id**, the **start** and the **end** of
amine@379 105 each detected audio event. The latter two values represent the absolute position
amine@379 106 of the event within input stream (file or microphone) in seconds. The following
amine@379 107 listing is an example output with the default format:
amine@379 108
amine@379 109 .. code:: bash
amine@379 110
amine@379 111 1 1.160 2.390
amine@379 112 2 3.420 4.330
amine@379 113 3 5.010 5.720
amine@379 114 4 7.230 7.800
amine@379 115
amine@379 116 The format of the output is controlled by the ``--printf`` option. Alongside
amine@379 117 ``{id}``, ``{start}`` and ``{end}`` placeholders, you can use ``{duration}`` and
amine@379 118 ``{timestamp}`` (system timestamp of detected event) placeholders.
amine@379 119
amine@379 120 Using the following format for example:
amine@379 121
amine@379 122 .. code:: bash
amine@379 123
amine@379 124 auditok audio.wav --printf "{id}: [{timestamp}] start:{start}, end:{end}, dur: {duration}"
amine@379 125
amine@379 126 the output would be something like:
amine@379 127
amine@379 128 .. code:: bash
amine@379 129
amine@379 130 1: [2021/02/17 20:16:02] start:1.160, end:2.390, dur: 1.230
amine@379 131 2: [2021/02/17 20:16:04] start:3.420, end:4.330, dur: 0.910
amine@379 132 3: [2021/02/17 20:16:06] start:5.010, end:5.720, dur: 0.710
amine@379 133 4: [2021/02/17 20:16:08] start:7.230, end:7.800, dur: 0.570
amine@379 134
amine@379 135
amine@379 136 The format of ``{timestamp}`` is controlled by ``--timestamp-format`` (default:
amine@379 137 `"%Y/%m/%d %H:%M:%S"`) whereas that of ``{start}``, ``{end}`` and ``{duration}``
amine@379 138 by ``--time-format`` (default: `%S`, absolute number of seconds). A more detailed
amine@379 139 format with ``--time-format`` using `%h` (hours), `%m` (minutes), `%s` (seconds)
amine@379 140 and `%i` (milliseconds) directives is possible (e.g., "%h:%m:%s.%i).
amine@379 141
amine@379 142 To completely disable printing detection information use ``-q``.
amine@379 143
amine@379 144 Save detections
amine@379 145 ---------------
amine@379 146
amine@379 147 You can save audio events to disk as they're detected using ``-o`` or
amine@379 148 ``--save-detections-as``. To get a uniq file name for each event, you can use
amine@379 149 ``{id}``, ``{start}``, ``{end}`` and ``{duration}`` placeholders. Example:
amine@379 150
amine@379 151
amine@379 152 .. code:: bash
amine@379 153
amine@379 154 auditok --save-detections-as "{id}_{start}_{end}.wav"
amine@379 155
amine@379 156 When using ``{start}``, ``{end}`` and ``{duration}`` placeholders, it's
amine@379 157 recommended that the number of decimals of the corresponding values be limited
amine@379 158 to 3. You can use something like:
amine@379 159
amine@379 160 .. code:: bash
amine@379 161
amine@379 162 auditok -o "{id}_{start:.3f}_{end:.3f}.wav"
amine@379 163
amine@379 164
amine@379 165 Save whole audio stream
amine@379 166 -----------------------
amine@379 167
amine@379 168 When reading audio data from the microphone, you most certainly want to save it
amine@379 169 to disk. For this you can use the ``-O`` or ``--save-stream`` option.
amine@379 170
amine@379 171 .. code:: bash
amine@379 172
amine@379 173 auditok --save-stream "stream.wav"
amine@379 174
amine@379 175 Note this will work even if you read data from another file on disk.
amine@379 176
amine@379 177
amine@379 178 Plot detections
amine@379 179 ---------------
amine@379 180
amine@379 181 Audio signal and detections can be plotted using the ``-p`` or ``--plot`` option.
amine@379 182 You can also save plot to disk using ``--save-image``. The following example
amine@379 183 does both:
amine@379 184
amine@379 185 .. code:: bash
amine@379 186
amine@379 187 auditok -p --save-image "plot.png" # can also be 'pdf' or another image format
amine@379 188
amine@379 189 output example:
amine@379 190
amine@379 191 .. image:: figures/example_1.png
amine@379 192
amine@379 193 Plotting requires `matplotlib <https://matplotlib.org/stable/index.html>`_.