!7 // Source: AudioLoop from jsresources.org import javax.sound.sampled.*; static boolean DEBUG; static int DEFAULT_INTERNAL_BUFSIZ = 40960; static int DEFAULT_EXTERNAL_BUFSIZ = 40960; p { S strMixerName = null; float fFrameRate = 44100.0F; int nInternalBufferSize = DEFAULT_INTERNAL_BUFSIZ; int nExternalBufferSize = DEFAULT_EXTERNAL_BUFSIZ; AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, fFrameRate, 16, 2, 4, fFrameRate, false); if (DEBUG) { print("AudioLoop.main(): audio format: " + audioFormat); } new AudioLoop(audioFormat, nInternalBufferSize, nExternalBufferSize, strMixerName).start(); } sclass AudioLoop extends Thread { TargetDataLine m_targetLine; SourceDataLine m_sourceLine; boolean m_bRecording; int m_nExternalBufferSize; *(AudioFormat format, int nInternalBufferSize, int nExternalBufferSize, String strMixerName) throws LineUnavailableException { Mixer mixer = null; if (strMixerName != null) { Mixer.Info mixerInfo = javaSound_getMixerInfo(strMixerName); if (DEBUG) { print("AudioLoop.(): mixer info: " + mixerInfo); } mixer = AudioSystem.getMixer(mixerInfo); if (DEBUG) { print("AudioLoop.(): mixer: " + mixer); } } DataLine.Info targetInfo = new DataLine.Info(TargetDataLine.class, format, nInternalBufferSize); DataLine.Info sourceInfo = new DataLine.Info(SourceDataLine.class, format, nInternalBufferSize); if (mixer != null) { m_targetLine = (TargetDataLine) mixer.getLine(targetInfo); m_sourceLine = (SourceDataLine) mixer.getLine(sourceInfo); } else { m_targetLine = (TargetDataLine) AudioSystem.getLine(targetInfo); m_sourceLine = (SourceDataLine) AudioSystem.getLine(sourceInfo); } if (DEBUG) { print("AudioLoop.(): SourceDataLine: " + m_sourceLine); } if (DEBUG) { print("AudioLoop.(): TargetDataLine: " + m_targetLine); } m_targetLine.open(format, nInternalBufferSize); m_sourceLine.open(format, nInternalBufferSize); m_nExternalBufferSize = nExternalBufferSize; } public void start() { m_targetLine.start(); m_sourceLine.start(); super.start(); } public void run() { byte[] abBuffer = new byte[m_nExternalBufferSize]; int nBufferSize = abBuffer.length; m_bRecording = true; while (m_bRecording) { if (DEBUG) { print("Trying to read: " + nBufferSize); } /* * read a block of data from the recording line. */ int nBytesRead = m_targetLine.read(abBuffer, 0, nBufferSize); if (DEBUG) { print("Read: " + nBytesRead); } /* * And now, we write the block to the playback * line. */ m_sourceLine.write(abBuffer, 0, nBytesRead); } } }