Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to play sound in Python?

Reply
Thread Tools

How to play sound in Python?

 
 
Bill Dandreta
Guest
Posts: n/a
 
      10-23-2004
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
 
Reply With Quote
 
 
 
 
Dmitry Borisov
Guest
Posts: n/a
 
      10-23-2004
"Bill Dandreta" <> wrote in message
news:Msved.21689$...
> 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/


 
Reply With Quote
 
 
 
 
bearophile
Guest
Posts: n/a
 
      10-24-2004
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
 
Reply With Quote
 
Johannes Nix
Guest
Posts: n/a
 
      11-11-2004

just a quick note....

Bill Dandreta <> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
play sound file Lam ASP .Net 1 03-03-2005 10:09 PM
Play a sound Rick Lederman ASP .Net 3 12-20-2004 12:50 AM
How to play sound Lord2702 ASP .Net 1 10-04-2004 10:10 AM
How to beep or play any sound? YeeCN ASP .Net 1 05-13-2004 08:14 AM
IE don't play the sound Giuliano Pascali Java 0 09-18-2003 06:56 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57