Welcome, guest | Sign In | My Account | Store | Cart

I've needed just to play audio in my Tkinter application, but it seems there is no easy way to do this, so I have made this simple code. It is small ctypes wrapper around SDL_mixer.

It should work for Win and *nix also .. I've tested it on ubuntu-14.04.3-desktop-i386

Python, 93 lines
 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import ctypes
from ctypes.util import find_library
import os


_sdl_audio_init=False
_sdl_audio_music=None
_sdl_audio_sounds={}

_libsdl=None
_libsdl_mixer=None

def sdl_audio_load(snd,fn=None,reload=False):
 global _sdl_audio_sounds,_libsdl,_libsdl_mixer
 if snd not in _sdl_audio_sounds or reload:
  sdl_audio_init()
  if snd in _sdl_audio_sounds: _libsdl_mixer.Mix_FreeChunk(_sdl_audio_sounds[snd])
  rw=_libsdl.SDL_RWFromFile(fn,'rb')
  _sdl_audio_sounds[snd]=_libsdl_mixer.Mix_LoadWAV_RW(rw,1)

def sdl_audio_play(snd,loop=0,channel=-1):
 global _sdl_audio_sounds,_libsdl_mixer
 sdl_audio_load(snd)
 _libsdl_mixer.Mix_PlayChannelTimed(channel,_sdl_audio_sounds[snd],loop,-1)

def sdl_audio_stop(channel=-1):
 global _libsdl_mixer
 return _libsdl_mixer.Mix_HaltChannel(channel)

def sdl_audio_volume(volume=-1,channel=-1):
 global _libsdl_mixer
 if type(volume) is type(.1): volume=int(volume*128)
 return _libsdl_mixer.Mix_Volume(channel,volume)


def sdl_audio_init():
 global _sdl_audio_init,_libsdl,_libsdl_mixer
 if _sdl_audio_init: return None

 try: _libsdl=ctypes.cdll.SDL
 except:
  name=find_library('SDL')
  if name is None: return False
  _libsdl=ctypes.cdll.LoadLibrary(name)
 
 try: _libsdl_mixer=ctypes.cdll.SDL_mixer
 except:
  name=find_library('SDL_mixer')
  if name is None: return False
  _libsdl_mixer=ctypes.cdll.LoadLibrary(name)
 
 _libsdl.SDL_Init(0)
 _libsdl.SDL_InitSubSystem(0x00000010)# Init audio subsystem
 _libsdl_mixer.Mix_Init()
 _libsdl_mixer.Mix_OpenAudio(44100,0x8010,2,512)
 _sdl_audio_init=True
 return True


def sdl_audio_playmusic(fn,loop=1):
 global _sdl_audio_music,_libsdl_mixer
 sdl_audio_init()
 sdl_audio_stopmusic()
 _sdl_audio_music=_libsdl_mixer.Mix_LoadMUS(os.path.realpath(fn))
 _libsdl_mixer.Mix_PlayMusic(_sdl_audio_music,loop)

def sdl_audio_pausemusic():
 global _sdl_audio_music,_libsdl_mixer
 if _sdl_audio_music is not None:
  if _libsdl_mixer.Mix_PlayingMusic(): _libsdl_mixer.Mix_PauseMusic()

def sdl_audio_stopmusic():
 global _sdl_audio_music,_libsdl_mixer
 sdl_audio_pausemusic()
 _libsdl_mixer.Mix_HaltMusic()
 _libsdl_mixer.Mix_FreeMusic(_sdl_audio_music)
 _sdl_audio_music=None

def sdl_audio_volumemusic(volume=-1):
 global _libsdl_mixer
 if type(volume) is type(.1): volume=int(volume*128)
 return _libsdl_mixer.Mix_VolumeMusic(volume)

def sdl_audio_quit():
 global _sdl_audio_init,_sdl_audio_sounds,_libsdl,_libsdl_mixer
 if not _sdl_audio_init: return False
 sdl_audio_stopmusic()
 sdl_audio_stop()
 map(lambda chunk: _libsdl_mixer.Mix_FreeChunk(chunk),_sdl_audio_sounds.values())
 _libsdl_mixer.Mix_CloseAudio()
 _libsdl_mixer.Mix_Quit()
 _libsdl.SDL_Quit()
 return True

[Prerequisites]

You will need to install these DLLs to make it work:
SDL
SDL_mixer

Simply download DLLs and unpack them to your app directory.

For linux users, this should be enought:
sudo apt-get install libsdl1.2debian
sudo apt-get install libsdl1.2-dev
sudo apt-get install libsdl-mixer1.2
sudo apt-get install libsdl-mixer1.2-dev

[Usage and notes]

It will do all ini stuff for you.

sdl_audio_volumemusic works AFTER sdl_audio_playmusic is called
volume can be 0 - 128

At the end just call sdl_audio_quit to clear stuff.

[Example]

import sdl_audio# lib
import time

sdl_audio.sdl_audio_playmusic('./ambient_background.ogg',-1)
time.sleep(3)# play music fo 3 secs
sdl_audio.sdl_audio_play('./shot.wav')
time.sleep(1)# wait for shot
sdl_audio.sdl_audio_quit()

[Updates]

17.8.2015

  • better sdl searching
  • now should work on linux too (tested on ubuntu-14.04.3-desktop-i386)
  • sdl_audio_init() returns True on success, None if already initialized and False on error
  • minor fixes

29.6.2015

  • added sound support
  • sdl_audio_init is now function by itself