Package javax.sound.sampled

Examples of javax.sound.sampled.SourceDataLine


  public static final float SAMPLE_RATE = 44100F;

  public static void play() {
    byte[] buf = new byte[1];
   
    SourceDataLine sdl = null;
    try {
    AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, false);
    sdl = AudioSystem.getSourceDataLine(af);
    sdl = AudioSystem.getSourceDataLine(af);
    sdl.open(af);
    sdl.start();
    for (int i = 0; i < (int)SAMPLE_RATE; ++i) {
      double angle = i / (SAMPLE_RATE / 440) * 2.0 * Math.PI;
      buf[0] = (byte) (Math.sin(angle) * 128);
      sdl.write(buf, 0, 1);
    }
    } catch (LineUnavailableException e) {
      //no-op
    } finally {
      if (sdl != null) {
        sdl.drain();
        sdl.stop();
        sdl.close();
      }
    }
  }
View Full Code Here


            AudioInputStream ais = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, fis);
            AudioFormat af = ais.getFormat();
            //System.out.println("AudioFormat: " + af.toString());

            SourceDataLine line = AudioSystem.getSourceDataLine(af);

            line.open(af);
            int bufSize = line.getBufferSize();

            line.start();

            byte[] data = new byte[bufSize];
            int bytesRead;

            while ((bytesRead = ais.read(data,0,data.length)) != -1) {
                line.write(data,0,bytesRead);
            }

            line.drain();
            line.stop();
            line.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
View Full Code Here

            format.getFrameRate(),
            true );
    }

    private SourceDataLine createDataLine(AudioFormat format) throws Exception {
        SourceDataLine dataLine;
        // -- output line not instantiated yet --
        // -- can we find a suitable kind of line? --
        DataLine.Info outInfo = new DataLine.Info( SourceDataLine.class, format );
        if ( !AudioSystem.isLineSupported( outInfo ) ) {
            throw new Exception( "Line matching " + outInfo + " not supported." );
        }

        // -- open the source data line (the output line) --
        dataLine = (SourceDataLine) AudioSystem.getLine( outInfo );
        dataLine.open( format, 50000 );
        dataLine.start();
        return dataLine;
    }
View Full Code Here

Debug.println("���������������� output PCM to file: " + pcmFileName);
os = new BufferedOutputStream(new FileOutputStream(pcmFileName));
}

            DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
            SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
            line.open(audioFormat);
            line.start();
FloatControl gainControl = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
double gain = .2d; // number between 0 and 1 (loudest)
float dB = (float) (Math.log(gain) / Math.log(10.0) * 20.0);
gainControl.setValue(dB);
            byte[] buf = new byte[1024];
            while (iss[0].available() > 0) {
                if (channels == 1) {
                    int l = iss[0].read(buf, 0, 1024);
//Debug.dump(buf, 64);
                    line.write(buf, 0, l);
if (os != null) {
os.write(buf, 0, l);
}
                } else {
                    int lL = iss[0].read(buf, 0, 512);
                    /*int lR = */iss[1].read(buf, 512, 512);
//System.err.println("l : " + lL + ", r: " + lR);
                    for (int i = 0; i < lL / 2; i++) {
                        byte[] temp = new byte[4];
                        temp[0] = buf[i * 2];
                        temp[1] = buf[i * 2 + 1];
                        temp[2] = buf[512 + i * 2];
                        temp[3] = buf[512 + i * 2 + 1];
                        line.write(temp, 0, 4);
                    }
                }
            }
            line.drain();
            line.stop();
            line.close();
if (os != null) {
os.flush();
os.close();
}
        } catch (IOException e) {
View Full Code Here

System.err.println("available: " + is.available());
os = new BufferedOutputStream(new FileOutputStream(args[1]));
}

        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
        SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
        line.open(format);
        line.start();
        byte[] buf = new byte[1024];
        int l = 0;
