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