1 /*
2  * Copyright (c) 2008,2009 Bertrand Janin <tamentis@neopulsar.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 
28 #include <stdio.h>
29 #include <time.h>
30 
31 #include "SDL.h"
32 #include "SDL_mixer.h"
33 
34 #include "rezerwar.h"
35 
36 bool has_sound = false;
37 
38 Mix_Music *music = NULL;
39 Mix_Chunk *tick1;
40 Mix_Chunk *tack1;
41 Mix_Chunk *horn;
42 Mix_Chunk *boom;
43 Mix_Chunk *lazer;
44 Mix_Chunk *menunav;
45 Mix_Chunk *menuselect;
46 Mix_Chunk *splash;
47 
48 void
sfx_init()49 sfx_init()
50 {
51 	/* Open a mixer */
52 	if (Mix_OpenAudio(48000, MIX_DEFAULT_FORMAT, 2, 2048) >= 0)
53 		has_sound = true;
54 
55 	if (has_sound)
56 		Mix_AllocateChannels(16);
57 }
58 
59 
60 void
sfx_kill()61 sfx_kill()
62 {
63 	Mix_CloseAudio();
64 }
65 
66 
67 Mix_Chunk*
sfx_load_sample(char * filename)68 sfx_load_sample(char *filename)
69 {
70 	Mix_Chunk *sample;
71 	char *path;
72 
73 	path = dpath(filename);
74 	sample = Mix_LoadWAV(path);
75 	r_free(path);
76 
77 	if (!sample) {
78 		fprintf(stderr, "Mix_LoadWAV: %s\n", Mix_GetError());
79 		exit(-1);
80 	}
81 
82 	return sample;
83 }
84 
85 void
sfx_unload_library()86 sfx_unload_library()
87 {
88 	if (!has_sound)
89 		return;
90 
91 	Mix_FreeChunk(tick1);
92 	Mix_FreeChunk(tack1);
93 	Mix_FreeChunk(horn);
94 	Mix_FreeChunk(boom);
95 	Mix_FreeChunk(lazer);
96 	Mix_FreeChunk(menunav);
97 	Mix_FreeChunk(menuselect);
98 	Mix_FreeChunk(splash);
99 }
100 
101 
102 void
sfx_load_library()103 sfx_load_library()
104 {
105 	if (!has_sound)
106 		return;
107 
108 	tick1 = sfx_load_sample("sfx/tick1.wav");
109 	tack1 = sfx_load_sample("sfx/tack1.wav");
110 	horn = sfx_load_sample("sfx/horn.wav");
111 	boom = sfx_load_sample("sfx/boom.wav");
112 	lazer = sfx_load_sample("sfx/lazer.wav");
113 	menunav = sfx_load_sample("sfx/menunav.wav");
114 	menuselect = sfx_load_sample("sfx/menuselect.wav");
115 	splash = sfx_load_sample("sfx/splash.wav");
116 }
117 
118 
sfx_play_tack1()119 void sfx_play_tack1() { if (has_sound) Mix_PlayChannel(-1, tack1, 0); }
sfx_play_tick1()120 void sfx_play_tick1() { if (has_sound) Mix_PlayChannel(-1, tick1, 0); }
sfx_play_horn()121 void sfx_play_horn() { if (has_sound) Mix_PlayChannel(-1, horn, 0); }
sfx_play_boom()122 void sfx_play_boom() { if (has_sound) Mix_PlayChannel(-1, boom, 0); }
sfx_play_lazer()123 void sfx_play_lazer() { if (has_sound) Mix_PlayChannel(-1, lazer, 0); }
sfx_play_menunav()124 void sfx_play_menunav() { if (has_sound) Mix_PlayChannel(-1, menunav, 0); }
sfx_play_menuselect()125 void sfx_play_menuselect() { if (has_sound) Mix_PlayChannel(-1, menuselect, 0); }
sfx_play_splash()126 void sfx_play_splash() { if (has_sound) Mix_PlayChannel(-1, menuselect, 0); }
127 
128 void
sfx_play_music(char * filename)129 sfx_play_music(char *filename)
130 {
131 	char *path;
132 
133 	if (!has_sound)
134 		return;
135 
136 	Mix_FreeMusic(music);
137 
138 	path = dpath(filename);
139 	// load the song
140 	if(!(music=Mix_LoadMUS(path))) {
141 		fprintf(stderr, "Mix_LoadMUS error (%s)\n", filename);
142 		exit(-1);
143 	}
144 	r_free(path);
145 
146 	// set the post mix processor up
147 	if (Mix_PlayMusic(music, -1)==-1) {
148 		fprintf(stderr, "Mix_LoadMUS error\n");
149 		exit(-1);
150 	}
151 }
152 
153 void
sfx_stop_music()154 sfx_stop_music()
155 {
156 	Mix_FadeOutMusic(200);
157 	Mix_FreeMusic(music);
158 	music = NULL;
159 }
160 
161 void
sfx_toggle_mute(bool yup)162 sfx_toggle_mute(bool yup)
163 {
164 	if (yup) {
165 		Mix_Volume(-1, 0);
166 		Mix_VolumeMusic(0);
167 	} else {
168 		Mix_Volume(-1, MIX_MAX_VOLUME);
169 		Mix_VolumeMusic(MIX_MAX_VOLUME);
170 	}
171 }
172