comparison music2/music.cc @ 53:ddbcbd000206

* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons * ParseMoji moved to TextStream * Some cleaning (0 -> NULL when needed, removal of useless returns, ...)
author thib
date Sun, 19 Apr 2009 11:44:05 +0000
parents 15a18fbe6f21
children 4416cfac86ae
comparison
equal deleted inserted replaced
52:15a18fbe6f21 53:ddbcbd000206
32 #include <stdio.h> 32 #include <stdio.h>
33 #include <unistd.h> 33 #include <unistd.h>
34 #include <fcntl.h> 34 #include <fcntl.h>
35 #include <ctype.h> 35 #include <ctype.h>
36 #include <signal.h> 36 #include <signal.h>
37 #include"system/system_config.h" 37 #include "system/system_config.h"
38 #include"system/file.h" 38 #include "system/file.h"
39 #include "music.h" 39 #include "music.h"
40 #include <SDL.h> 40 #include <SDL.h>
41 #include <SDL_mixer.h> 41 #include <SDL_mixer.h>
42 #include "wavfile.h" 42 #include "wavfile.h"
43 43
44 using namespace std; 44 using namespace std;
45 45
46 int pcm_enable = 0; 46 MuSys * MuSys::_singleton = NULL;
47 Mix_Chunk *play_chunk[MIX_PCM_SIZE]; 47
48 48 MuSys* MuSys::GetInstance(void)
49 MuSys::MuSys(AyuSysConfig& _config) : config(_config), movie_id(-1), music_enable(1) { 49 {
50 if (_singleton == NULL)
51 _singleton = new MuSys;
52 return _singleton;
53 }
54
55 void MuSys::Quit(void)
56 {
57 if (_singleton != NULL) {
58 _singleton->FinalizeMusic();
59 delete _singleton;
60 _singleton = NULL;
61 }
62 }
63
64 MuSys::MuSys() : movie_id(-1), music_enable(1) {
50 int i; 65 int i;
66 config = AyuSysConfig::GetInstance();
51 for (i=0; i<MIX_PCM_SIZE; i++) 67 for (i=0; i<MIX_PCM_SIZE; i++)
52 play_chunk[i] = 0; 68 play_chunk[i] = 0;
53 cdrom_track[0] = 0; 69 cdrom_track[0] = 0;
54 effec_track[0] = 0; 70 effec_track[0] = 0;
55 } 71 }
56 72
57 73
58 // #define delete fprintf(stderr,"smus.cc: %d.",__LINE__), delete 74 // #define delete fprintf(stderr,"smus.cc: %d.",__LINE__), delete
59 75
60 void bgm_start(const char* path, int loop_pt, int * volmod); 76 void bgm_start(const char* path, int loop_pt);
61 void effec_start(int chn, const char* path, int loop, int fadein_time, int * volmod); 77 void effec_start(int chn, const char* path, int loop, int fadein_time);
62 void bgm_fadeout(int time); 78 void bgm_fadeout(int time);
63 79
64 void MuSys::PlayCDROM(char* name, int play_count) { 80 void MuSys::PlayCDROM(char* name, int play_count) {
65 config.GetParam("#VOLMOD", 4, &volmod[0], &volmod[1], &volmod[2], &volmod[3]); 81 config->GetParam("#VOLMOD", 4, &volmod[0], &volmod[1], &volmod[2], &volmod[3]);
66 82
67 char wave[128]; 83 char wave[128];
68 wave[127] = '\0'; 84 wave[127] = '\0';
69 wave[0] = '\0'; 85 wave[0] = '\0';
70 86
72 88
73 StopCDROM(0); 89 StopCDROM(0);
74 strcpy(cdrom_track, name); 90 strcpy(cdrom_track, name);
75 91
76 /* name -> track */ 92 /* name -> track */
77 int track =config.track_name.CDTrack(name); 93 int track =config->track_name.CDTrack(name);
78 if (track == -1) track = atoi(name); 94 if (track == -1) track = atoi(name);
79 if (config.track_name.WaveTrack(name) != NULL) strncpy(wave, config.track_name.WaveTrack(name), 127); 95 if (config->track_name.WaveTrack(name) != NULL)
96 strncpy(wave, config->track_name.WaveTrack(name), 127);
80 if (wave[0] == 0 && track != 0) { /* DSTRACK が見つからない場合、CDTRACKを使用する */ 97 if (wave[0] == 0 && track != 0) { /* DSTRACK が見つからない場合、CDTRACKを使用する */
81 sprintf(wave, "audio_%02d", track); 98 sprintf(wave, "audio_%02d", track);
82 } 99 }
83 if (wave == 0) return; 100 if (wave == 0) return;
84 // BGM 再生 101 // BGM 再生
85 if (!pcm_enable) return; 102 if (!pcm_enable) return;
86 if (play_count == 0) 103 if (play_count == 0)
87 bgm_start(wave, -1, volmod); 104 bgm_start(wave, -1);
88 else 105 else
89 bgm_start(wave, config.track_name.TrackStart(name), volmod); 106 bgm_start(wave, config->track_name.TrackStart(name));
90 return; 107 return;
91 } 108 }
92 109
93 void MuSys::StopCDROM(int time) 110 void MuSys::StopCDROM(int time)
94 { 111 {
98 } 115 }
99 116
100 void MuSys::PlaySE(const char* se, int loop_flag, int channel) { 117 void MuSys::PlaySE(const char* se, int loop_flag, int channel) {
101 if (!pcm_enable) return; 118 if (!pcm_enable) return;
102 if (loop_flag) 119 if (loop_flag)
103 effec_start(MIX_PCM_EFFEC, se, 10000, 0, volmod); 120 effec_start(MIX_PCM_EFFEC, se, 10000, 0);
104 else 121 else
105 effec_start(MIX_PCM_EFFEC, se, 0, 0, volmod); 122 effec_start(MIX_PCM_EFFEC, se, 0, 0);
106 return; 123 return;
107 } 124 }
108 void MuSys::PlaySE(int number) { 125 void MuSys::PlaySE(int number) {
109 if (! pcm_enable) return; 126 if (! pcm_enable) return;
110 const char* se_name = config.track_name.SETrack(number); 127 const char* se_name = config->track_name.SETrack(number);
111 if (se_name == NULL) return; 128 if (se_name == NULL) return;
112 effec_start(MIX_PCM_EFFEC, se_name, 0, 0, volmod); 129 effec_start(MIX_PCM_EFFEC, se_name, 0, 0);
113 return; 130 return;
114 } 131 }
115 void MuSys::StopSE(int time) { 132 void MuSys::StopSE(int time) {
116 if (!pcm_enable) return; 133 if (!pcm_enable) return;
117 if (time == 0) 134 if (time == 0)
146 WAVFILE::channels = channels; 163 WAVFILE::channels = channels;
147 } 164 }
148 pcm_enable = 1; 165 pcm_enable = 1;
149 Mix_AllocateChannels( MIX_PCM_SIZE); 166 Mix_AllocateChannels( MIX_PCM_SIZE);
150 music_enable = 2; 167 music_enable = 2;
151 return; 168 }
152 } 169
153 void MuSys::FinalizeMusic(void) 170 void MuSys::FinalizeMusic(void)
154 { 171 {
155 if (music_enable != 2) return; 172 if (music_enable != 2) return;
156 int i; 173 int i;
157 for (i=0; i<MIX_PCM_SIZE; i++) { 174 for (i=0; i<MIX_PCM_SIZE; i++) {
233 fadetime_total = time; 250 fadetime_total = time;
234 } 251 }
235 252
236 static SDL_RWops* OpenSDLRW(const char* path); 253 static SDL_RWops* OpenSDLRW(const char* path);
237 static WAVFILE* OpenWaveFile(const char* path); 254 static WAVFILE* OpenWaveFile(const char* path);
238 void bgm_start(const char* path, int loop_pt, int * volmod) { 255 void bgm_start(const char* path, int loop_pt) {
239 if (! pcm_enable) return; 256 MuSys* mu = MuSys::GetInstance();
257
258 if (!mu->pcm_enable) return;
240 fprintf(stderr,"bgm start %s\n",path); 259 fprintf(stderr,"bgm start %s\n",path);
241 WAVFILE* wav = OpenWaveFile(path); 260 WAVFILE* wav = OpenWaveFile(path);
242 if (wav == NULL) return; 261 if (wav == NULL) return;
243 Mix_PauseMusic(); 262 Mix_PauseMusic();
244 Mix_HaltMusic(); 263 Mix_HaltMusic();
248 delete wav_playing.wav; 267 delete wav_playing.wav;
249 wav_playing.wav = NULL; 268 wav_playing.wav = NULL;
250 } 269 }
251 wav_playing.wav = wav; 270 wav_playing.wav = wav;
252 wav_playing.loop_pt = loop_pt; 271 wav_playing.loop_pt = loop_pt;
253 wav_playing.volmod = &volmod[0]; 272 wav_playing.volmod = mu->volmod;
254 fadetime_total = 0; 273 fadetime_total = 0;
255 fadecount = 0; 274 fadecount = 0;
256 Mix_HookMusic( &(WavChunk::callback), (void*)&wav_playing); 275 Mix_HookMusic( &(WavChunk::callback), (void*)&wav_playing);
257 } 276 }
258 277
259 void effec_start(int chn, const char* path, int loop, int fadein_time, int * volmod) { 278 void effec_start(int chn, const char* path, int loop, int fadein_time) {
260 if (! pcm_enable) return; 279 MuSys* mu = MuSys::GetInstance();
280
281 if (!mu->pcm_enable) return;
282
261 SDL_RWops* op = OpenSDLRW(path); 283 SDL_RWops* op = OpenSDLRW(path);
262 if (op == NULL) { // ファイルが見付からない 284 if (op == NULL) { // ファイルが見付からない
263 return; 285 return;
264 } 286 }
265 Mix_Pause(chn); 287 Mix_Pause(chn);
266 288
267 if (play_chunk[chn]) { 289 if (mu->play_chunk[chn] != NULL) {
268 Mix_FreeChunk(play_chunk[chn]); 290 Mix_FreeChunk(mu->play_chunk[chn]);
269 } 291 }
270 play_chunk[chn] = Mix_LoadWAV_RW(op, 1); 292 mu->play_chunk[chn] = Mix_LoadWAV_RW(op, 1);
271 if (fadein_time <= 0) { 293 if (fadein_time <= 0) {
272 Mix_Volume(chn, volmod[3]*SDL_MIX_MAXVOLUME/255); 294 Mix_Volume(chn, mu->volmod[3]*SDL_MIX_MAXVOLUME/255);
273 Mix_PlayChannel(chn, play_chunk[chn], loop); 295 Mix_PlayChannel(chn, mu->play_chunk[chn], loop);
274 } else { 296 } else {
275 Mix_Volume(chn, volmod[3]*SDL_MIX_MAXVOLUME/255); 297 Mix_Volume(chn, mu->volmod[3]*SDL_MIX_MAXVOLUME/255);
276 Mix_FadeInChannel(chn, play_chunk[chn], loop, fadein_time); 298 Mix_FadeInChannel(chn, mu->play_chunk[chn], loop, fadein_time);
277 } 299 }
278 } 300 }
279 301
280 void MuSys::PlayKoe(const char* path) { 302 void MuSys::PlayKoe(const char* path) {
281 if (!pcm_enable) return; 303 if (!pcm_enable) return;
304
305 MuSys* mu = MuSys::GetInstance();
306
282 static char* playing_koedata = NULL; 307 static char* playing_koedata = NULL;
283 int len = 0; 308 int len = 0;
284 AvgKoeInfo koeinfo; 309 AvgKoeInfo koeinfo;
285 int chn = MIX_PCM_KOE; 310 int chn = MIX_PCM_KOE;
286 311
287 Mix_Pause(chn); 312 Mix_Pause(chn);
288 Mix_HaltChannel(chn); // これで RWop が解放されるはず… 313 Mix_HaltChannel(chn); // これで RWop が解放されるはず…
289 if (play_chunk[chn]) { 314 if (mu->play_chunk[chn] != NULL) {
290 Mix_FreeChunk(play_chunk[chn]); 315 Mix_FreeChunk(mu->play_chunk[chn]);
291 play_chunk[chn] = 0; 316 mu->play_chunk[chn] = NULL;
292 } 317 }
293 318
294 if (playing_koedata) { 319 if (playing_koedata != NULL) {
295 free(playing_koedata); 320 free(playing_koedata);
296 playing_koedata = 0; 321 playing_koedata = NULL;
297 } 322 }
298 323
299 koeinfo = OpenKoeFile(path); 324 koeinfo = OpenKoeFile(path);
300 325
301 if (koeinfo.stream == NULL) return; 326 if (koeinfo.stream == NULL) return;
302 playing_koedata = decode_koe(koeinfo, &len); 327 playing_koedata = decode_koe(koeinfo, &len);
303 fclose(koeinfo.stream); 328 fclose(koeinfo.stream);
304 if (playing_koedata == NULL) { 329 if (playing_koedata == NULL) {
305 return; 330 return;
306 } 331 }
307 Mix_Volume(chn, volmod[1]*SDL_MIX_MAXVOLUME/255); 332 Mix_Volume(chn, mu->volmod[1]*SDL_MIX_MAXVOLUME/255);
308 play_chunk[chn] = Mix_LoadWAV_RW(SDL_RWFromMem(playing_koedata, len+0x2c), 1); 333 mu->play_chunk[chn] = Mix_LoadWAV_RW(SDL_RWFromMem(playing_koedata, len+0x2c), 1);
309 Mix_PlayChannel(chn, play_chunk[chn], 0); 334 Mix_PlayChannel(chn, mu->play_chunk[chn], 0);
310 } 335 }
311 336
312 AvgKoeInfo OpenKoeFile(const char* path) { 337 AvgKoeInfo OpenKoeFile(const char* path) {
313 int radix = 10000; 338 int radix = 10000;
314 /* if (global_system.Version() >= 2) */ radix *= 10; 339 /* if (global_system.Version() >= 2) */ radix *= 10;
322 int file_no = pointer / radix; 347 int file_no = pointer / radix;
323 int index = pointer % radix; 348 int index = pointer % radix;
324 info = FindKoe(file_no, index); 349 info = FindKoe(file_no, index);
325 } else { // ファイル 350 } else { // ファイル
326 int length; 351 int length;
327 ARCINFO* arcinfo = file_searcher.Find(FILESEARCH::KOE, path, ".WPD"); 352 ARCINFO* arcinfo = FileSearcher::GetInstance()->Find(FileSearcher::KOE, path, ".WPD");
328 if (arcinfo == NULL) return info; 353 if (arcinfo == NULL) return info;
329 info.stream = arcinfo->OpenFile(&length); 354 info.stream = arcinfo->OpenFile(&length);
330 info.rate = 22050; 355 info.rate = 22050;
331 info.length = length; 356 info.length = length;
332 info.offset = ftell(info.stream); 357 info.offset = ftell(info.stream);
336 return info; 361 return info;
337 } 362 }
338 363
339 static SDL_RWops* OpenSDLRW(const char* path) { 364 static SDL_RWops* OpenSDLRW(const char* path) {
340 /* まず wav ファイルを探す */ 365 /* まず wav ファイルを探す */
341 ARCINFO* info = file_searcher.Find(FILESEARCH::WAV, path, ".wav"); 366 FileSearcher* file_searcher = FileSearcher::GetInstance();
367 ARCINFO* info = file_searcher->Find(FileSearcher::WAV, path, ".wav");
342 if (info == NULL) { 368 if (info == NULL) {
343 info = file_searcher.Find(FILESEARCH::WAV, path, ".nwa"); 369 info = file_searcher->Find(FileSearcher::WAV, path, ".nwa");
344 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "nwa"); 370 if (info == NULL) info = file_searcher->Find(FileSearcher::BGM, path, "nwa");
345 if (info != NULL) { // read NWA file 371 if (info != NULL) { // read NWA file
346 int dummy; 372 int dummy;
347 FILE* f = info->OpenFile(&dummy); 373 FILE* f = info->OpenFile(&dummy);
348 static char* d = 0; 374 static char* d = 0;
349 int sz; 375 int sz;
350 if (d != 0) delete[] d; 376 if (d != 0) delete[] d;
351 d = NWAFILE::ReadAll(f, sz); 377 d = NWAFILE::ReadAll(f, sz);
352 return SDL_RWFromMem(d, sz); 378 return SDL_RWFromMem(d, sz);
353 } 379 }
354 } 380 }
355 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "wav"); 381 if (info == NULL) info = file_searcher->Find(FileSearcher::BGM, path, "wav");
356 if (info == NULL) info = file_searcher.Find(FILESEARCH::WAV, path, ".ogg"); 382 if (info == NULL) info = file_searcher->Find(FileSearcher::WAV, path, ".ogg");
357 if (info != NULL) { 383 if (info != NULL) {
358 int dummy; 384 int dummy;
359 FILE* f = info->OpenFile(&dummy); 385 FILE* f = info->OpenFile(&dummy);
360 delete info; 386 delete info;
361 if (f == NULL) return NULL; 387 if (f == NULL) return NULL;
365 return NULL; 391 return NULL;
366 } 392 }
367 393
368 static WAVFILE* OpenWaveFile(const char* path) { 394 static WAVFILE* OpenWaveFile(const char* path) {
369 /* まず wav ファイルを探す */ 395 /* まず wav ファイルを探す */
370 ARCINFO* info = file_searcher.Find(FILESEARCH::WAV, path, ".wav"); 396 FileSearcher* file_searcher = FileSearcher::GetInstance();
371 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "wav"); 397 ARCINFO* info = file_searcher->Find(FileSearcher::WAV, path, ".wav");
398 if (info == NULL) info = file_searcher->Find(FileSearcher::BGM, path, "wav");
372 if (info != NULL) { 399 if (info != NULL) {
373 int size; 400 int size;
374 FILE* f = info->OpenFile(&size); 401 FILE* f = info->OpenFile(&size);
375 delete info; 402 delete info;
376 if (f == NULL) return NULL; 403 if (f == NULL) return NULL;
377 WAVFILE* w = WAVFILE::MakeConverter(new WAVFILE_Stream(f, size)); 404 WAVFILE* w = WAVFILE::MakeConverter(new WAVFILE_Stream(f, size));
378 return w; 405 return w;
379 } 406 }
380 /* 次に nwa ファイル */ 407 /* 次に nwa ファイル */
381 info = file_searcher.Find(FILESEARCH::WAV, path, ".nwa"); 408 info = file_searcher->Find(FileSearcher::WAV, path, ".nwa");
382 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "nwa"); 409 if (info == NULL) info = file_searcher->Find(FileSearcher::BGM, path, "nwa");
383 if (info != NULL) { 410 if (info != NULL) {
384 int size; 411 int size;
385 FILE* f = info->OpenFile(&size); 412 FILE* f = info->OpenFile(&size);
386 delete info; 413 delete info;
387 if (f == NULL) return NULL; 414 if (f == NULL) return NULL;
388 WAVFILE* w = WAVFILE::MakeConverter(new NWAFILE(f)); 415 WAVFILE* w = WAVFILE::MakeConverter(new NWAFILE(f));
389 return w; 416 return w;
390 } 417 }
391 418
392 /* 次に mp3 ファイル */ 419 /* 次に mp3 ファイル */
393 info = file_searcher.Find(FILESEARCH::WAV, path, ".mp3"); 420 info = file_searcher->Find(FileSearcher::WAV, path, ".mp3");
394 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "mp3"); 421 if (info == NULL) info = file_searcher->Find(FileSearcher::BGM, path, "mp3");
395 if (info != NULL) { 422 if (info != NULL) {
396 int size; 423 int size;
397 FILE* f = info->OpenFile(&size); 424 FILE* f = info->OpenFile(&size);
398 delete info; 425 delete info;
399 if (f == NULL) return NULL; 426 if (f == NULL) return NULL;
403 } 430 }
404 delete w; 431 delete w;
405 } 432 }
406 433
407 /* 次に ogg ファイル */ 434 /* 次に ogg ファイル */
408 info = file_searcher.Find(FILESEARCH::WAV, path, ".ogg"); 435 info = file_searcher->Find(FileSearcher::WAV, path, ".ogg");
409 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "ogg"); 436 if (info == NULL) info = file_searcher->Find(FileSearcher::BGM, path, "ogg");
410 if (info != NULL) { 437 if (info != NULL) {
411 int size; 438 int size;
412 FILE* f = info->OpenFile(&size); 439 FILE* f = info->OpenFile(&size);
413 delete info; 440 delete info;
414 if (f == NULL) return NULL; 441 if (f == NULL) return NULL;