I have been running SoundCard (0.4.1) on Jupyter Notebook for several years using a laptop with Mint 18.3, Python 3.6.4:Anaconda, Jupyter 4.4.0 . No problems… still works today.
I now run exactly the same notebook on a NUC i3 with Mint 22.1, Python 3.12.3, Jupyter 7.4.3.
In line with recommendations on this forum to work in a virtual environment, I run using uv as follows:
uv run --with jupyter --with numpy --with soundcard jupyter notebook
Installation appears to complete correctly.
import numpy as np
import soundcard as sc
x = np.linspace(0,1,1440001) # 2 minutes of sine wave
y = np.sin (2np.pi1500*120 * x) # at 1500 Hz
speakers = sc.all_speakers()
print(speakers)
my_speaker = sc.get_speaker (‘Audio Analog’)
my_speaker.play (y[:100000], samplerate = 120000) # First 8 seconds
I now get the following error message
AttributeError Traceback (most recent call last)
Cell In[1], line 11
8 print(speakers)
10 my_speaker = sc.get_speaker (‘Audio Analog’)
—> 11 my_speaker.play (y[:100000], samplerate = 120000) # First 8 seconds
File ~/.cache/uv/archive-v0/X6dwS-78MQS5Kvb-mB40E/lib/python3.12/site-packages/soundcard/pulseaudio.py:555, in _Speaker.play(self, data, samplerate, channels, blocksize)
553 channels = self.channels
554 with _Player(self._id, samplerate, channels, blocksize) as s:
→ 555 s.play(data)
File ~/.cache/uv/archive-v0/X6dwS-78MQS5Kvb-mB40E/lib/python3.12/site-packages/soundcard/pulseaudio.py:786, in _Player.play(self, data)
784 time.sleep(0.001)
785 continue
→ 786 bytes = data[:nwrite].ravel().tostring()
787 _pulse._pa_stream_write(self.stream, bytes, len(bytes), _ffi.NULL, 0, _pa.PA_SEEK_RELATIVE)
788 data = data[nwrite:]
AttributeError: ‘numpy.ndarray’ object has no attribute ‘tostring’
Is this a problem from Jupyter, from Mint or from Python? What is the solution?
Thanks for help.