[android-developers] Re: How to record the audio from mic in raw pcm format into a buffer, using the native libraries directly?

2009-11-30 Thread Kevin
I am pushing for better audio support in the NDK in Bug 3434.
http://code.google.com/p/android/issues/detail?id=3434
Kevin

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] Re: How to record the audio from mic in raw pcm format into a buffer, using the native libraries directly?

2009-10-09 Thread Vishal

My Code Samples:

#define SAMPLING_RATE   8000
#define N_FRAMES_PER_BUFFER 1 /* low-level driver counts in terms
of frames, not samples */
#define N_TUPLES_PER_FRAME  1 /* tuple: a set of samples (set of 1
if mono, set of 2 if stereo */
#define N_CHANNELS_PER_TUPLE1  /* 1: mono; 2: stereo */
#define N_TUPLES_PER_BUFFER (N_FRAMES_PER_BUFFER *
N_TUPLES_PER_FRAME) /* 160 = 160 * 1 */
#define N_SAMPLES_PER_BUFFER(N_TUPLES_PER_BUFFER *
N_CHANNELS_PER_TUPLE) /* 160 = 160 * 1 */
#define N_SECONDS_TO_RECORD 10
#define N_SAMPLES_TO_RECORD (SAMPLING_RATE * N_SECONDS_TO_RECORD *
N_CHANNELS_PER_TUPLE) /* 8 = 8000 * 10 * 1 */

int AudioCap(void)
{
/* Audio record - Start */
short AudBuf[N_SAMPLES_TO_RECORD * 1];
int AudBufSize = 160;
int rc;
unsigned int i;

memset(AudBuf, 0, (N_SAMPLES_TO_RECORD * sizeof(short)));

rc = AudioSetInputFormat(SAMPLING_RATE, N_CHANNELS_PER_TUPLE);
if (rc != 0) {
LOGE(AudioSetInputFormat() Failed! RetVal: %d, rc);
exit(1);
}
else {
LOGE(AudioSetInputFormat() Success! RetVal: %d, rc);
}

rc = AudioOpen();
if (rc  0) {
LOGE(AudioOpen() Failed! RetVal: %d, rc);
exit(1);
}
else {
LOGE(AudioOpen() Success! RetVal: %d, rc);
}

/*i = 0;
while (i = N_SAMPLES_TO_RECORD - N_SAMPLES_PER_BUFFER)
{
rc = AudioRead((AudBuf[i]), N_FRAMES_PER_BUFFER);
if (rc  0) {
i += (rc * N_TUPLES_PER_FRAME * N_CHANNELS_PER_TUPLE);
LOGE(AudioRead() Success);
}
else {
LOGE(ERROR: AudioRead() returns %d\n, rc);
}
}*/

rc = AudioRead(AudBuf, AudBufSize);
if (rc == AudBufSize) {
LOGE(AudioRead() Success! Read Samples: %d , rc);
}
else {
LOGE(AudioRead() Error! Read Samples: %d , rc);
}

rc = AudioClose();
if (rc != 0) {
LOGE(AudioClose() Failed! RetVal: %d, rc);
exit(1);
}
else {
LOGE(AudioClose() Success! RetVal: %d, rc);
}

return 0;
}



using namespace android;

extern C
{

static AudioRecord* record;
static int sampleRate;
static int numChannels;

typedef void (*callback_t)(int event, void* user, void *info);

int AudioSetInputFormat(int sample_rate, int channel_count)
{
sampleRate = sample_rate;
numChannels = channel_count;
LOGE ( AudioSetInputFormat Success! SampleRate: %d, 
ChannelCount:
%d , sampleRate, numChannels );
return 0;
}

int AudioOpen(void)
{
int retVal = NO_ERROR;
unsigned int Val = 0;

record = new android::AudioRecord(
android::AudioRecord::MIC_INPUT, /* Input 
Source */
sampleRate, /* Sample Rate */
android::AudioSystem::PCM_16_BIT, /* Format */
numChannels, /* Channel Count */
1, /* Frame Count */
android::AudioRecord::RECORD_AGC_ENABLE, /* 
Flags */
NULL, /* Callback Function */
NULL, /* Context for use by the callback 
receiver */
1 /* Notification Frames */ );
if (!record) {
LOGE ( AudioOpen-AudioRecord Failed! RetVal: 0x%x , 
record );
return -1;
}
else {
LOGE ( AudioOpen-AudioRecord Success! RetVal: 0x%x , 
record );
}

retVal = record-set(
android::AudioRecord::MIC_INPUT, /* Input 
Source */
sampleRate, /* Sample Rate */
android::AudioSystem::PCM_16_BIT, /* Format */
numChannels, /* Channel Count */
1, /* Frame Count */
android::AudioRecord::RECORD_AGC_ENABLE, /* 
Flags */
NULL, /* Callback Function */
NULL, /* Context for use by the callback 
receiver */
1 /* Notification Frames */ );
if (retVal == NO_ERROR) {
LOGE ( AudioOpen-set Success! retVal: %d , retVal );
}
else if (retVal == INVALID_OPERATION) {
LOGE ( AudioOpen-set Invalid Operation! retVal: %d , 
retVal );
}

[android-developers] Re: How to record the audio from mic in raw pcm format into a buffer, using the native libraries directly?

2009-10-09 Thread Mark Murphy

Vishal wrote:
 My application cannot handle the JNIE overhead, as I need to make 2
 JNIE calls to the native C/C++ code for every 20msec. So I am trying
 to use the native media libraries and the realted C/C++ files provided
 by Android in the folder //external/srec/audio/test of the donut
 build. Ex. AudioHardwareRecord, AudioInRecord etc.. I am implementing
 all the calls in the native layer.

Try the [android-ndk] list if you are trying to use the NDK, or
[android-porting] if you are trying to modify firmware for your own
device, or [android-platform] if you are trying to modify firmware for
contribution back to the AOSP.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

_Android Programming Tutorials_ Version 1.0 Available!

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---