1 
2 /* SOUND.C
3   PXT/SS/Org sound interface
4 */
5 #include <stdio.h>
6 #include <string.h>
7 
8 #include "../nx.h"
9 #include "../settings.h"
10 #include "pxt.h"
11 #include "sound.h"
12 #include "sound.fdh"
13 
14 #include "libretro_shared.h"
15 
16 #ifdef _WIN32
17 #include "msvc_compat.h"
18 #endif
19 
20 #define MUSIC_OFF		0
21 #define MUSIC_ON		1
22 #define MUSIC_BOSS_ONLY	2
23 static int lastsong = 0;		// this holds the previous song, for <RMU
24 static int cursong = 0;
25 
26 // there are more than this around 9b; those are drums and are loaded by the org module
27 #define NUM_SOUNDS		0x75
28 #define ORG_VOLUME		75
29 
30 const char *org_names[] =
31 {
32 	NULL,
33 	"egg",
34    "safety",
35    "gameover",
36    "gravity",
37    "grasstown",
38    "meltdown2",
39    "eyesofflame",
40 	"gestation",
41    "town",
42    "fanfale1",
43    "balrog",
44    "cemetary",
45    "plant",
46    "pulse",
47    "fanfale2",
48 	"fanfale3",
49    "tyrant",
50    "run",
51    "jenka1",
52    "labyrinth",
53    "access",
54    "oppression",
55    "geothermal",
56 	"theme",
57    "oside",
58    "heroend",
59    "scorching",
60    "quiet",
61    "lastcave",
62    "balcony",
63    "charge",
64 	"lastbattle",
65    "credits",
66    "zombie",
67    "breakdown",
68    "hell",
69    "jenka2",
70    "waterway",
71    "seal",
72 	"toroko",
73    "white",
74    "azarashi",
75    NULL
76 };
77 
78 static const char bossmusic[] = { 4, 7, 10, 11, 15, 16, 17, 18, 21, 22, 31, 33, 35, 0 };
79 
sound_init(FILE * fp)80 bool sound_init(FILE *fp)
81 {
82 	if (SSInit()) return 1;
83 	if (pxt_init()) return 1;
84 
85 	if (pxt_LoadSoundFX(fp, NUM_SOUNDS))
86       return 1;
87 
88 	if (org_init(fp, ORG_VOLUME))
89 	{
90 		NX_ERR("Music failed to initialize\n");
91 		return 1;
92 	}
93 
94 	return 0;
95 }
96 
sound_close(void)97 void sound_close(void)
98 {
99 	pxt_freeSoundFX();
100 	SSClose();
101 }
102 
103 /*
104 void c------------------------------() {}
105 */
106 
sound(int snd)107 void sound(int snd)
108 {
109 	if (!settings->sound_enabled)
110 		return;
111 
112 	pxt_Stop(snd);
113 	pxt_Play(-1, snd, 0);
114 }
115 
sound_loop(int snd)116 void sound_loop(int snd)
117 {
118 	if (!settings->sound_enabled)
119 		return;
120 
121 	pxt_Play(-1, snd, -1);
122 }
123 
sound_stop(int snd)124 void sound_stop(int snd)
125 {
126 	pxt_Stop(snd);
127 }
128 
sound_is_playing(int snd)129 bool sound_is_playing(int snd)
130 {
131 	return pxt_IsPlaying(snd);
132 }
133 
134 
StartStreamSound(int freq)135 void StartStreamSound(int freq)
136 {
137 	// pxt_ChangePitch(SND_STREAM1, some_formula);
138 	// pxt_ChangePitch(SND_STREAM2, some_other_formula);
139 	sound_loop(SND_STREAM1);
140 	sound_loop(SND_STREAM2);
141 }
142 
StartPropSound(void)143 void StartPropSound(void)
144 {
145 	sound_loop(SND_PROPELLOR);
146 }
147 
StopLoopSounds(void)148 void StopLoopSounds(void)
149 {
150 	sound_stop(SND_STREAM1);
151 	sound_stop(SND_STREAM2);
152 	sound_stop(SND_PROPELLOR);
153 }
154 
155 /*
156 void c------------------------------() {}
157 */
158 
music(int songno)159 void music(int songno)
160 {
161 	if (songno == cursong)
162 		return;
163 
164 	lastsong = cursong;
165 	cursong = songno;
166 
167 	NX_LOG(" >> music(%d)\n", songno);
168 
169 	if (songno != 0 && !should_music_play(songno, settings->music_enabled))
170 	{
171 		NX_WARN("Not playing track %d because music_enabled is %d\n", songno, settings->music_enabled);
172 		org_stop();
173 		return;
174 	}
175 
176 	start_track(songno);
177 }
178 
179 
should_music_play(int songno,int musicmode)180 bool should_music_play(int songno, int musicmode)
181 {
182 	if (game.mode == GM_TITLE || game.mode == GM_CREDITS)
183 		return true;
184 
185 	switch(musicmode)
186 	{
187 		case MUSIC_OFF: return false;
188 		case MUSIC_ON:  return true;
189 		case MUSIC_BOSS_ONLY:
190 			return music_is_boss(songno);
191 	}
192 
193 	return false;
194 }
195 
music_is_boss(int songno)196 bool music_is_boss(int songno)
197 {
198 	if (strchr(bossmusic, songno))
199 		return true;
200 	else
201 		return false;
202 }
203 
music_set_enabled(int newstate)204 void music_set_enabled(int newstate)
205 {
206 	if (newstate != settings->music_enabled)
207 	{
208 		NX_LOG("music_set_enabled(%d)\n", newstate);
209 
210 		settings->music_enabled = newstate;
211 		bool play = should_music_play(cursong, newstate);
212 
213 		if (play != org_is_playing())
214 		{
215 			if (play)
216 				start_track(cursong);
217 			else
218 				org_stop();
219 		}
220 	}
221 }
222 
start_track(int songno)223 static void start_track(int songno)
224 {
225 	if (songno == 0)
226 	{
227 		org_stop();
228 		return;
229 	}
230 
231    NX_LOG("start_track: %d\n\n", songno);
232 
233 	if (!org_load(songno))
234 		org_start(0);
235 }
236 
music_cursong()237 int music_cursong()		{ return cursong; }
music_lastsong()238 int music_lastsong() 	{ return lastsong; }
239 
240 
241