ddf / Minim

A Java audio library, designed to be used with Processing.
http://code.compartmental.net/tools/minim
GNU Lesser General Public License v3.0
668 stars 136 forks source link

BufferSize Null Pointer Expectation Issue on new Mac Pro 2018 #82

Open cyrus-education opened 5 years ago

cyrus-education commented 5 years ago

When trying to record audio through microphone on new MacPro 2018, there will return the "null pointer expectation", it looks that the microphone is denied, any solutions? Thanks.

matt0rtega commented 5 years ago

Might have to do with the new permissions settings for audio in Mojave.

ddf commented 5 years ago

Yes, I'd assume it's a permission thing as well. Unfortunately, I don't have a Mac that can run Mojave, so I'm unable to investigate this.

ddf commented 5 years ago

Even without my being able to reproduce the issue, if you could provide a callstack of where the exception comes from, it might be possible to deal with the null pointer better. If a working AudioInput can't be created, then getLineIn will return null. This is the expected behavior and it is the caller's responsibility to ensure they check for null before using the returned AudioInput. However, if the null pointer exception is happening inside of the getLineIn call, I'd want to address that.

matt0rtega commented 5 years ago

I finally figured this out but posted in the Processing Discourse. The issue was that the RecordAudioInput example would no longer run, but running it with the getLineIn(Minim.MONO) argument got it working. Not sure why the change with the newer MacBook Pro or OS, but if others are experiencing this, might be worth updating the example.

` /**

import ddf.minim.*;

Minim minim; AudioInput in; AudioRecorder recorder;

void setup() { size(512, 200, P3D);

minim = new Minim(this);

in = minim.getLineIn(Minim.MONO); // create a recorder that will record from the input to the filename specified // the file will be located in the sketch's root folder. recorder = minim.createRecorder(in, "myrecording.wav");

textFont(createFont("Arial", 12)); }

void draw() { background(0); stroke(255); // draw the waveforms // the values returned by left.get() and right.get() will be between -1 and 1, // so we need to scale them up to see the waveform for(int i = 0; i < in.bufferSize() - 1; i++) { line(i, 50 + in.left.get(i)50, i+1, 50 + in.left.get(i+1)50); line(i, 150 + in.right.get(i)50, i+1, 150 + in.right.get(i+1)50); }

if ( recorder.isRecording() ) { text("Currently recording...", 5, 15); } else { text("Not recording.", 5, 15); } }

void keyReleased() { if ( key == 'r' ) { // to indicate that you want to start or stop capturing audio data, you must call // beginRecord() and endRecord() on the AudioRecorder object. You can start and stop // as many times as you like, the audio data will be appended to the end of the buffer // (in the case of buffered recording) or to the end of the file (in the case of streamed recording). if ( recorder.isRecording() ) { recorder.endRecord(); } else { recorder.beginRecord(); } } if ( key == 's' ) { // we've filled the file out buffer, // now write it to the file we specified in createRecorder // in the case of buffered recording, if the buffer is large, // this will appear to freeze the sketch for sometime // in the case of streamed recording, // it will not freeze as the data is already in the file and all that is being done // is closing the file. // the method returns the recorded audio as an AudioRecording, // see the example AudioRecorder >> RecordAndPlayback for more about that recorder.save(); println("Done saving."); } } `

ddf commented 5 years ago

Ah, thanks for the info. I will try this out on my Macbook and see if I get a similar result. The version of getLineIn requests a STEREO input, but a Macbook microphone is definitely a MONO input. Maybe macOS was less picky about that before.

ddf commented 5 years ago

Could you do me a favor and open up your Audio MIDI Setup and have a look at the Audio Devices window? I'm curious to see if your "Built-in Input" or similar is set to 1 channel or 2. Mine is set to 2 channels (see below) and doesn't have any settings for 1 channel. So I am able to run the sketch as written without any problems. It also runs just fine if use getLineIn(Minim.MONO).

screen shot 2019-03-02 at 4 48 26 pm

matt0rtega commented 5 years ago

I think you've found it. Only one channel appearing I guess in the new OS. Not sure if this is something I can change.

Screen Shot 2019-03-11 at 4 19 52 PM
ddf commented 5 years ago

Ok, yep, then I think your initial suggestion to change the example so it gets a MONO input is the best approach. I should also probably add a comment to the effect that STEREO inputs are supported, but availability will depend on the computer's audio input configuration. And should probably also check for null in case someone runs the sketch on a machine that has all audio inputs disabled...