• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.android;
18 
19 import android.media.AudioManager;
20 import android.media.SoundPool;
21 
22 import com.badlogic.gdx.audio.Sound;
23 import com.badlogic.gdx.utils.IntArray;
24 
25 final class AndroidSound implements Sound {
26 	final SoundPool soundPool;
27 	final AudioManager manager;
28 	final int soundId;
29 	final IntArray streamIds = new IntArray(8);
30 
AndroidSound(SoundPool pool, AudioManager manager, int soundId)31 	AndroidSound (SoundPool pool, AudioManager manager, int soundId) {
32 		this.soundPool = pool;
33 		this.manager = manager;
34 		this.soundId = soundId;
35 	}
36 
37 	@Override
dispose()38 	public void dispose () {
39 		soundPool.unload(soundId);
40 	}
41 
42 	@Override
play()43 	public long play () {
44 		return play(1);
45 	}
46 
47 	@Override
play(float volume)48 	public long play (float volume) {
49 		if (streamIds.size == 8) streamIds.pop();
50 		int streamId = soundPool.play(soundId, volume, volume, 1, 0, 1);
51 		// standardise error code with other backends
52 		if (streamId == 0) return -1;
53 		streamIds.insert(0, streamId);
54 		return streamId;
55 	}
56 
stop()57 	public void stop () {
58 		for (int i = 0, n = streamIds.size; i < n; i++)
59 			soundPool.stop(streamIds.get(i));
60 	}
61 
62 	@Override
stop(long soundId)63 	public void stop (long soundId) {
64 		soundPool.stop((int)soundId);
65 	}
66 
67 	@Override
pause()68 	public void pause () {
69 		soundPool.autoPause();
70 	}
71 
72 	@Override
pause(long soundId)73 	public void pause (long soundId) {
74 		soundPool.pause((int)soundId);
75 	}
76 
77 	@Override
resume()78 	public void resume () {
79 		soundPool.autoResume();
80 	}
81 
82 	@Override
resume(long soundId)83 	public void resume (long soundId) {
84 		soundPool.resume((int)soundId);
85 	}
86 
87 	@Override
setPitch(long soundId, float pitch)88 	public void setPitch (long soundId, float pitch) {
89 		soundPool.setRate((int)soundId, pitch);
90 	}
91 
92 	@Override
setVolume(long soundId, float volume)93 	public void setVolume (long soundId, float volume) {
94 		soundPool.setVolume((int)soundId, volume, volume);
95 	}
96 
97 	@Override
loop()98 	public long loop () {
99 		return loop(1);
100 	}
101 
102 	@Override
loop(float volume)103 	public long loop (float volume) {
104 		if (streamIds.size == 8) streamIds.pop();
105 		int streamId = soundPool.play(soundId, volume, volume, 1, -1, 1);
106 		// standardise error code with other backends
107 		if (streamId == 0) return -1;
108 		streamIds.insert(0, streamId);
109 		return streamId;
110 	}
111 
112 	@Override
setLooping(long soundId, boolean looping)113 	public void setLooping (long soundId, boolean looping) {
114 		soundPool.setLoop((int)soundId, looping ? -1 : 0);
115 	}
116 
117 	@Override
setPan(long soundId, float pan, float volume)118 	public void setPan (long soundId, float pan, float volume) {
119 		float leftVolume = volume;
120 		float rightVolume = volume;
121 
122 		if (pan < 0) {
123 			rightVolume *= (1 - Math.abs(pan));
124 		} else if (pan > 0) {
125 			leftVolume *= (1 - Math.abs(pan));
126 		}
127 
128 		soundPool.setVolume((int)soundId, leftVolume, rightVolume);
129 	}
130 
131 	@Override
play(float volume, float pitch, float pan)132 	public long play (float volume, float pitch, float pan) {
133 		if (streamIds.size == 8) streamIds.pop();
134 		float leftVolume = volume;
135 		float rightVolume = volume;
136 		if (pan < 0) {
137 			rightVolume *= (1 - Math.abs(pan));
138 		} else if (pan > 0) {
139 			leftVolume *= (1 - Math.abs(pan));
140 		}
141 		int streamId = soundPool.play(soundId, leftVolume, rightVolume, 1, 0, pitch);
142 		// standardise error code with other backends
143 		if (streamId == 0) return -1;
144 		streamIds.insert(0, streamId);
145 		return streamId;
146 	}
147 
148 	@Override
loop(float volume, float pitch, float pan)149 	public long loop (float volume, float pitch, float pan) {
150 		if (streamIds.size == 8) streamIds.pop();
151 		float leftVolume = volume;
152 		float rightVolume = volume;
153 		if (pan < 0) {
154 			rightVolume *= (1 - Math.abs(pan));
155 		} else if (pan > 0) {
156 			leftVolume *= (1 - Math.abs(pan));
157 		}
158 		int streamId = soundPool.play(soundId, leftVolume, rightVolume, 1, -1, pitch);
159 		// standardise error code with other backends
160 		if (streamId == 0) return -1;
161 		streamIds.insert(0, streamId);
162 		return streamId;
163 	}
164 }
165