| 1 |
# Copyright 2014 Patrick Dawson <[email protected]> |
| 2 |
# |
| 3 |
# This software is provided 'as-is', without any express or implied |
| 4 |
# warranty. In no event will the authors be held liable for any damages |
| 5 |
# arising from the use of this software. |
| 6 |
# |
| 7 |
# Permission is granted to anyone to use this software for any purpose, |
| 8 |
# including commercial applications, and to alter it and redistribute it |
| 9 |
# freely, subject to the following restrictions: |
| 10 |
# |
| 11 |
# 1. The origin of this software must not be misrepresented; you must not |
| 12 |
# claim that you wrote the original software. If you use this software |
| 13 |
# in a product, an acknowledgment in the product documentation would be |
| 14 |
# appreciated but is not required. |
| 15 |
# 2. Altered source versions must be plainly marked as such, and must not be |
| 16 |
# misrepresented as being the original software. |
| 17 |
# 3. This notice may not be removed or altered from any source distribution. |
| 18 |
|
| 19 |
from sdl2 cimport * |
| 20 |
from sdl2_mixer cimport * |
| 21 |
from pygame_sdl2.rwobject cimport to_rwops |
| 22 |
from libc.string cimport memset |
| 23 |
|
| 24 |
from pygame_sdl2.error import error |
| 25 |
|
| 26 |
cdef Mix_Music *current_music = NULL |
| 27 |
cdef object queued_music = None |
| 28 |
cdef int endevent = 0 |
| 29 |
|
| 30 |
cdef void music_finished() noexcept: |
| 31 |
global queued_music |
| 32 |
if queued_music: |
| 33 |
load(queued_music) |
| 34 |
play() |
| 35 |
queued_music = None |
| 36 |
|
| 37 |
cdef SDL_Event e |
| 38 |
if endevent != 0: |
| 39 |
memset(&e, 0, sizeof(SDL_Event)) |
| 40 |
e.type = endevent |
| 41 |
SDL_PushEvent(&e) |
| 42 |
|
| 43 |
|
| 44 |
def load(fi): |
| 45 |
global current_music |
| 46 |
|
| 47 |
# Free any previously loaded music. |
| 48 |
if current_music != NULL: |
| 49 |
Mix_FreeMusic(current_music) |
| 50 |
|
| 51 |
current_music = Mix_LoadMUS_RW(to_rwops(fi), 1) |
| 52 |
if current_music == NULL: |
| 53 |
raise error() |
| 54 |
|
| 55 |
def play(loops=0, double start=0.0): |
| 56 |
Mix_FadeInMusicPos(current_music, loops, 0, start) |
| 57 |
|
| 58 |
def rewind(): |
| 59 |
Mix_RewindMusic() |
| 60 |
|
| 61 |
def stop(): |
| 62 |
Mix_HaltMusic() |
| 63 |
|
| 64 |
def pause(): |
| 65 |
Mix_PauseMusic() |
| 66 |
|
| 67 |
def unpause(): |
| 68 |
Mix_ResumeMusic() |
| 69 |
|
| 70 |
def fadeout(time): |
| 71 |
Mix_FadeOutMusic(time) |
| 72 |
|
| 73 |
def set_volume(double value): |
| 74 |
Mix_VolumeMusic(<int>(MIX_MAX_VOLUME * value)) |
| 75 |
|
| 76 |
def get_volume(): |
| 77 |
return Mix_VolumeMusic(-1) / <double>MIX_MAX_VOLUME |
| 78 |
|
| 79 |
def get_busy(): |
| 80 |
return Mix_PlayingMusic() |
| 81 |
|
| 82 |
def set_pos(double pos): |
| 83 |
Mix_SetMusicPosition(pos) |
| 84 |
|
| 85 |
def get_pos(): |
| 86 |
# TODO: Use a Mix_SetPostMix callback to track position. |
| 87 |
raise error("Not implemented.") |
| 88 |
|
| 89 |
def queue(fi): |
| 90 |
Mix_HookMusicFinished(music_finished) |
| 91 |
if get_busy(): |
| 92 |
global queued_music |
| 93 |
queued_music = fi |
| 94 |
else: |
| 95 |
load(fi) |
| 96 |
play() |
| 97 |
|
| 98 |
def set_endevent(type=None): |
| 99 |
Mix_HookMusicFinished(music_finished) |
| 100 |
global endevent |
| 101 |
endevent = type or 0 |
| 102 |
|
| 103 |
def get_endevent(): |
| 104 |
return endevent |