diff 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
line wrap: on
line diff
--- a/music2/music.cc
+++ b/music2/music.cc
@@ -37,13 +37,13 @@
 #include"system/system_config.h"
 #include"system/file.h"
 #include "music.h"
-#include<SDL.h>
-#include<SDL_mixer.h>
-#include"wavfile.h"
+#include <SDL.h>
+#include <SDL_mixer.h>
+#include "wavfile.h"
 
 using namespace std;
 
-int pcm_enable   = 0;
+int pcm_enable = 0;
 Mix_Chunk *play_chunk[MIX_PCM_SIZE];
 
 MuSys::MuSys(AyuSysConfig& _config) : config(_config), movie_id(-1), music_enable(1) {
@@ -63,7 +63,10 @@ void bgm_fadeout(int time);
 
 void MuSys::PlayCDROM(char* name, int play_count) {
 	config.GetParam("#VOLMOD", 4, &volmod[0], &volmod[1], &volmod[2], &volmod[3]);
-	char wave[128]; wave[127] = '\0'; wave[0] = '\0';
+
+	char wave[128];
+	wave[127] = '\0';
+	wave[0] = '\0';
 
 	strcpy(cdrom_track, name);
 
@@ -73,9 +76,9 @@ void MuSys::PlayCDROM(char* name, int pl
 	/* name -> track */
 	int track =config.track_name.CDTrack(name);
 	if (track == -1) track = atoi(name);
-	if (config.track_name.WaveTrack(name) != 0) strncpy(wave, config.track_name.WaveTrack(name), 127);
+	if (config.track_name.WaveTrack(name) != NULL) strncpy(wave, config.track_name.WaveTrack(name), 127);
 	if (wave[0] == 0 && track != 0) { /* DSTRACK が見つからない場合、CDTRACKを使用する */
-		sprintf(wave, "audio_%02d",track);
+		sprintf(wave, "audio_%02d", track);
 	}
 	if (wave == 0) return;
 	// BGM 再生
@@ -95,7 +98,7 @@ void MuSys::StopCDROM(int time)
 }
 
 void MuSys::PlaySE(const char* se, int loop_flag, int channel) {
-	if (! pcm_enable) return;
+	if (!pcm_enable) return;
 	if (loop_flag)
 		effec_start(MIX_PCM_EFFEC, se, 10000, 0, volmod);
 	else
@@ -105,25 +108,25 @@ void MuSys::PlaySE(const char* se, int l
 void MuSys::PlaySE(int number) {
 	if (! pcm_enable) return;
 	const char* se_name = config.track_name.SETrack(number);
-	if (se_name == 0) return;
+	if (se_name == NULL) return;
 	effec_start(MIX_PCM_EFFEC, se_name, 0, 0, volmod);
 	return;
 }
 void MuSys::StopSE(int time) {
-	if (! pcm_enable) return;
+	if (!pcm_enable) return;
 	if (time == 0) 
 		Mix_HaltChannel(MIX_PCM_EFFEC);
 	else
 		Mix_FadeOutChannel(MIX_PCM_EFFEC, time);
 }
 bool MuSys::IsStopSE(void) {
-	if (! pcm_enable) return true;
+	if (!pcm_enable) return true;
 	if (Mix_Playing(MIX_PCM_EFFEC) != 0) return false;
 	return true;
 }
 
 void MuSys::StopKoe(int time) {
-	if (! pcm_enable) return;
+	if (!pcm_enable) return;
 	if (time == 0) Mix_HaltChannel(MIX_PCM_KOE);
 	else Mix_FadeOutChannel(MIX_PCM_KOE, time);
 }
@@ -176,6 +179,7 @@ struct WavChunk {
 	int *volmod;
 	static void callback(void* userdata, Uint8* stream, int len);
 };
+
 WavChunk wav_playing;
 static int fadetime_total;
 static int fadecount;
@@ -235,14 +239,14 @@ void bgm_start(const char* path, int loo
 	if (! pcm_enable) return;
 fprintf(stderr,"bgm start %s\n",path);
 	WAVFILE* wav = OpenWaveFile(path);
-	if (wav == 0) return;
+	if (wav == NULL) return;
 	Mix_PauseMusic();
 	Mix_HaltMusic();
 	Mix_HookMusic(0,0);
 	/* 前に再生していたのを終了 */
-	if (wav_playing.wav) {
+	if (wav_playing.wav != NULL) {
 		delete wav_playing.wav;
-		wav_playing.wav = 0;
+		wav_playing.wav = NULL;
 	}
 	wav_playing.wav = wav;
 	wav_playing.loop_pt = loop_pt;
@@ -250,13 +254,12 @@ fprintf(stderr,"bgm start %s\n",path);
 	fadetime_total = 0;
 	fadecount = 0;
 	Mix_HookMusic( &(WavChunk::callback), (void*)&wav_playing);
-	return;
 }
 
 void effec_start(int chn, const char* path, int loop, int fadein_time, int * volmod) {
 	if (! pcm_enable) return;
 	SDL_RWops* op = OpenSDLRW(path);
-	if (op == 0) { // ファイルが見付からない
+	if (op == NULL) { // ファイルが見付からない
 		return;
 	}
 	Mix_Pause(chn);
@@ -267,17 +270,16 @@ void effec_start(int chn, const char* pa
 	play_chunk[chn] = Mix_LoadWAV_RW(op, 1);
 	if (fadein_time <= 0) {
 		Mix_Volume(chn, volmod[3]*SDL_MIX_MAXVOLUME/255);
-		Mix_PlayChannel(chn, play_chunk[chn],loop);
+		Mix_PlayChannel(chn, play_chunk[chn], loop);
 	} else {
 		Mix_Volume(chn, volmod[3]*SDL_MIX_MAXVOLUME/255);
-		Mix_FadeInChannel(chn, play_chunk[chn],loop,fadein_time);
+		Mix_FadeInChannel(chn, play_chunk[chn], loop, fadein_time);
 	}
-	return;
 }
 
 void MuSys::PlayKoe(const char* path) {
-	if (! pcm_enable) return;
-	static char* playing_koedata = 0;
+	if (!pcm_enable) return;
+	static char* playing_koedata = NULL;
 	int len = 0;
 	AvgKoeInfo koeinfo;
 	int chn = MIX_PCM_KOE;
@@ -296,23 +298,25 @@ void MuSys::PlayKoe(const char* path) {
 
 	koeinfo = OpenKoeFile(path);
 
-	if (koeinfo.stream == 0) return;
+	if (koeinfo.stream == NULL) return;
 	playing_koedata = decode_koe(koeinfo, &len);
 	fclose(koeinfo.stream);
-	if (playing_koedata == 0) {
+	if (playing_koedata == NULL) {
 		return;
 	}
 	Mix_Volume(chn, volmod[1]*SDL_MIX_MAXVOLUME/255);
 	play_chunk[chn] = Mix_LoadWAV_RW(SDL_RWFromMem(playing_koedata, len+0x2c), 1);
-	Mix_PlayChannel(chn, play_chunk[chn],0);
-	return;
+	Mix_PlayChannel(chn, play_chunk[chn], 0);
 }
+
 AvgKoeInfo OpenKoeFile(const char* path) {
 	int radix = 10000;
 	/* if (global_system.Version() >= 2) */ radix *= 10;
 	AvgKoeInfo info;
-	info.stream = 0; info.length = 0; info.offset = 0;
-	if (isdigit(path[0]) && strchr(path,'.') == 0) { // 数値 (拡張子等なし)
+	info.stream = NULL;
+	info.length = 0;
+	info.offset = 0;
+	if (isdigit(path[0]) && strchr(path,'.') == NULL) { // 数値 (拡張子等なし)
 		/* avg32 形式の音声アーカイブのキャッシュを検索 */
 		int pointer = atoi(path);
 		int file_no = pointer / radix;
@@ -320,8 +324,8 @@ AvgKoeInfo OpenKoeFile(const char* path)
 		info = FindKoe(file_no, index);
 	} else { // ファイル
 		int length;
-		ARCINFO* arcinfo = file_searcher.Find(FILESEARCH::KOE,path,".WPD");
-		if (arcinfo == 0) return info;
+		ARCINFO* arcinfo = file_searcher.Find(FILESEARCH::KOE, path, ".WPD");
+		if (arcinfo == NULL) return info;
 		info.stream = arcinfo->OpenFile(&length);
 		info.rate = 22050;
 		info.length = length;
@@ -334,12 +338,11 @@ AvgKoeInfo OpenKoeFile(const char* path)
 
 static SDL_RWops* OpenSDLRW(const char* path) {
 	/* まず wav ファイルを探す */
-	ARCINFO* info = file_searcher.Find(FILESEARCH::WAV,path,".wav");
-	if (info == 0) {
-		info = file_searcher.Find(FILESEARCH::WAV,path,".nwa");
-		if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"nwa");
-		if (info) { // read NWA file
-
+	ARCINFO* info = file_searcher.Find(FILESEARCH::WAV, path, ".wav");
+	if (info == NULL) {
+		info = file_searcher.Find(FILESEARCH::WAV, path, ".nwa");
+		if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "nwa");
+		if (info != NULL) { // read NWA file
 			int dummy;
 			FILE* f = info->OpenFile(&dummy);
 			static char* d = 0;
@@ -349,72 +352,72 @@ static SDL_RWops* OpenSDLRW(const char* 
 			return SDL_RWFromMem(d, sz);
 		}
 	}
-	if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"wav");
-	if (info == 0) info = file_searcher.Find(FILESEARCH::WAV,path,".ogg");
-	if (info) {
+	if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "wav");
+	if (info == NULL) info = file_searcher.Find(FILESEARCH::WAV, path, ".ogg");
+	if (info != NULL) {
 		int dummy;
 		FILE* f = info->OpenFile(&dummy);
 		delete info;
-		if (f == 0) return 0;
+		if (f == NULL) return NULL;
 		SDL_RWops* op = SDL_RWFromFP(f, 1);
 		return op;
 	}
-	return 0;
+	return NULL;
 }
 
 static WAVFILE* OpenWaveFile(const char* path) {
 	/* まず wav ファイルを探す */
-	ARCINFO* info = file_searcher.Find(FILESEARCH::WAV,path,".wav");
-	if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"wav");
-	if (info) {
+	ARCINFO* info = file_searcher.Find(FILESEARCH::WAV, path, ".wav");
+	if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "wav");
+	if (info != NULL) {
 		int size;
 		FILE* f = info->OpenFile(&size);
 		delete info;
-		if (f == 0) return 0;
+		if (f == NULL) return NULL;
 		WAVFILE* w = WAVFILE::MakeConverter(new WAVFILE_Stream(f, size));
 		return w;
 	}
 	/* 次に nwa ファイル */
-	info = file_searcher.Find(FILESEARCH::WAV,path,".nwa");
-	if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"nwa");
-	if (info) {
+	info = file_searcher.Find(FILESEARCH::WAV, path, ".nwa");
+	if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "nwa");
+	if (info != NULL) {
 		int size;
 		FILE* f = info->OpenFile(&size);
 		delete info;
-		if (f == 0) return 0;
+		if (f == NULL) return NULL;
 		WAVFILE* w = WAVFILE::MakeConverter(new NWAFILE(f));
 		return w;
 	}
 
 	/* 次に mp3 ファイル */
-	info = file_searcher.Find(FILESEARCH::WAV,path,".mp3");
-	if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"mp3");
-	if (info) {
+	info = file_searcher.Find(FILESEARCH::WAV, path, ".mp3");
+	if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "mp3");
+	if (info != NULL) {
 		int size;
 		FILE* f = info->OpenFile(&size);
 		delete info;
-		if (f == 0) return 0;
+		if (f == NULL) return NULL;
 		MP3FILE* w = new MP3FILE(f, size);
-		if (w->pimpl) {
+		if (w->pimpl != NULL) {
 			return WAVFILE::MakeConverter(w);
 		}
 		delete w;
 	}
 
 	/* 次に ogg ファイル */
-	info = file_searcher.Find(FILESEARCH::WAV,path,".ogg");
-	if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"ogg");
-	if (info) {
+	info = file_searcher.Find(FILESEARCH::WAV, path, ".ogg");
+	if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "ogg");
+	if (info != NULL) {
 		int size;
 		FILE* f = info->OpenFile(&size);
 		delete info;
-		if (f == 0) return 0;
+		if (f == NULL) return NULL;
 		OggFILE* w = new OggFILE(f, size);
-		if (w->pimpl) {
+		if (w->pimpl != NULL) {
 			return WAVFILE::MakeConverter(w);
 		}
 		delete w;
 	}
-	return 0;
+	return NULL;
 }