1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.backends.gwt; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.audio.Sound; 21 import com.badlogic.gdx.backends.gwt.soundmanager2.SMSound; 22 import com.badlogic.gdx.backends.gwt.soundmanager2.SMSoundOptions; 23 import com.badlogic.gdx.backends.gwt.soundmanager2.SoundManager; 24 import com.badlogic.gdx.files.FileHandle; 25 import com.badlogic.gdx.utils.Array; 26 import com.badlogic.gdx.utils.BooleanArray; 27 28 public class GwtSound implements Sound { 29 30 /** The maximum number of sound instances to create to support simultaneous playback. */ 31 private static final int MAX_SOUNDS = 8; 32 33 /** Our sounds. */ 34 private GwtMusic[] sounds; 35 /** The next player we think should be available for play - we circle through them to find a free one. */ 36 private int soundIndex; 37 /** The path to the sound file. */ 38 private FileHandle soundFile; 39 GwtSound(FileHandle file)40 public GwtSound (FileHandle file) { 41 soundFile = file; 42 sounds = new GwtMusic[MAX_SOUNDS]; 43 sounds[0] = new GwtMusic(file); 44 soundIndex = 0; 45 } 46 47 /** Let's find a sound that isn't currently playing. 48 * @return The index of the sound or -1 if none is available. */ findAvailableSound()49 private int findAvailableSound() { 50 for (int i = 0; i < sounds.length; i++) { 51 int index = (soundIndex + i) % sounds.length; 52 if (sounds[index] == null || !sounds[index].isPlaying()) { 53 // point to the next likely free player 54 soundIndex = (index + 1) % sounds.length; 55 56 // return the free player 57 return index; 58 } 59 } 60 61 // all are busy playing, stop the next sound in the queue and reuse it 62 int index = soundIndex % sounds.length; 63 soundIndex = (index + 1) % sounds.length; 64 return index; 65 } 66 67 @Override play()68 public long play () { 69 return play(1.0f, 1.0f, 0.0f, false); 70 } 71 72 @Override play(float volume)73 public long play (float volume) { 74 return play(volume, 1.0f, 0.0f, false); 75 } 76 77 @Override play(float volume, float pitch, float pan)78 public long play (float volume, float pitch, float pan) { 79 return play(volume, pitch, pan, false); 80 } 81 play(float volume, float pitch, float pan, boolean loop)82 private long play (float volume, float pitch, float pan, boolean loop) { 83 int soundId = findAvailableSound(); 84 if (soundId >= 0) { 85 GwtMusic sound; 86 if (sounds[soundId] == null) { 87 sounds[soundId] = new GwtMusic(soundFile); 88 } 89 sound = sounds[soundId]; 90 sound.stop(); 91 sound.setPan(pan, volume); 92 sound.setLooping(loop); 93 sound.play(); 94 } 95 return soundId; 96 } 97 98 @Override loop()99 public long loop () { 100 return play(1.0f, 1.0f, 0.0f, true); 101 } 102 103 @Override loop(float volume)104 public long loop (float volume) { 105 return play(volume, 1.0f, 0.0f, true); 106 } 107 108 @Override loop(float volume, float pitch, float pan)109 public long loop (float volume, float pitch, float pan) { 110 return play(volume, pitch, pan, true); 111 } 112 113 @Override stop()114 public void stop () { 115 for (int i = 0; i < sounds.length; i++) { 116 if (sounds[i] != null) 117 sounds[i].stop(); 118 } 119 } 120 121 @Override dispose()122 public void dispose () { 123 stop(); 124 for (int i = 0; i < sounds.length; i++) { 125 if (sounds[i] != null) 126 sounds[i].dispose(); 127 } 128 sounds = null; 129 } 130 131 @Override stop(long soundId)132 public void stop (long soundId) { 133 if (soundId >= 0 && sounds[(int)soundId] != null) 134 sounds[(int)soundId].stop(); 135 } 136 137 @Override pause()138 public void pause () { 139 for (int i = 0; i < sounds.length; i++) { 140 if (sounds[i] != null) 141 sounds[i].pause(); 142 } 143 } 144 145 @Override pause(long soundId)146 public void pause (long soundId) { 147 if (soundId >= 0 && sounds[(int)soundId] != null) 148 sounds[(int)soundId].pause(); 149 } 150 151 @Override resume()152 public void resume () { 153 for (int i = 0; i < sounds.length; i++) { 154 if (sounds[i] != null) 155 sounds[i].resume(); 156 } 157 } 158 159 @Override resume(long soundId)160 public void resume (long soundId) { 161 if (soundId >= 0 && sounds[(int)soundId] != null) 162 sounds[(int)soundId].resume(); 163 } 164 165 @Override setLooping(long soundId, boolean looping)166 public void setLooping (long soundId, boolean looping) { 167 if (soundId >= 0 && sounds[(int)soundId] != null) 168 sounds[(int)soundId].setLooping(looping); 169 } 170 171 @Override setPitch(long soundId, float pitch)172 public void setPitch (long soundId, float pitch) { 173 // FIXME - Not possible? 174 } 175 176 @Override setVolume(long soundId, float volume)177 public void setVolume (long soundId, float volume) { 178 if (soundId >= 0 && sounds[(int)soundId] != null) 179 sounds[(int)soundId].setVolume(volume); 180 } 181 182 @Override setPan(long soundId, float pan, float volume)183 public void setPan (long soundId, float pan, float volume) { 184 if (soundId >= 0 && sounds[(int)soundId] != null) { 185 sounds[(int)soundId].setPan(pan, volume); 186 } 187 } 188 }