I need a piece of code that plays a sound, any sound to indicate an operation is finished. It should work on Linux and play some kind of sound on the currently selected audio output. I don't want to have to provide a file, I just need some code that can play sound that is easy to copy paste in a jupyter notebook and works on Linux.
Asked
Active
Viewed 46 times
0
-
2Does this answer your question? [Sound alarm when code finishes](https://stackoverflow.com/questions/16573051/sound-alarm-when-code-finishes) – 001 Jun 23 '23 at 15:09
-
2For fun try `print("\a")` – JonSG Jun 23 '23 at 15:14
-
2I think user wants this to sound from the notebook window (the , if so, Fillippos text-to-speech answer in https://stackoverflow.com/questions/16241944/playing-a-sound-in-a-ipython-notebook should work, without requiring any additional python packages. – Mikael Öhman Jun 23 '23 at 15:16
2 Answers
1
Just for fun another way
import numpy as np
import sounddevice as sd
def bip(freq, dur, a=0, d=0, s=1, r=0):
t=np.arange(0,dur,1/44100)
env=np.interp(t, [0, a, (a+d), dur-r, dur], [0, 1, s, s, 0])
sound=np.sin(2*np.pi*freq*t)*env
sd.play(sound, samplerate=44100)
# Example
# A banal computer bip
bip(880, 0.1)
# With a more sophisticated envelope. Djdoing
bip(880,0.4, a=0, d=0.24, s=0.28, r=0.09)
# Softer attack
bip(440,0.6, a=0.1, d=0.2, s=0.5, r=0.2)
It is more or less the way good old 8 bits computers were playing sound: with the specification of a frequency and an envelope. And people, at that time, myself included, but I forgot everything, knew what kind of envelope to use to imitate a piano, a trumpet, etc. Well, we weren't very picky on the quality. And of course, at that time, it was not the CPU computing the enveloped sinus; that was just some hardware oscillator.
We could make this fancier by dealing with harmonics (having an array of adsr, for example, one for each harmonic). But I think the objective was to keep it simple, not to rebuild a 90s synthetizer. Besides, there are certainly some synthetizer/midi libraries out there.

chrslg
- 9,023
- 5
- 17
- 31
-
Note: this could be adapted to `pyaudio` or `IPython.display.audio`. I am used to `sounddevice` because, with my students (who are learning linear algebra, not sound handling. Sound is just the fun application) I wanted the audio part to be just a one-liner, and sounddevice has this, synchronous, blocking, API (and also a non-blocking on) that makes it very simple. But well, my point in this comment was more about how to synthetize quickly a sound, rather than how to send it to the sound card afterward. – chrslg Jun 23 '23 at 16:19
-
unfortunately this gives me the following error: cannot load library 'libportaudio.so.2': /home/user/miniconda3/envs/tf/lib/python3.9/site-packages/zmq/backend/cython/../../../../.././libstdc++.so.6: version `GLIBCXX_3.4.30' not found (required by /usr/lib/libjack.so.0) This would require fixing on every individual machine – user2741831 Jun 23 '23 at 16:24
0
import numpy as np
import sounddevice as sd
from time import sleep
# Parameters for the sine wave
duration = 5 # Duration in seconds
frequency = 440 # Frequency in Hz (A4 note)
# Generate the time axis
sample_rate = 44100 # Sample rate in Hz
t = np.linspace(0, duration, int(duration * sample_rate), endpoint=False)
# Generate the sine wave
waveform = np.sin(2 * np.pi * frequency * t)
# Play the sine wave
sd.play(waveform, sample_rate)
sleep(0.5)
sd.stop()
waveform = np.sin(4 * np.pi * frequency * t)
sd.play(waveform, sample_rate)
sleep(0.5)
sd.stop()
waveform = np.sin(5 * np.pi * frequency * t)
sd.play(waveform, sample_rate)
sleep(0.5)
sd.stop()
sd.wait()
this one works fine

user2741831
- 2,120
- 2
- 22
- 43