How to capture audio data from mic into a buffer?

All your problems with Audio, Video and Images.

How to capture audio data from mic into a buffer?

Postby vishalgaonkar » Thu Sep 24, 2009 11:08 am

I am working on a packet switched network. I want to capture the audio data from mic into a buffer at a desired sample rate.

How can I capture the audio data from mic into a buffer instead of a file?
Also how can I control the audio capture rate as per the desired sample rate?
vishalgaonkar
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Sep 23, 2009 10:03 am

Top

Postby vishalgaonkar » Fri Oct 16, 2009 1:34 pm

Have found the solution. Used AudioRecord and AudioTrack interfaces.
vishalgaonkar
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Sep 23, 2009 10:03 am

Postby Silverspitz » Wed Oct 28, 2009 7:47 am

In your main activity:

private static final int SAMPLING_RATE = 8000;
private boolean isRecording = false;

//create and launch a record thread, where isRecording is set to true and record() is called in its run() method

//create a stop method that sets isRecording to false to terminate the loop in the recording thread



Then in your record() method:
{

int minRecordBufferSizeInBytes = AudioRecord.getMinBufferSize(SAMPLING_RATE,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);

AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLING_RATE,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,
minRecordBufferSizeInBytes);

short[] buffer = new short[minRecordBufferSizeInShorts];

audioRecord.startRecording();

while(isRecording) {
int bufferReadResult = audioRecord.read(buffer, 0, minRecordBufferSizeInShorts);

//then do some processing of the short array "buffer" here
//before its content gets replaced in the next while loop cycle
//note that since this is a thread the processing can only extend to variables/objects
//declared inside the thread, otherwise a handler needs to be used instead

}

audioRecord.stop();

}

something like that will work, you may opt to use DataOutputStream-BufferedOutputStream-FileOutputStream objects to write the buffer to a file, and call <DataOutputStream object name>.writeShort(buffer[i]); for all i's from 0 to bufferReadResult - 1, in every while loop cycle in the record() method
Silverspitz
Freshman
Freshman
 
Posts: 2
Joined: Wed Oct 28, 2009 6:50 am

Re: How to capture audio data from mic into a buffer?

Postby www.rzr.online.fr » Thu Aug 11, 2011 9:49 am

Do you know how to force some sample rate to higher frequency that 44100 , i fear this cant be done at this level


API: java: android.media

http://developer.android.com/reference/ ... ecord.html


sampleRateInHz the sample rate expressed in Hertz. 44100Hz is currently the only rate that is guaranteed to work on all devices, but other rates such as 22050, 16000, and 11025 may work on some devices.
User avatar
www.rzr.online.fr
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Feb 03, 2009 11:39 am
Location: fr/35/rennes

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 23 guests