diff options
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: |
