question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sounddevice.play vs manual OutputStream - samplerate confusion

See original GitHub issue

When I use the convenience function play to play a wav file at a particular samplerate, it seems to play back at the correct rate. However, when I attempt to play the same wav file by manually creating an OutputStream with the same samplerate, the sample seems to play back at far too high a rate.

To the best of my knowledge, I set things up correctly, but I’m wondering if I’m missing something obvious. I’d appreciate some advice if so. Below are some details - pardon me if I’ve left anything important out, I can update this (such as with a link to the sound file itself) if needed.

Platform/API: macOS/Core Audio Audio device: Scarlett 4i4 USB (6 in, 4 out) (device 3 according to query_devices()) Python module versions:

% python3 -m pip show sounddevice
Name: sounddevice
Version: 0.4.2 ...

% python3 -m pip show soundfile
Name: SoundFile
Version: 0.10.3.post1 ...

The minimal(ish) script that reproduces this on my system:

import sounddevice
import soundfile
data_type = "int16"
file = soundfile.read("samples/misc_crow.wav", dtype=data_type)
data, sample_rate = file # sample_rate is 44100
sounddevice.play(data, sample_rate, device=3) # sounds fine
sounddevice.wait()

output = sounddevice.OutputStream(device=3, dtype=data_type, samplerate=sample_rate)
output.start()
output.write(data) # sounds much too fast/high pitched
output.close()

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mgeiercommented, Oct 4, 2021

Thanks for testing! I’ve just merged #372. If there’s anything else not working, please let me know!

1reaction
mgeiercommented, Sep 29, 2021

I’m wondering if I’m missing something obvious.

No, you might have found a bug.

Is your file a mono file?

It looks like when data is one-dimensional, the Stream.write() function doesn’t check whether the stream has one channel.

In your case, the stream has 4 channels, so the audio data is distributed over 4 channels, each one being four times faster (i.e. two octaves higher) than intended (and probably having some aliasing artifacts).

So I think this is a bug.

As a work-around, you can use channels=1 when creating the OutputStream.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python-sounddevice, version 0.4.4
[docs]def play(data, samplerate=None, mapping=None, blocking=False, loop=False, ... Create an `OutputStream` and a callback function for taking care of the ...
Read more >
python-sounddevice/sounddevice.py at master - GitHub
sound: Play and Record Sound with Python :snake:. ... (either with the *samplerate* argument or by assigning a value to. `default.samplerate`), the audio ......
Read more >
Python Sounddevice.play() on Threads - Stack Overflow
__init__() ? There you could also do all the sd.default stuff. If you still have problems during the initialization (or if you insist...
Read more >
python-sounddevice - Read the Docs
3.3 Play a Very Long Sound File without Using NumPy . ... without inputs (e.g. OutputStream) or streams without outputs (e.g. InputStream).
Read more >
play(1) - Linux man page
When playing a file with a sample rate that is not supported by the audio output ... adjustments that have been selected (either...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found