1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.markerlayer;
|
---|
3 |
|
---|
4 | import java.awt.event.ActionEvent;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.net.URL;
|
---|
7 | import java.util.Collections;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
10 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
---|
11 | import org.openstreetmap.josm.data.gpx.GpxLink;
|
---|
12 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
13 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
14 | import org.openstreetmap.josm.io.audio.AudioPlayer;
|
---|
15 | import org.openstreetmap.josm.io.audio.AudioUtil;
|
---|
16 | import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Marker class with audio playback capability.
|
---|
20 | *
|
---|
21 | * @author Frederik Ramm
|
---|
22 | *
|
---|
23 | */
|
---|
24 | public class AudioMarker extends ButtonMarker {
|
---|
25 |
|
---|
26 | private final URL audioUrl;
|
---|
27 | private static volatile AudioMarker recentlyPlayedMarker;
|
---|
28 | public double syncOffset;
|
---|
29 | public boolean timeFromAudio; // as opposed to from the GPX track
|
---|
30 |
|
---|
31 | public AudioMarker(LatLon ll, TemplateEngineDataProvider dataProvider, URL audioUrl, MarkerLayer parentLayer, double time, double offset) {
|
---|
32 | super(ll, dataProvider, /* ICON(markers/) */ "speech", parentLayer, time, offset);
|
---|
33 | this.audioUrl = audioUrl;
|
---|
34 | this.syncOffset = 0.0;
|
---|
35 | this.timeFromAudio = false;
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | public void actionPerformed(ActionEvent ev) {
|
---|
40 | play();
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Returns the marker played the most recently, if any.
|
---|
45 | * @return the marker played the most recently, or {@code null}
|
---|
46 | */
|
---|
47 | public static AudioMarker recentlyPlayedMarker() {
|
---|
48 | return recentlyPlayedMarker;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Forgets the marker played the most recently, if any.
|
---|
53 | */
|
---|
54 | static void resetRecentlyPlayedMarker() {
|
---|
55 | recentlyPlayedMarker = null;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public URL url() {
|
---|
59 | return audioUrl;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Starts playing the audio associated with the marker offset by the given amount
|
---|
64 | * @param after : seconds after marker where playing should start
|
---|
65 | */
|
---|
66 | public void play(double after) {
|
---|
67 | try {
|
---|
68 | // first enable tracing the audio along the track
|
---|
69 | MainApplication.getMap().mapView.playHeadMarker.animate();
|
---|
70 |
|
---|
71 | AudioPlayer.play(audioUrl, offset + syncOffset + after);
|
---|
72 | recentlyPlayedMarker = this;
|
---|
73 | } catch (IOException | InterruptedException e) {
|
---|
74 | AudioUtil.audioMalfunction(e);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Starts playing the audio associated with the marker: used in response to pressing
|
---|
80 | * the marker as well as indirectly
|
---|
81 | *
|
---|
82 | */
|
---|
83 | public void play() {
|
---|
84 | play(0.0);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void adjustOffset(double adjustment) {
|
---|
88 | syncOffset = adjustment; // added to offset may turn out negative, but that's ok
|
---|
89 | }
|
---|
90 |
|
---|
91 | public double syncOffset() {
|
---|
92 | return syncOffset;
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | protected TemplateEntryProperty getTextTemplate() {
|
---|
97 | return TemplateEntryProperty.forAudioMarker(parentLayer.getName());
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public WayPoint convertToWayPoint() {
|
---|
102 | WayPoint wpt = super.convertToWayPoint();
|
---|
103 | GpxLink link = new GpxLink(audioUrl.toString());
|
---|
104 | link.type = "audio";
|
---|
105 | wpt.put(GpxConstants.META_LINKS, Collections.singleton(link));
|
---|
106 | wpt.addExtension("offset", Double.toString(offset));
|
---|
107 | wpt.addExtension("sync-offset", Double.toString(syncOffset));
|
---|
108 | return wpt;
|
---|
109 | }
|
---|
110 | }
|
---|