Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio recording from microphone #67

Closed
jkolobok opened this issue Dec 12, 2014 · 8 comments
Closed

Audio recording from microphone #67

jkolobok opened this issue Dec 12, 2014 · 8 comments

Comments

@jkolobok
Copy link

I'm trying to record a pure audio file(mp3) capturing audio from the microphone and saving it using ffmpeg recorder.
I'm getting file, I can hear sound, but there is A LOT of noise. Capturing the same way but writing it using javasound api don't produce so much sound.
This is a small test. In the future I'll be creating video+audio from mic (demo shows the same problem - correct video and noisy audio).
Do you have any samples how to write pure audio?

@jkolobok
Copy link
Author

One of the possible issues is that audio format supported by microphone is
new AudioFormat(22050,
8,
1,
signed=true,
bigEndian=true);
And if you set signed to false then I don't get any supported lines

@saudet
Copy link
Member

saudet commented Dec 12, 2014

You could ask this guy to see if he came up with anything:
https://code.google.com/p/javacv/issues/detail?id=493#c4
It would be great to add a sample! Let me know if you end up making one so we can add it, thanks!

@jkolobok
Copy link
Author

I found a solution yesterday.
There is a way to transform signed audio to unsigned using
AudioSystem.getAudioInputStream(new AudioFormat(audioFormat.getSampleRate(), audioFormat.getSampleSizeInBits(),audioFormat.getChannels(), false, true), sourceStream);
where
audioFormat - original signed audio format.
sourceAudio - audio input stream that wraps targetDataLine:
AudioInputStream sourceStream = new AudioInputStream(microphoneDataLine);

i'll try to create a sample if I have time, but this should explain what to do.

@saudet
Copy link
Member

saudet commented Apr 11, 2015

If you're still up to creating a sample, we're still interested! Let us know what you need to get started, thanks!

@benjdavenport
Copy link
Contributor

Hey dudes and dudettes,

Just wanted to chime in on this thread and share a solution to capturing CLEAN audio from a TargetDataLine source (mic or other) and writing to an FFmpegFrameRecorder instance using recordSamples (similar results can be achieved using org.bytedeco.javacv.Frame and setting the samples property).

// 44.1 sample rate, 16 bits, stereo, signed, little endian
AudioFormat audioFormat = new AudioFormat(44100.0F, 16, 2, true, false);

Mixer.Info[] minfoSet = AudioSystem.getMixerInfo();
                Mixer mixer = AudioSystem.getMixer(minfoSet[DEVICE_INDEX]);
                DataLine.Info dataLineInfo = new DataLine.Info( TargetDataLine.class, audioFormat );

// Open and start capturing audio    
                    TargetDataLine line = (TargetDataLine) mixer.getLine(dataLineInfo);
                    line.open(audioFormat);
                    line.start();

                    int audioBufferSize = (int) audioFormat.getSampleRate() * audioFormat.getChannels();
                    byte[] audioBytes = new byte[audioBufferSize];

                    AudioInputStream isS = new AudioInputStream(line);

                    while (true)
                    {
                        try
                        {
                            // Read from the line... non-blocking   
                            int nBytesRead = isS.read(audioBytes, 0, line.available());

                            if (nBytesRead > 0)
                            {
// Since we specified 16 bits in the AudioFormat, we need to convert our read byte[] to short[] (see source from FFmpegFrameRecorder.recordSamples for AV_SAMPLE_FMT_S16)
// Let's initialize our short[] array
                                int nSamplesRead = nBytesRead / 2;
                                short[] samples = new short[nSamplesRead];

// Let's wrap our short[] into a ShortBuffer and pass it to recordSamples
                             ByteBuffer.wrap(audioBytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(samples);
                                ShortBuffer sBuff = ShortBuffer.wrap(samples, 0, nSamplesRead);

                                // recorder is instance of org.bytedeco.javacv.FFmpegFrameRecorder
                                recorder.recordSamples((int) audioFormat.getSampleRate(), audioFormat.getChannels(), sBuff);
                            }
                        } 
                        catch (IOException | org.bytedeco.javacv.FrameRecorder.Exception e)
                        {
                            e.printStackTrace();
                        }
                    }

I hope this helps anyone having issues with capture / quality :o)

@saudet
Copy link
Member

saudet commented Apr 22, 2015

@bennyveo Nice! Thanks a lot. Would it be possible to "bundle" that into a self-contained sample, just something like a main() method with your name and message above in comments, and send a pull request to have it added to the samples directory? It would be awesome!

@benjdavenport
Copy link
Contributor

Sure brutha, sounds good I’ll do that… Currently wrestling with getting timestamps in audio/video aligned, so let me tackle that and put it all in a single app. :o)

On Apr 22, 2015, at 9:38 AM, Samuel Audet [email protected] wrote:

@bennyveo https://github.com/bennyveo Nice! Thanks a lot. Would it be possible to "bundle" that into a self-contained sample, just something like a main() method with your name and message above in comments, and send a pull request to have it added to the samples directory? It would be awesome!


Reply to this email directly or view it on GitHub #67 (comment).

@saudet
Copy link
Member

saudet commented Apr 26, 2015

New sample merged: #131 Thanks @bennyveo !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants