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"
|
52
|
40 #include <SDL.h>
|
|
41 #include <SDL_mixer.h>
|
|
42 #include "wavfile.h"
|
0
|
43
|
|
44 using namespace std;
|
|
45
|
52
|
46 int pcm_enable = 0;
|
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]);
|
52
|
66
|
|
67 char wave[128];
|
|
68 wave[127] = '\0';
|
|
69 wave[0] = '\0';
|
0
|
70
|
|
71 strcpy(cdrom_track, name);
|
|
72
|
|
73 StopCDROM(0);
|
|
74 strcpy(cdrom_track, name);
|
|
75
|
|
76 /* name -> track */
|
|
77 int track =config.track_name.CDTrack(name);
|
|
78 if (track == -1) track = atoi(name);
|
52
|
79 if (config.track_name.WaveTrack(name) != NULL) strncpy(wave, config.track_name.WaveTrack(name), 127);
|
0
|
80 if (wave[0] == 0 && track != 0) { /* DSTRACK が見つからない場合、CDTRACKを使用する */
|
52
|
81 sprintf(wave, "audio_%02d", track);
|
0
|
82 }
|
|
83 if (wave == 0) return;
|
|
84 // BGM 再生
|
|
85 if (!pcm_enable) return;
|
|
86 if (play_count == 0)
|
26
|
87 bgm_start(wave, -1, volmod);
|
0
|
88 else
|
26
|
89 bgm_start(wave, config.track_name.TrackStart(name), volmod);
|
0
|
90 return;
|
|
91 }
|
|
92
|
|
93 void MuSys::StopCDROM(int time)
|
|
94 {
|
|
95 cdrom_track[0] = '\0';
|
|
96 if (!pcm_enable) return;
|
|
97 bgm_fadeout(time);
|
|
98 }
|
|
99
|
|
100 void MuSys::PlaySE(const char* se, int loop_flag, int channel) {
|
52
|
101 if (!pcm_enable) return;
|
0
|
102 if (loop_flag)
|
26
|
103 effec_start(MIX_PCM_EFFEC, se, 10000, 0, volmod);
|
0
|
104 else
|
26
|
105 effec_start(MIX_PCM_EFFEC, se, 0, 0, volmod);
|
0
|
106 return;
|
|
107 }
|
|
108 void MuSys::PlaySE(int number) {
|
|
109 if (! pcm_enable) return;
|
|
110 const char* se_name = config.track_name.SETrack(number);
|
52
|
111 if (se_name == NULL) return;
|
26
|
112 effec_start(MIX_PCM_EFFEC, se_name, 0, 0, volmod);
|
0
|
113 return;
|
|
114 }
|
|
115 void MuSys::StopSE(int time) {
|
52
|
116 if (!pcm_enable) return;
|
0
|
117 if (time == 0)
|
|
118 Mix_HaltChannel(MIX_PCM_EFFEC);
|
|
119 else
|
|
120 Mix_FadeOutChannel(MIX_PCM_EFFEC, time);
|
|
121 }
|
|
122 bool MuSys::IsStopSE(void) {
|
52
|
123 if (!pcm_enable) return true;
|
0
|
124 if (Mix_Playing(MIX_PCM_EFFEC) != 0) return false;
|
|
125 return true;
|
|
126 }
|
|
127
|
|
128 void MuSys::StopKoe(int time) {
|
52
|
129 if (!pcm_enable) return;
|
0
|
130 if (time == 0) Mix_HaltChannel(MIX_PCM_KOE);
|
|
131 else Mix_FadeOutChannel(MIX_PCM_KOE, time);
|
|
132 }
|
|
133
|
|
134 void MuSys::InitMusic(void)
|
|
135 {
|
|
136 if (music_enable != 1) return;
|
|
137 cdrom_track[0] = '\0';
|
|
138 if ( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, DEFAULT_AUDIOBUF ) < 0 ){
|
|
139 // if ( Mix_OpenAudio( 48000, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, DEFAULT_AUDIOBUF ) < 0 ){
|
|
140 return;
|
|
141 }
|
|
142 int freq, channels; Uint16 format;
|
|
143 if ( Mix_QuerySpec(&freq, &format, &channels) ) {
|
|
144 WAVFILE::freq = freq;
|
|
145 WAVFILE::format = format;
|
|
146 WAVFILE::channels = channels;
|
|
147 }
|
|
148 pcm_enable = 1;
|
|
149 Mix_AllocateChannels( MIX_PCM_SIZE);
|
|
150 music_enable = 2;
|
|
151 return;
|
|
152 }
|
|
153 void MuSys::FinalizeMusic(void)
|
|
154 {
|
|
155 if (music_enable != 2) return;
|
|
156 int i;
|
|
157 for (i=0; i<MIX_PCM_SIZE; i++) {
|
|
158 Mix_HaltChannel(i);
|
|
159 if (play_chunk[i]) {
|
|
160 Mix_FreeChunk(play_chunk[i]);
|
|
161 }
|
|
162 play_chunk[i] = 0;
|
|
163 }
|
|
164 Mix_HaltMusic();
|
|
165 Mix_HookMusic(0,0);
|
|
166 Mix_CloseAudio();
|
|
167 pcm_enable = 0;
|
|
168 music_enable = 1;
|
|
169 }
|
|
170
|
|
171 /*************************************************************************
|
|
172 **
|
|
173 ** ファイル読み込み / 外部コマンド呼び出し
|
|
174 */
|
|
175
|
|
176 struct WavChunk {
|
|
177 WAVFILE* wav;
|
|
178 int loop_pt;
|
26
|
179 int *volmod;
|
0
|
180 static void callback(void* userdata, Uint8* stream, int len);
|
|
181 };
|
52
|
182
|
0
|
183 WavChunk wav_playing;
|
|
184 static int fadetime_total;
|
|
185 static int fadecount;
|
|
186
|
|
187 void WavChunk::callback(void *userdata, Uint8 *stream, int len)
|
|
188 {
|
|
189 WavChunk* chunk = (WavChunk*)userdata;
|
|
190 int count;
|
|
191 if (chunk->loop_pt == -2) { // 再生終了後
|
|
192 memset(stream, 0, len);
|
|
193 return;
|
|
194 }
|
37
|
195
|
|
196 char* stream_dup = new char[len];
|
|
197 count = chunk->wav->Read( (char*)stream_dup, 4, len/4);
|
0
|
198
|
|
199 if (count != len/4) {
|
|
200 // 最後まで再生した
|
|
201 if (chunk->loop_pt == -1) { // 終了
|
|
202 chunk->loop_pt = -2;
|
37
|
203 memset(stream_dup+count*4, 0, len-count*4);
|
0
|
204 } else {
|
|
205 chunk->wav->Seek(chunk->loop_pt);
|
37
|
206 chunk->wav->Read( (char*)(stream_dup+count*4), 4, len/4-count);
|
0
|
207 }
|
|
208 }
|
3
|
209
|
26
|
210 int cur_vol = (*chunk->volmod)*SDL_MIX_MAXVOLUME/255;
|
3
|
211
|
0
|
212 if (fadetime_total) {
|
|
213 // 音楽を停止中 (fade out)
|
|
214 int count_total = fadetime_total*(WAVFILE::freq/1000);
|
|
215 if (fadecount > count_total || fadetime_total == 1) { // 音楽停止
|
|
216 chunk->loop_pt = -2;
|
|
217 memset(stream, 0, len);
|
37
|
218 delete[] stream_dup;
|
0
|
219 return;
|
|
220 }
|
3
|
221 cur_vol = cur_vol*(count_total-fadecount)/count_total;
|
0
|
222 fadecount += len/4;
|
|
223 }
|
37
|
224
|
3
|
225 SDL_MixAudio(stream, (Uint8*)stream_dup, len, cur_vol);
|
7
|
226 delete[] stream_dup;
|
37
|
227
|
0
|
228 return;
|
|
229 }
|
|
230 void bgm_fadeout(int time) {
|
|
231 fadecount = 0;
|
|
232 if (time <= 0) time = 1;
|
|
233 fadetime_total = time;
|
|
234 }
|
|
235
|
|
236 static SDL_RWops* OpenSDLRW(const char* path);
|
|
237 static WAVFILE* OpenWaveFile(const char* path);
|
26
|
238 void bgm_start(const char* path, int loop_pt, int * volmod) {
|
0
|
239 if (! pcm_enable) return;
|
|
240 fprintf(stderr,"bgm start %s\n",path);
|
|
241 WAVFILE* wav = OpenWaveFile(path);
|
52
|
242 if (wav == NULL) return;
|
0
|
243 Mix_PauseMusic();
|
|
244 Mix_HaltMusic();
|
|
245 Mix_HookMusic(0,0);
|
|
246 /* 前に再生していたのを終了 */
|
52
|
247 if (wav_playing.wav != NULL) {
|
0
|
248 delete wav_playing.wav;
|
52
|
249 wav_playing.wav = NULL;
|
0
|
250 }
|
|
251 wav_playing.wav = wav;
|
|
252 wav_playing.loop_pt = loop_pt;
|
26
|
253 wav_playing.volmod = &volmod[0];
|
0
|
254 fadetime_total = 0;
|
|
255 fadecount = 0;
|
|
256 Mix_HookMusic( &(WavChunk::callback), (void*)&wav_playing);
|
|
257 }
|
|
258
|
26
|
259 void effec_start(int chn, const char* path, int loop, int fadein_time, int * volmod) {
|
0
|
260 if (! pcm_enable) return;
|
|
261 SDL_RWops* op = OpenSDLRW(path);
|
52
|
262 if (op == NULL) { // ファイルが見付からない
|
0
|
263 return;
|
|
264 }
|
|
265 Mix_Pause(chn);
|
|
266
|
|
267 if (play_chunk[chn]) {
|
|
268 Mix_FreeChunk(play_chunk[chn]);
|
|
269 }
|
|
270 play_chunk[chn] = Mix_LoadWAV_RW(op, 1);
|
|
271 if (fadein_time <= 0) {
|
26
|
272 Mix_Volume(chn, volmod[3]*SDL_MIX_MAXVOLUME/255);
|
52
|
273 Mix_PlayChannel(chn, play_chunk[chn], loop);
|
0
|
274 } else {
|
26
|
275 Mix_Volume(chn, volmod[3]*SDL_MIX_MAXVOLUME/255);
|
52
|
276 Mix_FadeInChannel(chn, play_chunk[chn], loop, fadein_time);
|
0
|
277 }
|
|
278 }
|
|
279
|
|
280 void MuSys::PlayKoe(const char* path) {
|
52
|
281 if (!pcm_enable) return;
|
|
282 static char* playing_koedata = NULL;
|
0
|
283 int len = 0;
|
|
284 AvgKoeInfo koeinfo;
|
|
285 int chn = MIX_PCM_KOE;
|
|
286
|
|
287 Mix_Pause(chn);
|
|
288 Mix_HaltChannel(chn); // これで RWop が解放されるはず…
|
|
289 if (play_chunk[chn]) {
|
|
290 Mix_FreeChunk(play_chunk[chn]);
|
|
291 play_chunk[chn] = 0;
|
|
292 }
|
|
293
|
|
294 if (playing_koedata) {
|
7
|
295 free(playing_koedata);
|
0
|
296 playing_koedata = 0;
|
|
297 }
|
|
298
|
|
299 koeinfo = OpenKoeFile(path);
|
|
300
|
52
|
301 if (koeinfo.stream == NULL) return;
|
0
|
302 playing_koedata = decode_koe(koeinfo, &len);
|
|
303 fclose(koeinfo.stream);
|
52
|
304 if (playing_koedata == NULL) {
|
0
|
305 return;
|
|
306 }
|
26
|
307 Mix_Volume(chn, volmod[1]*SDL_MIX_MAXVOLUME/255);
|
0
|
308 play_chunk[chn] = Mix_LoadWAV_RW(SDL_RWFromMem(playing_koedata, len+0x2c), 1);
|
52
|
309 Mix_PlayChannel(chn, play_chunk[chn], 0);
|
0
|
310 }
|
52
|
311
|
0
|
312 AvgKoeInfo OpenKoeFile(const char* path) {
|
|
313 int radix = 10000;
|
|
314 /* if (global_system.Version() >= 2) */ radix *= 10;
|
|
315 AvgKoeInfo info;
|
52
|
316 info.stream = NULL;
|
|
317 info.length = 0;
|
|
318 info.offset = 0;
|
|
319 if (isdigit(path[0]) && strchr(path,'.') == NULL) { // 数値 (拡張子等なし)
|
0
|
320 /* avg32 形式の音声アーカイブのキャッシュを検索 */
|
|
321 int pointer = atoi(path);
|
|
322 int file_no = pointer / radix;
|
|
323 int index = pointer % radix;
|
|
324 info = FindKoe(file_no, index);
|
|
325 } else { // ファイル
|
|
326 int length;
|
52
|
327 ARCINFO* arcinfo = file_searcher.Find(FILESEARCH::KOE, path, ".WPD");
|
|
328 if (arcinfo == NULL) return info;
|
0
|
329 info.stream = arcinfo->OpenFile(&length);
|
|
330 info.rate = 22050;
|
|
331 info.length = length;
|
|
332 info.offset = ftell(info.stream);
|
|
333 info.type = koe_unknown;
|
|
334 delete arcinfo;
|
|
335 }
|
|
336 return info;
|
|
337 }
|
|
338
|
|
339 static SDL_RWops* OpenSDLRW(const char* path) {
|
|
340 /* まず wav ファイルを探す */
|
52
|
341 ARCINFO* info = file_searcher.Find(FILESEARCH::WAV, path, ".wav");
|
|
342 if (info == NULL) {
|
|
343 info = file_searcher.Find(FILESEARCH::WAV, path, ".nwa");
|
|
344 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "nwa");
|
|
345 if (info != NULL) { // read NWA file
|
0
|
346 int dummy;
|
|
347 FILE* f = info->OpenFile(&dummy);
|
|
348 static char* d = 0;
|
|
349 int sz;
|
|
350 if (d != 0) delete[] d;
|
|
351 d = NWAFILE::ReadAll(f, sz);
|
|
352 return SDL_RWFromMem(d, sz);
|
|
353 }
|
|
354 }
|
52
|
355 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "wav");
|
|
356 if (info == NULL) info = file_searcher.Find(FILESEARCH::WAV, path, ".ogg");
|
|
357 if (info != NULL) {
|
0
|
358 int dummy;
|
|
359 FILE* f = info->OpenFile(&dummy);
|
|
360 delete info;
|
52
|
361 if (f == NULL) return NULL;
|
0
|
362 SDL_RWops* op = SDL_RWFromFP(f, 1);
|
|
363 return op;
|
|
364 }
|
52
|
365 return NULL;
|
0
|
366 }
|
|
367
|
|
368 static WAVFILE* OpenWaveFile(const char* path) {
|
|
369 /* まず wav ファイルを探す */
|
52
|
370 ARCINFO* info = file_searcher.Find(FILESEARCH::WAV, path, ".wav");
|
|
371 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "wav");
|
|
372 if (info != NULL) {
|
0
|
373 int size;
|
|
374 FILE* f = info->OpenFile(&size);
|
|
375 delete info;
|
52
|
376 if (f == NULL) return NULL;
|
0
|
377 WAVFILE* w = WAVFILE::MakeConverter(new WAVFILE_Stream(f, size));
|
|
378 return w;
|
|
379 }
|
|
380 /* 次に nwa ファイル */
|
52
|
381 info = file_searcher.Find(FILESEARCH::WAV, path, ".nwa");
|
|
382 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "nwa");
|
|
383 if (info != NULL) {
|
0
|
384 int size;
|
|
385 FILE* f = info->OpenFile(&size);
|
|
386 delete info;
|
52
|
387 if (f == NULL) return NULL;
|
0
|
388 WAVFILE* w = WAVFILE::MakeConverter(new NWAFILE(f));
|
|
389 return w;
|
|
390 }
|
|
391
|
|
392 /* 次に mp3 ファイル */
|
52
|
393 info = file_searcher.Find(FILESEARCH::WAV, path, ".mp3");
|
|
394 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "mp3");
|
|
395 if (info != NULL) {
|
0
|
396 int size;
|
|
397 FILE* f = info->OpenFile(&size);
|
|
398 delete info;
|
52
|
399 if (f == NULL) return NULL;
|
0
|
400 MP3FILE* w = new MP3FILE(f, size);
|
52
|
401 if (w->pimpl != NULL) {
|
0
|
402 return WAVFILE::MakeConverter(w);
|
|
403 }
|
|
404 delete w;
|
|
405 }
|
|
406
|
|
407 /* 次に ogg ファイル */
|
52
|
408 info = file_searcher.Find(FILESEARCH::WAV, path, ".ogg");
|
|
409 if (info == NULL) info = file_searcher.Find(FILESEARCH::BGM, path, "ogg");
|
|
410 if (info != NULL) {
|
0
|
411 int size;
|
|
412 FILE* f = info->OpenFile(&size);
|
|
413 delete info;
|
52
|
414 if (f == NULL) return NULL;
|
0
|
415 OggFILE* w = new OggFILE(f, size);
|
52
|
416 if (w->pimpl != NULL) {
|
0
|
417 return WAVFILE::MakeConverter(w);
|
|
418 }
|
|
419 delete w;
|
|
420 }
|
52
|
421 return NULL;
|
0
|
422 }
|
|
423
|