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