Git Product home page Git Product logo

Comments (16)

mgeier avatar mgeier commented on July 28, 2024

You should check for buffer under-/overflows by running sd.get_status() right after sd.playrec().

If there are under-/overflows, you should try a larger block size with blocksize=1024 or blocksize=2048.

If not, I don't really know ...
Your waveform indeed looks quite nasty.

from python-sounddevice.

Plecto123 avatar Plecto123 commented on July 28, 2024

How do I get the information out of sd.get_status()? My python skills aren't that great :(

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

Have a look at the play_file.py example.

You can just print the returned status variable.

from python-sounddevice.

Plecto123 avatar Plecto123 commented on July 28, 2024

Printing the status variable gets me nothing, seems like it's empty. It says that the status variable is of type <class 'sounddevice.CallbackFlags'> which I'm not sure of what is, can it simply be printed?

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

Yes, it can simply be printed.
If there are no errors, it will just print an empty string.
If you want to avoid printing empty strings, you can use it like in the aforementioned example.

Here's the docs: http://python-sounddevice.readthedocs.io/#sounddevice.CallbackFlags
You can also look at the source code, the relevant part is the __str__() method, which automatically gets called by print().

If you don't get any over-/underflow errors, I don't really know what's going wrong.

Can you please still try it with different block sizes?

Do you also hear the strange noise while playrec() is running or is it only in the recording?

Does the same strange noise appear if you only use play() or only use rec()?

from python-sounddevice.

Plecto123 avatar Plecto123 commented on July 28, 2024

Same thing happens with the sd.play(). Simply recording from a different frequency generator gives a fine result so it definitely has to do with playback.

Block size of 1024 or 2048 made no difference. Neither does changing sample rate, all sample rates give the same crackling although higher sample rates reduces the time between cracks. One exception being using a Fs of 44100 which results in pure static.

Since everything was fine with my previous USB sound card, could this be a driver issue?

from python-sounddevice.

Plecto123 avatar Plecto123 commented on July 28, 2024

Alright, I did something here. I used scipy.io.wavfile.read to get a song stored as wav and played it with sd.play() and it sounds perfect. How does sd.play() interpret the array I create with scipy.io.wavfile.read() and the array I create with np.sin() differently? Why does it decide to go ham on the np.sin() array? It has to do with the bitrate, right? The song being 16-bit while the np.sin() is 64-bit? I guess I should try to play a high bitrate song with aplay(), if that doesn't work this probably has nothing to do with sounddevice.

from python-sounddevice.

Plecto123 avatar Plecto123 commented on July 28, 2024

It seems to be with the data types. The wave file is dtype=int16 while the sine wave I'm playing is a np.float64. My thinking was to simply create an int instead of a float by:

x=np.linspace(0.0, t, Fs*t)
y=32000*np.sin(1000*2.0*np.pi*x)
y=y.astype(np.int16)

But that gave me this result when using sd.play() or sd.playrec():

http://i64.tinypic.com/2ensx8y.jpg
http://i64.tinypic.com/2ensx8y.jpg

Why this happens is beyond me. Increasing the block size will increase the on/off time that can been seen in the image. I'm having a hard time understanding how sounddevice can differentiate between two int16 arrays as the wav file plays just fine.

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

I have no idea. This is really strange.

When I try your code, it works fine. I hear a single uninterrupted tone for 10 seconds.

Can you please try to save your generated signal (y) to a WAV file and then read it back in and play it?

I guess the generated signal looks fine when you plot it, right?

Did you try using your sound card with other software that uses PortAudio (e.g. Audacity)?

BTW, you can simply drag images into the comments here (while editing), then they will be directly visible without having to click on a link.

from python-sounddevice.

Plecto123 avatar Plecto123 commented on July 28, 2024

I generated it as a wav file, transferred it to my computer and it plays just fine, plotting the waveform shows that the array itself is without any issues. This has to do with sd.play() which for some reason is struggeling to handle the data even after it has been converted to a wav file and back into a int16 array :(

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

The sounddevice module theoretically supports float64, float32, int32, int16, int8 and uint8. And it shouldn't matter if you create the signal with NumPy or if you load it from a file.

You could try using the newest release of PortAudio (19.6.0). I guess there is no Raspbian package yet, so you'll have to compile it manually.

from python-sounddevice.

Plecto123 avatar Plecto123 commented on July 28, 2024

The issue was with the dimension of the array. Giving the sd.play() a two dimensional array for both left and right channel made it work :) Don't ask me why :(

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

Great that you found a way that works!

It would still be interesting to find out if I can do something to avoid such errors in the future.

What was the dimension of the array when it worked and when it didn't work?
What were their shapes?

from python-sounddevice.

Plecto123 avatar Plecto123 commented on July 28, 2024

print y.dtype, y.shape, y.ndim, y.size, type(y)

int16 (48000, 2) 2 96000 <type 'numpy.ndarray'>

While the one that gave the result in the image above:

int16 (48000,) 1 48000 <type 'numpy.ndarray'>

Using int32 does not seem to work though, it just outputs nothing and records loud static regardless.

capture

I also have a different problem that's not directly related to the topic of this thread though. I have a function that calculated the THD+N of a signal. Sending the y-array directly to this function returns a THD+N of something like 0.0003%, but sending the signal after it has been played and recorded gives a THD+N of 0.14% or there abouts. Calculating the THD+N of sine waves generated by my phone returns about 0.02% which is reasonable from a phone I guess. This USB sound card is supposed to ble able to output 0.0006 % THD+N at 1khz, so why does it output 0.14%? :\

I really appreciete the replies by the way :)

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

Just to make sure that I understand this correctly:
This only happens with one specific USB sound card, right?
If you use a different sound card, it doesn't happen?

This very much looks like a problem of PortAudio and the driver of the USB sound card.

Did you try Audacity?
Or any other app from http://portaudio.com/apps.html?

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

@Plecto123 Any news on this?

from python-sounddevice.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.