1 /*
2 audio.c:
3
4 Code for audio-related functions
5
6 Copyright 2000, 2003, 2010.
7 Authors: Jesse Andrews, David Bruce.
8
9 Project email: <tux4kids-tuxtype-dev@lists.alioth.debian.org>
10 Project website: http://tux4kids.alioth.debian.org
11
12 audio.c is part of Tux Typing, a.k.a "tuxtype".
13
14 Tux Typing is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 Tux Typing is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 */
27
28
29
30
31 #include "globals.h"
32 #include "funcs.h"
33
34 static Mix_Music* defaultMusic = NULL; // holds music for audioMusicLoad/unload
35
36 // play sound once
PlaySound(Mix_Chunk * snd)37 void PlaySound(Mix_Chunk* snd)
38 {
39 PlaySoundLoop(snd, 0);
40 }
41
42 // play sound with optional repeats, or -1 for infinite
PlaySoundLoop(Mix_Chunk * snd,int loops)43 void PlaySoundLoop(Mix_Chunk* snd, int loops)
44 {
45 if(!snd)
46 return;
47 if (!settings.sys_sound)
48 return;
49
50 Mix_PlayChannel(-1, snd, loops);
51 }
52
53 // halt a channel or -1 for all
audioHaltChannel(int channel)54 void audioHaltChannel(int channel)
55 {
56 Mix_HaltChannel(channel);
57 }
58
59 /* MusicLoad attempts to load and play the music file
60 * Note: loops == -1 means forever
61 */
MusicLoad(const char * musicFilename,int loops)62 void MusicLoad(const char* musicFilename, int loops)
63 {
64 Mix_Music* tmp_music = NULL;
65
66 if (!settings.sys_sound) return;
67 if (!musicFilename) return;
68
69 tmp_music = LoadMusic(musicFilename);
70
71 if (tmp_music)
72 {
73 MusicUnload(); //Unload previous defaultMusic
74 defaultMusic = tmp_music;
75 Mix_PlayMusic(defaultMusic, loops);
76 }
77 }
78
79
80 /* MusicUnload attempts to unload any music data that was
81 * loaded using the audioMusicLoad function
82 */
MusicUnload(void)83 void MusicUnload(void)
84 {
85 if (!settings.sys_sound) return;
86
87 if (defaultMusic)
88 {
89 Mix_FreeMusic(defaultMusic);
90 defaultMusic = NULL;
91 }
92 }
93
94
95 /* audioMusicPlay attempts to play the passed music data.
96 * if a music file was loaded using the audioMusicLoad
97 * it will be stopped and unloaded
98 * Note: loops == -1 means forever
99 */
MusicPlay(Mix_Music * musicData,int loops)100 void MusicPlay(Mix_Music* musicData, int loops)
101 {
102 if (!settings.sys_sound) return;
103 if (!musicData) return;
104
105 /* Stop previous music before playing new one: */
106 MusicUnload();
107 Mix_PlayMusic(musicData, loops);
108 }
109