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