Play Sound with Python

What is the easiest way to play an audio file (.wav) in Python? Most simply, I mean both the majority of the platform-independent ones and those requiring the least dependencies. pygame is, of course, an option, but it seems like it just overshadows the sound.

+81
python audio platform-independent
Nov 20 '08 at 23:40
source share
10 answers

Snack Sound Toolkit can play wav, au, and mp3 files.

s = Sound() s.read('sound.wav') s.play() 
+30
Nov 21 '08 at 1:11
source share

On Windows, you can use winsound. It is built in

 import winsound winsound.PlaySound('sound.wav', winsound.SND_FILENAME) 

You can use ossaudiodev for linux:

 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: from sys import byteorder 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() 

(Credit to ossaudiodev: Bill Dandreta http://mail.python.org/pipermail/python-list/2004-October/288905.html )

+79
Nov 22 '08 at 18:43
source share

It seems ridiculous and distant, but you can always use Windows (or any other OS that you like) to control the sound for you!

 import os os.system("start C:/thepathyouwant/file") 

Simple, no extensions, somewhat slow and crazy, but working.

+17
Jul 31 '13 at 18:53
source share

Definitely use Pyglet . This is kind of a big package, but it is pure python without expansion modules. This will certainly be the easiest to deploy. He also got a great format and codec support.

 import pyglet music = pyglet.resource.media('music.mp3') music.play() pyglet.app.run() 
+14
Nov 21 '08 at 22:13
source share

After the play () command adds a delay of 10 seconds or so, it will work

 import pygame import time pygame.init() pygame.mixer.music.load("test.wav") pygame.mixer.music.play() time.sleep(10) 

It also plays .mp3 files.

+5
Sep 29 2018-11-11T00:
source share

The pyMedia sample sound does just that . That should be all you need.

 import time, wave, pymedia.audio.sound as sound f= wave.open( 'YOUR FILE NAME', 'rb' ) sampleRate= f.getframerate() channels= f.getnchannels() format= sound.AFMT_S16_LE snd= sound.Output( sampleRate, channels, format ) s= f.readframes( 300000 ) snd.play( s ) 
+4
Nov 20 '08 at 23:45
source share

I like pygame and the command below should work:

 pygame.init() pygame.mixer.Sound('sound.wav').play() 

but it does not work on any of my computers, and there is limited help on this. edit: I understood why the pygame sound doesn’t work for me, it doesn’t load most sounds correctly, the “length” attribute is ~ 0,0002 when loading. perhaps loading them using something other than mygame will lead to more evasion.

with pyglet. I get an error not found by the resource. Using the above example, mix both relative and full file paths.

using pyglet.media.load() instead of pyglet.resource.media() allows me to upload files.

but sound.play() only plays the first part of a second of the file, unless I run pyglet.app.run() , which blocks everything else ...

+2
Dec 05 '09 at 12:52
source share

wxPython supports playing wav files on Windows and Unix - I'm not sure if this includes a Mac. However, it only supports wav files, as far as I can tell - it does not support other common formats such as mp3 or ogg.

+2
Dec 05 '09 at 14:53
source share

I just released a simple python shell around sox that will play sound with Python. It is very easy to install, since you need Python 2.6 or higher, sox (easy to get binaries for most architectures) and shell ( https://github.com/standarddeviant/sound4python ), If you don't have sox, go here: http : //sourceforge.net/projects/sox/files/sox/

You will play audio using:

 from sound4python import sound import random a = [] for idx in xrange(1*16000): a.append(random.randint(-16384,16384)) sound(a) 

Keep in mind that the only parts actually involved in reproducing sound are only the following:

 from sound4python import sound ... sound(a) 
+2
Mar 10 '13 at 14:35
source share

For a Linux user, if low pcm data processing is required, try the alsaaudio module. Inside the package there is also an example playwav.py.

+1
Apr 2 '13 at 15:46
source share



All Articles