473,288 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

How to play sound in Python?

I posted a message ("Help with my 1st Tkinter program") a few days ago
complaining that Python did not have any built in basic cross platform
sound capability. I was wrong (at least partly). Python comes with some
sound playing ability for some platforms.

Since no one responded to my post, I assume the capability is not well
known so I will post a code snippet from my practice program that
demonstrates how I play a sound file with Python on both Windows and
Linux. It may save someone else some time.

The winsound module lets you play .wav files under windows. The
ossaudiodev module in combination with the wave module lets you play
..wav files under Linux (and some other nix's as well).

===========================
file='tada.wav'
if platform.startswith('win'):
from winsound import PlaySound, SND_FILENAME, SND_ASYNC
PlaySound(file, SND_FILENAME|SND_ASYNC)
elif platform.find('linux')>-1:
from wave import open as waveOpen
from ossaudiodev import open as ossOpen
s = waveOpen('tada.wav','rb')
(nc,sw,fr,nf,comptype, compname) = s.getparams( )
dsp = ossOpen('/dev/dsp','w')
try:
from ossaudiodev import AFMT_S16_NE
except ImportError:
if byteorder == "little":
AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
else:
AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
dsp.setparameters(AFMT_S16_NE, nc, fr)
data = s.readframes(nf)
s.close()
dsp.write(data)
dsp.close()
===============================

Bill
Jul 18 '05 #1
3 7404
"Bill Dandreta" <wj********@att.net> wrote in message
news:Ms******************@bgtnsc05-news.ops.worldnet.att.net...
I posted a message ("Help with my 1st Tkinter program") a few days ago
complaining that Python did not have any built in basic cross platform
sound capability. I was wrong (at least partly). Python comes with some
sound playing ability for some platforms.


Take a look: http://pymedia.org/
Dmitry/
Jul 18 '05 #2
Bill Dandreta:
The winsound module lets you play .wav files under windows.


Thank you.
You can also create "on the fly" memory-mapped wav files (with wave
and mmap modules, I think), and play them.

How to use the flag SND_PURGE for WAVs loaded from filename or memory
mapped?

Bear hugs,
Bearophile
Jul 18 '05 #3

just a quick note....

Bill Dandreta <wj********@att.net> writes:
I posted a message ("Help with my 1st Tkinter program") a few days ago
complaining that Python did not have any built in basic cross platform
sound capability. I was wrong (at least partly). Python comes with
some sound playing ability for some platforms.


I remember that somebody has implemented Python bindings for the
portaudio library, which is a cross-platform library for real-time
audio.
Johannes
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Askari | last post by:
Hi, I search how do for play more that one sound in same time with Python. Actually, I use the module "winsound" and I can play/stop a sound but can't play more that one sound in same time. :-( ...
22
by: The Road To Utopia | last post by:
Here's one for the trolls...a common jibe from them is setting up audio/video hardware under linux. Ok, true story: at work today, someone asked me if I could tell him why his XP Home would play...
1
by: Ron Provost | last post by:
Hello, I'm developing a piece of software to assist illiteraate adults to learn to read. I'm trying to figure out how, if possible, to make audio playback asynchrnous but still controllable. ...
2
by: NB | last post by:
I've done a thorough search, but could not find any posts If I store a sound file within the mdb, How do I play it (in a form event, for example) I can save it to a temp folder and call the...
22
by: MLH | last post by:
I have some audio help files that play fine from within Access 97 and Access 2.0. Both are running on a Windows XP box. But I do not know what program plays the files. If I click Start, Run and...
1
by: Lam | last post by:
how can I play sound file in a .aspx page written in C#? I try to use the code like the following. But whenI call the play function play("sound.wav", this.SND_ASYNC) my computer give out "be"...
3
by: Jared | last post by:
I'm using the first code sample below to play WAV files stored as embedded resources. For some reason I *occasionally* get scratching and crackling. I'm using a couple WAVs that ship with...
5
by: | last post by:
Hello, I am wrtting a program that does some sound effects... the files are stored in a subfolder in the application folder... and I check the existence of the files before calling the method to...
26
by: Jake Barnes | last post by:
I did a search on the newsgroup comp.lang.javascript. I was searching for "how to play a sound with Javascript". I'm somewhat suprised that the majority of entries are from the 1990s, and there are...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.