📄 captureplayback.java
字号:
if ((errStr = msg) != null) { System.out.println(errStr); samplingGraph.repaint(); } } /** * Write data to the OutputChannel. */ public class Playback implements Runnable { SourceDataLine line; Thread thread; public void start() { errStr = null; thread = new Thread(this); thread.setName("Playback"); thread.start(); } public void stop() { thread = null; } private void shutDown(String message) { if ((errStr = message) != null) { System.err.println(errStr); samplingGraph.repaint(); } if (thread != null) { thread = null; samplingGraph.stop(); captB.setEnabled(true); pausB.setEnabled(false); playB.setText("Play"); } } public void run() { // reload the file if loaded by file if (file != null) { createAudioInputStream(file, false); } // make sure we have something to play if (audioInputStream == null) { shutDown("No loaded audio to play back"); return; } // reset to the beginnning of the stream try { audioInputStream.reset(); } catch (Exception e) { shutDown("Unable to reset the stream\n" + e); return; } // get an AudioInputStream of the desired format for playback AudioFormat format = formatControls.getFormat(); AudioInputStream playbackInputStream = AudioSystem.getAudioInputStream(format, audioInputStream); if (playbackInputStream == null) { shutDown("Unable to convert stream of format " + audioInputStream + " to format " + format); return; } // define the required attributes for our line, // and make sure a compatible line is supported. DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); if (!AudioSystem.isLineSupported(info)) { shutDown("Line matching " + info + " not supported."); return; } // get and open the source data line for playback. try { line = (SourceDataLine) AudioSystem.getLine(info); line.open(format, bufSize); } catch (LineUnavailableException ex) { shutDown("Unable to open the line: " + ex); return; } // play back the captured audio data int frameSizeInBytes = format.getFrameSize(); int bufferLengthInFrames = line.getBufferSize() / 8; int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes; byte[] data = new byte[bufferLengthInBytes]; int numBytesRead = 0; // start the source data line line.start(); while (thread != null) { try { if ((numBytesRead = playbackInputStream.read(data)) == -1) { break; } int numBytesRemaining = numBytesRead; while (numBytesRemaining > 0 ) { numBytesRemaining -= line.write(data, 0, numBytesRemaining); } } catch (Exception e) { shutDown("Error during playback: " + e); break; } } // we reached the end of the stream. let the data play out, then // stop and close the line. if (thread != null) { line.drain(); } line.stop(); line.close(); line = null; shutDown(null); } } // End class Playback /** * 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.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 = 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; } // 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; } // play back the captured audio 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(); 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(); } // 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; } samplingGraph.createWaveForm(audioBytes); } } // End class Capture /** * Controls for the AudioFormat. */ class FormatControls extends JPanel { Vector groups = new Vector(); JToggleButton linrB, ulawB, alawB, rate8B, rate11B, rate16B, rate22B, rate44B; JToggleButton size8B, size16B, signB, unsignB, litB, bigB, monoB,sterB; public FormatControls() { setLayout(new GridLayout(0,1)); EmptyBorder eb = new EmptyBorder(0,0,0,5); BevelBorder bb = new BevelBorder(BevelBorder.LOWERED); CompoundBorder cb = new CompoundBorder(eb, bb); setBorder(new CompoundBorder(cb, new EmptyBorder(8,5,5,5))); JPanel p1 = new JPanel(); ButtonGroup encodingGroup = new ButtonGroup(); linrB = addToggleButton(p1, encodingGroup, "linear", true); ulawB = addToggleButton(p1, encodingGroup, "ulaw", false); alawB = addToggleButton(p1, encodingGroup, "alaw", false); add(p1); groups.addElement(encodingGroup); JPanel p2 = new JPanel(); JPanel p2b = new JPanel(); ButtonGroup sampleRateGroup = new ButtonGroup(); rate8B = addToggleButton(p2, sampleRateGroup, "8000", false); rate11B = addToggleButton(p2, sampleRateGroup, "11025", false); rate16B = addToggleButton(p2b, sampleRateGroup, "16000", false); rate22B = addToggleButton(p2b, sampleRateGroup, "22050", false); rate44B = addToggleButton(p2b, sampleRateGroup, "44100", true); add(p2); add(p2b); groups.addElement(sampleRateGroup); JPanel p3 = new JPanel(); ButtonGroup sampleSizeInBitsGroup = new ButtonGroup(); size8B = addToggleButton(p3, sampleSizeInBitsGroup, "8", false); size16B = addToggleButton(p3, sampleSizeInBitsGroup, "16", true); add(p3); groups.addElement(sampleSizeInBitsGroup); JPanel p4 = new JPanel(); ButtonGroup signGroup = new ButtonGroup(); signB = addToggleButton(p4, signGroup, "signed", true); unsignB = addToggleButton(p4, signGroup, "unsigned", false); add(p4); groups.addElement(signGroup); JPanel p5 = new JPanel(); ButtonGroup endianGroup = new ButtonGroup(); litB = addToggleButton(p5, endianGroup, "little endian", false); bigB = addToggleButton(p5, endianGroup, "big endian", true); add(p5); groups.addElement(endianGroup); JPanel p6 = new JPanel(); ButtonGroup channelsGroup = new ButtonGroup();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -