0

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.");}
}
  • Just in general, I would suspect that the problem you've encountered has to do with not carefully following the protocol set out by the package you're using. Most likely you're expected to execute some calls that return "handles" or perhaps internally store a "handle", and then later you're expected to execute the corresponding calls to release the internal objects belonging to those handles. You probably either garbled a handle (if it's a `long`, don't convert to `int` or `double` and don't do arithmetic on it), or you've executed more "release" type calls than you did "allocate" calls. – Hot Licks Apr 07 '12 at 23:27
  • I'm curious--could you post the link about the "rogue library" for OSX? – Phil Freihofner Apr 08 '12 at 07:25
  • Somebody else was experiencing the same problem (http://stackoverflow.com/questions/9333311/non-aligned-pointer-being-freed-in-java) and tracked it down to the CoreMIDI library in OSX, but I'm not using any MIDI features of Java Sound, so I don't think it's that library that's triggering the error. – user1319675 Apr 08 '12 at 07:34

1 Answers1

0

This is a slightly ambitious course of action, but:

You could do exactly what it says and debug it (will this help? I'm not sure). If you install the CDT package, you can debug the compilation with gdb.

Community
  • 1
  • 1
william.berg
  • 85
  • 3
  • 14