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