0
|
1 /*
|
|
2 * Copyright (c) 2004-2006 Kazunori "jagarl" Ueno
|
|
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 * 3. The name of the author may not be used to endorse or promote products
|
|
14 * derived from this software without specific prior written permission.
|
|
15 *
|
|
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
26 */
|
|
27
|
|
28 /* music.cc SDL_mixer ¤òÍѤ¤¤¿²»³ÚºÆÀ¸¥ë¡¼¥Á¥ó */
|
|
29
|
|
30
|
|
31 #include <string.h>
|
|
32 #include <stdio.h>
|
|
33 #include <unistd.h>
|
|
34 #include <fcntl.h>
|
|
35 #include <ctype.h>
|
|
36 #include <signal.h>
|
|
37 #include"system/system_config.h"
|
|
38 #include"system/file.h"
|
|
39 #include "music.h"
|
|
40 #include<SDL.h>
|
|
41 #include<SDL_mixer.h>
|
|
42 #include"wavfile.h"
|
|
43
|
|
44 using namespace std;
|
|
45
|
|
46 int pcm_enable = 0;
|
|
47 Mix_Chunk *play_chunk[MIX_PCM_SIZE];
|
|
48
|
|
49 MuSys::MuSys(AyuSysConfig& _config) : config(_config), movie_id(-1), music_enable(1) {
|
|
50 int i;
|
|
51 for (i=0; i<MIX_PCM_SIZE; i++)
|
|
52 play_chunk[i] = 0;
|
|
53 cdrom_track[0] = 0;
|
|
54 effec_track[0] = 0;
|
|
55 }
|
|
56
|
|
57
|
|
58 // #define delete fprintf(stderr,"smus.cc: %d.",__LINE__), delete
|
|
59
|
26
|
60 void bgm_start(const char* path, int loop_pt, int * volmod);
|
|
61 void effec_start(int chn, const char* path, int loop, int fadein_time, int * volmod);
|
0
|
62 void bgm_fadeout(int time);
|
|
63
|
|
64 void MuSys::PlayCDROM(char* name, int play_count) {
|
26
|
65 config.GetParam("#VOLMOD", 4, &volmod[0], &volmod[1], &volmod[2], &volmod[3]);
|
0
|
66 char wave[128]; wave[127] = '\0'; wave[0] = '\0';
|
|
67
|
|
68 strcpy(cdrom_track, name);
|
|
69
|
|
70 StopCDROM(0);
|
|
71 strcpy(cdrom_track, name);
|
|
72
|
|
73 /* name -> track */
|
|
74 int track =config.track_name.CDTrack(name);
|
|
75 if (track == -1) track = atoi(name);
|
|
76 if (config.track_name.WaveTrack(name) != 0) strncpy(wave, config.track_name.WaveTrack(name), 127);
|
|
77 if (wave[0] == 0 && track != 0) { /* DSTRACK ¤¬¸«¤Ä¤«¤é¤Ê¤¤¾ì¹ç¡¢CDTRACK¤ò»ÈÍѤ¹¤ë */
|
|
78 sprintf(wave, "audio_%02d",track);
|
|
79 }
|
|
80 if (wave == 0) return;
|
|
81 // BGM ºÆÀ¸
|
|
82 if (!pcm_enable) return;
|
|
83 if (play_count == 0)
|
26
|
84 bgm_start(wave, -1, volmod);
|
0
|
85 else
|
26
|
86 bgm_start(wave, config.track_name.TrackStart(name), volmod);
|
0
|
87 return;
|
|
88 }
|
|
89
|
|
90 void MuSys::StopCDROM(int time)
|
|
91 {
|
|
92 cdrom_track[0] = '\0';
|
|
93 if (!pcm_enable) return;
|
|
94 bgm_fadeout(time);
|
|
95 }
|
|
96
|
|
97 void MuSys::PlaySE(const char* se, int loop_flag, int channel) {
|
|
98 if (! pcm_enable) return;
|
|
99 if (loop_flag)
|
26
|
100 effec_start(MIX_PCM_EFFEC, se, 10000, 0, volmod);
|
0
|
101 else
|
26
|
102 effec_start(MIX_PCM_EFFEC, se, 0, 0, volmod);
|
0
|
103 return;
|
|
104 }
|
|
105 void MuSys::PlaySE(int number) {
|
|
106 if (! pcm_enable) return;
|
|
107 const char* se_name = config.track_name.SETrack(number);
|
|
108 if (se_name == 0) return;
|
26
|
109 effec_start(MIX_PCM_EFFEC, se_name, 0, 0, volmod);
|
0
|
110 return;
|
|
111 }
|
|
112 void MuSys::StopSE(int time) {
|
|
113 if (! pcm_enable) return;
|
|
114 if (time == 0)
|
|
115 Mix_HaltChannel(MIX_PCM_EFFEC);
|
|
116 else
|
|
117 Mix_FadeOutChannel(MIX_PCM_EFFEC, time);
|
|
118 }
|
|
119 bool MuSys::IsStopSE(void) {
|
|
120 if (! pcm_enable) return true;
|
|
121 if (Mix_Playing(MIX_PCM_EFFEC) != 0) return false;
|
|
122 return true;
|
|
123 }
|
|
124
|
|
125 void MuSys::StopKoe(int time) {
|
|
126 if (! pcm_enable) return;
|
|
127 if (time == 0) Mix_HaltChannel(MIX_PCM_KOE);
|
|
128 else Mix_FadeOutChannel(MIX_PCM_KOE, time);
|
|
129 }
|
|
130
|
|
131 void MuSys::InitMusic(void)
|
|
132 {
|
|
133 if (music_enable != 1) return;
|
|
134 cdrom_track[0] = '\0';
|
|
135 if ( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, DEFAULT_AUDIOBUF ) < 0 ){
|
|
136 // if ( Mix_OpenAudio( 48000, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, DEFAULT_AUDIOBUF ) < 0 ){
|
|
137 return;
|
|
138 }
|
|
139 int freq, channels; Uint16 format;
|
|
140 if ( Mix_QuerySpec(&freq, &format, &channels) ) {
|
|
141 WAVFILE::freq = freq;
|
|
142 WAVFILE::format = format;
|
|
143 WAVFILE::channels = channels;
|
|
144 }
|
|
145 pcm_enable = 1;
|
|
146 Mix_AllocateChannels( MIX_PCM_SIZE);
|
|
147 music_enable = 2;
|
|
148 return;
|
|
149 }
|
|
150 void MuSys::FinalizeMusic(void)
|
|
151 {
|
|
152 if (music_enable != 2) return;
|
|
153 int i;
|
|
154 for (i=0; i<MIX_PCM_SIZE; i++) {
|
|
155 Mix_HaltChannel(i);
|
|
156 if (play_chunk[i]) {
|
|
157 Mix_FreeChunk(play_chunk[i]);
|
|
158 }
|
|
159 play_chunk[i] = 0;
|
|
160 }
|
|
161 Mix_HaltMusic();
|
|
162 Mix_HookMusic(0,0);
|
|
163 Mix_CloseAudio();
|
|
164 pcm_enable = 0;
|
|
165 music_enable = 1;
|
|
166 }
|
|
167
|
|
168 /*************************************************************************
|
|
169 **
|
|
170 ** ¥Õ¥¡¥¤¥ëÆɤ߹þ¤ß / ³°Éô¥³¥Þ¥ó¥É¸Æ¤Ó½Ð¤·
|
|
171 */
|
|
172
|
|
173 struct WavChunk {
|
|
174 WAVFILE* wav;
|
|
175 int loop_pt;
|
26
|
176 int *volmod;
|
0
|
177 static void callback(void* userdata, Uint8* stream, int len);
|
|
178 };
|
|
179 WavChunk wav_playing;
|
|
180 static int fadetime_total;
|
|
181 static int fadecount;
|
|
182
|
|
183 void WavChunk::callback(void *userdata, Uint8 *stream, int len)
|
|
184 {
|
|
185 WavChunk* chunk = (WavChunk*)userdata;
|
|
186 int count;
|
|
187 if (chunk->loop_pt == -2) { // ºÆÀ¸½ªÎ»¸å
|
|
188 memset(stream, 0, len);
|
|
189 return;
|
|
190 }
|
37
|
191
|
|
192 char* stream_dup = new char[len];
|
|
193 count = chunk->wav->Read( (char*)stream_dup, 4, len/4);
|
0
|
194
|
|
195 if (count != len/4) {
|
|
196 // ºÇ¸å¤Þ¤ÇºÆÀ¸¤·¤¿
|
|
197 if (chunk->loop_pt == -1) { // ½ªÎ»
|
|
198 chunk->loop_pt = -2;
|
37
|
199 memset(stream_dup+count*4, 0, len-count*4);
|
0
|
200 } else {
|
|
201 chunk->wav->Seek(chunk->loop_pt);
|
37
|
202 chunk->wav->Read( (char*)(stream_dup+count*4), 4, len/4-count);
|
0
|
203 }
|
|
204 }
|
3
|
205
|
26
|
206 int cur_vol = (*chunk->volmod)*SDL_MIX_MAXVOLUME/255;
|
3
|
207
|
0
|
208 if (fadetime_total) {
|
|
209 // ²»³Ú¤òÄä»ßÃæ (fade out)
|
|
210 int count_total = fadetime_total*(WAVFILE::freq/1000);
|
|
211 if (fadecount > count_total || fadetime_total == 1) { // ²»³ÚÄä»ß
|
|
212 chunk->loop_pt = -2;
|
|
213 memset(stream, 0, len);
|
37
|
214 delete[] stream_dup;
|
0
|
215 return;
|
|
216 }
|
3
|
217 cur_vol = cur_vol*(count_total-fadecount)/count_total;
|
0
|
218 fadecount += len/4;
|
|
219 }
|
37
|
220
|
3
|
221 SDL_MixAudio(stream, (Uint8*)stream_dup, len, cur_vol);
|
7
|
222 delete[] stream_dup;
|
37
|
223
|
0
|
224 return;
|
|
225 }
|
|
226 void bgm_fadeout(int time) {
|
|
227 fadecount = 0;
|
|
228 if (time <= 0) time = 1;
|
|
229 fadetime_total = time;
|
|
230 }
|
|
231
|
|
232 static SDL_RWops* OpenSDLRW(const char* path);
|
|
233 static WAVFILE* OpenWaveFile(const char* path);
|
26
|
234 void bgm_start(const char* path, int loop_pt, int * volmod) {
|
0
|
235 if (! pcm_enable) return;
|
|
236 fprintf(stderr,"bgm start %s\n",path);
|
|
237 WAVFILE* wav = OpenWaveFile(path);
|
|
238 if (wav == 0) return;
|
|
239 Mix_PauseMusic();
|
|
240 Mix_HaltMusic();
|
|
241 Mix_HookMusic(0,0);
|
|
242 /* Á°¤ËºÆÀ¸¤·¤Æ¤¤¤¿¤Î¤ò½ªÎ» */
|
|
243 if (wav_playing.wav) {
|
|
244 delete wav_playing.wav;
|
|
245 wav_playing.wav = 0;
|
|
246 }
|
|
247 wav_playing.wav = wav;
|
|
248 wav_playing.loop_pt = loop_pt;
|
26
|
249 wav_playing.volmod = &volmod[0];
|
0
|
250 fadetime_total = 0;
|
|
251 fadecount = 0;
|
|
252 Mix_HookMusic( &(WavChunk::callback), (void*)&wav_playing);
|
|
253 return;
|
|
254 }
|
|
255
|
26
|
256 void effec_start(int chn, const char* path, int loop, int fadein_time, int * volmod) {
|
0
|
257 if (! pcm_enable) return;
|
|
258 SDL_RWops* op = OpenSDLRW(path);
|
|
259 if (op == 0) { // ¥Õ¥¡¥¤¥ë¤¬¸«ÉÕ¤«¤é¤Ê¤¤
|
|
260 return;
|
|
261 }
|
|
262 Mix_Pause(chn);
|
|
263
|
|
264 if (play_chunk[chn]) {
|
|
265 Mix_FreeChunk(play_chunk[chn]);
|
|
266 }
|
|
267 play_chunk[chn] = Mix_LoadWAV_RW(op, 1);
|
|
268 if (fadein_time <= 0) {
|
26
|
269 Mix_Volume(chn, volmod[3]*SDL_MIX_MAXVOLUME/255);
|
0
|
270 Mix_PlayChannel(chn, play_chunk[chn],loop);
|
|
271 } else {
|
26
|
272 Mix_Volume(chn, volmod[3]*SDL_MIX_MAXVOLUME/255);
|
0
|
273 Mix_FadeInChannel(chn, play_chunk[chn],loop,fadein_time);
|
|
274 }
|
|
275 return;
|
|
276 }
|
|
277
|
|
278 void MuSys::PlayKoe(const char* path) {
|
|
279 if (! pcm_enable) return;
|
|
280 static char* playing_koedata = 0;
|
|
281 int len = 0;
|
|
282 AvgKoeInfo koeinfo;
|
|
283 int chn = MIX_PCM_KOE;
|
|
284
|
|
285 Mix_Pause(chn);
|
|
286 Mix_HaltChannel(chn); // ¤³¤ì¤Ç RWop ¤¬²òÊü¤µ¤ì¤ë¤Ï¤º¡Ä
|
|
287 if (play_chunk[chn]) {
|
|
288 Mix_FreeChunk(play_chunk[chn]);
|
|
289 play_chunk[chn] = 0;
|
|
290 }
|
|
291
|
|
292 if (playing_koedata) {
|
7
|
293 free(playing_koedata);
|
0
|
294 playing_koedata = 0;
|
|
295 }
|
|
296
|
|
297 koeinfo = OpenKoeFile(path);
|
|
298
|
|
299 if (koeinfo.stream == 0) return;
|
|
300 playing_koedata = decode_koe(koeinfo, &len);
|
|
301 fclose(koeinfo.stream);
|
|
302 if (playing_koedata == 0) {
|
|
303 return;
|
|
304 }
|
26
|
305 Mix_Volume(chn, volmod[1]*SDL_MIX_MAXVOLUME/255);
|
0
|
306 play_chunk[chn] = Mix_LoadWAV_RW(SDL_RWFromMem(playing_koedata, len+0x2c), 1);
|
|
307 Mix_PlayChannel(chn, play_chunk[chn],0);
|
|
308 return;
|
|
309 }
|
|
310 AvgKoeInfo OpenKoeFile(const char* path) {
|
|
311 int radix = 10000;
|
|
312 /* if (global_system.Version() >= 2) */ radix *= 10;
|
|
313 AvgKoeInfo info;
|
|
314 info.stream = 0; info.length = 0; info.offset = 0;
|
|
315 if (isdigit(path[0]) && strchr(path,'.') == 0) { // ¿ôÃÍ (³ÈÄ¥»ÒÅù¤Ê¤·)
|
|
316 /* avg32 ·Á¼°¤Î²»À¼¥¢¡¼¥«¥¤¥Ö¤Î¥¥ã¥Ã¥·¥å¤ò¸¡º÷ */
|
|
317 int pointer = atoi(path);
|
|
318 int file_no = pointer / radix;
|
|
319 int index = pointer % radix;
|
|
320 info = FindKoe(file_no, index);
|
|
321 } else { // ¥Õ¥¡¥¤¥ë
|
|
322 int length;
|
|
323 ARCINFO* arcinfo = file_searcher.Find(FILESEARCH::KOE,path,".WPD");
|
|
324 if (arcinfo == 0) return info;
|
|
325 info.stream = arcinfo->OpenFile(&length);
|
|
326 info.rate = 22050;
|
|
327 info.length = length;
|
|
328 info.offset = ftell(info.stream);
|
|
329 info.type = koe_unknown;
|
|
330 delete arcinfo;
|
|
331 }
|
|
332 return info;
|
|
333 }
|
|
334
|
|
335 static SDL_RWops* OpenSDLRW(const char* path) {
|
|
336 /* ¤Þ¤º wav ¥Õ¥¡¥¤¥ë¤òõ¤¹ */
|
|
337 ARCINFO* info = file_searcher.Find(FILESEARCH::WAV,path,".wav");
|
|
338 if (info == 0) {
|
|
339 info = file_searcher.Find(FILESEARCH::WAV,path,".nwa");
|
|
340 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"nwa");
|
|
341 if (info) { // read NWA file
|
|
342
|
|
343 int dummy;
|
|
344 FILE* f = info->OpenFile(&dummy);
|
|
345 static char* d = 0;
|
|
346 int sz;
|
|
347 if (d != 0) delete[] d;
|
|
348 d = NWAFILE::ReadAll(f, sz);
|
|
349 return SDL_RWFromMem(d, sz);
|
|
350 }
|
|
351 }
|
|
352 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"wav");
|
|
353 if (info == 0) info = file_searcher.Find(FILESEARCH::WAV,path,".ogg");
|
|
354 if (info) {
|
|
355 int dummy;
|
|
356 FILE* f = info->OpenFile(&dummy);
|
|
357 delete info;
|
|
358 if (f == 0) return 0;
|
|
359 SDL_RWops* op = SDL_RWFromFP(f, 1);
|
|
360 return op;
|
|
361 }
|
|
362 return 0;
|
|
363 }
|
|
364
|
|
365 static WAVFILE* OpenWaveFile(const char* path) {
|
|
366 /* ¤Þ¤º wav ¥Õ¥¡¥¤¥ë¤òõ¤¹ */
|
|
367 ARCINFO* info = file_searcher.Find(FILESEARCH::WAV,path,".wav");
|
|
368 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"wav");
|
|
369 if (info) {
|
|
370 int size;
|
|
371 FILE* f = info->OpenFile(&size);
|
|
372 delete info;
|
|
373 if (f == 0) return 0;
|
|
374 WAVFILE* w = WAVFILE::MakeConverter(new WAVFILE_Stream(f, size));
|
|
375 return w;
|
|
376 }
|
|
377 /* ¼¡¤Ë nwa ¥Õ¥¡¥¤¥ë */
|
|
378 info = file_searcher.Find(FILESEARCH::WAV,path,".nwa");
|
|
379 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"nwa");
|
|
380 if (info) {
|
|
381 int size;
|
|
382 FILE* f = info->OpenFile(&size);
|
|
383 delete info;
|
|
384 if (f == 0) return 0;
|
|
385 WAVFILE* w = WAVFILE::MakeConverter(new NWAFILE(f));
|
|
386 return w;
|
|
387 }
|
|
388
|
|
389 /* ¼¡¤Ë mp3 ¥Õ¥¡¥¤¥ë */
|
|
390 info = file_searcher.Find(FILESEARCH::WAV,path,".mp3");
|
|
391 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"mp3");
|
|
392 if (info) {
|
|
393 int size;
|
|
394 FILE* f = info->OpenFile(&size);
|
|
395 delete info;
|
|
396 if (f == 0) return 0;
|
|
397 MP3FILE* w = new MP3FILE(f, size);
|
|
398 if (w->pimpl) {
|
|
399 return WAVFILE::MakeConverter(w);
|
|
400 }
|
|
401 delete w;
|
|
402 }
|
|
403
|
|
404 /* ¼¡¤Ë ogg ¥Õ¥¡¥¤¥ë */
|
|
405 info = file_searcher.Find(FILESEARCH::WAV,path,".ogg");
|
|
406 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"ogg");
|
|
407 if (info) {
|
|
408 int size;
|
|
409 FILE* f = info->OpenFile(&size);
|
|
410 delete info;
|
|
411 if (f == 0) return 0;
|
|
412 OggFILE* w = new OggFILE(f, size);
|
|
413 if (w->pimpl) {
|
|
414 return WAVFILE::MakeConverter(w);
|
|
415 }
|
|
416 delete w;
|
|
417 }
|
|
418 return 0;
|
|
419 }
|
|
420
|