To capture microphone audio input using the TargetDataLine
class in Java, you can use the javax.sound.sampled
package. Here’s a step-by-step explanation of how you can achieve this:
Steps to Capture Microphone Input
- Prepare the Audio Format: Define an
AudioFormat
object, specifying the audio sample rate, sample size, number of channels, etc. - Get the TargetDataLine: Use
AudioSystem
to obtain and open aTargetDataLine
. - Start Capturing Audio: Begin capturing audio from the
TargetDataLine
. - Read Data from the Line: Continuously read data from the
TargetDataLine
into a byte buffer. - (Optional) Save the Data: Write the captured audio data to a file or process it as needed.
Example Code
Below is a complete example of how to capture microphone input using TargetDataLine
:
package org.kodejava.sound;
import javax.sound.sampled.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class MicrophoneCapture {
// Volatile flag for ensuring proper thread shutdown
private volatile boolean running;
public static void main(String[] args) {
new MicrophoneCapture().start();
}
public void start() {
// Define the audio format
AudioFormat audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED, // Encoding
44100.0f, // Sample rate (44.1kHz)
16, // Sample size in bits
2, // Channels (stereo)
4, // Frame size (frame size = 16 bits/sample * 2 channels = 4 bytes)
44100.0f, // Frame rate (matches sample rate for PCM)
false // Big-endian (false = little-endian)
);
// Get and configure the TargetDataLine
TargetDataLine microphone;
try {
microphone = AudioSystem.getTargetDataLine(audioFormat);
microphone.open(audioFormat);
// Start capturing audio
microphone.start();
System.out.println("Recording started... Press Ctrl+C or stop to terminate.");
// Register a shutdown hook for graceful termination
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
stop(microphone);
System.out.println("Recording stopped.");
}));
// Start capturing in another thread
captureMicrophoneAudio(microphone);
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
private void captureMicrophoneAudio(TargetDataLine microphone) {
byte[] buffer = new byte[4096];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
running = true;
// Capture audio in a loop
try (microphone) {
while (running) {
int bytesRead = microphone.read(buffer, 0, buffer.length);
if (bytesRead > 0) {
outputStream.write(buffer, 0, bytesRead);
}
}
// Save captured audio to a raw file
saveAudioToFile(outputStream.toByteArray(), "D:/Sound/output.raw");
} catch (Exception e) {
e.printStackTrace();
}
}
private void saveAudioToFile(byte[] audioData, String fileName) {
try (FileOutputStream fileOutputStream = new FileOutputStream(new File(fileName))) {
fileOutputStream.write(audioData);
System.out.println("Audio saved to " + fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
public void stop(TargetDataLine microphone) {
running = false; // Stop the loop
if (microphone != null && microphone.isOpen()) {
microphone.flush();
microphone.stop();
microphone.close();
}
}
}
Explanation
- Audio Format: The
AudioFormat
object defines the format of the captured audio (e.g., PCM encoding, 44.1 kHz sample rate, 16-bit sample size, stereo channels). - TargetDataLine Setup:
TargetDataLine
is the primary interface to access audio input lines, such as the microphone. Theopen()
method ensures it’s properly configured with the specified format. - Reading Audio Data: Data from the microphone is captured into a
byte[]
buffer using theread()
method. - Saving the Audio: The audio data can be saved to a file (e.g.,
.raw
for raw PCM data).
Points to Note
- Permissions: Ensure your application has permission to access the microphone, particularly when running on platforms like macOS or Windows.
- Audio Processing: If you need further audio processing (e.g., writing to a WAV file), you’ll need to add additional logic to wrap the raw PCM data in a WAV file format header.
- Thread Safety: For a real-time application, consider running the audio capture logic in a separate thread.