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);
|
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);
|
|
235 void bgm_start(const char* path, int loop_pt) {
|
|
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;
|
|
250 fadetime_total = 0;
|
|
251 fadecount = 0;
|
|
252 Mix_HookMusic( &(WavChunk::callback), (void*)&wav_playing);
|
3
|
253 Mix_VolumeMusic(MUSIC_VOLUME*MIX_MAX_VOLUME);
|
0
|
254 return;
|
|
255 }
|
|
256
|
|
257 void effec_start(int chn, const char* path, int loop, int fadein_time) {
|
|
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) {
|
|
270 Mix_Volume(chn, 128);
|
|
271 Mix_PlayChannel(chn, play_chunk[chn],loop);
|
|
272 } else {
|
|
273 Mix_Volume(chn, 128);
|
|
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 }
|
|
306 Mix_Volume(chn, 128);
|
|
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
|