You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ap...@apache.org on 2008/01/31 12:18:03 UTC

svn commit: r617103 - /harmony/enhanced/jdktools/trunk/modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/ViewerAudioClip.java

Author: apetrenko
Date: Thu Jan 31 03:17:59 2008
New Revision: 617103

URL: http://svn.apache.org/viewvc?rev=617103&view=rev
Log:
Patch for HARMONY-5444 "[jdktools][appletviewer] appletviewer doesn't 
play sound tracks"

Modified:
    harmony/enhanced/jdktools/trunk/modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/ViewerAudioClip.java

Modified: harmony/enhanced/jdktools/trunk/modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/ViewerAudioClip.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/jdktools/trunk/modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/ViewerAudioClip.java?rev=617103&r1=617102&r2=617103&view=diff
==============================================================================
--- harmony/enhanced/jdktools/trunk/modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/ViewerAudioClip.java (original)
+++ harmony/enhanced/jdktools/trunk/modules/tools/src/main/java/org/apache/harmony/tools/appletviewer/ViewerAudioClip.java Thu Jan 31 03:17:59 2008
@@ -20,61 +20,191 @@
 import java.applet.AudioClip;
 import java.net.URL;
 
-import javax.sound.midi.MidiSystem;
-import javax.sound.midi.Receiver;
-import javax.sound.midi.Sequence;
-import javax.sound.midi.Sequencer;
-import javax.sound.midi.Synthesizer;
-import javax.sound.midi.Transmitter;
+import javax.sound.sampled.AudioFormat;
+import javax.sound.sampled.AudioInputStream;
+import javax.sound.sampled.AudioSystem;
+import javax.sound.sampled.DataLine;
+import javax.sound.sampled.SourceDataLine;
 
 class ViewerAudioClip implements AudioClip {
-    //private Clip clip;
-    private Sequencer sequencer;
-    
+
+    private ClipImpl clip;
+
     public ViewerAudioClip(URL url) {
+        AudioFormat af = null;
+        AudioInputStream ais = null;
+        SourceDataLine line = null;
         try {
-            sequencer = MidiSystem.getSequencer();
-            sequencer.open();
-            
-            Sequence sequence = MidiSystem.getSequence(url);
-            sequencer.setSequence(sequence);
-            
-            if (!(sequencer instanceof Synthesizer)) {
-                Synthesizer synthesizer = MidiSystem.getSynthesizer();
-                synthesizer.open();
-                Receiver receiver = synthesizer.getReceiver();
-                Transmitter transmitter = sequencer.getTransmitter();
-                transmitter.setReceiver(receiver);
-            }
+            ais = AudioSystem.getAudioInputStream(url);
         } catch (Exception e) {
-            sequencer = null;
+            System.out.println(url.toString());
+            e.printStackTrace();
+            ais = null;
+            return;
         }
-//      try {
-//          this.clip = AudioSystem.getClip();
-//          clip.open(AudioSystem.getAudioInputStream(url));
-//      } catch (Exception e) {
-//          this.clip = null;
-//      }       
+        af = ais.getFormat();
+        DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
+
+        boolean isSupported = AudioSystem.isLineSupported(info);
+        if (!isSupported){
+            AudioFormat tf = new AudioFormat(
+                AudioFormat.Encoding.PCM_SIGNED,
+                af.getSampleRate(),
+                16,
+                af.getChannels(),
+                af.getChannels() << 1,
+                af.getSampleRate(),
+                false);
+                ais = AudioSystem.getAudioInputStream(tf, ais);
+                af = ais.getFormat();
+                info = new DataLine.Info(SourceDataLine.class, af);
+        }
+        try{
+            line = (SourceDataLine) AudioSystem.getLine(info);
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+
+        clip = new ClipImpl(af, ais, line);
+
     }
 
     public void loop() {
-        if (sequencer != null)
-            sequencer.start();
-//      if (clip != null) 
-//          clip.loop(Clip.LOOP_CONTINUOUSLY);
+        if(clip != null)
+            clip.loop();
     }
 
     public void play() {
-        if (sequencer != null)
-            sequencer.start();
-//      if (clip != null) 
-//          clip.start();       
+        if(clip != null)
+            clip.play();
     }
 
     public void stop() {
-        if (sequencer != null)
-            sequencer.stop();
-//      if (clip != null) 
-//          clip.stop();
+        if(clip != null)
+            clip.stop();
+    }
+
+    private static class ClipImpl implements AudioClip, Runnable {
+
+        static final int BufferSize = 1024;
+        static final int UNLIMITED = -1;
+
+        AudioFormat af;
+        AudioInputStream ais;
+        SourceDataLine line;
+        Thread clip;
+        boolean started;
+        int streamLength;
+        int count;
+
+        ClipImpl(AudioFormat af, AudioInputStream ais, SourceDataLine line){
+            this.af = af;
+            this.ais = ais;
+            this.line = line;
+
+            if(ais.getFrameLength() == AudioSystem.NOT_SPECIFIED ||
+                af.getFrameSize() == AudioSystem.NOT_SPECIFIED){
+
+                streamLength = -1;
+            }
+
+            long length = ais.getFrameLength() * af.getFrameSize();
+
+            if(length > Integer.MAX_VALUE){
+                streamLength = -1;
+            }
+
+            streamLength = (int)length;
+            clip = new Thread(this);
+        }
+
+        public void run(){
+            if(streamLength < 0) return;
+
+            started = true;
+
+            int bytesRead = 0;
+            byte[] data = new byte[BufferSize];
+
+            try{
+                line.open(af);
+            }catch (Exception e){
+                e.printStackTrace();
+            }
+
+            // Main cycle
+
+            while(true){
+ 
+                line.start();
+
+                do{
+
+                    ais.mark(streamLength);
+                    bytesRead = 0;
+                    while (bytesRead != -1){
+                        try{
+                            bytesRead = ais.read(data, 0, data.length);
+                        }catch (Exception e){
+                            e.printStackTrace();
+                        }
+
+                        if (bytesRead >= 0){
+                            line.write(data, 0, bytesRead);
+                        }
+                    }
+
+                    try{
+                        ais.reset();
+                    }catch (Exception e){
+                        e.printStackTrace();
+                    }
+                }while(count < 0 || --count > 0);
+
+                synchronized(clip){
+                    try{
+                        clip.wait();
+                    }catch (Exception e){
+                        e.printStackTrace();
+                    }
+                }
+
+            }
+        }
+
+        public void play(){
+            if(!started) clip.start();
+            synchronized(this){
+                count = 1;
+                synchronized(clip){
+                    clip.notify();
+                }
+            }
+        }
+
+        public void loop(){
+            if(!started) clip.start();
+            synchronized(this){
+                count = UNLIMITED;
+                synchronized(clip){
+                    clip.notify();
+                }
+            }
+        }
+
+        public void stop(){
+            synchronized(this){
+                line.stop();
+                count = 1;
+            }
+        }
+
+        protected void finalize(){
+            if(line != null && line.isOpen()){
+                line.drain();
+                line.close();
+            }
+        }
+
     }
 }