comparison music2/music.cc @ 26:f45da03ca631

* Corrected --disable-vorbis in configure.ac * Added sound configuration * Added misc functions
author thib
date Sun, 01 Mar 2009 19:08:02 +0000
parents 4d7486cb20a9
children f88d47a4bf87
comparison
equal deleted inserted replaced
25:2110e0cf64ac 26:f45da03ca631
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 #define MUSIC_VOLUME 0.2
47 #define MUSIC_VOLUME 1
48
49 int pcm_enable = 0; 46 int pcm_enable = 0;
50 Mix_Chunk *play_chunk[MIX_PCM_SIZE]; 47 Mix_Chunk *play_chunk[MIX_PCM_SIZE];
51 48
52 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) {
53 int i; 50 int i;
58 } 55 }
59 56
60 57
61 // #define delete fprintf(stderr,"smus.cc: %d.",__LINE__), delete 58 // #define delete fprintf(stderr,"smus.cc: %d.",__LINE__), delete
62 59
63 void bgm_start(const char* path, int loop_pt); 60 void bgm_start(const char* path, int loop_pt, int * volmod);
64 void effec_start(int chn, const char* path, int loop, int fadein_time); 61 void effec_start(int chn, const char* path, int loop, int fadein_time, int * volmod);
65 void bgm_fadeout(int time); 62 void bgm_fadeout(int time);
66 63
67 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]);
68 char wave[128]; wave[127] = '\0'; wave[0] = '\0'; 66 char wave[128]; wave[127] = '\0'; wave[0] = '\0';
69 67
70 strcpy(cdrom_track, name); 68 strcpy(cdrom_track, name);
71 69
72 StopCDROM(0); 70 StopCDROM(0);
81 } 79 }
82 if (wave == 0) return; 80 if (wave == 0) return;
83 // BGM 再生 81 // BGM 再生
84 if (!pcm_enable) return; 82 if (!pcm_enable) return;
85 if (play_count == 0) 83 if (play_count == 0)
86 bgm_start(wave, -1); 84 bgm_start(wave, -1, volmod);
87 else 85 else
88 bgm_start(wave, config.track_name.TrackStart(name)); 86 bgm_start(wave, config.track_name.TrackStart(name), volmod);
89 return; 87 return;
90 } 88 }
91 89
92 void MuSys::StopCDROM(int time) 90 void MuSys::StopCDROM(int time)
93 { 91 {
97 } 95 }
98 96
99 void MuSys::PlaySE(const char* se, int loop_flag, int channel) { 97 void MuSys::PlaySE(const char* se, int loop_flag, int channel) {
100 if (! pcm_enable) return; 98 if (! pcm_enable) return;
101 if (loop_flag) 99 if (loop_flag)
102 effec_start(MIX_PCM_EFFEC, se, 10000, 0); 100 effec_start(MIX_PCM_EFFEC, se, 10000, 0, volmod);
103 else 101 else
104 effec_start(MIX_PCM_EFFEC, se, 0, 0); 102 effec_start(MIX_PCM_EFFEC, se, 0, 0, volmod);
105 return; 103 return;
106 } 104 }
107 void MuSys::PlaySE(int number) { 105 void MuSys::PlaySE(int number) {
108 if (! pcm_enable) return; 106 if (! pcm_enable) return;
109 const char* se_name = config.track_name.SETrack(number); 107 const char* se_name = config.track_name.SETrack(number);
110 if (se_name == 0) return; 108 if (se_name == 0) return;
111 effec_start(MIX_PCM_EFFEC, se_name, 0, 0); 109 effec_start(MIX_PCM_EFFEC, se_name, 0, 0, volmod);
112 return; 110 return;
113 } 111 }
114 void MuSys::StopSE(int time) { 112 void MuSys::StopSE(int time) {
115 if (! pcm_enable) return; 113 if (! pcm_enable) return;
116 if (time == 0) 114 if (time == 0)
173 */ 171 */
174 172
175 struct WavChunk { 173 struct WavChunk {
176 WAVFILE* wav; 174 WAVFILE* wav;
177 int loop_pt; 175 int loop_pt;
176 int *volmod;
178 static void callback(void* userdata, Uint8* stream, int len); 177 static void callback(void* userdata, Uint8* stream, int len);
179 }; 178 };
180 WavChunk wav_playing; 179 WavChunk wav_playing;
181 static int fadetime_total; 180 static int fadetime_total;
182 static int fadecount; 181 static int fadecount;
200 chunk->wav->Seek(chunk->loop_pt); 199 chunk->wav->Seek(chunk->loop_pt);
201 chunk->wav->Read( (char*)(stream+count*4), 4, len/4-count); 200 chunk->wav->Read( (char*)(stream+count*4), 4, len/4-count);
202 } 201 }
203 } 202 }
204 203
205 int cur_vol = MUSIC_VOLUME*SDL_MIX_MAXVOLUME; 204 int cur_vol = (*chunk->volmod)*SDL_MIX_MAXVOLUME/255;
206 205
207 if (fadetime_total) { 206 if (fadetime_total) {
208 // 音楽を停止中 (fade out) 207 // 音楽を停止中 (fade out)
209 int count_total = fadetime_total*(WAVFILE::freq/1000); 208 int count_total = fadetime_total*(WAVFILE::freq/1000);
210 if (fadecount > count_total || fadetime_total == 1) { // 音楽停止 209 if (fadecount > count_total || fadetime_total == 1) { // 音楽停止
231 fadetime_total = time; 230 fadetime_total = time;
232 } 231 }
233 232
234 static SDL_RWops* OpenSDLRW(const char* path); 233 static SDL_RWops* OpenSDLRW(const char* path);
235 static WAVFILE* OpenWaveFile(const char* path); 234 static WAVFILE* OpenWaveFile(const char* path);
236 void bgm_start(const char* path, int loop_pt) { 235 void bgm_start(const char* path, int loop_pt, int * volmod) {
237 if (! pcm_enable) return; 236 if (! pcm_enable) return;
238 fprintf(stderr,"bgm start %s\n",path); 237 fprintf(stderr,"bgm start %s\n",path);
239 WAVFILE* wav = OpenWaveFile(path); 238 WAVFILE* wav = OpenWaveFile(path);
240 if (wav == 0) return; 239 if (wav == 0) return;
241 Mix_PauseMusic(); 240 Mix_PauseMusic();
246 delete wav_playing.wav; 245 delete wav_playing.wav;
247 wav_playing.wav = 0; 246 wav_playing.wav = 0;
248 } 247 }
249 wav_playing.wav = wav; 248 wav_playing.wav = wav;
250 wav_playing.loop_pt = loop_pt; 249 wav_playing.loop_pt = loop_pt;
250 wav_playing.volmod = &volmod[0];
251 fadetime_total = 0; 251 fadetime_total = 0;
252 fadecount = 0; 252 fadecount = 0;
253 Mix_HookMusic( &(WavChunk::callback), (void*)&wav_playing); 253 Mix_HookMusic( &(WavChunk::callback), (void*)&wav_playing);
254 Mix_VolumeMusic(MUSIC_VOLUME*MIX_MAX_VOLUME); 254 return;
255 return; 255 }
256 } 256
257 257 void effec_start(int chn, const char* path, int loop, int fadein_time, int * volmod) {
258 void effec_start(int chn, const char* path, int loop, int fadein_time) {
259 if (! pcm_enable) return; 258 if (! pcm_enable) return;
260 SDL_RWops* op = OpenSDLRW(path); 259 SDL_RWops* op = OpenSDLRW(path);
261 if (op == 0) { // ファイルが見付からない 260 if (op == 0) { // ファイルが見付からない
262 return; 261 return;
263 } 262 }
266 if (play_chunk[chn]) { 265 if (play_chunk[chn]) {
267 Mix_FreeChunk(play_chunk[chn]); 266 Mix_FreeChunk(play_chunk[chn]);
268 } 267 }
269 play_chunk[chn] = Mix_LoadWAV_RW(op, 1); 268 play_chunk[chn] = Mix_LoadWAV_RW(op, 1);
270 if (fadein_time <= 0) { 269 if (fadein_time <= 0) {
271 Mix_Volume(chn, 128); 270 Mix_Volume(chn, volmod[3]*SDL_MIX_MAXVOLUME/255);
272 Mix_PlayChannel(chn, play_chunk[chn],loop); 271 Mix_PlayChannel(chn, play_chunk[chn],loop);
273 } else { 272 } else {
274 Mix_Volume(chn, 128); 273 Mix_Volume(chn, volmod[3]*SDL_MIX_MAXVOLUME/255);
275 Mix_FadeInChannel(chn, play_chunk[chn],loop,fadein_time); 274 Mix_FadeInChannel(chn, play_chunk[chn],loop,fadein_time);
276 } 275 }
277 return; 276 return;
278 } 277 }
279 278
302 playing_koedata = decode_koe(koeinfo, &len); 301 playing_koedata = decode_koe(koeinfo, &len);
303 fclose(koeinfo.stream); 302 fclose(koeinfo.stream);
304 if (playing_koedata == 0) { 303 if (playing_koedata == 0) {
305 return; 304 return;
306 } 305 }
307 Mix_Volume(chn, 128); 306 Mix_Volume(chn, volmod[1]*SDL_MIX_MAXVOLUME/255);
308 play_chunk[chn] = Mix_LoadWAV_RW(SDL_RWFromMem(playing_koedata, len+0x2c), 1); 307 play_chunk[chn] = Mix_LoadWAV_RW(SDL_RWFromMem(playing_koedata, len+0x2c), 1);
309 Mix_PlayChannel(chn, play_chunk[chn],0); 308 Mix_PlayChannel(chn, play_chunk[chn],0);
310 return; 309 return;
311 } 310 }
312 AvgKoeInfo OpenKoeFile(const char* path) { 311 AvgKoeInfo OpenKoeFile(const char* path) {