annotate music2/music.cc @ 23:f94d28dcf7bc

No need for music2/config.h (which doesn't exist anymore)
author thib
date Sun, 02 Nov 2008 13:58:59 +0000
parents 4d7486cb20a9
children f45da03ca631
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
223b71206888 Initial import
thib
parents:
diff changeset
1 /*
223b71206888 Initial import
thib
parents:
diff changeset
2 * Copyright (c) 2004-2006 Kazunori "jagarl" Ueno
223b71206888 Initial import
thib
parents:
diff changeset
3 * All rights reserved.
223b71206888 Initial import
thib
parents:
diff changeset
4 *
223b71206888 Initial import
thib
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
223b71206888 Initial import
thib
parents:
diff changeset
6 * modification, are permitted provided that the following conditions
223b71206888 Initial import
thib
parents:
diff changeset
7 * are met:
223b71206888 Initial import
thib
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
223b71206888 Initial import
thib
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
223b71206888 Initial import
thib
parents:
diff changeset
10 * 2. Redistributions in binary form must reproduce the above copyright
223b71206888 Initial import
thib
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer in the
223b71206888 Initial import
thib
parents:
diff changeset
12 * documentation and/or other materials provided with the distribution.
223b71206888 Initial import
thib
parents:
diff changeset
13 * 3. The name of the author may not be used to endorse or promote products
223b71206888 Initial import
thib
parents:
diff changeset
14 * derived from this software without specific prior written permission.
223b71206888 Initial import
thib
parents:
diff changeset
15 *
223b71206888 Initial import
thib
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
223b71206888 Initial import
thib
parents:
diff changeset
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
223b71206888 Initial import
thib
parents:
diff changeset
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
223b71206888 Initial import
thib
parents:
diff changeset
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
223b71206888 Initial import
thib
parents:
diff changeset
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223b71206888 Initial import
thib
parents:
diff changeset
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
223b71206888 Initial import
thib
parents:
diff changeset
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
223b71206888 Initial import
thib
parents:
diff changeset
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
223b71206888 Initial import
thib
parents:
diff changeset
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
223b71206888 Initial import
thib
parents:
diff changeset
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
223b71206888 Initial import
thib
parents:
diff changeset
26 */
223b71206888 Initial import
thib
parents:
diff changeset
27
223b71206888 Initial import
thib
parents:
diff changeset
28 /* music.cc SDL_mixer を用いた音楽再生ルーチン */
223b71206888 Initial import
thib
parents:
diff changeset
29
223b71206888 Initial import
thib
parents:
diff changeset
30
223b71206888 Initial import
thib
parents:
diff changeset
31 #include <string.h>
223b71206888 Initial import
thib
parents:
diff changeset
32 #include <stdio.h>
223b71206888 Initial import
thib
parents:
diff changeset
33 #include <unistd.h>
223b71206888 Initial import
thib
parents:
diff changeset
34 #include <fcntl.h>
223b71206888 Initial import
thib
parents:
diff changeset
35 #include <ctype.h>
223b71206888 Initial import
thib
parents:
diff changeset
36 #include <signal.h>
223b71206888 Initial import
thib
parents:
diff changeset
37 #include"system/system_config.h"
223b71206888 Initial import
thib
parents:
diff changeset
38 #include"system/file.h"
223b71206888 Initial import
thib
parents:
diff changeset
39 #include "music.h"
223b71206888 Initial import
thib
parents:
diff changeset
40 #include<SDL.h>
223b71206888 Initial import
thib
parents:
diff changeset
41 #include<SDL_mixer.h>
223b71206888 Initial import
thib
parents:
diff changeset
42 #include"wavfile.h"
223b71206888 Initial import
thib
parents:
diff changeset
43
223b71206888 Initial import
thib
parents:
diff changeset
44 using namespace std;
223b71206888 Initial import
thib
parents:
diff changeset
45
3
c4af1e9ab8d1 music volume can be changed (harcorded constant atm)
thib
parents: 0
diff changeset
46 #define MUSIC_VOLUME 0.2
18
4d7486cb20a9 Sync with upstream
thib
parents: 7
diff changeset
47 #define MUSIC_VOLUME 1
3
c4af1e9ab8d1 music volume can be changed (harcorded constant atm)
thib
parents: 0
diff changeset
48
0
223b71206888 Initial import
thib
parents:
diff changeset
49 int pcm_enable = 0;
223b71206888 Initial import
thib
parents:
diff changeset
50 Mix_Chunk *play_chunk[MIX_PCM_SIZE];
223b71206888 Initial import
thib
parents:
diff changeset
51
223b71206888 Initial import
thib
parents:
diff changeset
52 MuSys::MuSys(AyuSysConfig& _config) : config(_config), movie_id(-1), music_enable(1) {
223b71206888 Initial import
thib
parents:
diff changeset
53 int i;
223b71206888 Initial import
thib
parents:
diff changeset
54 for (i=0; i<MIX_PCM_SIZE; i++)
223b71206888 Initial import
thib
parents:
diff changeset
55 play_chunk[i] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
56 cdrom_track[0] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
57 effec_track[0] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
58 }
223b71206888 Initial import
thib
parents:
diff changeset
59
223b71206888 Initial import
thib
parents:
diff changeset
60
223b71206888 Initial import
thib
parents:
diff changeset
61 // #define delete fprintf(stderr,"smus.cc: %d.",__LINE__), delete
223b71206888 Initial import
thib
parents:
diff changeset
62
223b71206888 Initial import
thib
parents:
diff changeset
63 void bgm_start(const char* path, int loop_pt);
223b71206888 Initial import
thib
parents:
diff changeset
64 void effec_start(int chn, const char* path, int loop, int fadein_time);
223b71206888 Initial import
thib
parents:
diff changeset
65 void bgm_fadeout(int time);
223b71206888 Initial import
thib
parents:
diff changeset
66
223b71206888 Initial import
thib
parents:
diff changeset
67 void MuSys::PlayCDROM(char* name, int play_count) {
223b71206888 Initial import
thib
parents:
diff changeset
68 char wave[128]; wave[127] = '\0'; wave[0] = '\0';
223b71206888 Initial import
thib
parents:
diff changeset
69
223b71206888 Initial import
thib
parents:
diff changeset
70 strcpy(cdrom_track, name);
223b71206888 Initial import
thib
parents:
diff changeset
71
223b71206888 Initial import
thib
parents:
diff changeset
72 StopCDROM(0);
223b71206888 Initial import
thib
parents:
diff changeset
73 strcpy(cdrom_track, name);
223b71206888 Initial import
thib
parents:
diff changeset
74
223b71206888 Initial import
thib
parents:
diff changeset
75 /* name -> track */
223b71206888 Initial import
thib
parents:
diff changeset
76 int track =config.track_name.CDTrack(name);
223b71206888 Initial import
thib
parents:
diff changeset
77 if (track == -1) track = atoi(name);
223b71206888 Initial import
thib
parents:
diff changeset
78 if (config.track_name.WaveTrack(name) != 0) strncpy(wave, config.track_name.WaveTrack(name), 127);
223b71206888 Initial import
thib
parents:
diff changeset
79 if (wave[0] == 0 && track != 0) { /* DSTRACK が見つからない場合、CDTRACKを使用する */
223b71206888 Initial import
thib
parents:
diff changeset
80 sprintf(wave, "audio_%02d",track);
223b71206888 Initial import
thib
parents:
diff changeset
81 }
223b71206888 Initial import
thib
parents:
diff changeset
82 if (wave == 0) return;
223b71206888 Initial import
thib
parents:
diff changeset
83 // BGM 再生
223b71206888 Initial import
thib
parents:
diff changeset
84 if (!pcm_enable) return;
223b71206888 Initial import
thib
parents:
diff changeset
85 if (play_count == 0)
223b71206888 Initial import
thib
parents:
diff changeset
86 bgm_start(wave, -1);
223b71206888 Initial import
thib
parents:
diff changeset
87 else
223b71206888 Initial import
thib
parents:
diff changeset
88 bgm_start(wave, config.track_name.TrackStart(name));
223b71206888 Initial import
thib
parents:
diff changeset
89 return;
223b71206888 Initial import
thib
parents:
diff changeset
90 }
223b71206888 Initial import
thib
parents:
diff changeset
91
223b71206888 Initial import
thib
parents:
diff changeset
92 void MuSys::StopCDROM(int time)
223b71206888 Initial import
thib
parents:
diff changeset
93 {
223b71206888 Initial import
thib
parents:
diff changeset
94 cdrom_track[0] = '\0';
223b71206888 Initial import
thib
parents:
diff changeset
95 if (!pcm_enable) return;
223b71206888 Initial import
thib
parents:
diff changeset
96 bgm_fadeout(time);
223b71206888 Initial import
thib
parents:
diff changeset
97 }
223b71206888 Initial import
thib
parents:
diff changeset
98
223b71206888 Initial import
thib
parents:
diff changeset
99 void MuSys::PlaySE(const char* se, int loop_flag, int channel) {
223b71206888 Initial import
thib
parents:
diff changeset
100 if (! pcm_enable) return;
223b71206888 Initial import
thib
parents:
diff changeset
101 if (loop_flag)
223b71206888 Initial import
thib
parents:
diff changeset
102 effec_start(MIX_PCM_EFFEC, se, 10000, 0);
223b71206888 Initial import
thib
parents:
diff changeset
103 else
223b71206888 Initial import
thib
parents:
diff changeset
104 effec_start(MIX_PCM_EFFEC, se, 0, 0);
223b71206888 Initial import
thib
parents:
diff changeset
105 return;
223b71206888 Initial import
thib
parents:
diff changeset
106 }
223b71206888 Initial import
thib
parents:
diff changeset
107 void MuSys::PlaySE(int number) {
223b71206888 Initial import
thib
parents:
diff changeset
108 if (! pcm_enable) return;
223b71206888 Initial import
thib
parents:
diff changeset
109 const char* se_name = config.track_name.SETrack(number);
223b71206888 Initial import
thib
parents:
diff changeset
110 if (se_name == 0) return;
223b71206888 Initial import
thib
parents:
diff changeset
111 effec_start(MIX_PCM_EFFEC, se_name, 0, 0);
223b71206888 Initial import
thib
parents:
diff changeset
112 return;
223b71206888 Initial import
thib
parents:
diff changeset
113 }
223b71206888 Initial import
thib
parents:
diff changeset
114 void MuSys::StopSE(int time) {
223b71206888 Initial import
thib
parents:
diff changeset
115 if (! pcm_enable) return;
223b71206888 Initial import
thib
parents:
diff changeset
116 if (time == 0)
223b71206888 Initial import
thib
parents:
diff changeset
117 Mix_HaltChannel(MIX_PCM_EFFEC);
223b71206888 Initial import
thib
parents:
diff changeset
118 else
223b71206888 Initial import
thib
parents:
diff changeset
119 Mix_FadeOutChannel(MIX_PCM_EFFEC, time);
223b71206888 Initial import
thib
parents:
diff changeset
120 }
223b71206888 Initial import
thib
parents:
diff changeset
121 bool MuSys::IsStopSE(void) {
223b71206888 Initial import
thib
parents:
diff changeset
122 if (! pcm_enable) return true;
223b71206888 Initial import
thib
parents:
diff changeset
123 if (Mix_Playing(MIX_PCM_EFFEC) != 0) return false;
223b71206888 Initial import
thib
parents:
diff changeset
124 return true;
223b71206888 Initial import
thib
parents:
diff changeset
125 }
223b71206888 Initial import
thib
parents:
diff changeset
126
223b71206888 Initial import
thib
parents:
diff changeset
127 void MuSys::StopKoe(int time) {
223b71206888 Initial import
thib
parents:
diff changeset
128 if (! pcm_enable) return;
223b71206888 Initial import
thib
parents:
diff changeset
129 if (time == 0) Mix_HaltChannel(MIX_PCM_KOE);
223b71206888 Initial import
thib
parents:
diff changeset
130 else Mix_FadeOutChannel(MIX_PCM_KOE, time);
223b71206888 Initial import
thib
parents:
diff changeset
131 }
223b71206888 Initial import
thib
parents:
diff changeset
132
223b71206888 Initial import
thib
parents:
diff changeset
133 void MuSys::InitMusic(void)
223b71206888 Initial import
thib
parents:
diff changeset
134 {
223b71206888 Initial import
thib
parents:
diff changeset
135 if (music_enable != 1) return;
223b71206888 Initial import
thib
parents:
diff changeset
136 cdrom_track[0] = '\0';
223b71206888 Initial import
thib
parents:
diff changeset
137 if ( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, DEFAULT_AUDIOBUF ) < 0 ){
223b71206888 Initial import
thib
parents:
diff changeset
138 // if ( Mix_OpenAudio( 48000, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, DEFAULT_AUDIOBUF ) < 0 ){
223b71206888 Initial import
thib
parents:
diff changeset
139 return;
223b71206888 Initial import
thib
parents:
diff changeset
140 }
223b71206888 Initial import
thib
parents:
diff changeset
141 int freq, channels; Uint16 format;
223b71206888 Initial import
thib
parents:
diff changeset
142 if ( Mix_QuerySpec(&freq, &format, &channels) ) {
223b71206888 Initial import
thib
parents:
diff changeset
143 WAVFILE::freq = freq;
223b71206888 Initial import
thib
parents:
diff changeset
144 WAVFILE::format = format;
223b71206888 Initial import
thib
parents:
diff changeset
145 WAVFILE::channels = channels;
223b71206888 Initial import
thib
parents:
diff changeset
146 }
223b71206888 Initial import
thib
parents:
diff changeset
147 pcm_enable = 1;
223b71206888 Initial import
thib
parents:
diff changeset
148 Mix_AllocateChannels( MIX_PCM_SIZE);
223b71206888 Initial import
thib
parents:
diff changeset
149 music_enable = 2;
223b71206888 Initial import
thib
parents:
diff changeset
150 return;
223b71206888 Initial import
thib
parents:
diff changeset
151 }
223b71206888 Initial import
thib
parents:
diff changeset
152 void MuSys::FinalizeMusic(void)
223b71206888 Initial import
thib
parents:
diff changeset
153 {
223b71206888 Initial import
thib
parents:
diff changeset
154 if (music_enable != 2) return;
223b71206888 Initial import
thib
parents:
diff changeset
155 int i;
223b71206888 Initial import
thib
parents:
diff changeset
156 for (i=0; i<MIX_PCM_SIZE; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
157 Mix_HaltChannel(i);
223b71206888 Initial import
thib
parents:
diff changeset
158 if (play_chunk[i]) {
223b71206888 Initial import
thib
parents:
diff changeset
159 Mix_FreeChunk(play_chunk[i]);
223b71206888 Initial import
thib
parents:
diff changeset
160 }
223b71206888 Initial import
thib
parents:
diff changeset
161 play_chunk[i] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
162 }
223b71206888 Initial import
thib
parents:
diff changeset
163 Mix_HaltMusic();
223b71206888 Initial import
thib
parents:
diff changeset
164 Mix_HookMusic(0,0);
223b71206888 Initial import
thib
parents:
diff changeset
165 Mix_CloseAudio();
223b71206888 Initial import
thib
parents:
diff changeset
166 pcm_enable = 0;
223b71206888 Initial import
thib
parents:
diff changeset
167 music_enable = 1;
223b71206888 Initial import
thib
parents:
diff changeset
168 }
223b71206888 Initial import
thib
parents:
diff changeset
169
223b71206888 Initial import
thib
parents:
diff changeset
170 /*************************************************************************
223b71206888 Initial import
thib
parents:
diff changeset
171 **
223b71206888 Initial import
thib
parents:
diff changeset
172 ** ファイル読み込み / 外部コマンド呼び出し
223b71206888 Initial import
thib
parents:
diff changeset
173 */
223b71206888 Initial import
thib
parents:
diff changeset
174
223b71206888 Initial import
thib
parents:
diff changeset
175 struct WavChunk {
223b71206888 Initial import
thib
parents:
diff changeset
176 WAVFILE* wav;
223b71206888 Initial import
thib
parents:
diff changeset
177 int loop_pt;
223b71206888 Initial import
thib
parents:
diff changeset
178 static void callback(void* userdata, Uint8* stream, int len);
223b71206888 Initial import
thib
parents:
diff changeset
179 };
223b71206888 Initial import
thib
parents:
diff changeset
180 WavChunk wav_playing;
223b71206888 Initial import
thib
parents:
diff changeset
181 static int fadetime_total;
223b71206888 Initial import
thib
parents:
diff changeset
182 static int fadecount;
223b71206888 Initial import
thib
parents:
diff changeset
183
223b71206888 Initial import
thib
parents:
diff changeset
184 void WavChunk::callback(void *userdata, Uint8 *stream, int len)
223b71206888 Initial import
thib
parents:
diff changeset
185 {
223b71206888 Initial import
thib
parents:
diff changeset
186 WavChunk* chunk = (WavChunk*)userdata;
223b71206888 Initial import
thib
parents:
diff changeset
187 int count;
223b71206888 Initial import
thib
parents:
diff changeset
188 if (chunk->loop_pt == -2) { // 再生終了後
223b71206888 Initial import
thib
parents:
diff changeset
189 memset(stream, 0, len);
223b71206888 Initial import
thib
parents:
diff changeset
190 return;
223b71206888 Initial import
thib
parents:
diff changeset
191 }
223b71206888 Initial import
thib
parents:
diff changeset
192 count = chunk->wav->Read( (char*)stream, 4, len/4);
223b71206888 Initial import
thib
parents:
diff changeset
193
223b71206888 Initial import
thib
parents:
diff changeset
194 if (count != len/4) {
223b71206888 Initial import
thib
parents:
diff changeset
195 memset(stream+count*4, 0, len-count*4);
223b71206888 Initial import
thib
parents:
diff changeset
196 // 最後まで再生した
223b71206888 Initial import
thib
parents:
diff changeset
197 if (chunk->loop_pt == -1) { // 終了
223b71206888 Initial import
thib
parents:
diff changeset
198 chunk->loop_pt = -2;
223b71206888 Initial import
thib
parents:
diff changeset
199 } else {
223b71206888 Initial import
thib
parents:
diff changeset
200 chunk->wav->Seek(chunk->loop_pt);
223b71206888 Initial import
thib
parents:
diff changeset
201 chunk->wav->Read( (char*)(stream+count*4), 4, len/4-count);
223b71206888 Initial import
thib
parents:
diff changeset
202 }
223b71206888 Initial import
thib
parents:
diff changeset
203 }
3
c4af1e9ab8d1 music volume can be changed (harcorded constant atm)
thib
parents: 0
diff changeset
204
c4af1e9ab8d1 music volume can be changed (harcorded constant atm)
thib
parents: 0
diff changeset
205 int cur_vol = MUSIC_VOLUME*SDL_MIX_MAXVOLUME;
c4af1e9ab8d1 music volume can be changed (harcorded constant atm)
thib
parents: 0
diff changeset
206
0
223b71206888 Initial import
thib
parents:
diff changeset
207 if (fadetime_total) {
223b71206888 Initial import
thib
parents:
diff changeset
208 // 音楽を停止中 (fade out)
223b71206888 Initial import
thib
parents:
diff changeset
209 int count_total = fadetime_total*(WAVFILE::freq/1000);
223b71206888 Initial import
thib
parents:
diff changeset
210 if (fadecount > count_total || fadetime_total == 1) { // 音楽停止
223b71206888 Initial import
thib
parents:
diff changeset
211 chunk->loop_pt = -2;
223b71206888 Initial import
thib
parents:
diff changeset
212 memset(stream, 0, len);
223b71206888 Initial import
thib
parents:
diff changeset
213 return;
223b71206888 Initial import
thib
parents:
diff changeset
214 }
223b71206888 Initial import
thib
parents:
diff changeset
215 // int cur_vol = 256*(count_total-fadecount)/count_total;
3
c4af1e9ab8d1 music volume can be changed (harcorded constant atm)
thib
parents: 0
diff changeset
216 cur_vol = cur_vol*(count_total-fadecount)/count_total;
0
223b71206888 Initial import
thib
parents:
diff changeset
217 fadecount += len/4;
223b71206888 Initial import
thib
parents:
diff changeset
218 }
3
c4af1e9ab8d1 music volume can be changed (harcorded constant atm)
thib
parents: 0
diff changeset
219
c4af1e9ab8d1 music volume can be changed (harcorded constant atm)
thib
parents: 0
diff changeset
220 char* stream_dup = new char[len];
c4af1e9ab8d1 music volume can be changed (harcorded constant atm)
thib
parents: 0
diff changeset
221 memcpy(stream_dup, stream, len);
c4af1e9ab8d1 music volume can be changed (harcorded constant atm)
thib
parents: 0
diff changeset
222 memset(stream, 0, len);
c4af1e9ab8d1 music volume can be changed (harcorded constant atm)
thib
parents: 0
diff changeset
223 SDL_MixAudio(stream, (Uint8*)stream_dup, len, cur_vol);
7
fa8511a21d05 Fixes somes memory leaks
thib
parents: 3
diff changeset
224 delete[] stream_dup;
3
c4af1e9ab8d1 music volume can be changed (harcorded constant atm)
thib
parents: 0
diff changeset
225
0
223b71206888 Initial import
thib
parents:
diff changeset
226 return;
223b71206888 Initial import
thib
parents:
diff changeset
227 }
223b71206888 Initial import
thib
parents:
diff changeset
228 void bgm_fadeout(int time) {
223b71206888 Initial import
thib
parents:
diff changeset
229 fadecount = 0;
223b71206888 Initial import
thib
parents:
diff changeset
230 if (time <= 0) time = 1;
223b71206888 Initial import
thib
parents:
diff changeset
231 fadetime_total = time;
223b71206888 Initial import
thib
parents:
diff changeset
232 }
223b71206888 Initial import
thib
parents:
diff changeset
233
223b71206888 Initial import
thib
parents:
diff changeset
234 static SDL_RWops* OpenSDLRW(const char* path);
223b71206888 Initial import
thib
parents:
diff changeset
235 static WAVFILE* OpenWaveFile(const char* path);
223b71206888 Initial import
thib
parents:
diff changeset
236 void bgm_start(const char* path, int loop_pt) {
223b71206888 Initial import
thib
parents:
diff changeset
237 if (! pcm_enable) return;
223b71206888 Initial import
thib
parents:
diff changeset
238 fprintf(stderr,"bgm start %s\n",path);
223b71206888 Initial import
thib
parents:
diff changeset
239 WAVFILE* wav = OpenWaveFile(path);
223b71206888 Initial import
thib
parents:
diff changeset
240 if (wav == 0) return;
223b71206888 Initial import
thib
parents:
diff changeset
241 Mix_PauseMusic();
223b71206888 Initial import
thib
parents:
diff changeset
242 Mix_HaltMusic();
223b71206888 Initial import
thib
parents:
diff changeset
243 Mix_HookMusic(0,0);
223b71206888 Initial import
thib
parents:
diff changeset
244 /* 前に再生していたのを終了 */
223b71206888 Initial import
thib
parents:
diff changeset
245 if (wav_playing.wav) {
223b71206888 Initial import
thib
parents:
diff changeset
246 delete wav_playing.wav;
223b71206888 Initial import
thib
parents:
diff changeset
247 wav_playing.wav = 0;
223b71206888 Initial import
thib
parents:
diff changeset
248 }
223b71206888 Initial import
thib
parents:
diff changeset
249 wav_playing.wav = wav;
223b71206888 Initial import
thib
parents:
diff changeset
250 wav_playing.loop_pt = loop_pt;
223b71206888 Initial import
thib
parents:
diff changeset
251 fadetime_total = 0;
223b71206888 Initial import
thib
parents:
diff changeset
252 fadecount = 0;
223b71206888 Initial import
thib
parents:
diff changeset
253 Mix_HookMusic( &(WavChunk::callback), (void*)&wav_playing);
3
c4af1e9ab8d1 music volume can be changed (harcorded constant atm)
thib
parents: 0
diff changeset
254 Mix_VolumeMusic(MUSIC_VOLUME*MIX_MAX_VOLUME);
0
223b71206888 Initial import
thib
parents:
diff changeset
255 return;
223b71206888 Initial import
thib
parents:
diff changeset
256 }
223b71206888 Initial import
thib
parents:
diff changeset
257
223b71206888 Initial import
thib
parents:
diff changeset
258 void effec_start(int chn, const char* path, int loop, int fadein_time) {
223b71206888 Initial import
thib
parents:
diff changeset
259 if (! pcm_enable) return;
223b71206888 Initial import
thib
parents:
diff changeset
260 SDL_RWops* op = OpenSDLRW(path);
223b71206888 Initial import
thib
parents:
diff changeset
261 if (op == 0) { // ファイルが見付からない
223b71206888 Initial import
thib
parents:
diff changeset
262 return;
223b71206888 Initial import
thib
parents:
diff changeset
263 }
223b71206888 Initial import
thib
parents:
diff changeset
264 Mix_Pause(chn);
223b71206888 Initial import
thib
parents:
diff changeset
265
223b71206888 Initial import
thib
parents:
diff changeset
266 if (play_chunk[chn]) {
223b71206888 Initial import
thib
parents:
diff changeset
267 Mix_FreeChunk(play_chunk[chn]);
223b71206888 Initial import
thib
parents:
diff changeset
268 }
223b71206888 Initial import
thib
parents:
diff changeset
269 play_chunk[chn] = Mix_LoadWAV_RW(op, 1);
223b71206888 Initial import
thib
parents:
diff changeset
270 if (fadein_time <= 0) {
223b71206888 Initial import
thib
parents:
diff changeset
271 Mix_Volume(chn, 128);
223b71206888 Initial import
thib
parents:
diff changeset
272 Mix_PlayChannel(chn, play_chunk[chn],loop);
223b71206888 Initial import
thib
parents:
diff changeset
273 } else {
223b71206888 Initial import
thib
parents:
diff changeset
274 Mix_Volume(chn, 128);
223b71206888 Initial import
thib
parents:
diff changeset
275 Mix_FadeInChannel(chn, play_chunk[chn],loop,fadein_time);
223b71206888 Initial import
thib
parents:
diff changeset
276 }
223b71206888 Initial import
thib
parents:
diff changeset
277 return;
223b71206888 Initial import
thib
parents:
diff changeset
278 }
223b71206888 Initial import
thib
parents:
diff changeset
279
223b71206888 Initial import
thib
parents:
diff changeset
280 void MuSys::PlayKoe(const char* path) {
223b71206888 Initial import
thib
parents:
diff changeset
281 if (! pcm_enable) return;
223b71206888 Initial import
thib
parents:
diff changeset
282 static char* playing_koedata = 0;
223b71206888 Initial import
thib
parents:
diff changeset
283 int len = 0;
223b71206888 Initial import
thib
parents:
diff changeset
284 AvgKoeInfo koeinfo;
223b71206888 Initial import
thib
parents:
diff changeset
285 int chn = MIX_PCM_KOE;
223b71206888 Initial import
thib
parents:
diff changeset
286
223b71206888 Initial import
thib
parents:
diff changeset
287 Mix_Pause(chn);
223b71206888 Initial import
thib
parents:
diff changeset
288 Mix_HaltChannel(chn); // これで RWop が解放されるはず…
223b71206888 Initial import
thib
parents:
diff changeset
289 if (play_chunk[chn]) {
223b71206888 Initial import
thib
parents:
diff changeset
290 Mix_FreeChunk(play_chunk[chn]);
223b71206888 Initial import
thib
parents:
diff changeset
291 play_chunk[chn] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
292 }
223b71206888 Initial import
thib
parents:
diff changeset
293
223b71206888 Initial import
thib
parents:
diff changeset
294 if (playing_koedata) {
7
fa8511a21d05 Fixes somes memory leaks
thib
parents: 3
diff changeset
295 free(playing_koedata);
0
223b71206888 Initial import
thib
parents:
diff changeset
296 playing_koedata = 0;
223b71206888 Initial import
thib
parents:
diff changeset
297 }
223b71206888 Initial import
thib
parents:
diff changeset
298
223b71206888 Initial import
thib
parents:
diff changeset
299 koeinfo = OpenKoeFile(path);
223b71206888 Initial import
thib
parents:
diff changeset
300
223b71206888 Initial import
thib
parents:
diff changeset
301 if (koeinfo.stream == 0) return;
223b71206888 Initial import
thib
parents:
diff changeset
302 playing_koedata = decode_koe(koeinfo, &len);
223b71206888 Initial import
thib
parents:
diff changeset
303 fclose(koeinfo.stream);
223b71206888 Initial import
thib
parents:
diff changeset
304 if (playing_koedata == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
305 return;
223b71206888 Initial import
thib
parents:
diff changeset
306 }
223b71206888 Initial import
thib
parents:
diff changeset
307 Mix_Volume(chn, 128);
223b71206888 Initial import
thib
parents:
diff changeset
308 play_chunk[chn] = Mix_LoadWAV_RW(SDL_RWFromMem(playing_koedata, len+0x2c), 1);
223b71206888 Initial import
thib
parents:
diff changeset
309 Mix_PlayChannel(chn, play_chunk[chn],0);
223b71206888 Initial import
thib
parents:
diff changeset
310 return;
223b71206888 Initial import
thib
parents:
diff changeset
311 }
223b71206888 Initial import
thib
parents:
diff changeset
312 AvgKoeInfo OpenKoeFile(const char* path) {
223b71206888 Initial import
thib
parents:
diff changeset
313 int radix = 10000;
223b71206888 Initial import
thib
parents:
diff changeset
314 /* if (global_system.Version() >= 2) */ radix *= 10;
223b71206888 Initial import
thib
parents:
diff changeset
315 AvgKoeInfo info;
223b71206888 Initial import
thib
parents:
diff changeset
316 info.stream = 0; info.length = 0; info.offset = 0;
223b71206888 Initial import
thib
parents:
diff changeset
317 if (isdigit(path[0]) && strchr(path,'.') == 0) { // 数値 (拡張子等なし)
223b71206888 Initial import
thib
parents:
diff changeset
318 /* avg32 形式の音声アーカイブのキャッシュを検索 */
223b71206888 Initial import
thib
parents:
diff changeset
319 int pointer = atoi(path);
223b71206888 Initial import
thib
parents:
diff changeset
320 int file_no = pointer / radix;
223b71206888 Initial import
thib
parents:
diff changeset
321 int index = pointer % radix;
223b71206888 Initial import
thib
parents:
diff changeset
322 info = FindKoe(file_no, index);
223b71206888 Initial import
thib
parents:
diff changeset
323 } else { // ファイル
223b71206888 Initial import
thib
parents:
diff changeset
324 int length;
223b71206888 Initial import
thib
parents:
diff changeset
325 ARCINFO* arcinfo = file_searcher.Find(FILESEARCH::KOE,path,".WPD");
223b71206888 Initial import
thib
parents:
diff changeset
326 if (arcinfo == 0) return info;
223b71206888 Initial import
thib
parents:
diff changeset
327 info.stream = arcinfo->OpenFile(&length);
223b71206888 Initial import
thib
parents:
diff changeset
328 info.rate = 22050;
223b71206888 Initial import
thib
parents:
diff changeset
329 info.length = length;
223b71206888 Initial import
thib
parents:
diff changeset
330 info.offset = ftell(info.stream);
223b71206888 Initial import
thib
parents:
diff changeset
331 info.type = koe_unknown;
223b71206888 Initial import
thib
parents:
diff changeset
332 delete arcinfo;
223b71206888 Initial import
thib
parents:
diff changeset
333 }
223b71206888 Initial import
thib
parents:
diff changeset
334 return info;
223b71206888 Initial import
thib
parents:
diff changeset
335 }
223b71206888 Initial import
thib
parents:
diff changeset
336
223b71206888 Initial import
thib
parents:
diff changeset
337 static SDL_RWops* OpenSDLRW(const char* path) {
223b71206888 Initial import
thib
parents:
diff changeset
338 char cmdline_buf[1024];
223b71206888 Initial import
thib
parents:
diff changeset
339 /* まず wav ファイルを探す */
223b71206888 Initial import
thib
parents:
diff changeset
340 ARCINFO* info = file_searcher.Find(FILESEARCH::WAV,path,".wav");
223b71206888 Initial import
thib
parents:
diff changeset
341 if (info == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
342 info = file_searcher.Find(FILESEARCH::WAV,path,".nwa");
223b71206888 Initial import
thib
parents:
diff changeset
343 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"nwa");
223b71206888 Initial import
thib
parents:
diff changeset
344 if (info) { // read NWA file
223b71206888 Initial import
thib
parents:
diff changeset
345
223b71206888 Initial import
thib
parents:
diff changeset
346 static char* nwa_buffer = 0;
223b71206888 Initial import
thib
parents:
diff changeset
347 int dummy;
223b71206888 Initial import
thib
parents:
diff changeset
348 FILE* f = info->OpenFile(&dummy);
223b71206888 Initial import
thib
parents:
diff changeset
349 static char* d = 0;
223b71206888 Initial import
thib
parents:
diff changeset
350 int sz;
223b71206888 Initial import
thib
parents:
diff changeset
351 if (d != 0) delete[] d;
223b71206888 Initial import
thib
parents:
diff changeset
352 d = NWAFILE::ReadAll(f, sz);
223b71206888 Initial import
thib
parents:
diff changeset
353 return SDL_RWFromMem(d, sz);
223b71206888 Initial import
thib
parents:
diff changeset
354 }
223b71206888 Initial import
thib
parents:
diff changeset
355 }
223b71206888 Initial import
thib
parents:
diff changeset
356 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"wav");
223b71206888 Initial import
thib
parents:
diff changeset
357 if (info == 0) info = file_searcher.Find(FILESEARCH::WAV,path,".ogg");
223b71206888 Initial import
thib
parents:
diff changeset
358 if (info) {
223b71206888 Initial import
thib
parents:
diff changeset
359 int dummy;
223b71206888 Initial import
thib
parents:
diff changeset
360 FILE* f = info->OpenFile(&dummy);
223b71206888 Initial import
thib
parents:
diff changeset
361 delete info;
223b71206888 Initial import
thib
parents:
diff changeset
362 if (f == 0) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
363 SDL_RWops* op = SDL_RWFromFP(f, 1);
223b71206888 Initial import
thib
parents:
diff changeset
364 return op;
223b71206888 Initial import
thib
parents:
diff changeset
365 }
223b71206888 Initial import
thib
parents:
diff changeset
366 return 0;
223b71206888 Initial import
thib
parents:
diff changeset
367 }
223b71206888 Initial import
thib
parents:
diff changeset
368
223b71206888 Initial import
thib
parents:
diff changeset
369 static WAVFILE* OpenWaveFile(const char* path) {
223b71206888 Initial import
thib
parents:
diff changeset
370 char cmdline_buf[1024];
223b71206888 Initial import
thib
parents:
diff changeset
371 /* まず wav ファイルを探す */
223b71206888 Initial import
thib
parents:
diff changeset
372 ARCINFO* info = file_searcher.Find(FILESEARCH::WAV,path,".wav");
223b71206888 Initial import
thib
parents:
diff changeset
373 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"wav");
223b71206888 Initial import
thib
parents:
diff changeset
374 if (info) {
223b71206888 Initial import
thib
parents:
diff changeset
375 int size;
223b71206888 Initial import
thib
parents:
diff changeset
376 FILE* f = info->OpenFile(&size);
223b71206888 Initial import
thib
parents:
diff changeset
377 delete info;
223b71206888 Initial import
thib
parents:
diff changeset
378 if (f == 0) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
379 WAVFILE* w = WAVFILE::MakeConverter(new WAVFILE_Stream(f, size));
223b71206888 Initial import
thib
parents:
diff changeset
380 return w;
223b71206888 Initial import
thib
parents:
diff changeset
381 }
223b71206888 Initial import
thib
parents:
diff changeset
382 /* 次に nwa ファイル */
223b71206888 Initial import
thib
parents:
diff changeset
383 info = file_searcher.Find(FILESEARCH::WAV,path,".nwa");
223b71206888 Initial import
thib
parents:
diff changeset
384 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"nwa");
223b71206888 Initial import
thib
parents:
diff changeset
385 if (info) {
223b71206888 Initial import
thib
parents:
diff changeset
386 int size;
223b71206888 Initial import
thib
parents:
diff changeset
387 FILE* f = info->OpenFile(&size);
223b71206888 Initial import
thib
parents:
diff changeset
388 delete info;
223b71206888 Initial import
thib
parents:
diff changeset
389 if (f == 0) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
390 WAVFILE* w = WAVFILE::MakeConverter(new NWAFILE(f));
223b71206888 Initial import
thib
parents:
diff changeset
391 return w;
223b71206888 Initial import
thib
parents:
diff changeset
392 }
223b71206888 Initial import
thib
parents:
diff changeset
393
223b71206888 Initial import
thib
parents:
diff changeset
394 /* 次に mp3 ファイル */
223b71206888 Initial import
thib
parents:
diff changeset
395 info = file_searcher.Find(FILESEARCH::WAV,path,".mp3");
223b71206888 Initial import
thib
parents:
diff changeset
396 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"mp3");
223b71206888 Initial import
thib
parents:
diff changeset
397 if (info) {
223b71206888 Initial import
thib
parents:
diff changeset
398 int size;
223b71206888 Initial import
thib
parents:
diff changeset
399 FILE* f = info->OpenFile(&size);
223b71206888 Initial import
thib
parents:
diff changeset
400 delete info;
223b71206888 Initial import
thib
parents:
diff changeset
401 if (f == 0) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
402 MP3FILE* w = new MP3FILE(f, size);
223b71206888 Initial import
thib
parents:
diff changeset
403 if (w->pimpl) {
223b71206888 Initial import
thib
parents:
diff changeset
404 return WAVFILE::MakeConverter(w);
223b71206888 Initial import
thib
parents:
diff changeset
405 }
223b71206888 Initial import
thib
parents:
diff changeset
406 delete w;
223b71206888 Initial import
thib
parents:
diff changeset
407 }
223b71206888 Initial import
thib
parents:
diff changeset
408
223b71206888 Initial import
thib
parents:
diff changeset
409 /* 次に ogg ファイル */
223b71206888 Initial import
thib
parents:
diff changeset
410 info = file_searcher.Find(FILESEARCH::WAV,path,".ogg");
223b71206888 Initial import
thib
parents:
diff changeset
411 if (info == 0) info = file_searcher.Find(FILESEARCH::BGM,path,"ogg");
223b71206888 Initial import
thib
parents:
diff changeset
412 if (info) {
223b71206888 Initial import
thib
parents:
diff changeset
413 int size;
223b71206888 Initial import
thib
parents:
diff changeset
414 FILE* f = info->OpenFile(&size);
223b71206888 Initial import
thib
parents:
diff changeset
415 delete info;
223b71206888 Initial import
thib
parents:
diff changeset
416 if (f == 0) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
417 OggFILE* w = new OggFILE(f, size);
223b71206888 Initial import
thib
parents:
diff changeset
418 if (w->pimpl) {
223b71206888 Initial import
thib
parents:
diff changeset
419 return WAVFILE::MakeConverter(w);
223b71206888 Initial import
thib
parents:
diff changeset
420 }
223b71206888 Initial import
thib
parents:
diff changeset
421 delete w;
223b71206888 Initial import
thib
parents:
diff changeset
422 }
223b71206888 Initial import
thib
parents:
diff changeset
423 return 0;
223b71206888 Initial import
thib
parents:
diff changeset
424 }
223b71206888 Initial import
thib
parents:
diff changeset
425