diff options
| author | Matthias Geier <Matthias.Geier@gmail.com> | 2021-02-09 16:13:11 +0100 |
|---|---|---|
| committer | Matthias Geier <Matthias.Geier@gmail.com> | 2021-02-09 16:14:56 +0100 |
| commit | b2a311d0256b214225109b0e27dd3bf02d4f1f75 (patch) | |
| tree | 15b448b189c45a2fd2eaba7414eac824ce56fdf1 /examples/play_long_file.py | |
| parent | f063cd1b427b90907a671a7be4427f6c2290914c (diff) | |
DOC: Use NumPy in play_long_file.py, move non-NumPy version to play_long_file_raw.py
Diffstat (limited to 'examples/play_long_file.py')
| -rwxr-xr-x | examples/play_long_file.py | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/examples/play_long_file.py b/examples/play_long_file.py index 1d554e2..bb975b9 100755 --- a/examples/play_long_file.py +++ b/examples/play_long_file.py @@ -1,16 +1,16 @@ #!/usr/bin/env python3 """Play an audio file using a limited amount of memory. -The soundfile module (https://PySoundFile.readthedocs.io/) must be -installed for this to work. NumPy is not needed. +The soundfile module (https://python-soundfile.readthedocs.io/) must be +installed for this to work. In contrast to play_file.py, which loads the whole file into memory before starting playback, this example program only holds a given number of audio blocks in memory and is therefore able to play files that are larger than the available RAM. -A similar example could of course be implemented using NumPy, -but this example shows what can be done when NumPy is not available. +This example is implemented using NumPy, see play_long_file_raw.py +for a version that doesn't need NumPy. """ import argparse @@ -77,7 +77,7 @@ def callback(outdata, frames, time, status): raise sd.CallbackAbort from e if len(data) < len(outdata): outdata[:len(data)] = data - outdata[len(data):] = b'\x00' * (len(outdata) - len(data)) + outdata[len(data):].fill(0) raise sd.CallbackStop else: outdata[:] = data @@ -86,18 +86,18 @@ def callback(outdata, frames, time, status): try: with sf.SoundFile(args.filename) as f: for _ in range(args.buffersize): - data = f.buffer_read(args.blocksize, dtype='float32') - if not data: + data = f.read(args.blocksize) + if not len(data): break q.put_nowait(data) # Pre-fill queue - stream = sd.RawOutputStream( + stream = sd.OutputStream( samplerate=f.samplerate, blocksize=args.blocksize, - device=args.device, channels=f.channels, dtype='float32', + device=args.device, channels=f.channels, callback=callback, finished_callback=event.set) with stream: timeout = args.blocksize * args.buffersize / f.samplerate - while data: - data = f.buffer_read(args.blocksize, dtype='float32') + while len(data): + data = f.read(args.blocksize) q.put(data, timeout=timeout) event.wait() # Wait until playback is finished except KeyboardInterrupt: |
