Changeset 552 in josm


Ignore:
Timestamp:
Feb 20, 2008 6:53:10 PM (5 years ago)
Author:
david
Message:

audio synchronization for tracks without waypoints

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/gpx/WayPoint.java

    r444 r552  
    33 
    44package org.openstreetmap.josm.data.gpx; 
     5 
     6import java.text.ParsePosition; 
     7import java.text.SimpleDateFormat; 
     8import java.util.Date; 
    59 
    610import org.openstreetmap.josm.Main; 
     
    2226                return "WayPoint (" + (attr.containsKey("name") ? attr.get("name") + ", " :"") + latlon.toString() + ", " + attr + ")"; 
    2327        } 
     28         
     29        /** 
     30         * convert the time stamp of ther waypoint into seconds from the epoch 
     31         * @return seconds 
     32         */ 
     33        public double time () { 
     34                if (! attr.containsKey("time")) 
     35                        return 0.0; 
     36                SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); // ignore timezone 
     37                Date d = f.parse(attr.get("time").toString(), new ParsePosition(0)); 
     38                if (d == null /* failed to parse */) 
     39                        return 0.0; 
     40                return d.getTime() / 1000.0; /* ms => seconds */ 
     41        } 
     42 
    2443} 
  • trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java

    r547 r552  
    1111import java.awt.GridBagLayout; 
    1212import java.awt.Point; 
     13import java.util.ArrayList; 
    1314import java.util.Collection; 
    1415import java.util.LinkedList; 
     
    5758import org.openstreetmap.josm.gui.dialogs.LayerListDialog; 
    5859import org.openstreetmap.josm.gui.dialogs.LayerListPopup; 
     60import org.openstreetmap.josm.gui.layer.markerlayer.AudioMarker; 
    5961import org.openstreetmap.josm.gui.layer.markerlayer.Marker; 
    6062import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer; 
     
    140142                markersFromNamedTrackpoints.addActionListener(new ActionListener() { 
    141143                        public void actionPerformed(ActionEvent e) { 
    142 /* 
    143                                 public Collection<GpxTrack> tracks = new LinkedList<GpxTrack>(); 
    144                                 public Collection<Collection<WayPoint>> trackSegs 
    145                                 = new LinkedList<Collection<WayPoint>>(); 
    146                                 */ 
    147144                                GpxData namedTrackPoints = new GpxData(); 
    148145                                for (GpxTrack track : data.tracks) 
     
    156153                        Main.main.addLayer(ml); 
    157154                    } 
     155                        } 
     156                }); 
     157 
     158                JMenuItem applyAudio = new JMenuItem(tr("Make Sampled Audio Layer"), ImageProvider.get("applyaudio")); 
     159                applyAudio.addActionListener(new ActionListener() { 
     160                        public void actionPerformed(ActionEvent e) { 
     161                                JFileChooser fc = new JFileChooser(Main.pref.get("tagimages.lastdirectory")); 
     162                                fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 
     163                                fc.setAcceptAllFileFilterUsed(false); 
     164                                fc.setFileFilter(new FileFilter(){ 
     165                                        @Override public boolean accept(File f) { 
     166                                                return f.isDirectory() || f.getName().toLowerCase().endsWith(".wav"); 
     167                                        } 
     168                                        @Override public String getDescription() { 
     169                                                return tr("Wave Audio files (*.wav)"); 
     170                                        } 
     171                                }); 
     172                                fc.showOpenDialog(Main.parent); 
     173                                File sel = fc.getSelectedFile(); 
     174                                if (sel == null) 
     175                                        return; 
     176                                applyAudio(sel); 
     177                                Main.map.repaint(); 
    158178                        } 
    159179                }); 
     
    216236                                line, 
    217237                                tagimage, 
     238                                applyAudio, 
    218239                                markersFromNamedTrackpoints, 
    219240                                new JMenuItem(new ConvertToDataLayerAction()), 
     
    456477        } 
    457478 
     479        /** 
     480         *  
     481         * 
     482         */ 
     483        private void applyAudio(File wavFile) { 
     484                String uri = "file:".concat(wavFile.getAbsolutePath()); 
     485            double audioGapSecs = 15.0;  
     486                try { 
     487                        audioGapSecs = Double.parseDouble(Main.pref.get("marker.audiosampleminsecs", Double.toString(audioGapSecs))); 
     488                } catch (NumberFormatException e) { 
     489                } 
     490            double audioGapMetres = 75.0;  
     491                try { 
     492                        audioGapMetres = Double.parseDouble(Main.pref.get("marker.audiosampleminmetres", Double.toString(audioGapMetres))); 
     493                } catch (NumberFormatException e) { 
     494                } 
     495                double audioGapRadians = (audioGapMetres / 40041455.0 /* circumference of Earth in metres */) * 2.0 * Math.PI; 
     496                double audioGapRadiansSquared = audioGapRadians * audioGapRadians; 
     497                double firstTime = -1.0; 
     498            double prevOffset = - (audioGapSecs + 1.0); // first point always exceeds time difference 
     499            WayPoint prevPoint = null; 
     500 
     501            MarkerLayer ml = new MarkerLayer(new GpxData(), tr("Sampled audio markers from {0}", name), associatedFile); 
     502             
     503                for (GpxTrack track : data.tracks) { 
     504                        for (Collection<WayPoint> seg : track.trackSegs) { 
     505                                for (WayPoint point : seg) { 
     506                                        double time = point.time(); 
     507                                        if (firstTime < 0.0) 
     508                                                firstTime = time; 
     509                                        double offset = time - firstTime; 
     510                                        if (prevPoint == null || 
     511                                                (offset - prevOffset > audioGapSecs && 
     512                                                /* note: distance is misleading: it actually gives distance _squared_ */ 
     513                                                point.eastNorth.distance(prevPoint.eastNorth) > audioGapRadiansSquared)) 
     514                                        { 
     515                                                String markerName; 
     516                                                int wholeSeconds = (int)(offset + 0.5); 
     517                                                if (wholeSeconds < 60) 
     518                                                        markerName = Integer.toString(wholeSeconds); 
     519                                                else if (wholeSeconds < 3600) 
     520                                                        markerName = String.format("%d:%02d", wholeSeconds / 60, wholeSeconds % 60); 
     521                                                else 
     522                                                        markerName = String.format("%d:%02d:%02d", wholeSeconds / 3600, (wholeSeconds % 3600)/60, wholeSeconds % 60); 
     523                                                AudioMarker am = AudioMarker.create(point.latlon, markerName, uri, offset); 
     524                                                ml.data.add(am); 
     525                                                prevPoint = point; 
     526                                                prevOffset = offset; 
     527                                        } 
     528                                } 
     529                        } 
     530                } 
     531 
     532        if (ml.data.size() > 0) { 
     533                Main.main.addLayer(ml); 
     534        } 
     535        } 
    458536} 
  • trunk/src/org/openstreetmap/josm/gui/layer/markerlayer/ButtonMarker.java

    r547 r552  
    6161                        b.paintBorder(mv, g, r.x, r.y, r.width, r.height); 
    6262                } 
    63                 if ((text != null) && (show.equalsIgnoreCase("show")) && Main.pref.getBoolean("marker.buttonlabels")) 
     63                if ((text != null) && (show.equalsIgnoreCase("show")) && Main.pref.getBoolean("marker.buttonlabels", true)) 
    6464                        g.drawString(text, screen.x+4, screen.y+2); 
    6565        } 
  • trunk/src/org/openstreetmap/josm/gui/layer/markerlayer/MarkerLayer.java

    r551 r552  
    1717import java.util.Collection; 
    1818import java.util.Iterator; 
    19 import java.util.Date; 
    20 import java.text.SimpleDateFormat; 
    21 import java.text.ParsePosition; 
    22 import java.text.ParseException; 
    2319import java.net.URL; 
    2420 
     
    7066                this.associatedFile = associatedFile; 
    7167                this.data = new ArrayList<Marker>(); 
    72                 double offset = 0.0; 
    73                 Date firstDate = null; 
     68                double firstTime = -1.0; 
    7469 
    7570                for (WayPoint wpt : indata.waypoints) { 
    7671                        /* calculate time differences in waypoints */ 
    77                         if (wpt.attr.containsKey("time")) { 
    78                                 SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); // ignore timezone, as it is all relative 
    79                                 Date d = f.parse(wpt.attr.get("time").toString(), new ParsePosition(0)); 
    80                                 if (d == null /* failed to parse */) { 
    81                                         offset = 0.0; 
    82                                 } else if (firstDate == null) { 
    83                                         firstDate = d; 
    84                                         offset = 0.0; 
    85                                 } else { 
    86                                         offset = (d.getTime() - firstDate.getTime()) / 1000.0; /* ms => seconds */ 
    87                                 } 
    88                         } 
    89                          
    90             Marker m = Marker.createMarker(wpt, indata.storageFile, offset); 
     72                        double time = wpt.time(); 
     73                        if (firstTime < 0) 
     74                                firstTime = time; 
     75            Marker m = Marker.createMarker(wpt, indata.storageFile, time - firstTime); 
    9176            if (m != null) 
    9277                data.add(m); 
Note: See TracChangeset for help on using the changeset viewer.