forked from kivy/python-for-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenPySound.java
More file actions
298 lines (231 loc) · 7.26 KB
/
RenPySound.java
File metadata and controls
298 lines (231 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package org.renpy.android;
import android.media.MediaPlayer;
import android.util.Log;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
public class RenPySound {
private static class Channel implements MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener {
// MediaPlayers for the currently playing and queued up
// sounds.
MediaPlayer player[];
// Filenames for the currently playing and queued up sounds.
String filename[];
// Is the corresponding player prepareD?
boolean prepared[];
// The volume for the left and right channel.
double volume;
double secondary_volume;
double left_volume;
double right_volume;
Channel() {
player = new MediaPlayer[2];
filename = new String[2];
prepared = new boolean[2];
player[0] = new MediaPlayer();
player[1] = new MediaPlayer();
volume = 1.0;
secondary_volume = 1.0;
left_volume = 1.0;
right_volume = 1.0;
}
/**
* Queue up a sound file.
*/
synchronized void queue(String fn, String real_fn, long base, long length) {
MediaPlayer mp = player[1];
mp.reset();
try {
FileInputStream f = new FileInputStream(real_fn);
if (length >= 0) {
mp.setDataSource(f.getFD(), base, length);
} else {
mp.setDataSource(f.getFD());
}
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
mp.prepareAsync();
f.close();
} catch (IOException e) {
Log.w("RenPySound", e);
return;
}
filename[1] = fn;
}
/**
* Play the queued-up sound.
*/
synchronized void play() {
MediaPlayer tmp;
player[0].reset();
tmp = player[0];
player[0] = player[1];
player[1] = tmp;
filename[0] = filename[1];
filename[1] = null;
prepared[0] = prepared[1];
prepared[1] = false;
if (filename[0] != null) {
updateVolume();
if (prepared[0]) {
player[0].start();
}
}
}
/**
* Stop playback on this channel.
*/
synchronized void stop() {
player[0].reset();
player[1].reset();
filename[0] = null;
filename[1] = null;
prepared[0] = false;
prepared[1] = false;
}
/**
* Dequeue the queued file on this channel.
*/
synchronized void dequeue() {
player[1].reset();
filename[1] = null;
prepared[1] = false;
}
/**
* Updates the volume on the playing file.
*/
synchronized void updateVolume() {
player[0].setVolume((float) (volume * secondary_volume * left_volume),
(float) (volume * secondary_volume * right_volume));
}
/**
* Called to update the volume.
*/
synchronized void set_volume(float v) {
volume = v;
updateVolume();
}
/**
* Called to update the volume.
*/
synchronized void set_secondary_volume(float v) {
secondary_volume = v;
updateVolume();
}
/**
* Called to update the pan. (By setting up the fractional
* volume.)
*/
synchronized void set_pan(float pan) {
if (pan < 0) {
left_volume = 1.0;
right_volume = 1.0 + pan;
} else {
left_volume = 1.0 - pan;
right_volume = 1.0;
}
updateVolume();
}
synchronized void pause() {
if (filename[0] != null) {
player[0].pause();
}
}
synchronized void unpause() {
if (filename[0] != null) {
player[0].start();
}
}
synchronized public void onPrepared(MediaPlayer mp) {
if (mp == player[0]) {
prepared[0] = true;
player[0].start();
}
if (mp == player[1]) {
prepared[1] = true;
}
}
/**
* Called on completion.
*/
synchronized public void onCompletion(MediaPlayer mp) {
if (mp == player[0]) {
play();
}
}
}
// A map from channel number to channel object.
static HashMap<Integer, Channel> channels = new HashMap<Integer, Channel>();
/**
* Gets the Channel object for the numbered channel, returning a
* new channel object as necessary.
*/
static Channel getChannel(int num) {
Channel rv = channels.get(num);
if (rv == null) {
rv = new Channel();
channels.put(num, rv);
}
return rv;
}
static void queue(int channel, String filename, String real_fn, long base, long length) {
Channel c = getChannel(channel);
c.queue(filename, real_fn, base, length);
if (c.filename[0] == null) {
c.play();
}
}
static void play(int channel,
String filename,
String real_fn,
long base,
long length) {
Channel c = getChannel(channel);
c.queue(filename, real_fn, base, length);
c.play();
}
static void stop(int channel) {
Channel c = getChannel(channel);
c.stop();
}
static void dequeue(int channel) {
Channel c = getChannel(channel);
c.dequeue();
}
static String playing_name(int channel) {
Channel c = getChannel(channel);
if (c.filename[0] == null) {
return "";
}
return c.filename[0];
}
static int queue_depth(int channel) {
Channel c = getChannel(channel);
if (c.filename[0] == null) return 0;
if (c.filename[1] == null) return 1;
return 2;
}
static void set_volume(int channel, float v) {
Channel c = getChannel(channel);
c.set_volume(v);
}
static void set_secondary_volume(int channel, float v) {
Channel c = getChannel(channel);
c.set_secondary_volume(v);
}
static void set_pan(int channel, float pan) {
Channel c = getChannel(channel);
c.set_pan(pan);
}
static void pause(int channel) {
Channel c = getChannel(channel);
c.pause();
}
static void unpause(int channel) {
Channel c = getChannel(channel);
c.unpause();
}
static {
new MediaPlayer();
}
}