Package javax.sound.sampled

Examples of javax.sound.sampled.SourceDataLine


        final AudioFormat format = ais.getFormat();
        final DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        if(AudioSystem.isLineSupported(info)) {
          final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);

          line.open(format);
          line.start();

          new Thread("Reminder audio playing") {
            private boolean stopped;
            @Override
            public void run() {
              byte[] myData = new byte[1024 * format.getFrameSize()];
              int numBytesToRead = myData.length;
              int numBytesRead = 0;
              int total = 0;
              int totalToRead = (int) (format.getFrameSize() * ais.getFrameLength());
              stopped = false;

              line.addLineListener(new LineListener() {
                public void update(LineEvent event) {
                  if(line != null && !line.isRunning()) {
                    stopped = true;
                    line.close();
                    try {
                      ais.close();
                    }catch(Exception ee) {
                      // ignore
                    }
                  }
                }
              });

              try {
                while (total < totalToRead && !stopped) {
                  numBytesRead = ais.read(myData, 0, numBytesToRead);

                  if (numBytesRead == -1) {
                    break;
                  }

                  total += numBytesRead;
                  line.write(myData, 0, numBytesRead);
                }
              }catch(Exception e) {}

              line.drain();
              line.stop();
            }
          }.start();

          return line;
        }else {
View Full Code Here


  private SourceDataLine getOutputLine()
  {
    for(Device device: mDevices)
    {
      SourceDataLine line = device.getLine(SourceDataLine.class, mAudioFormat);

      if(line != null)
        return line;
    }
View Full Code Here

        if(mSystemMixer != null && mSystemMixer.isLineSupported(info) && mSystemOutputs.size() < mMaxNumLines)
        {
            try
            {
                SourceDataLine line = (SourceDataLine)mSystemMixer.getLine(info);
                line.open();

        SystemOutput output = new SystemOutput(line);

        synchronized(mSystemOutputs)
        {
View Full Code Here

    {
      DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat, AudioSystem.NOT_SPECIFIED);

      try
            {
                SourceDataLine line = (SourceDataLine)mSystemMixer.getLine(info);
                line.open();

        mMixSystemOutput = new SystemOutput(line);
        mMaxNumLines     = mSystemMixer.getMaxLines(info);
        mMaxNumLines     = (mMaxNumLines == AudioSystem.NOT_SPECIFIED) ? (Integer.MAX_VALUE) : (mMaxNumLines);
        mMaxNumLines     = Math.min(useMaxMixerLines, (mMaxNumLines - 1));
            }
            catch(LineUnavailableException e)
      {
        logger.warn("cannot open output line of system mixer device: \"" + e + "\"");
      }
      catch(IllegalArgumentException e)
      {
        logger.warn("cannot open output line of system mixer device: \"" + e + "\"");
      }
      catch(SecurityException e)
      {
        logger.warn("cannot open output line of system mixer device: \"" + e + "\"");
      }
    }

        if(mSystemMixer == null)
        {
      logger.info("try to open a common output line");

            DataLine.Info datalineInfo = new DataLine.Info(SourceDataLine.class, audioFormat, AudioSystem.NOT_SPECIFIED);

            try
            {
                SourceDataLine line = (SourceDataLine)AudioSystem.getLine(datalineInfo);
                line.open();

        mMixSystemOutput = new SystemOutput(line);
        mMaxNumLines     = 0;
            }
            catch(LineUnavailableException e)
View Full Code Here

    for (int i = 0; i < mixers.length; i++)
      if (AudioSystem.getMixer(mixers[i]).isLineSupported(info))
        mixerToUse = i;

    // Obtain and open the line.
    SourceDataLine sdl = (SourceDataLine) AudioSystem.getMixer(
        mixers[mixerToUse]).getLine(info);
    sdl.open(format);
    return sdl;
  }
View Full Code Here

      short[] outputBuffer = new short[BLOCK_SIZE * NUM_OUTPUT_CHANNELS];
      byte[] bInputBuffer = new byte[inputBuffer.length * 2]; // 2 bytes per sample
      byte[] bOutputBuffer = new byte[outputBuffer.length * 2];
  
      TargetDataLine targetDataLine = null;
      SourceDataLine sourceDataLine = null;
      try {
        // TODO(mhroth): ensure that stereo input and output lines are actually being returned
        // by the system.
       
        // open the audio input (line-in or microphone)
        targetDataLine = (TargetDataLine) AudioSystem.getLine(inputLineInfo);
        targetDataLine.open(inputAudioFormat, bInputBuffer.length);
        targetDataLine.start();
       
        // open the audio output
        sourceDataLine = (SourceDataLine) AudioSystem.getLine(outputLineInfo);
        sourceDataLine.open(outputAudioFormat, bOutputBuffer.length);
        sourceDataLine.start();
      } catch (LineUnavailableException lue) {
        lue.printStackTrace(System.err);
        System.exit(1);
      }
     
      // load the Pd patch
      File pdFile = new File(args[0]);
      ZenGarden pdPatch = null;
      ZenGardenAdapter zgAdapter = new ZenGardenAdapter();
      try {
        pdPatch = new ZenGarden(pdFile, BLOCK_SIZE, NUM_INPUT_CHANNELS, NUM_OUTPUT_CHANNELS,
            (float) SAMPLE_RATE);
        pdPatch.addListener(zgAdapter);
      } catch (NativeLoadException nle) {
        nle.printStackTrace(System.err);
        System.exit(2);
      }
     
      while (shouldContinuePlaying) {
        // run the patch in an infinite loop
        targetDataLine.read(bInputBuffer, 0, bInputBuffer.length); // read from the input
        // convert the byte buffer to a short buffer
        for (int i = 0, j = 0; i < inputBuffer.length; i++) {
          inputBuffer[i] = (short) (((int) bInputBuffer[j++]) << 8);
          inputBuffer[i] |= ((short) bInputBuffer[j++]) & 0x00FF;
        }
       
        pdPatch.process(inputBuffer, outputBuffer);
       
        // convert short buffer to byte buffer
        for (int i = 0, j = 0; i < outputBuffer.length; i++) {
          bOutputBuffer[j++] = (byte) ((outputBuffer[i] & 0xFF00) >> 8);
          bOutputBuffer[j++] = (byte) (outputBuffer[i] & 0x00FF);
        }
        // write to the output
        sourceDataLine.write(bOutputBuffer, 0, bOutputBuffer.length);
      }
     
      // release all audio resources
      targetDataLine.drain();
      targetDataLine.close();
      sourceDataLine.drain();
      sourceDataLine.close();
    }
  }
View Full Code Here

            e1.printStackTrace();
            return;
        }

        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) {
            e.printStackTrace();
            return;
        } catch (final Exception e) {
            e.printStackTrace();
            return;
        }

        if (auline.isControlSupported(FloatControl.Type.PAN)) {
            final FloatControl pan = (FloatControl) auline.getControl(FloatControl.Type.PAN);
            if (this.curPosition == Position.RIGHT) {
                pan.setValue(1.0f);
            } else if (this.curPosition == Position.LEFT) {
                pan.setValue(-1.0f);
            }
        }

        auline.start();
        int nBytesRead = 0;
        final byte[] abData = new byte[AePlayWave.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) {
            e.printStackTrace();
            return;
        } finally {
            auline.drain();
            auline.close();
        }

    }
