I'm trying to record audio from my microphone on my Mac using Java. I found the code at the very bottom after a bit of searching on Google, but 1 out of every 3 or 4 times when I run it in Eclipse, the error:
java(4946,0x1095f9000) malloc: *** error for object 0xd07400083: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
appears on the console. According to another post on here, it has to do with a rogue library provided by Apple for OSX, but I don't know any other way of capturing audio in Java. Is there any way to prevent this error by changing the Java code I'm using, or is this something that is out of the scope of the code?
This is the code I am using:
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
public class AudioCapture extends Thread
{
private TargetDataLine dataLine;
private AudioFileFormat.Type audioFileType;
private AudioInputStream incomingStream;
private File generatedFile;
public AudioCapture(TargetDataLine line,
AudioFileFormat.Type requiredFileType,
File file){
dataLine = line;
incomingStream = new AudioInputStream(line);
audioFileType = requiredFileType;
generatedFile = file;}
public void startCapture(){
dataLine.start();
super.start();}
public void stopCapture(){
dataLine.stop();
dataLine.close();}
public void run(){
try{
AudioSystem.write(
incomingStream,
audioFileType,
generatedFile);}
catch (IOException e){
e.printStackTrace();}
}
public static void main(String[] args){
String strFilename = "captureFile.wav";
File outputFile = new File(strFilename);
AudioFormat audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
22050.0F, 16, 2,
4, 22050.0F, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class,
audioFormat);
TargetDataLine targetDataLine = null;
try{
targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
targetDataLine.open(audioFormat);}
catch (LineUnavailableException e){
System.out.println("Unable to acquire an audio recording line");
e.printStackTrace();
System.exit(1);}
System.out.println("AudioFormat settings: " + audioFormat.toString());
AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
AudioCapture recorder = new AudioCapture(
targetDataLine,
targetType,
outputFile);
recorder.startCapture();
System.out.println("Starting the recording now...");
System.out.println("Please press to stop the recording.");
try{
System.in.read();}
catch (IOException e){
e.printStackTrace();}
recorder.stopCapture();
System.out.println("Capture stopped.");}
}