1+ .. currentmodule :: sounddevice
2+
13Usage
24=====
35
@@ -12,21 +14,28 @@ Playback
1214
1315Assuming you have a NumPy array named ``myarray `` holding audio data with a
1416sampling frequency of ``fs `` (in the most cases this will be 44100 or 48000
15- frames per second), you can play it back with `sounddevice. play() `:
17+ frames per second), you can play it back with `play() `:
1618
1719.. code :: python
1820
1921 sd.play(myarray, fs)
2022
2123 This function returns immediately but continues playing the audio signal in the
22- background. You can stop playback with `sounddevice. stop() `:
24+ background. You can stop playback with `stop() `:
2325
2426.. code :: python
2527
2628 sd.stop()
2729
30+ If you want to block the Python interpreter until playback is finished,
31+ you can use `wait() `:
32+
33+ .. code :: python
34+
35+ sd.wait()
36+
2837 If you know that you will use the same sampling frequency for a while, you can
29- set it as default using `sounddevice. default.samplerate `:
38+ set it as default using `default.samplerate `:
3039
3140.. code :: python
3241
@@ -38,18 +47,23 @@ After that, you can drop the *samplerate* argument:
3847
3948 sd.play(myarray)
4049
50+ .. note ::
51+
52+ If you don't specify the correct sampling frequency,
53+ the sound might be played back too slow or too fast!
54+
4155Recording
4256---------
4357
44- To record audio data from your sound device into a NumPy array, use
45- ` sounddevice. rec() `:
58+ To record audio data from your sound device into a NumPy array,
59+ you can use ` rec() `:
4660
4761.. code :: python
4862
4963 duration = 10.5 # seconds
5064 myrecording = sd.rec(int (duration * fs), samplerate = fs, channels = 2 )
5165
52- Again, for repeated use you can set defaults using `sounddevice. default `:
66+ Again, for repeated use you can set defaults using `default `:
5367
5468.. code :: python
5569
@@ -62,41 +76,35 @@ After that, you can drop the additional arguments:
6276
6377 myrecording = sd.rec(int (duration * fs))
6478
65- This function also returns immediately but continues recording in the
66- background. In the meantime, you can run other commands. If you want to check
67- if the recording is finished, you should use `sounddevice. wait() `:
79+ This function also returns immediately but continues recording in the background.
80+ In the meantime, you can run other commands.
81+ If you want to check if the recording is finished, you should use `wait() `:
6882
6983.. code :: python
7084
7185 sd.wait()
7286
73- If the recording was already finished, this returns immediately; if not, it
74- waits and returns as soon as the recording is finished.
87+ If the recording was already finished, this returns immediately;
88+ if not, it waits and returns as soon as the recording is finished.
7589
76- Alternatively, you could have used the *blocking * argument in the first place:
90+ By default, the recorded array has the data type ``'float32' ``
91+ (see `default.dtype `), but this can be changed with the *dtype * argument:
7792
7893.. code :: python
7994
80- myrecording = sd.rec(duration * fs, blocking = True )
81-
82- By default, the recorded array has the data type ``'float32' `` (see
83- `sounddevice.default.dtype `), but this can be changed with the *dtype * argument:
84-
85- .. code :: python
86-
87- myrecording = sd.rec(duration * fs, dtype = ' float64' )
95+ myrecording = sd.rec(int (duration * fs), dtype = ' float64' )
8896
8997 Simultaneous Playback and Recording
9098-----------------------------------
9199
92- To play back an array and record at the same time, use `sounddevice. playrec() `:
100+ To play back an array and record at the same time, you can use `playrec() `:
93101
94102.. code :: python
95103
96104 myrecording = sd.playrec(myarray, fs, channels = 2 )
97105
98- The number of output channels is obtained from ``myarray ``, but the number of
99- input channels still has to be specified.
106+ The number of output channels is obtained from ``myarray ``,
107+ but the number of input channels still has to be specified.
100108
101109Again, default values can be used:
102110
@@ -107,26 +115,27 @@ Again, default values can be used:
107115 myrecording = sd.playrec(myarray)
108116
109117 In this case the number of output channels is still taken from ``myarray ``
110- (which may or may not have 2 channels), but the number of input channels is
111- taken from `sounddevice. default.channels `.
118+ (which may or may not have 2 channels),
119+ but the number of input channels is taken from `default.channels `.
112120
113121Device Selection
114122----------------
115123
116124In many cases, the default input/output device(s) will be the one(s) you want,
117125but it is of course possible to choose a different device.
118- Use `sounddevice. query_devices() ` to get a list of supported devices.
126+ Use `query_devices() ` to get a list of supported devices.
119127The same list can be obtained from a terminal by typing the command ::
120128
121129 python3 -m sounddevice
122130
123131You can use the corresponding device ID to select a desired device by assigning
124- to `sounddevice. default.device ` or by passing it as *device * argument to
125- `sounddevice. play() `, `sounddevice. Stream() ` etc.
132+ to `default.device ` or by passing it as *device * argument to
133+ `play() `, `Stream() ` etc.
126134
127135Instead of the numerical device ID, you can also use a space-separated list of
128- case-insensitive substrings of the device name (and the host API name, if
129- needed). See `sounddevice.default.device ` for details.
136+ case-insensitive substrings of the device name
137+ (and the host API name, if needed).
138+ See `default.device ` for details.
130139
131140.. code :: python
132141
@@ -138,7 +147,25 @@ needed). See `sounddevice.default.device` for details.
138147 Callback Streams
139148----------------
140149
141- Callback "wire" with `sounddevice.Stream `:
150+ The aforementioned convenience functions `play() `, `rec() ` and `playrec() `
151+ (as well as the related functions `wait() `, `stop() `, `get_status() ` and
152+ `get_stream() `) are designed for small scripts and interactive use
153+ (e.g. in a Jupyter _ notebook).
154+ They are supposed to be simple and convenient,
155+ but their use cases are quite limited.
156+
157+ If you need more control (e.g. continuous recording, realtime processing, ...),
158+ you should use the lower-level "stream" classes
159+ (e.g. `Stream `, `InputStream `, `RawInputStream `),
160+ either with the "non-blocking" callback interface or with the "blocking"
161+ `Stream.read() ` and `Stream.write() ` methods, see `Blocking Read/Write Streams `_.
162+
163+ As an example for the "non-blocking" interface,
164+ the following code creates a `Stream ` with a callback function
165+ that obtains audio data from the input channels
166+ and simply forwards everything to the output channels
167+ (be careful with the output volume, because this might cause acoustic feedback
168+ if your microphone is close to your loudspeakers):
142169
143170.. code :: python
144171
@@ -153,7 +180,8 @@ Callback "wire" with `sounddevice.Stream`:
153180 with sd.Stream(channels = 2 , callback = callback):
154181 sd.sleep(int (duration * 1000 ))
155182
156- Same thing with `sounddevice.RawStream `:
183+ The same thing can be done with `RawStream `
184+ (NumPy _ doesn't have to be installed):
157185
158186.. code :: python
159187
@@ -171,11 +199,20 @@ Same thing with `sounddevice.RawStream`:
171199 .. note :: We are using 24-bit samples here for no particular reason
172200 (just because we can).
173201
202+ You can of course extend the callback functions
203+ to do arbitrarily more complicated stuff.
204+ You can also use streams without inputs (e.g. `OutputStream `)
205+ or streams without outputs (e.g. `InputStream `).
206+
207+ See :doc: `examples ` for more examples.
208+
174209Blocking Read/Write Streams
175210---------------------------
176211
177- Instead of using a callback function, you can also use the blocking methods
178- `sounddevice.Stream.read() ` and `sounddevice.Stream.write() ` (and of course the
179- corresponding methods in `sounddevice.InputStream `, `sounddevice.OutputStream `,
180- `sounddevice.RawStream `, `sounddevice.RawInputStream ` and
181- `sounddevice.RawOutputStream `).
212+ Instead of using a callback function,
213+ you can also use the "blocking" methods `Stream.read() ` and `Stream.write() `
214+ (and of course the corresponding methods in `InputStream `, `OutputStream `,
215+ `RawStream `, `RawInputStream ` and `RawOutputStream `).
216+
217+ .. _Jupyter : https://jupyter.org/
218+ .. _NumPy : https://numpy.org/
0 commit comments