📄 captureplayback.java
字号:
monoB = addToggleButton(p6, channelsGroup, "mono", false); sterB = addToggleButton(p6, channelsGroup, "stereo", true); add(p6); groups.addElement(channelsGroup); } private JToggleButton addToggleButton(JPanel p, ButtonGroup g, String name, boolean state) { JToggleButton b = new JToggleButton(name, state); p.add(b); g.add(b); return b; } public AudioFormat getFormat() { Vector v = new Vector(groups.size()); for (int i = 0; i < groups.size(); i++) { ButtonGroup g = (ButtonGroup) groups.get(i); for (Enumeration e = g.getElements();e.hasMoreElements();) { AbstractButton b = (AbstractButton) e.nextElement(); if (b.isSelected()) { v.add(b.getText()); break; } } } AudioFormat.Encoding encoding = AudioFormat.Encoding.ULAW; String encString = (String) v.get(0); float rate = Float.valueOf((String) v.get(1)).floatValue(); int sampleSize = Integer.valueOf((String) v.get(2)).intValue(); String signedString = (String) v.get(3); boolean bigEndian = ((String) v.get(4)).startsWith("big"); int channels = ((String) v.get(5)).equals("mono") ? 1 : 2; if (encString.equals("linear")) { if (signedString.equals("signed")) { encoding = AudioFormat.Encoding.PCM_SIGNED; } else { encoding = AudioFormat.Encoding.PCM_UNSIGNED; } } else if (encString.equals("alaw")) { encoding = AudioFormat.Encoding.ALAW; } return new AudioFormat(encoding, rate, sampleSize, channels, (sampleSize/8)*channels, rate, bigEndian); } public void setFormat(AudioFormat format) { AudioFormat.Encoding type = format.getEncoding(); if (type == AudioFormat.Encoding.ULAW) { ulawB.doClick(); } else if (type == AudioFormat.Encoding.ALAW) { alawB.doClick(); } else if (type == AudioFormat.Encoding.PCM_SIGNED) { linrB.doClick(); signB.doClick(); } else if (type == AudioFormat.Encoding.PCM_UNSIGNED) { linrB.doClick(); unsignB.doClick(); } float rate = format.getFrameRate(); if (rate == 8000) { rate8B.doClick(); } else if (rate == 11025) { rate11B.doClick(); } else if (rate == 16000) { rate16B.doClick(); } else if (rate == 22050) { rate22B.doClick(); } else if (rate == 44100) { rate44B.doClick(); } switch (format.getSampleSizeInBits()) { case 8 : size8B.doClick(); break; case 16 : size16B.doClick(); break; } if (format.isBigEndian()) { bigB.doClick(); } else { litB.doClick(); } if (format.getChannels() == 1) { monoB.doClick(); } else { sterB.doClick(); } } } // End class FormatControls /** * Render a WaveForm. */ 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(); } public void paint(Graphics g) { Dimension d = getSize(); int w = d.width; int h = d.height; int INFOPAD = 15; Graphics2D g2 = (Graphics2D) g; g2.setBackground(getBackground()); g2.clearRect(0, 0, w, h); g2.setColor(Color.white); g2.fillRect(0, h-INFOPAD, w, INFOPAD); 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(); } } else if (capture.thread != null) { g2.setColor(Color.black); g2.setFont(font12); g2.drawString("Length: " + String.valueOf(seconds), 3, h-4); } else { g2.setColor(Color.black); g2.setFont(font12); g2.drawString("File: " + fileName + " Length: " + String.valueOf(duration) + " Position: " + 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[]) { CapturePlayback capturePlayback = new CapturePlayback(); capturePlayback.open(); JFrame f = new JFrame("Capture/Playback"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.getContentPane().add("Center", capturePlayback); f.pack(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int w = 720; int h = 340; f.setLocation(screenSize.width/2 - w/2, screenSize.height/2 - h/2); f.setSize(w, h); f.show(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -