⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 captureplaybackserver.java

📁 java语言编写的语音聊天室程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      }
      catch (LineUnavailableException ex) {
        shutDown("Unable to open the line: " + ex);
        return;
      }
      //对line的操作次序:open();getBufferSize();start();write(data,0,num);drain();
      /**
       * line的最大BufferSize是16384;一个frame大小由AudioFormat决定;frame大小乘上(buffer里的frame数量)=这里line实际用的bufferData大小
       */
      // 重放捕获的声音数据 play back the captured audio data
      //**frame大小由AudioFormat决定,这里为4字节长;
       int frameSizeInBytes = format.getFrameSize();
      //For a source data line, this is maximum size of the buffer to which data can be written.  For a target data line, it is maximum size of the buffer from which data can be read.
      int bufferLengthInFrames = line.getBufferSize() / 8;
      //bufferLengthInBytes为8192----bufferLengthInFrames为2048
      int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
      //在线程里,data被不断初始化!
      byte[] data = new byte[bufferLengthInBytes];
      int numBytesRead = 0;

      // Allows a line to engage in data I/O.(the line must be flushed or stoped)
      line.start();
      //playbackInputStream->data[]->dataline
      while (thread != null) {
        try {
          //取出playbackInputStream并写到字节流data[byte]中!返回事实读取的量,跳出。
          if ( (numBytesRead = playbackInputStream.read(data)) == -1) {
            break;
          }
          int numBytesRemaining = numBytesRead;
          while (numBytesRemaining > 0) {
            //line.write(data, 0, numBytesRemaining)返回已写入line的数据数量
            numBytesRemaining -= line.write(data, 0, numBytesRemaining);
          }
        }
        catch (Exception e) {
          shutDown("Error during playback: " + e);
          break;
        }
      }
      // 向line写完字节流,结束when reached the end of the stream,let the data play out, then
      // stop and close the line.
      if (thread != null) {
        //排出Drains queued data from the line by continuing data I/O until line's internal buffer has been emptied.
        line.drain();
        //line.flush()---Flushes queued data from the line.
      }
      line.stop();
      line.close();
      line = null;
      shutDown(null);
    }
  } // End class Playback
//fuwuqi
  /**
   * 从输入渠道中捕获数据,并写到输出流中;Reads data from the input channel and writes to the output stream
   */
  class Capture
      implements Runnable {

    TargetDataLine line;
    Thread thread;

    public void start() {
      errStr = null;
      thread = new Thread(this);
      //thread是有名字的;
      thread.setName("Capture");
      thread.start();
    }

    public void stop() {
      thread = null;
    }

    private void shutDown(String message) {
      if ( (errStr = message) != null && thread != null) {
        thread = null;
        samplingGraph.stop();
        loadB.setEnabled(true);
        playB.setEnabled(true);
        pausB.setEnabled(false);
        auB.setEnabled(true);
        aiffB.setEnabled(true);
        waveB.setEnabled(true);
        captB.setText("Record");
        System.err.println(errStr);
        samplingGraph.repaint();
      }
    }

    public void run() {
      duration = 0;
      //将audioInputStream置空!
      audioInputStream = null;

      // define the required attributes for our line,
      // and make sure a compatible line is supported.

      AudioFormat format = formatControls.getFormat();
      DataLine.Info info = new DataLine.Info(TargetDataLine.class,
                                             format);

      if (!AudioSystem.isLineSupported(info)) {
        shutDown("Line matching " + info + " not supported.");
        return;
      }

      // 获得并打开目标dataline来捕获get and open the target data line for capture.

      try {
        line = (TargetDataLine) AudioSystem.getLine(info);
        //这里将获得声音!!
        line.open(format, line.getBufferSize());
      }
      catch (LineUnavailableException ex) {
        shutDown("Unable to open the line: " + ex);
        return;
      }
      catch (SecurityException ex) {
        shutDown(ex.toString());
//        JavaSound.showInfoDialog();
        return;
      }
      catch (Exception ex) {
        shutDown(ex.toString());
        return;
      }

      // line.read(data[])——播放
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      int frameSizeInBytes = format.getFrameSize();
      int bufferLengthInFrames = line.getBufferSize() / 8;
      int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
      byte[] data = new byte[bufferLengthInBytes];
      int numBytesRead;

      line.start();
    //循环——从IO中读取到data[byte],长为bufferLengthInBytes  !
      while (thread != null) {
        if ( (numBytesRead = line.read(data, 0, bufferLengthInBytes)) == -1) {
          break;
        }
        out.write(data, 0, numBytesRead);
      }

      // we reached the end of the stream.  stop and close the line.
      line.stop();
      line.close();
      line = null;

      // 停止和关闭输出流stop and close the output stream
      try {
        out.flush();
        out.close();
      }
      catch (IOException ex) {
        ex.printStackTrace();
      }

      // !!!将输入流载入到audioInputStream对象中!load bytes into the audio input stream for playback

      byte audioBytes[] = out.toByteArray();
      ByteArrayInputStream bais = new ByteArrayInputStream(audioBytes);
      audioInputStream = new AudioInputStream(bais, format,
                                              audioBytes.length /
                                              frameSizeInBytes);

      long milliseconds = (long) ( (audioInputStream.getFrameLength() * 1000) /
                                  format.getFrameRate());
      duration = milliseconds / 1000.0;

      try {
        audioInputStream.reset();
      }
      catch (Exception ex) {
        ex.printStackTrace();
        return;
      }
//在捕获的线程里调用画图!hlf   //kehu---duankai2---huahua
      samplingGraph.createWaveForm(audioBytes);
    }
  } // End class Capture
  //fuwuqi
  /**
   * 画波形图.//hlf!!
   */
  class SamplingGraph extends JPanel implements Runnable {

    private Thread thread;
    private Font font10 = new Font("serif", Font.PLAIN, 10);
    private Font font12 = new Font("serif", Font.PLAIN, 12);
    //深色底面
    Color jfcBlue = new Color(204, 204, 255);
    //粉色画当前位置线
    Color pink = new Color(255, 175, 175);

    public SamplingGraph() {
      setBackground(new Color(20, 20, 20));
    }

    //画波形图!
    public void createWaveForm(byte[] audioBytes) {

      lines.removeAllElements(); // clear the old vector

      AudioFormat format = audioInputStream.getFormat();

      if (audioBytes == null) {
        try {
          audioBytes = new byte[ (int) (audioInputStream.getFrameLength()
                                        * format.getFrameSize())];
          audioInputStream.read(audioBytes);
        }
        catch (Exception ex) {
          reportStatus(ex.toString());
          return;
        }
      }
      Dimension d = getSize();
      int w = d.width;
      int h = d.height - 15;
      int[] audioData = null;
      if (format.getSampleSizeInBits() == 16) {
        int nlengthInSamples = audioBytes.length / 2;
        audioData = new int[nlengthInSamples];
        if (format.isBigEndian()) {
          for (int i = 0; i < nlengthInSamples; i++) {
            /* First byte is MSB (high order) */
            int MSB = (int) audioBytes[2 * i];
            /* Second byte is LSB (low order) */
            int LSB = (int) audioBytes[2 * i + 1];
            audioData[i] = MSB << 8 | (255 & LSB);
          }
        }
        else {
          for (int i = 0; i < nlengthInSamples; i++) {
            /* First byte is LSB (low order) */
            int LSB = (int) audioBytes[2 * i];
            /* Second byte is MSB (high order) */
            int MSB = (int) audioBytes[2 * i + 1];
            audioData[i] = MSB << 8 | (255 & LSB);
          }
        }
      }
      else if (format.getSampleSizeInBits() == 8) {
        int nlengthInSamples = audioBytes.length;
        audioData = new int[nlengthInSamples];
        if (format.getEncoding().toString().startsWith("PCM_SIGN")) {
          for (int i = 0; i < audioBytes.length; i++) {
            audioData[i] = audioBytes[i];
          }
        }
        else {
          for (int i = 0; i < audioBytes.length; i++) {
            audioData[i] = audioBytes[i] - 128;
          }
        }
      }

      int frames_per_pixel = audioBytes.length / format.getFrameSize() / w;
      byte my_byte = 0;
      double y_last = 0;
      int numChannels = format.getChannels();
      for (double x = 0; x < w && audioData != null; x++) {
        int idx = (int) (frames_per_pixel * numChannels * x);
        if (format.getSampleSizeInBits() == 8) {
          my_byte = (byte) audioData[idx];
        }
        else {
          my_byte = (byte) (128 * audioData[idx] / 32768);
        }
        double y_new = (double) (h * (128 - my_byte) / 256);
        //增加点!
        lines.add(new Line2D.Double(x, y_last, x, y_new));
        y_last = y_new;
      }

      //repaint();//???
    }

//使用线程画波形图!hlf
    public void paint(Graphics g) {
    // 组件JPanel的大小;Returns the size of this component in the form of a <code>Dimension</code> object
      Dimension d = getSize();
      int w = d.width;
      int h = d.height;
      int INFOPAD = 15;
    // 精确控制几何图形 Graphics2D——sophisticated control over geometry
    // 在JPanel上画画!
      Graphics2D g2 = (Graphics2D) g;
      g2.setBackground(getBackground());
      g2.clearRect(0, 0, w, h);//???300,60
      g2.setColor(Color.white);
      g2.fillRect(0, h - INFOPAD, w, INFOPAD);
    //捕获出错的情况1
      if (errStr != null) {
        g2.setColor(jfcBlue);
        g2.setFont(new Font("serif", Font.BOLD, 18));
        g2.drawString("ERROR", 5, 20);
        AttributedString as = new AttributedString(errStr);
        as.addAttribute(TextAttribute.FONT, font12, 0, errStr.length());
        AttributedCharacterIterator aci = as.getIterator();
        FontRenderContext frc = g2.getFontRenderContext();
        LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
        float x = 5, y = 25;
        lbm.setPosition(0);
        while (lbm.getPosition() < errStr.length()) {
          TextLayout tl = lbm.nextLayout(w - x - 5);
          if (!tl.isLeftToRight()) {
            x = w - tl.getAdvance();
          }
          tl.draw(g2, x, y += tl.getAscent());
          y += tl.getDescent() + tl.getLeading();
        }
      }
      //正捕获中的情况2
      else if (capture.thread != null) {
        g2.setColor(Color.black);
        g2.setFont(font12);
        g2.drawString("Length: " + String.valueOf(seconds), 3, h - 4);
      }
      //捕获结束的情况3
      else {
        g2.setColor(Color.black);
        g2.setFont(font12);
        g2.drawString("文件名: " + fileName + "  长度: " +
                      String.valueOf(duration) + "  当前位置(秒): " +
                      String.valueOf(seconds), 3, h - 4);

        if (audioInputStream != null) {
          // 画声音波形图.. render sampling graph ..
          g2.setColor(jfcBlue);
          for (int i = 1; i < lines.size(); i++) {
            g2.draw( (Line2D) lines.get(i));
          }
          // 画当前位置图.. draw current position ..
          if (seconds != 0) {
            double loc = seconds / duration * w;
            g2.setColor(pink);
            g2.setStroke(new BasicStroke(3));
            g2.draw(new Line2D.Double(loc, 0, loc, h - INFOPAD - 2));
          }
        }
      }
    }

    public void start() {
      thread = new Thread(this);
      thread.setName("SamplingGraph");
      thread.start();
      seconds = 0;
    }

    public void stop() {
      if (thread != null) {
        thread.interrupt();
      }
      thread = null;
    }

    public void run() {
      seconds = 0;
      while (thread != null) {
        if ( (playback.line != null) && (playback.line.isOpen())) {

          long milliseconds = (long) (playback.line.getMicrosecondPosition() /
                                      1000);
          seconds = milliseconds / 1000.0;
        }
        else if ( (capture.line != null) && (capture.line.isActive())) {

          long milliseconds = (long) (capture.line.getMicrosecondPosition() /
                                      1000);
          seconds = milliseconds / 1000.0;
        }

        try {
          thread.sleep(100);
        }
        catch (Exception e) {
          break;
        }

        repaint();

        while ( (capture.line != null && !capture.line.isActive()) ||
               (playback.line != null && !playback.line.isOpen())) {
          try {
            thread.sleep(10);
          }
          catch (Exception e) {
            break;
          }
        }
      }
      seconds = 0;
      repaint();
    }
  } // End class SamplingGraph

  public static void main(String s[]) {
    CapturePlaybackServer CapturePlaybackServer = new CapturePlaybackServer();
    JFrame f = new JFrame("欢迎使用服务端语音系统");
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    f.getContentPane().add("Center", CapturePlaybackServer);
    f.pack();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int w = 720;
    int h = 340;
    //使frame居中
    f.setLocation(screenSize.width / 2 - w / 2, screenSize.height / 2 - h / 2);
    f.setSize(w, h);
    f.show();
    //打开监听
    CapturePlaybackServer.open();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -