| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
package tsukuba_bunko.peko.canvas.stage; |
| 20 |
|
|
| 21 |
|
import java.io.Serializable; |
| 22 |
|
|
| 23 |
|
import java.net.URL; |
| 24 |
|
|
| 25 |
|
import java.util.Map; |
| 26 |
|
|
| 27 |
|
import tsukuba_bunko.peko.Logger; |
| 28 |
|
|
| 29 |
|
import tsukuba_bunko.peko.resource.ResourceManager; |
| 30 |
|
|
| 31 |
|
import tsukuba_bunko.peko.canvas.stage.audio.SampledAudioClip; |
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
public class AudioPlayer implements Serializable { |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
private static final long serialVersionUID = 7799979848147586424L; |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
public static final int STOP_IMMEDIATELY = 0; |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
public static final int STOP_WITH_SYNC_FADEOUT = 1; |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
public static final int STOP_WITH_ASYNC_FADEOUT = 2; |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
0 |
transient private Map _clips = new java.util.HashMap( 17 ); |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
0 |
private Map _bgmRegistry = new java.util.HashMap( 17 ); |
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
0 |
private Map _seRegistry = new java.util.HashMap( 17 ); |
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
public AudioPlayer() |
| 83 |
|
{ |
| 84 |
0 |
super(); |
| 85 |
0 |
} |
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
public void playBGM( String id, String clipName, boolean loop ) |
| 95 |
|
{ |
| 96 |
0 |
ResourceManager resources = ResourceManager.getInstance(); |
| 97 |
|
try { |
| 98 |
0 |
if( _clips.containsKey(id) ) { |
| 99 |
0 |
Logger.error( "[canvas.stage] specified bgm clip id was already used. :" + id ); |
| 100 |
0 |
return; |
| 101 |
|
} |
| 102 |
0 |
_bgmRegistry.put( id, new Object[]{clipName, (loop?Boolean.TRUE:Boolean.FALSE)} ); |
| 103 |
0 |
URL clipURL = new URL( resources.getLocationResources().getSoundsDirecotryURL(), clipName ); |
| 104 |
0 |
playClip( id, clipURL, loop ); |
| 105 |
|
} |
| 106 |
0 |
catch( Exception e ) { |
| 107 |
0 |
Logger.error( "[canvas.stage] fail to play bgm :" + clipName, e ); |
| 108 |
0 |
} |
| 109 |
0 |
} |
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
public void playSE( String id, String clipName, boolean loop ) |
| 118 |
|
{ |
| 119 |
0 |
ResourceManager resources = ResourceManager.getInstance(); |
| 120 |
|
try { |
| 121 |
0 |
if( _seRegistry.containsKey(id) ) { |
| 122 |
0 |
Object[] info = (Object[])_seRegistry.get( id ); |
| 123 |
0 |
if( info[1] == Boolean.FALSE ) { |
| 124 |
0 |
stop( id, AudioPlayer.STOP_IMMEDIATELY ); |
| 125 |
0 |
} |
| 126 |
|
else { |
| 127 |
0 |
Logger.error( "[canvas.stage] specified se clip id was already used. :" + id ); |
| 128 |
0 |
return; |
| 129 |
|
} |
| 130 |
|
} |
| 131 |
0 |
_seRegistry.put( id, new Object[]{clipName, (loop?Boolean.TRUE:Boolean.FALSE)} ); |
| 132 |
0 |
URL clipURL = new URL( resources.getLocationResources().getSoundsDirecotryURL(), clipName ); |
| 133 |
0 |
playClip( id, clipURL, loop ); |
| 134 |
|
} |
| 135 |
0 |
catch( Exception e ) { |
| 136 |
0 |
Logger.error( "[canvas.stage] fail to play bgm :" + clipName, e ); |
| 137 |
0 |
} |
| 138 |
0 |
} |
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
protected void playClip( String id, URL clipURL, boolean loop ) |
| 147 |
|
{ |
| 148 |
|
try { |
| 149 |
0 |
AudioClip clip = getAudioClip( id, clipURL ); |
| 150 |
0 |
if( clip != null ) { |
| 151 |
0 |
register( clip ); |
| 152 |
0 |
clip.play( loop ); |
| 153 |
|
} |
| 154 |
|
} |
| 155 |
0 |
catch( Exception e ) { |
| 156 |
0 |
Logger.error( "[canvas.stage] fail to play clip. :" + clipURL, e ); |
| 157 |
0 |
} |
| 158 |
0 |
} |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
protected void register( AudioClip clip ) |
| 165 |
|
{ |
| 166 |
0 |
clip.setAudioPlayer( this ); |
| 167 |
0 |
_clips.put( clip.getID(), clip ); |
| 168 |
0 |
Logger.debug( "[canvas.stage] registered clip :" + clip.getID() ); |
| 169 |
0 |
} |
| 170 |
|
|
| 171 |
|
|
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
protected void unregister( AudioClip clip ) |
| 176 |
|
{ |
| 177 |
0 |
_clips.remove( clip.getID() ); |
| 178 |
0 |
_bgmRegistry.remove( clip.getID() ); |
| 179 |
0 |
_seRegistry.remove( clip.getID() ); |
| 180 |
0 |
Logger.debug( "[canvas.stage] unregistered clip :" + clip.getID() ); |
| 181 |
0 |
} |
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
|
| 186 |
|
|
| 187 |
|
public void stop( String id ) |
| 188 |
|
{ |
| 189 |
0 |
stop( id, AudioPlayer.STOP_WITH_ASYNC_FADEOUT ); |
| 190 |
0 |
} |
| 191 |
|
|
| 192 |
|
|
| 193 |
|
|
| 194 |
|
|
| 195 |
|
|
| 196 |
|
|
| 197 |
|
public void stop( String id, int mode ) |
| 198 |
|
{ |
| 199 |
0 |
AudioClip clip = (AudioClip)_clips.get( id ); |
| 200 |
0 |
if( clip != null ) { |
| 201 |
0 |
clip.stop( mode ); |
| 202 |
|
} |
| 203 |
0 |
} |
| 204 |
|
|
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
|
public void stopAll() |
| 209 |
|
{ |
| 210 |
0 |
AudioClip[] clips = (AudioClip[])_clips.values().toArray( new AudioClip[_clips.size()] ); |
| 211 |
0 |
for( int i = 0; i < clips.length; ++i ) { |
| 212 |
0 |
clips[i].stop(); |
| 213 |
|
} |
| 214 |
0 |
} |
| 215 |
|
|
| 216 |
|
|
| 217 |
|
|
| 218 |
|
|
| 219 |
|
|
| 220 |
|
|
| 221 |
|
protected AudioClip getAudioClip( String id, URL clipURL ) |
| 222 |
|
{ |
| 223 |
0 |
String clipName = clipURL.getFile().toLowerCase(); |
| 224 |
0 |
if( clipName.endsWith(".mid") || clipName.equals(".smf") ) { |
| 225 |
0 |
Logger.error( "[canvas.stage] unsupported format :" + clipURL ); |
| 226 |
0 |
return null; |
| 227 |
|
} |
| 228 |
0 |
else if( clipName.endsWith( "mp3") || clipName.endsWith(".wav") || clipName.endsWith(".au") ) { |
| 229 |
0 |
return new SampledAudioClip( id, clipURL ); |
| 230 |
|
} |
| 231 |
|
else { |
| 232 |
0 |
Logger.error( "[canvas.stage] unsupported format :" + clipURL ); |
| 233 |
0 |
return null; |
| 234 |
|
} |
| 235 |
|
} |
| 236 |
|
|
| 237 |
|
|
| 238 |
|
|
| 239 |
|
|
| 240 |
|
|
| 241 |
|
public void prepare() |
| 242 |
|
{ |
| 243 |
0 |
if( _clips == null ) { |
| 244 |
0 |
Logger.debug( "[cavnas.stage] initialize clips." ); |
| 245 |
0 |
_clips = new java.util.HashMap( 17 ); |
| 246 |
|
} |
| 247 |
|
|
| 248 |
0 |
String[] id = null; |
| 249 |
0 |
Object[] clip = null; |
| 250 |
|
|
| 251 |
0 |
Map registry = _bgmRegistry; |
| 252 |
0 |
if( registry.size() > 0 ) { |
| 253 |
0 |
id = (String[])registry.keySet().toArray( new String[registry.size()] ); |
| 254 |
0 |
for( int i = 0; i < id.length; ++i ) { |
| 255 |
0 |
clip = (Object[])registry.get( id[i] ); |
| 256 |
0 |
playBGM( id[i], (String)clip[0], ((Boolean)clip[1]).booleanValue() ); |
| 257 |
|
} |
| 258 |
|
} |
| 259 |
|
|
| 260 |
0 |
registry = _seRegistry; |
| 261 |
0 |
if( registry.size() > 0 ) { |
| 262 |
0 |
id = (String[])registry.keySet().toArray( new String[registry.size()] ); |
| 263 |
0 |
for( int i = 0; i < id.length; ++i ) { |
| 264 |
0 |
clip = (Object[])registry.get( id[i] ); |
| 265 |
0 |
playSE( id[i], (String)clip[0], ((Boolean)clip[1]).booleanValue() ); |
| 266 |
|
} |
| 267 |
|
} |
| 268 |
0 |
} |
| 269 |
|
} |