View Full Code Here

  }

  private synchronized void rawplay(AudioFormat targetFormat,
      AudioInputStream din) throws IOException, LineUnavailableException {
    byte[] data = new byte[4096];
    SourceDataLine line = getLine(targetFormat);
    if (line != null) {
      // Start
      line.start();
      int nBytesRead = 0, nBytesWritten = 0;
      while (nBytesRead != -1) {
        nBytesRead = din.read(data, 0, data.length);
        if (nBytesRead != -1) {
          nBytesWritten = line.write(data, 0, nBytesRead);
        }

      }
      // Stop
      line.drain();
      line.stop();
      line.close();
      din.close();
    }

  }
View Full Code Here

        bigEndian);
  }

  private synchronized SourceDataLine getLine(AudioFormat audioFormat)
      throws LineUnavailableException {
    SourceDataLine res = null;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class,
        audioFormat);
    res = (SourceDataLine) AudioSystem.getLine(info);
    res.open(audioFormat);
    return res;
  }
View Full Code Here


    private void rawplay(AudioFormat targetFormat,
            AudioInputStream din) throws IOException, LineUnavailableException {
        data = new byte[4096];
        SourceDataLine line = getLine(targetFormat);

        if (line != null) {
            // Start
            line.start();
            int nBytesRead = 0, nBytesWritten = 0;
            bytes = 0;

            while (nBytesRead != -1) {
                nBytesRead = din.read(data, 0, data.length);
                bytes += nBytesRead;
              //  System.out.println(data[0] + " " + data[1]);
                // System.out.println(bytes);
                if (nBytesRead != -1) {
                       nBytesWritten = line.write(data, 0, nBytesRead);
                }

            }

            // Stop
            line.drain();
            line.stop();
            line.close();
            din.close();
            //96515374
        }
    }
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.