How to play sound in Python WITHOUT interrupting music/other sounds from playing

2024/10/9 6:20:54

I'm working on a timer in python which sounds a chime when the waiting time is over. I use the following code:

from wave import open as wave_open
from ossaudiodev import open as oss_opendef _play_chime():"""Play a sound file once."""sound_file = wave_open('chime.wav','rb')(nc,sw,fr,nf,comptype, compname) = sound_file.getparams( )dsp = oss_open('/dev/dsp','w')try:from ossaudiodev import AFMT_S16_NEexcept ImportError:if byteorder == "little":AFMT_S16_NE = ossaudiodev.AFMT_S16_LEelse:AFMT_S16_NE = ossaudiodev.AFMT_S16_BEdsp.setparameters(AFMT_S16_NE, nc, fr)data = sound_file.readframes(nf)sound_file.close()dsp.write(data)dsp.close()

It works pretty good, unless any other device is already outputing sound.

How could I do basically the same (under linux) without having the prerequisite that no sound is being played?

If you think the process would require an API to ensure software mixing, please suggest a method :)

Thx for the support :)

Answer

The easy answer is "Switch from OSS to PulseAudio." (Or set up ALSA to use dmix, or get a soundcard with better Linux drivers...)

The more complicated answer is, your code already works the way you want it to... on some soundcards. OSS drivers can expose hardware mixers so that you can have multiple audio streams playing simultaneously, or they can expose a single stream which results in the blocking audio you see on your system. The only correct solution here is to use an API that ensures software mixing.

https://en.xdnf.cn/q/70048.html

Related Q&A

How can I use click to parse a string for arguments?

Say I have a list of strings containing arguments and options, with argparse, I’m able to parse this list using the parse_args function into an object, as follows:import argparseextra_params = [‘—su…

Tornado and WTForms

I am using WTForms for the first time. Using WTForms to validate POST requests in Tornado Below is my forms forms.pyclass UserForm(Form):user = TextField(user, [validators.Length(min=23, max=23)])In t…

Modifying a python docstring with a decorator: Is it a good idea?

A python docstring must be given as a literal string; but sometimes its useful to have similar docstrings for several functions (e.g., different constructors), or several access methods might accept th…

Counting number of columns in text file with Python

I have two text files composed of spaced-separated columns. These are excerpts of these two files:FileA1 1742.420 -0.410 20.1530 0.4190 1.7080 0.59402 1872.060 0.070 21.4710 0.2950 0.0…

Django on Apache web server dict object has no attribute render_context

Im having a bit of a problem, I uploaded my Django project to a webserver running apache, mod_python, and django. On the computer I developed on the following works finenameBox = getNamesBox().render(l…

Verbose log abbriviations meaning in SVC, scikit-learn

I am looking for the meaning of verbose log abbriviations of SVC function in scikit-learn?If nSV is the number of support vectors, #iter is the number of iteration, what dose nBSV, rho,obj mean?This …

networkx draw_networkx_edges capstyle

Does anyone know if it is possible to have fine-grained control over line properties when drawing networkx edges via (for example) draw_networkx_edges? I would like to control the line solid_capstyle …

Setting up the path so AWS cli works properly

I installed AWSCLI by using:pip install --upgrade --user awscliNow if I type aws configure in the cmd I get: aws is not recognized as an internal or external command...Im pretty sure the path needs to …

Comparing value with neighbor elements in numpy

Lets say I have a numpy arraya b c A = i j ku v wI want to compare the value central element with some of its eight neighbor elements (along the axis or along the diagonal). Is there any faster way exc…

Is Python bad at XML? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…