forked from vesanieminen/VaadinAudioPlayer
/
AudioPlayer.java
485 lines (417 loc) · 15.3 KB
/
AudioPlayer.java
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package org.vaadin.addon.audio.server;
import com.vaadin.flow.component.*;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.polymertemplate.PolymerTemplate;
import com.vaadin.flow.server.StreamRegistration;
import com.vaadin.flow.server.StreamResource;
import com.vaadin.flow.server.StreamResourceRegistry;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.templatemodel.TemplateModel;
import elemental.json.Json;
import elemental.json.JsonArray;
import elemental.json.JsonObject;
import org.vaadin.addon.audio.server.state.PlaybackState;
import org.vaadin.addon.audio.server.state.StateChangeCallback;
import org.vaadin.addon.audio.server.state.VolumeChangeCallback;
import org.vaadin.addon.audio.server.util.StringFormatter;
import org.vaadin.addon.audio.shared.ChunkDescriptor;
import org.vaadin.addon.audio.shared.SharedEffect;
import org.vaadin.addon.audio.shared.util.Log;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
// This is the server-side UI component that provides public API for AudioPlayer
@SuppressWarnings("serial")
@Tag("audio-player")
@JsModule("./audio-player.js")
public class AudioPlayer extends PolymerTemplate<TemplateModel> {
private Stream stream = null;
private PlaybackState playbackState = PlaybackState.STOPPED;
private int currentPosition = 0;
private double volume = 1;
private double[] channelVolumes = new double[0];
private List<StreamRegistration> chunkRegistrations = new ArrayList<>();
// TODO: use a proper event system
private List<StateChangeCallback> stateCallbacks = new ArrayList<>();
private List<VolumeChangeCallback> volumeCallbacks = new ArrayList<>();
public int chunkTimeMillis;
public int numChunksPreload;
public int duration;
public int reportPositionRepeatTime = 500;
public final List<SharedEffect> effects = new ArrayList<SharedEffect>();
/**
* Create new AudioPlayer
*
* @param stream
* Stream to use
*/
public AudioPlayer(Stream stream) {
setupAudioPlayer(stream);
}
/**
* Create new AudioPlayer
*
* @param stream
* Stream to use
* @param reportPositionRepeatTime
* Define the interval for position reporting, default 500ms
*/
public AudioPlayer(Stream stream, int reportPositionRepeatTime) {
this.reportPositionRepeatTime = reportPositionRepeatTime;
setupAudioPlayer(stream);
}
private void setupAudioPlayer(Stream stream) {
// Register stream, set up chunk table in state
setStream(stream);
getElement().setProperty("reportPositionRepeatTime", reportPositionRepeatTime);
}
@ClientCallable
public void reportPlaybackPosition(int position_millis) {
Log.message(this,"received position report: " + position_millis);
if (position_millis != currentPosition) {
currentPosition = position_millis;
for (StateChangeCallback cb : stateCallbacks) {
cb.playbackPositionChanged(position_millis);
}
}
}
@ClientCallable
public void reportPlaybackStarted() {
Log.message(this, "received playback state change to PLAYING");
playbackState = PlaybackState.PLAYING;
for (StateChangeCallback cb : stateCallbacks) {
cb.playbackStateChanged(playbackState);
}
}
@ClientCallable
public void reportPlaybackPaused() {
Log.message(this, "received playback state change to PAUSED");
playbackState = PlaybackState.PAUSED;
for (StateChangeCallback cb : stateCallbacks) {
cb.playbackStateChanged(playbackState);
}
}
@ClientCallable
public void reportPlaybackStopped() {
Log.message(this, "received playback state change to STOPPED");
playbackState = PlaybackState.STOPPED;
for (StateChangeCallback cb : stateCallbacks) {
cb.playbackStateChanged(playbackState);
}
}
@ClientCallable
public void reportVolumeChange(double volume, double[] channelVolumes) {
Log.message(this, "volume change reported from client");
this.volume = volume;
this.channelVolumes = channelVolumes;
for (VolumeChangeCallback cb : volumeCallbacks) {
cb.onVolumeChange(volume, channelVolumes);
}
}
public void destroy() {
// ui.removeExtension(this);
}
/**
* Gets Stream object that supplies audio data to this AudioPlayer.
*
* @return Stream
*/
public Stream getStream() {
return stream;
}
public Stream setStream(Stream stream) {
unregisterStreamChunks();
this.stream = stream;
registerStreamChunks();
// TODO: prettify this verbose JSON serialization
JsonArray chunksJson = Json.createArray();
List<ChunkDescriptor> chunks = stream.getChunks();
for (int i = 0; i < chunks.size(); i++) {
ChunkDescriptor chunk = chunks.get(i);
JsonObject chunkDescriptor = Json.createObject();
chunkDescriptor.put("id", chunk.getId());
chunkDescriptor.put("startTimeOffset", chunk.getStartTimeOffset());
chunkDescriptor.put("endTimeOffset", chunk.getEndTimeOffset());
chunkDescriptor.put("leadInDuration", chunk.getLeadInDuration());
chunkDescriptor.put("leadOutDuration", chunk.getLeadOutDuration());
chunkDescriptor.put("overlapTime", chunk.getOverlapTime());
chunkDescriptor.put("startSampleOffset", chunk.getStartSampleOffset());
chunkDescriptor.put("endSampleOffset", chunk.getEndSampleOffset());
chunkDescriptor.put("url", chunk.getUrl().toASCIIString());
chunksJson.set(i, chunkDescriptor);
}
getElement().setPropertyJson("chunks", chunksJson);
duration = stream.getDuration();
getElement().setProperty("duration", duration);
chunkTimeMillis = stream.getChunkLength();
getElement().setProperty("chunkTimeMillis", chunkTimeMillis);
return stream;
}
private void unregisterStreamChunks() {
for (int i = 0; i < chunkRegistrations.size(); i++) {
chunkRegistrations.get(i).unregister();
}
chunkRegistrations.clear();
}
private void registerStreamChunks() {
if (this.stream == null) {
return;
}
List<ChunkDescriptor> chunks = this.stream.getChunks();
for (int i = 0; i < chunks.size(); i++) {
registerChunkResource(chunks.get(i));
}
}
@Override
protected void onAttach(AttachEvent attachEvent) {
if (this.stream != null && this.chunkRegistrations.isEmpty()) {
registerStreamChunks();
}
}
@Override
protected void onDetach(DetachEvent detachEvent) {
unregisterStreamChunks();
}
private void registerChunkResource(ChunkDescriptor chunk) {
StreamResource resource = new StreamResource("audio",
(OutputStream outputStream, VaadinSession session) -> stream.getChunkData(chunk, bytes -> {
try {
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
Log.message(this, "could not register audio chunk with id: " + chunk.getId());
}
}));
StreamResourceRegistry registry = UI.getCurrent().getSession().getResourceRegistry();
StreamRegistration registration = registry.registerResource(resource);
chunkRegistrations.add(registration);
chunk.setUrl(registration.getResourceUri());
}
/**
* Gets current audio files total duration in milliseconds.
*
* @return int milliseconds
*/
public int getDuration() {
return stream.getDuration();
}
/**
* Gets current audio players time position.
*
* @return int milliseconds
*/
public int getPosition() {
return currentPosition;
}
public void setPosition(int millis) {
if (millis == currentPosition) {
// Avoid setting the same position again loopback
return;
}
currentPosition = millis;
getElement().callFunction("setPlaybackPosition", millis);
Log.message(AudioPlayer.this, "set playback position: " + millis);
}
/**
* Moves play position by milliseconds.
*
* @param millis
* number of milliseconds to move
*/
public void skip(int millis) {
Log.message(AudioPlayer.this, "skip " + millis + " milliseconds");
}
/**
* Starts playing audio from the beginning of the audio file.
*/
public void play() {
getElement().callFunction("startPlayback");
Log.message(AudioPlayer.this, "start or restart playback");
}
/**
* Starts playing audio from the specified position (milliseconds).
* NOT IMPLEMENTED.
*
* @param offset_millis
* start position in milliseconds
*/
public void play(int offset_millis) {
Log.message(AudioPlayer.this, "start playback at time offset");
}
/**
* Pauses the current audio.
*/
public void pause() {
getElement().callFunction("pausePlayback");
Log.message(AudioPlayer.this, "pause playback");
}
/**
* Plays audio from last known position (usually used to play while paused).
*/
public void resume() {
getElement().callFunction("resumePlayback");
Log.message(AudioPlayer.this, "resume playback");
}
/**
* Stops playing the audio and resets the position to 0 (beginning of audio file).
*/
public void stop() {
getElement().callFunction("stopPlayback");
Log.message(AudioPlayer.this, "stop playback");
}
public boolean isPlaying() {
return playbackState == PlaybackState.PLAYING;
}
public boolean isPaused() {
return playbackState == PlaybackState.PAUSED;
}
public boolean isStopped() {
return playbackState == PlaybackState.STOPPED;
}
/**
* Sets the volume of the audio player. 1 is 100% volume (default), 2 is 200% volume, etc.
*
* @param volume
* volume level
*/
public void setVolume(double volume) {
getElement().callFunction("setVolume", volume);
Log.message(AudioPlayer.this, "setting volume to " + volume);
}
public void setVolumeOnChannel(double volume, int channel) {
getElement().callFunction("setVolumeOnChannel", volume, channel);
Log.message(AudioPlayer.this, "setting volume to " + volume + " on channel " + channel);
}
public double getVolume() {
return volume;
}
public double getVolumeOnChannel(int channel) {
if (channelVolumes.length > channel) {
return channelVolumes[channel];
}
return -1;
}
public double getNumberOfChannels() {
return channelVolumes.length;
}
/**
* Sets the speed at which the audio is played. Changing this will not change
* the pitch of the audio. 1 is 100% speed (default), 2 is 200%, etc.
*
* @param playbackSpeed
* speed ratio
*/
public void setPlaybackSpeed(double playbackSpeed) {
getElement().callFunction("setPlaybackSpeed", playbackSpeed);
Log.message(AudioPlayer.this, "setting playback speed to " + playbackSpeed);
}
/**
* Sets the spread of total gain (volume) between the left and right channels.
* -1 is to only play left channel.
* 0 is to play equally left and right channels (default).
* 1 is to only play right channel.
*
* @param balance
*/
public void setBalance(double balance) {
getElement().callFunction("setBalance", balance);
}
/**
* Sets the number of audio chunks that are loaded ahead of the current playing audio chunk.
*
* @param numChunksPreload
*/
public void setNumberChunksToPreload(int numChunksPreload) {
numChunksPreload = numChunksPreload;
}
/**
* Gets number of chunks to load each time audio chunks are requested.
*
* @return number of chunks
*/
public int getNumberChunksToPreload() {
return numChunksPreload;
}
protected ChunkDescriptor getChunkDescriptor(int chunkId) {
// TODO: return chunk descriptor
return null;
}
/**
* Gets String representing current player time position.
*
* @return String
*/
public String getPositionString() {
return StringFormatter.msToPlayerTimeStamp(getPosition());
}
/**
* Gets String representing current player's total time duration.
*
* @return String
*/
public String getDurationString() {
return StringFormatter.msToPlayerTimeStamp(getDuration());
}
// =========================================================================
// === Effects =============================================================
// =========================================================================
/**
* Add effect immediately to the audio player.
*
* @param effect
* Effect to add
*/
public void addEffect(Effect effect) {
// TODO: update effect if it already exists
effects.add(effect.getSharedEffectObject());
}
/**
* Removes effect immediately from audio player.
*
* @param effect
* Effect to remove
*/
public void removeEffect(Effect effect) {
// TODO: optimize removing effects so we don't have to loop
for (SharedEffect e : effects) {
if (effect.getID().equals(e.getID())) {
Log.message(AudioPlayer.this, "removing effect: " + e.getName().name());
effects.remove(e);
}
}
}
/**
* Updates properties of the effect and passes the changes to the client side.
*
* @param effect
* Effect to update
*/
public void updateEffect(Effect effect) {
for (SharedEffect e : effects) {
if (effect.getID().equals(e.getID())) {
Log.message(AudioPlayer.this, "updating effect: " + e.getName().name());
e.setProperties(effect.getSharedEffectObject().getProperties());
}
}
}
// =========================================================================
// === Listeners ===========================================================
// =========================================================================
public void addStateChangeListener(StateChangeCallback cb) {
stateCallbacks.add(cb);
}
public void removeStateChangeListener(StateChangeCallback cb) {
stateCallbacks.remove(cb);
}
public void addValueChangeListener(VolumeChangeCallback cb) {
volumeCallbacks.add(cb);
}
public void removeValueChangeListener(VolumeChangeCallback cb) {
volumeCallbacks.remove(cb);
}
// =========================================================================
// =========================================================================
// =========================================================================
}