FloatControl gainControl = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
double gain = .2d; // number between 0 and 1 (loudest)
float dB = (float) (Math.log(gain) / Math.log(10.0) * 20.0);
gainControl.setValue(dB);

        while (is.available() > 0) {
            l = is.read(buf, 0, 1024);
            line.write(buf, 0, l);
if (os != null) {
os.write(buf, 0, l);
}
        }
        line.drain();
        line.stop();
        line.close();
if (os != null) {
os.close();
}
        if (!isTest) {
            System.exit(0);
View Full Code Here

System.err.println("available: " + is.available());
os = new BufferedOutputStream(new FileOutputStream(args[1]));
}

        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
        SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
        line.open(format);
        line.start();
        byte[] buf = new byte[1024];
        int l = 0;
FloatControl gainControl = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
double gain = .2d; // number between 0 and 1 (loudest)
float dB = (float) (Math.log(gain) / Math.log(10.0) * 20.0);
gainControl.setValue(dB);

        while (is.available() > 0) {
            l = is.read(buf, 0, 1024);
            line.write(buf, 0, l);
if (os != null) {
os.write(buf, 0, l);
}
        }
        line.drain();
        line.stop();
        line.close();
if (os != null) {
os.close();
}
        if (!isTest) {
            System.exit(0);
View Full Code Here

//  OutputStream os =
//   new BufferedOutputStream(new FileOutputStream(args[1]));

        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
        line.open(audioFormat);
        line.addLineListener(new LineListener() {
            public void update(LineEvent ev) {
Debug.println(ev.getType());
            if (LineEvent.Type.STOP == ev.getType()) {
                System.exit(0);
            }
            }
        });
        line.start();
        byte[] buf = new byte[1024];
        int l = 0;

        while (is.available() > 0) {
            l = is.read(buf, 0, 1024);
            line.write(buf, 0, l);
//  os.write(buf, 0, l);
        }
        line.drain();
        line.stop();
        line.close();
//  os.close();
    }
View Full Code Here

     * @param audioInputStream
     * @param o Options object for failure reporting.
     */
    public static void playSound(AudioInputStream audioInputStream, Options o) {
        final AudioFormat format = audioInputStream.getFormat();
        SourceDataLine auline = null;
        final DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        try {
            auline = (SourceDataLine) AudioSystem.getLine(info);
            auline.open(format);
        } catch (final LineUnavailableException e) {
            o.failure(null, e, "playsound:noline", "Error opening output line.");
            return;
        } catch (final Exception e) {
            o.failure(null, e, "playsound:unknown", "General error opening output line.");
            return;
        }

    
        auline.start();
        int nBytesRead = 0;
        final byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

        try {
            while (nBytesRead != -1) {
                nBytesRead = audioInputStream.read(abData, 0, abData.length);
                if (nBytesRead >= 0) {
                    auline.write(abData, 0, nBytesRead);
                }
            }
        } catch (final IOException e) {
            o.failure(audioInputStream, e, "playsound:read", "Error getting sound data.");
            return;
        } finally {
            auline.drain();
            auline.close();
        }
    }
View Full Code Here

      System.exit(1);
    }
   
    // Get Audio Format information:
    AudioFormat audioFormat = audioInputStream.getFormat();
    SourceDataLine line = null; // Handle opening the line
    DataLine.Info info = new DataLine.Info(SourceDataLine.class,
        audioFormat);
   
    try {
      line = (SourceDataLine) AudioSystem.getLine(info);
      line.open(audioFormat);
    } catch (LineUnavailableException e) {
      e.printStackTrace();
      System.exit(1);
    } catch (Exception e) {
      e.printStackTrace();
View Full Code Here

    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
   
    SourceDataLine line = null; // Handle opening the line
    DataLine.Info info = new DataLine.Info(SourceDataLine.class,
        soundFormat);
   
    try {
      line = (SourceDataLine) AudioSystem.getLine(info);
      line.open(soundFormat);
    } catch (LineUnavailableException e) {
      e.printStackTrace();
      System.exit(1);
    } catch (Exception e) {
      e.printStackTrace();
View Full Code Here

TOP

Related Classes of javax.sound.sampled.SourceDataLine

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.