< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/SoftJitterCorrector.java

Print this page




   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.media.sound;
  26 
  27 import sun.misc.ManagedLocalsThread;
  28 
  29 import javax.sound.sampled.AudioFormat;
  30 import javax.sound.sampled.AudioInputStream;
  31 import java.io.EOFException;
  32 import java.io.IOException;
  33 import java.io.InputStream;
  34 
  35 /**
  36  * A jitter corrector to be used with SoftAudioPusher.
  37  *
  38  * @author Karl Helgason
  39  */
  40 public final class SoftJitterCorrector extends AudioInputStream {
  41 
  42     private static class JitterStream extends InputStream {
  43 
  44         static int MAX_BUFFER_SIZE = 1048576;
  45         boolean active = true;
  46         Thread thread;
  47         AudioInputStream stream;
  48         // Cyclic buffer


 199                         }
 200 
 201                         if (correction > 0) {
 202                             correction--;
 203                             next = System.nanoTime() + nanos;
 204                             continue;
 205                         }
 206                         long wait = next - System.nanoTime();
 207                         if (wait > 0) {
 208                             try {
 209                                 Thread.sleep(wait / 1000000L);
 210                             } catch (InterruptedException e) {
 211                                 //e.printStackTrace();
 212                             }
 213                         }
 214                         next += nanos;
 215                     }
 216                 }
 217             };
 218 
 219             thread = new ManagedLocalsThread(runnable);
 220             thread.setDaemon(true);
 221             thread.setPriority(Thread.MAX_PRIORITY);
 222             thread.start();
 223         }
 224 
 225         public void close() throws IOException {
 226             synchronized (this) {
 227                 active = false;
 228             }
 229             try {
 230                 thread.join();
 231             } catch (InterruptedException e) {
 232                 //e.printStackTrace();
 233             }
 234             stream.close();
 235         }
 236 
 237         public int read() throws IOException {
 238             byte[] b = new byte[1];
 239             if (read(b) == -1)




   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.media.sound;
  26 


  27 import javax.sound.sampled.AudioFormat;
  28 import javax.sound.sampled.AudioInputStream;
  29 import java.io.EOFException;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 
  33 /**
  34  * A jitter corrector to be used with SoftAudioPusher.
  35  *
  36  * @author Karl Helgason
  37  */
  38 public final class SoftJitterCorrector extends AudioInputStream {
  39 
  40     private static class JitterStream extends InputStream {
  41 
  42         static int MAX_BUFFER_SIZE = 1048576;
  43         boolean active = true;
  44         Thread thread;
  45         AudioInputStream stream;
  46         // Cyclic buffer


 197                         }
 198 
 199                         if (correction > 0) {
 200                             correction--;
 201                             next = System.nanoTime() + nanos;
 202                             continue;
 203                         }
 204                         long wait = next - System.nanoTime();
 205                         if (wait > 0) {
 206                             try {
 207                                 Thread.sleep(wait / 1000000L);
 208                             } catch (InterruptedException e) {
 209                                 //e.printStackTrace();
 210                             }
 211                         }
 212                         next += nanos;
 213                     }
 214                 }
 215             };
 216 
 217             thread = new Thread(null, runnable, "JitterCorrector", 0, false);
 218             thread.setDaemon(true);
 219             thread.setPriority(Thread.MAX_PRIORITY);
 220             thread.start();
 221         }
 222 
 223         public void close() throws IOException {
 224             synchronized (this) {
 225                 active = false;
 226             }
 227             try {
 228                 thread.join();
 229             } catch (InterruptedException e) {
 230                 //e.printStackTrace();
 231             }
 232             stream.close();
 233         }
 234 
 235         public int read() throws IOException {
 236             byte[] b = new byte[1];
 237             if (read(b) == -1)


< prev index next >