@@ -13,260 +13,6 @@ Source code repository and issue tracker:
1313License:
1414 MIT -- see the file ``LICENSE `` for details.
1515
16- Installation
17- ------------
18-
19- First of all, you'll need Python _.
20- Any version where CFFI _ is supported should work.
21- If you don't have Python installed yet, you should get one of the
22- distributions which already include CFFI and NumPy _ (and many other useful
23- things), e.g. Anaconda _ or WinPython _.
24-
25- .. image :: https://anaconda.org/conda-forge/python-sounddevice/badges/version.svg
26- :target: https://anaconda.org/conda-forge/python-sounddevice
27-
28- If you are using the ``conda `` package manager (e.g. with Anaconda _ for
29- Linux/macOS/Windows), you can install the ``sounddevice `` module from the
30- ``conda-forge `` channel::
31-
32- conda install -c conda-forge python-sounddevice
33-
34- There are also packages for several other package managers:
35-
36- .. image :: https://repology.org/badge/vertical-allrepos/python:sounddevice.svg
37- :target: https://repology.org/metapackage/python:sounddevice
38-
39- If you are using Windows, you can alternatively install one of the packages
40- provided at https://www.lfd.uci.edu/~gohlke/pythonlibs/#sounddevice.
41- The PortAudio library is included in the package and you can get the rest
42- of the dependencies on the same page.
43-
44- Note that some of the aforementioned packages may be out-of-date.
45- You can always get the newest ``sounddevice `` release from PyPI _
46- (using ``pip ``).
47- If you want to try the latest development version, have a look at the section
48- about `Contributing `_.
49-
50- .. image :: https://badge.fury.io/py/sounddevice.svg
51- :target: https://pypi.org/project/sounddevice/
52-
53- To install the latest release from PyPI, use::
54-
55- python3 -m pip install sounddevice --user
56-
57- If you want to install it system-wide for all users (assuming you have the
58- necessary rights), you can just drop the ``--user `` option.
59- If you have installed the module already, you can use the ``--upgrade `` flag to
60- get the newest release.
61-
62- To un-install, use::
63-
64- python3 -m pip uninstall sounddevice
65-
66- If you install the ``sounddevice `` module with ``pip `` on macOS or Windows, the
67- PortAudio _ library will be installed automagically.
68- On other platforms, you might have to install PortAudio with your package
69- manager (the package might be called ``libportaudio2 `` or similar).
70-
71- You might also have to install CFFI _ (from a package called ``python3-cffi `` or
72- similar).
73-
74- NumPy _ is only needed if you want to play back and record NumPy arrays.
75- The classes `sounddevice.RawStream `, `sounddevice.RawInputStream ` and
76- `sounddevice.RawOutputStream ` use plain Python buffer objects and don't need
77- NumPy at all.
78- If you need NumPy, you should install it with your package manager (from a
79- package named ``python3-numpy `` or similar) or use a Python distribution that
80- already includes NumPy (see above).
81- You can also install NumPy with ``pip ``, but depending on your platform, this
82- might require a compiler and several additional libraries.
83-
16+ .. _Python : https://www.python.org/
8417.. _PortAudio : http://www.portaudio.com/
8518.. _NumPy : http://www.numpy.org/
86- .. _Python : https://www.python.org/
87- .. _Anaconda : https://www.anaconda.com/download/
88- .. _WinPython : http://winpython.github.io/
89- .. _CFFI : http://cffi.readthedocs.io/
90- .. _PyPI : https://pypi.org/project/sounddevice/
91-
92- Usage
93- -----
94-
95- First, import the module:
96-
97- .. code :: python
98-
99- import sounddevice as sd
100-
101- Playback
102- ^^^^^^^^
103-
104- Assuming you have a NumPy array named ``myarray `` holding audio data with a
105- sampling frequency of ``fs `` (in the most cases this will be 44100 or 48000
106- frames per second), you can play it back with `sounddevice.play() `:
107-
108- .. code :: python
109-
110- sd.play(myarray, fs)
111-
112- This function returns immediately but continues playing the audio signal in the
113- background. You can stop playback with `sounddevice.stop() `:
114-
115- .. code :: python
116-
117- sd.stop()
118-
119- If you know that you will use the same sampling frequency for a while, you can
120- set it as default using `sounddevice.default.samplerate `:
121-
122- .. code :: python
123-
124- sd.default.samplerate = fs
125-
126- After that, you can drop the *samplerate * argument:
127-
128- .. code :: python
129-
130- sd.play(myarray)
131-
132- Recording
133- ^^^^^^^^^
134-
135- To record audio data from your sound device into a NumPy array, use
136- `sounddevice.rec() `:
137-
138- .. code :: python
139-
140- duration = 10.5 # seconds
141- myrecording = sd.rec(int (duration * fs), samplerate = fs, channels = 2 )
142-
143- Again, for repeated use you can set defaults using `sounddevice.default `:
144-
145- .. code :: python
146-
147- sd.default.samplerate = fs
148- sd.default.channels = 2
149-
150- After that, you can drop the additional arguments:
151-
152- .. code :: python
153-
154- myrecording = sd.rec(int (duration * fs))
155-
156- This function also returns immediately but continues recording in the
157- background. In the meantime, you can run other commands. If you want to check
158- if the recording is finished, you should use `sounddevice.wait() `:
159-
160- .. code :: python
161-
162- sd.wait()
163-
164- If the recording was already finished, this returns immediately; if not, it
165- waits and returns as soon as the recording is finished.
166-
167- Alternatively, you could have used the *blocking * argument in the first place:
168-
169- .. code :: python
170-
171- myrecording = sd.rec(duration * fs, blocking = True )
172-
173- By default, the recorded array has the data type ``'float32' `` (see
174- `sounddevice.default.dtype `), but this can be changed with the *dtype * argument:
175-
176- .. code :: python
177-
178- myrecording = sd.rec(duration * fs, dtype = ' float64' )
179-
180- Simultaneous Playback and Recording
181- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
182-
183- To play back an array and record at the same time, use `sounddevice.playrec() `:
184-
185- .. code :: python
186-
187- myrecording = sd.playrec(myarray, fs, channels = 2 )
188-
189- The number of output channels is obtained from ``myarray ``, but the number of
190- input channels still has to be specified.
191-
192- Again, default values can be used:
193-
194- .. code :: python
195-
196- sd.default.samplerate = fs
197- sd.default.channels = 2
198- myrecording = sd.playrec(myarray)
199-
200- In this case the number of output channels is still taken from ``myarray ``
201- (which may or may not have 2 channels), but the number of input channels is
202- taken from `sounddevice.default.channels `.
203-
204- Device Selection
205- ^^^^^^^^^^^^^^^^
206-
207- In many cases, the default input/output device(s) will be the one(s) you want,
208- but it is of course possible to choose a different device.
209- Use `sounddevice.query_devices() ` to get a list of supported devices.
210- The same list can be obtained from a terminal by typing the command ::
211-
212- python3 -m sounddevice
213-
214- You can use the corresponding device ID to select a desired device by assigning
215- to `sounddevice.default.device ` or by passing it as *device * argument to
216- `sounddevice.play() `, `sounddevice.Stream() ` etc.
217-
218- Instead of the numerical device ID, you can also use a space-separated list of
219- case-insensitive substrings of the device name (and the host API name, if
220- needed). See `sounddevice.default.device ` for details.
221-
222- .. code :: python
223-
224- import sounddevice as sd
225- sd.default.samplerate = 44100
226- sd.default.device = ' digital output'
227- sd.play(myarray)
228-
229- Callback Streams
230- ^^^^^^^^^^^^^^^^
231-
232- Callback "wire" with `sounddevice.Stream `:
233-
234- .. code :: python
235-
236- import sounddevice as sd
237- duration = 5.5 # seconds
238-
239- def callback (indata , outdata , frames , time , status ):
240- if status:
241- print (status)
242- outdata[:] = indata
243-
244- with sd.Stream(channels = 2 , callback = callback):
245- sd.sleep(int (duration * 1000 ))
246-
247- Same thing with `sounddevice.RawStream `:
248-
249- .. code :: python
250-
251- import sounddevice as sd
252- duration = 5.5 # seconds
253-
254- def callback (indata , outdata , frames , time , status ):
255- if status:
256- print (status)
257- outdata[:] = indata
258-
259- with sd.RawStream(channels = 2 , dtype = ' int24' , callback = callback):
260- sd.sleep(int (duration * 1000 ))
261-
262- .. note :: We are using 24-bit samples here for no particular reason
263- (just because we can).
264-
265- Blocking Read/Write Streams
266- ^^^^^^^^^^^^^^^^^^^^^^^^^^^
267-
268- Instead of using a callback function, you can also use the blocking methods
269- `sounddevice.Stream.read() ` and `sounddevice.Stream.write() ` (and of course the
270- corresponding methods in `sounddevice.InputStream `, `sounddevice.OutputStream `,
271- `sounddevice.RawStream `, `sounddevice.RawInputStream ` and
272- `sounddevice.RawOutputStream `).
0 commit comments