-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Comments
One of the possible issues is that audio format supported by microphone is |
You could ask this guy to see if he came up with anything: |
I found a solution yesterday. i'll try to create a sample if I have time, but this should explain what to do. |
If you're still up to creating a sample, we're still interested! Let us know what you need to get started, thanks! |
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) |
@bennyveo Nice! Thanks a lot. Would it be possible to "bundle" that into a self-contained sample, just something like a |
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)
|
New sample merged: #131 Thanks @bennyveo ! |
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?
The text was updated successfully, but these errors were encountered: