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 );
}