Mercurial > otakunoraifu
comparison music2/music.cc @ 52:15a18fbe6f21
* Known bugs added to the README
* Code cleaning (0 -> NULL when needed, indentation, spaces, ...)
| author | thib |
|---|---|
| date | Sat, 18 Apr 2009 18:35:39 +0000 |
| parents | 01aa5ddf7dc8 |
| children | ddbcbd000206 |
comparison
equal
deleted
inserted
replaced
| 51:cbb301016a4e | 52:15a18fbe6f21 |
|---|---|
| 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 int pcm_enable = 0; |
| 47 Mix_Chunk *play_chunk[MIX_PCM_SIZE]; | 47 Mix_Chunk *play_chunk[MIX_PCM_SIZE]; |
| 48 | 48 |
| 49 MuSys::MuSys(AyuSysConfig& _config) : config(_config), movie_id(-1), music_enable(1) { | 49 MuSys::MuSys(AyuSysConfig& _config) : config(_config), movie_id(-1), music_enable(1) { |
| 50 int i; | 50 int i; |
| 51 for (i=0; i<MIX_PCM_SIZE; i++) | 51 for (i=0; i<MIX_PCM_SIZE; i++) |
| 61 void effec_start(int chn, const char* path, int loop, int fadein_time, int * volmod); | 61 void effec_start(int chn, const char* path, int loop, int fadein_time, int * volmod); |
| 62 void bgm_fadeout(int time); | 62 void bgm_fadeout(int time); |
| 63 | 63 |
| 64 void MuSys::PlayCDROM(char* name, int play_count) { | 64 void MuSys::PlayCDROM(char* name, int play_count) { |
| 65 config.GetParam("#VOLMOD", 4, &volmod[0], &volmod[1], &volmod[2], &volmod[3]); | 65 config.GetParam("#VOLMOD", 4, &volmod[0], &volmod[1], &volmod[2], &volmod[3]); |
| 66 char wave[128]; wave[127] = '\0'; wave[0] = '\0'; | 66 |
| 67 char wave[128]; | |
| 68 wave[127] = '\0'; | |
| 69 wave[0] = '\0'; | |
| 67 | 70 |
| 68 strcpy(cdrom_track, name); | 71 strcpy(cdrom_track, name); |
| 69 | 72 |
| 70 StopCDROM(0); | 73 StopCDROM(0); |
| 71 strcpy(cdrom_track, name); | 74 strcpy(cdrom_track, name); |
| 72 | 75 |
| 73 /* name -> track */ | 76 /* name -> track */ |
| 74 int track =config.track_name.CDTrack(name); | 77 int track =config.track_name.CDTrack(name); |
| 75 if (track == -1) track = atoi(name); | 78 if (track == -1) track = atoi(name); |
| 76 if (config.track_name.WaveTrack(name) != 0) strncpy(wave, config.track_name.WaveTrack(name), 127); | 79 if (config.track_name.WaveTrack(name) != NULL) strncpy(wave, config.track_name.WaveTrack(name), 127); |
| 77 if (wave[0] == 0 && track != 0) { /* DSTRACK が見つからない場合、CDTRACKを使用する */ | 80 if (wave[0] == 0 && track != 0) { /* DSTRACK が見つからない場合、CDTRACKを使用する */ |
| 78 sprintf(wave, "audio_%02d",track); | 81 sprintf(wave, "audio_%02d", track); |
| 79 } | 82 } |
| 80 if (wave == 0) return; | 83 if (wave == 0) return; |
| 81 // BGM 再生 | 84 // BGM 再生 |
| 82 if (!pcm_enable) return; | 85 if (!pcm_enable) return; |
| 83 if (play_count == 0) | 86 if (play_count == 0) |
| 93 if (!pcm_enable) return; | 96 if (!pcm_enable) return; |
| 94 bgm_fadeout(time); | 97 bgm_fadeout(time); |
| 95 } | 98 } |
| 96 | 99 |
| 97 void MuSys::PlaySE(const char* se, int loop_flag, int channel) { | 100 void MuSys::PlaySE(const char* se, int loop_flag, int channel) { |
| 98 if (! pcm_enable) return; | 101 if (!pcm_enable) return; |
| 99 if (loop_flag) | 102 if (loop_flag) |
| 100 effec_start(MIX_PCM_EFFEC, se, 10000, 0, volmod); | 103 effec_start(MIX_PCM_EFFEC, se, 10000, 0, volmod); |
| 101 else | 104 else |
| 102 effec_start(MIX_PCM_EFFEC, se, 0, 0, volmod); | 105 effec_start(MIX_PCM_EFFEC, se, 0, 0, volmod); |
| 103 return; | 106 return; |
| 104 } | 107 } |
| 105 void MuSys::PlaySE(int number) { | 108 void MuSys::PlaySE(int number) { |
| 106 if (! pcm_enable) return; | 109 if (! pcm_enable) return; |
| 107 const char* se_name = config.track_name.SETrack(number); | 110 const char* se_name = config.track_name.SETrack(number); |
| 108 if (se_name == 0) return; | 111 if (se_name == NULL) return; |
| 109 effec_start(MIX_PCM_EFFEC, se_name, 0, 0, volmod); | 112 effec_start(MIX_PCM_EFFEC, se_name, 0, 0, volmod); |
| 110 return; | 113 return; |
| 111 } | 114 } |
| 112 void MuSys::StopSE(int time) { | 115 void MuSys::StopSE(int time) { |
| 113 if (! pcm_enable) return; | 116 if (!pcm_enable) return; |
| 114 if (time == 0) | 117 if (time == 0) |
| 115 Mix_HaltChannel(MIX_PCM_EFFEC); | 118 Mix_HaltChannel(MIX_PCM_EFFEC); |
| 116 else | 119 else |
| 117 Mix_FadeOutChannel(MIX_PCM_EFFEC, time); | 120 Mix_FadeOutChannel(MIX_PCM_EFFEC, time); |
| 118 } | 121 } |
| 119 bool MuSys::IsStopSE(void) { | 122 bool MuSys::IsStopSE(void) { |
| 120 if (! pcm_enable) return true; | 123 if (!pcm_enable) return true; |
| 121 if (Mix_Playing(MIX_PCM_EFFEC) != 0) return false; | 124 if (Mix_Playing(MIX_PCM_EFFEC) != 0) return false; |
| 122 return true; | 125 return true; |
| 123 } | 126 } |
| 124 | 127 |
| 125 void MuSys::StopKoe(int time) { | 128 void MuSys::StopKoe(int time) { |
| 126 if (! pcm_enable) return; | 129 if (!pcm_enable) return; |
| 127 if (time == 0) Mix_HaltChannel(MIX_PCM_KOE); | 130 if (time == 0) Mix_HaltChannel(MIX_PCM_KOE); |
| 128 else Mix_FadeOutChannel(MIX_PCM_KOE, time); | 131 else Mix_FadeOutChannel(MIX_PCM_KOE, time); |
| 129 } | 132 } |
| 130 | 133 |
| 131 void MuSys::InitMusic(void) | 134 void MuSys::InitMusic(void) |
| 174 WAVFILE* wav; | 177 WAVFILE* wav; |
| 175 int loop_pt; | 178 int loop_pt; |
| 176 int *volmod; | 179 int *volmod; |
| 177 static void callback(void* userdata, Uint8* stream, int len); | 180 static void callback(void* userdata, Uint8* stream, int len); |
| 178 }; | 181 }; |
| 182 | |
| 179 WavChunk wav_playing; | 183 WavChunk wav_playing; |
| 180 static int fadetime_total; | 184 static int fadetime_total; |
| 181 static int fadecount; | 185 static int fadecount; |
| 182 | 186 |
| 183 void WavChunk::callback(void *userdata, Uint8 *stream, int len) | 187 void WavChunk::callback(void *userdata, Uint8 *stream, int len) |
| 233 static WAVFILE* OpenWaveFile(const char* path); | 237 static WAVFILE* OpenWaveFile(const char* path); |
| 234 void bgm_start(const char* path, int loop_pt, int * volmod) { | 238 void bgm_start(const char* path, int loop_pt, int * volmod) { |
| 235 if (! pcm_enable) return; | 239 if (! pcm_enable) return; |
| 236 fprintf(stderr,"bgm start %s\n",path); | 240 fprintf(stderr,"bgm start %s\n",path); |
| 237 WAVFILE* wav = OpenWaveFile(path); | 241 WAVFILE* wav = OpenWaveFile(path); |
| 238 if (wav == 0) return; | 242 if (wav == NULL) return; |
| 239 Mix_PauseMusic(); | 243 Mix_PauseMusic(); |
| 240 Mix_HaltMusic(); | 244 Mix_HaltMusic(); |
| 241 Mix_HookMusic(0,0); | 245 Mix_HookMusic(0,0); |
| 242 /* 前に再生していたのを終了 */ | 246 /* 前に再生していたのを終了 */ |
| 243 if (wav_playing.wav) { | 247 if (wav_playing.wav != NULL) { |
| 244 delete wav_playing.wav; | 248 delete wav_playing.wav; |
| 245 wav_playing.wav = 0; | 249 wav_playing.wav = NULL; |
| 246 } | 250 } |
| 247 wav_playing.wav = wav; | 251 wav_playing.wav = wav; |
| 248 wav_playing.loop_pt = loop_pt; | 252 wav_playing.loop_pt = loop_pt; |
| 249 wav_playing.volmod = &volmod[0]; | 253 wav_playing.volmod = &volmod[0]; |
| 250 fadetime_total = 0; | 254 fadetime_total = 0; |
| 251 fadecount = 0; | 255 fadecount = 0; |
| 252 Mix_HookMusic( &(WavChunk::callback), (void*)&wav_playing); | 256 Mix_HookMusic( &(WavChunk::callback), (void*)&wav_playing); |
| 253 return; | |
| 254 } | 257 } |
| 255 | 258 |
| 256 void effec_start(int chn, const char* path, int loop, int fadein_time, int * volmod) { | 259 void effec_start(int chn, const char* path, int loop, int fadein_time, int * volmod) { |
| 257 if (! pcm_enable) return; | 260 if (! pcm_enable) return; |
| 258 SDL_RWops* op = OpenSDLRW(path); | 261 SDL_RWops* op = OpenSDLRW(path); |
| 259 if (op == 0) { // ファイルが見付からない | 262 if (op == NULL) { // ファイルが見付からない |
| 260 return; | 263 return; |
| 261 } | 264 } |
| 262 Mix_Pause(chn); | 265 Mix_Pause(chn); |
| 263 | 266 |
| 264 if (play_chunk[chn]) { | 267 if (play_chunk[chn]) { |
| 265 Mix_FreeChunk(play_chunk[chn]); | 268 Mix_FreeChunk(play_chunk[chn]); |
| 266 } | 269 } |
| 267 play_chunk[chn] = Mix_LoadWAV_RW(op, 1); | 270 play_chunk[chn] = Mix_LoadWAV_RW(op, 1); |
| 268 if (fadein_time <= 0) { | 271 if (fadein_time <= 0) { |
| 269 Mix_Volume(chn, volmod[3]*SDL_MIX_MAXVOLUME/255); | 272 Mix_Volume(chn, volmod[3]*SDL_MIX_MAXVOLUME/255); |
| 270 Mix_PlayChannel(chn, play_chunk[chn],loop); | 273 Mix_PlayChannel(chn, play_chunk[chn], loop); |
| 271 } else { | 274 } else { |
| 272 Mix_Volume(chn, volmod[3]*SDL_MIX_MAXVOLUME/255); | 275 Mix_Volume(chn, volmod[3]*SDL_MIX_MAXVOLUME/255); |
| 273 Mix_FadeInChannel(chn, play_chunk[chn],loop,fadein_time); | 276 Mix_FadeInChannel(chn, play_chunk[chn], loop, fadein_time); |
| 274 } | 277 } |
| 275 return; | |
| 276 } | 278 } |
| 277 | 279 |
| 278 void MuSys::PlayKoe(const char* path) { | 280 void MuSys::PlayKoe(const char* path) { |
| 279 if (! pcm_enable) return; | 281 if (!pcm_enable) return; |
| 280 static char* playing_koedata = 0; | 282 static char* playing_koedata = NULL; |
| 281 int len = 0; | 283 int len = 0; |
| 282 AvgKoeInfo koeinfo; | 284 AvgKoeInfo koeinfo; |
| 283 int chn = MIX_PCM_KOE; | 285 int chn = MIX_PCM_KOE; |
| 284 | 286 |
| 285 Mix_Pause(chn); | 287 Mix_Pause(chn); |
| 294 playing_koedata = 0; | 296 playing_koedata = 0; |
| 295 } | 297 } |
| 296 | 298 |
| 297 koeinfo = OpenKoeFile(path); | 299 koeinfo = OpenKoeFile(path); |
| 298 | 300 |
| 299 if (koeinfo.stream == 0) return; | 301 if (koeinfo.stream == NULL) return; |
| 300 playing_koedata = decode_koe(koeinfo, &len); | 302 playing_koedata = decode_koe(koeinfo, &len); |
| 301 fclose(koeinfo.stream); | 303 fclose(koeinfo.stream); |
| 302 if (playing_koedata == 0) { | 304 if (playing_koedata == NULL) { |
| 303 return; | 305 return; |
| 304 } | 306 } |
| 305 Mix_Volume(chn, volmod[1]*SDL_MIX_MAXVOLUME/255); | 307 Mix_Volume(chn, volmod[1]*SDL_MIX_MAXVOLUME/255); |
| 306 play_chunk[chn] = Mix_LoadWAV_RW(SDL_RWFromMem(playing_koedata, len+0x2c), 1); | 308 play_chunk[chn] = Mix_LoadWAV_RW(SDL_RWFromMem(playing_koedata, len+0x2c), 1); |
| 307 Mix_PlayChannel(chn, play_chunk[chn],0); | 309 Mix_PlayChannel(chn, play_chunk[chn], 0); |
| 308 return; | 310 } |
| 309 } | 311 |
| 310 AvgKoeInfo OpenKoeFile(const char* path) { | 312 AvgKoeInfo OpenKoeFile(const char* path) { |
| 311 int radix = 10000; | 313 int radix = 10000; |
| 312 /* if (global_system.Version() >= 2) */ radix *= 10; | 314 /* if (global_system.Version() >= 2) */ radix *= 10; |
| 313 AvgKoeInfo info; | 315 AvgKoeInfo info; |
| 314 info.stream = 0; info.length = 0; info.offset = 0; | 316 info.stream = NULL; |
| 315 if (isdigit(path[0]) && strchr(path,'.') == 0) { // 数値 (拡張子等なし) | 317 info.length = 0; |
| 318 info.offset = 0; | |
| 319 if (isdigit(path[0]) && strchr(path,'.') == NULL) { // 数値 (拡張子等なし) | |
| 316 /* avg32 形式の音声アーカイブのキャッシュを検索 */ | 320 /* avg32 形式の音声アーカイブのキャッシュを検索 */ |
| 317 int pointer = atoi(path); | 321 int pointer = atoi(path); |
| 318 int file_no = pointer / radix; | 322 int file_no = pointer / radix; |
| 319 int index = pointer % radix; | 323 int index = pointer % radix; |
| 320 info = FindKoe(file_no, index); | 324 info = FindKoe(file_no, index); |
| 321 } else { // ファイル | 325 } else { // ファイル |
| 322 int length; | 326 int length; |
| 323 ARCINFO* arcinfo = file_searcher.Find(FILESEARCH::KOE,path,".WPD"); | 327 ARCINFO* arcinfo = file_searcher.Find(FILESEARCH::KOE, path, ".WPD"); |
| 324 if (arcinfo == 0) return info; | 328 if (arcinfo == NULL) return info; |
| 325 info.stream = arcinfo->OpenFile(&length); | 329 info.stream = arcinfo->OpenFile(&length); |
| 326 info.rate = 22050; | 330 info.rate = 22050; |
| 327 info.length = length; | 331 info.length = length; |
| 328 info.offset = ftell(info.stream); | 332 info.offset = ftell(info.stream); |
| 329 info.type = koe_unknown; | 333 info.type = koe_unknown; |
| 332 return info; | 336 return info; |
| 333 } | 337 } |
| 334 | 338 |
| 335 static SDL_RWops* OpenSDLRW(const char* path) { | 339 static SDL_RWops* OpenSDLRW(const char* path) { |
| 336 /* まず wav ファイルを探す */ | 340 /* まず wav ファイルを探す */ |
| 337 ARCINFO* info = file_searcher.Find(FILESEARCH::WAV,path,".wav"); | 341 ARCINFO* info = file_searcher.Find(FILESEARCH::WAV, path, ".wav"); |
| 338 if (info == 0) { | 342 if (info == NULL) { |
| 339 info = file_searcher.Find(FILESEARCH::WAV,path,".nwa"); | 343 info = file_searcher.Find(FILESEARCH::WAV, path, ".nwa"); |
| 340 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"nwa"); | 344 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "nwa"); |
| 341 if (info) { // read NWA file | 345 if (info != NULL) { // read NWA file |
| 342 | |
| 343 int dummy; | 346 int dummy; |
| 344 FILE* f = info->OpenFile(&dummy); | 347 FILE* f = info->OpenFile(&dummy); |
| 345 static char* d = 0; | 348 static char* d = 0; |
| 346 int sz; | 349 int sz; |
| 347 if (d != 0) delete[] d; | 350 if (d != 0) delete[] d; |
| 348 d = NWAFILE::ReadAll(f, sz); | 351 d = NWAFILE::ReadAll(f, sz); |
| 349 return SDL_RWFromMem(d, sz); | 352 return SDL_RWFromMem(d, sz); |
| 350 } | 353 } |
| 351 } | 354 } |
| 352 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"wav"); | 355 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "wav"); |
| 353 if (info == 0) info = file_searcher.Find(FILESEARCH::WAV,path,".ogg"); | 356 if (info == NULL) info = file_searcher.Find(FILESEARCH::WAV, path, ".ogg"); |
| 354 if (info) { | 357 if (info != NULL) { |
| 355 int dummy; | 358 int dummy; |
| 356 FILE* f = info->OpenFile(&dummy); | 359 FILE* f = info->OpenFile(&dummy); |
| 357 delete info; | 360 delete info; |
| 358 if (f == 0) return 0; | 361 if (f == NULL) return NULL; |
| 359 SDL_RWops* op = SDL_RWFromFP(f, 1); | 362 SDL_RWops* op = SDL_RWFromFP(f, 1); |
| 360 return op; | 363 return op; |
| 361 } | 364 } |
| 362 return 0; | 365 return NULL; |
| 363 } | 366 } |
| 364 | 367 |
| 365 static WAVFILE* OpenWaveFile(const char* path) { | 368 static WAVFILE* OpenWaveFile(const char* path) { |
| 366 /* まず wav ファイルを探す */ | 369 /* まず wav ファイルを探す */ |
| 367 ARCINFO* info = file_searcher.Find(FILESEARCH::WAV,path,".wav"); | 370 ARCINFO* info = file_searcher.Find(FILESEARCH::WAV, path, ".wav"); |
| 368 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"wav"); | 371 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "wav"); |
| 369 if (info) { | 372 if (info != NULL) { |
| 370 int size; | 373 int size; |
| 371 FILE* f = info->OpenFile(&size); | 374 FILE* f = info->OpenFile(&size); |
| 372 delete info; | 375 delete info; |
| 373 if (f == 0) return 0; | 376 if (f == NULL) return NULL; |
| 374 WAVFILE* w = WAVFILE::MakeConverter(new WAVFILE_Stream(f, size)); | 377 WAVFILE* w = WAVFILE::MakeConverter(new WAVFILE_Stream(f, size)); |
| 375 return w; | 378 return w; |
| 376 } | 379 } |
| 377 /* 次に nwa ファイル */ | 380 /* 次に nwa ファイル */ |
| 378 info = file_searcher.Find(FILESEARCH::WAV,path,".nwa"); | 381 info = file_searcher.Find(FILESEARCH::WAV, path, ".nwa"); |
| 379 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"nwa"); | 382 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "nwa"); |
| 380 if (info) { | 383 if (info != NULL) { |
| 381 int size; | 384 int size; |
| 382 FILE* f = info->OpenFile(&size); | 385 FILE* f = info->OpenFile(&size); |
| 383 delete info; | 386 delete info; |
| 384 if (f == 0) return 0; | 387 if (f == NULL) return NULL; |
| 385 WAVFILE* w = WAVFILE::MakeConverter(new NWAFILE(f)); | 388 WAVFILE* w = WAVFILE::MakeConverter(new NWAFILE(f)); |
| 386 return w; | 389 return w; |
| 387 } | 390 } |
| 388 | 391 |
| 389 /* 次に mp3 ファイル */ | 392 /* 次に mp3 ファイル */ |
| 390 info = file_searcher.Find(FILESEARCH::WAV,path,".mp3"); | 393 info = file_searcher.Find(FILESEARCH::WAV, path, ".mp3"); |
| 391 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"mp3"); | 394 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "mp3"); |
| 392 if (info) { | 395 if (info != NULL) { |
| 393 int size; | 396 int size; |
| 394 FILE* f = info->OpenFile(&size); | 397 FILE* f = info->OpenFile(&size); |
| 395 delete info; | 398 delete info; |
| 396 if (f == 0) return 0; | 399 if (f == NULL) return NULL; |
| 397 MP3FILE* w = new MP3FILE(f, size); | 400 MP3FILE* w = new MP3FILE(f, size); |
| 398 if (w->pimpl) { | 401 if (w->pimpl != NULL) { |
| 399 return WAVFILE::MakeConverter(w); | 402 return WAVFILE::MakeConverter(w); |
| 400 } | 403 } |
| 401 delete w; | 404 delete w; |
| 402 } | 405 } |
| 403 | 406 |
| 404 /* 次に ogg ファイル */ | 407 /* 次に ogg ファイル */ |
| 405 info = file_searcher.Find(FILESEARCH::WAV,path,".ogg"); | 408 info = file_searcher.Find(FILESEARCH::WAV, path, ".ogg"); |
| 406 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"ogg"); | 409 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "ogg"); |
| 407 if (info) { | 410 if (info != NULL) { |
| 408 int size; | 411 int size; |
| 409 FILE* f = info->OpenFile(&size); | 412 FILE* f = info->OpenFile(&size); |
| 410 delete info; | 413 delete info; |
| 411 if (f == 0) return 0; | 414 if (f == NULL) return NULL; |
| 412 OggFILE* w = new OggFILE(f, size); | 415 OggFILE* w = new OggFILE(f, size); |
| 413 if (w->pimpl) { | 416 if (w->pimpl != NULL) { |
| 414 return WAVFILE::MakeConverter(w); | 417 return WAVFILE::MakeConverter(w); |
| 415 } | 418 } |
| 416 delete w; | 419 delete w; |
| 417 } | 420 } |
| 418 return 0; | 421 return NULL; |
| 419 } | 422 } |
| 420 | 423 |
