473,406 Members | 2,336 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,406 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 7425
"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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.