bastibe/PySoundCard

Segmentation fault

Closed this issue · 4 comments

Running this simple script:

import numpy as np
import pysoundcard as snd

fs = 44100
t = np.arange(0, 5, 1.0 / fs) # time vector of 5 seconds
x = .6 * np.random.random(t.shape) - .3
s = snd.Stream(sample_rate=fs, block_length=16, input_device=None)
print("Starting...")
s.start()
print("Started. Writing...")
s.write(x)
print("Written. Stopping...")
s.stop()
print("Stopped.")

gives me a segmentation fault:

gandalv@eddie:/tmp$ python3 script.py
ALSA lib pcm_dmix.c:1022:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
ALSA lib pcm_dmix.c:961:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dmix.c:1022:(snd_pcm_dmix_open) unable to open slave
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
Starting...
Started. Writing...
Segmentation fault (core dumped)

It actually plays a little bit of the noise (the longer array I make the longer it plays but not for the specified time). The same happens with python2.

I'm on 64bit Ubuntu 13.10.
Relevant installed packages:
libportaudio2 - 19+svn20111121-1build1
python{,3}-cffi - 0.6-1

This bug(?) is no way critical to me - in my project I'll be using your PySoundFile and write to files - but for testing purposes I wanted to play straight from the program.

Interesting. I just tried this on my maching (OSX) and it segfaults as well.

The problem is that you provide PySoundCard with one-dimensional data even though the sound card has two channels. If you create your audio like this:

x = .6 * np.random.random((t.shape[0], 2)) - 0.3

It will work correctly.

The issue should be fixed in 0.4.2 ( c3c8783 ). It is up on pip, but I haven't built Windows installers yet.

Now, if you provide mono data for a multi-channel device, it will play the same data on all channels.

Great, works like a charm!

Actually I had to make it not 2-dimensional but 32-dimensional since my default device was a "fake" pulse audio device which has 32 channels. Modifying the device structure by setting output_channels to 1 worked too.

One last question - is setting the number of channels to 1 more, less or equivalently efficient than using 1-D data?

Setting output_channels to 1 in the device structure is probably the fastest as portaudio (C-code) will take care of the channel multiplexing and Python will only ever have to deal with one channel. The other options will always copy all 32 channels of data around.

I'm glad to hear that PySoundCard is working in Jack and ALSA! Other people were reporting problems with that configuration. Thank you for your bug report!