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.iosrobovm; 18 19 import org.robovm.apple.foundation.NSArray; 20 21 import com.badlogic.gdx.Gdx; 22 import com.badlogic.gdx.audio.Sound; 23 import com.badlogic.gdx.backends.iosrobovm.objectal.ALBuffer; 24 import com.badlogic.gdx.backends.iosrobovm.objectal.ALChannelSource; 25 import com.badlogic.gdx.backends.iosrobovm.objectal.ALSource; 26 import com.badlogic.gdx.backends.iosrobovm.objectal.OALSimpleAudio; 27 import com.badlogic.gdx.files.FileHandle; 28 import com.badlogic.gdx.utils.IntArray; 29 30 /** @author tescott 31 * @author Tomski 32 * 33 * First pass at implementing OALSimpleAudio support. */ 34 public class IOSSound implements Sound { 35 36 private ALBuffer soundBuffer; 37 private String soundPath; 38 39 private ALChannelSource channel; 40 private NSArray<ALSource> sourcePool; 41 private IntArray streamIds = new IntArray(8); 42 43 public IOSSound (FileHandle filePath) { 44 soundPath = filePath.file().getPath().replace('\\', '/'); 45 soundBuffer = OALSimpleAudio.sharedInstance().preloadEffect(soundPath); 46 channel = OALSimpleAudio.sharedInstance().getChannelSource(); 47 sourcePool = channel.getSourcePool().getSources(); 48 } 49 50 @Override 51 public long play () { 52 return play(1, 1, 0, false); 53 } 54 55 @Override 56 public long play (float volume) { 57 return play(volume, 1, 0, false); 58 } 59 60 @Override 61 public long play (float volume, float pitch, float pan) { 62 return play(volume, pitch, pan, false); 63 } 64 65 public long play (float volume, float pitch, float pan, boolean loop) { 66 if (streamIds.size == 8) streamIds.pop(); 67 ALSource soundSource = OALSimpleAudio.sharedInstance().playBuffer(soundBuffer, volume, pitch, pan, loop); 68 if (soundSource == null) return -1; 69 if (soundSource.getSourceId() == -1) return -1; 70 streamIds.insert(0, soundSource.getSourceId()); 71 return soundSource.getSourceId(); 72 } 73 74 @Override 75 public long loop () { 76 return play(1, 1, 0, true); 77 } 78 79 @Override 80 public long loop (float volume) { 81 return play(volume, 1, 0, true); 82 } 83 84 @Override 85 public long loop (float volume, float pitch, float pan) { 86 return play(volume, pitch, pan, true); 87 } 88 89 @Override 90 public void stop () { 91 ALSource source; 92 for (int i = 0; i < streamIds.size; i++) { 93 if ((source = getSoundSource(streamIds.get(i))) != null) source.stop(); 94 } 95 } 96 97 @Override 98 public void dispose () { 99 stop(); 100 soundBuffer.dispose(); 101 } 102 103 @Override 104 public void stop (long soundId) { 105 ALSource source; 106 if ((source = getSoundSource(soundId)) != null) source.stop(); 107 } 108 109 @Override 110 public void setLooping (long soundId, boolean looping) { 111 ALSource source; 112 if ((source = getSoundSource(soundId)) != null) source.setLooping(looping); 113 } 114 115 @Override 116 public void setPitch (long soundId, float pitch) { 117 ALSource source; 118 if ((source = getSoundSource(soundId)) != null) source.setPitch(pitch); 119 } 120 121 @Override 122 public void setVolume (long soundId, float volume) { 123 ALSource source; 124 if ((source = getSoundSource(soundId)) != null) source.setVolume(volume); 125 } 126 127 @Override 128 public void setPan (long soundId, float pan, float volume) { 129 ALSource source; 130 if ((source = getSoundSource(soundId)) != null) { 131 source.setPan(pan); 132 source.setVolume(volume); 133 } 134 } 135 136 @Override 137 public void pause () { 138 ALSource source; 139 for (int i = 0; i < streamIds.size; i++) { 140 if ((source = getSoundSource(streamIds.get(i))) != null) source.setPaused(true); 141 } 142 } 143 144 @Override 145 public void resume () { 146 ALSource source; 147 for (int i = 0; i < streamIds.size; i++) { 148 if ((source = getSoundSource(streamIds.get(i))) != null) source.setPaused(false); 149 } 150 } 151 152 @Override 153 public void pause (long soundId) { 154 ALSource source; 155 if ((source = getSoundSource(soundId)) != null) source.setPaused(true); 156 } 157 158 @Override 159 public void resume (long soundId) { 160 ALSource source; 161 if ((source = getSoundSource(soundId)) != null) source.setPaused(false); 162 } 163 164 private ALSource getSoundSource (long soundId) { 165 for (ALSource source : sourcePool) { 166 if (source.getSourceId() == soundId) return source; 167 } 168 return null; 169 } 170 } 171