Search in sources :

Example 6 with AudioFile

use of blue.soundObject.AudioFile in project blue by kunstmusik.

the class ScoreTimelineDropTargetListener method drop.

/*
     * (non-Javadoc)
     * 
     * @see java.awt.dnd.DropTargetListener#drop(java.awt.dnd.DropTargetDropEvent)
     */
@Override
public void drop(DropTargetDropEvent dtde) {
    try {
        Transferable tr = dtde.getTransferable();
        if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            dtde.acceptDrop(DnDConstants.ACTION_LINK);
            List list = (List) tr.getTransferData(DataFlavor.javaFileListFlavor);
            if (list.size() != 1) {
                dtde.dropComplete(false);
                return;
            }
            String s = list.get(0).toString().trim();
            if (!s.toLowerCase().endsWith("wav") && !s.toLowerCase().endsWith("aif") && !s.toLowerCase().endsWith("aiff")) {
                dtde.dropComplete(false);
                return;
            }
            String sObjName = s.substring(s.lastIndexOf(File.separator) + 1);
            Point p = dtde.getLocation();
            int index = sTimeCanvas.pObj.getLayerNumForY(p.y);
            AudioFile af = new AudioFile();
            af.setName(sObjName);
            af.setSoundFileName(BlueSystem.getRelativePath(s));
            PolyObject pObj = sTimeCanvas.getPolyObject();
            float startTime = (float) p.x / timeState.getPixelSecond();
            float dur = SoundFileUtilities.getDurationInSeconds(s);
            af.setStartTime(startTime);
            af.setSubjectiveDuration(dur);
            pObj.addSoundObject(index, af);
            dtde.dropComplete(true);
            return;
        } else if (dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            dtde.acceptDrop(DnDConstants.ACTION_LINK);
            String str = (String) tr.getTransferData(DataFlavor.stringFlavor);
            if (!str.startsWith("file://")) {
                dtde.dropComplete(false);
                return;
            }
            str = str.substring(7).trim();
            if (!str.toLowerCase().endsWith("wav") && !str.toLowerCase().endsWith("aif") && !str.toLowerCase().endsWith("aiff")) {
                System.err.println("Could not open file: " + str);
                dtde.dropComplete(false);
                return;
            }
            str = URLDecoder.decode(str);
            str = str.replaceAll(" ", "\\ ");
            File f = new File(str);
            if (!f.exists()) {
                dtde.dropComplete(false);
                return;
            }
            String sObjName = str.substring(str.lastIndexOf(File.separator) + 1);
            Point p = dtde.getLocation();
            int index = sTimeCanvas.pObj.getLayerNumForY(p.y);
            AudioFile af = new AudioFile();
            af.setName(sObjName);
            af.setSoundFileName(str);
            PolyObject pObj = sTimeCanvas.getPolyObject();
            float startTime = (float) p.x / timeState.getPixelSecond();
            float dur = SoundFileUtilities.getDurationInSeconds(str);
            af.setStartTime(startTime);
            af.setSubjectiveDuration(dur);
            pObj.addSoundObject(index, af);
            dtde.dropComplete(true);
            return;
        }
        dtde.rejectDrop();
    } catch (UnsupportedFlavorException | IOException | UnsupportedAudioFileException e) {
        e.printStackTrace();
        dtde.rejectDrop();
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) Point(java.awt.Point) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) Point(java.awt.Point) AudioFile(blue.soundObject.AudioFile) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) List(java.util.List) AudioFile(blue.soundObject.AudioFile) File(java.io.File) PolyObject(blue.soundObject.PolyObject)

Aggregations

AudioFile (blue.soundObject.AudioFile)6 PolyObject (blue.soundObject.PolyObject)3 File (java.io.File)3 SoundObject (blue.soundObject.SoundObject)2 AudioWaveformData (blue.ui.utilities.audio.AudioWaveformData)1 AudioWaveformListener (blue.ui.utilities.audio.AudioWaveformListener)1 Point (java.awt.Point)1 Transferable (java.awt.datatransfer.Transferable)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 IOException (java.io.IOException)1 List (java.util.List)1 AudioFileFormat (javax.sound.sampled.AudioFileFormat)1 AudioFormat (javax.sound.sampled.AudioFormat)1 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)1