Java Code Examples for android.media.AudioRecord#ERROR

The following examples show how to use android.media.AudioRecord#ERROR . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SaidItService.java    From echo with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int consume(final byte[] array, final int offset, final int count) throws IOException {

    final int bytes = Math.min(readLimit, count);
    //Log.d(TAG, "READING " + bytes + " B");
    final int read = audioRecord.read(array, offset, bytes);
    if (read == AudioRecord.ERROR_BAD_VALUE) {
        Log.e(TAG, "AUDIO RECORD ERROR - BAD VALUE");
        return 0;
    }
    if (read == AudioRecord.ERROR_INVALID_OPERATION) {
        Log.e(TAG, "AUDIO RECORD ERROR - INVALID OPERATION");
        return 0;
    }
    if (read == AudioRecord.ERROR) {
        Log.e(TAG, "AUDIO RECORD ERROR - UNKNOWN ERROR");
        return 0;
    }
    if (wavFileWriter != null && read > 0) {
        wavFileWriter.write(array, offset, read);
    }
    audioHandler.post(audioReader);
    return read;
}
 
Example 2
Source File: RecorderConstants.java    From Alexa-Voice-Service with MIT License 5 votes vote down vote up
private int getBufferSize(){
    //the function below gives min buffer size which is necesaary for audio recording
    int minBufferSizeInBytes = AudioRecord.getMinBufferSize(mSampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
    if (minBufferSizeInBytes == AudioRecord.ERROR_BAD_VALUE) {
        throw new IllegalArgumentException("SpeechRecord.getMinBufferSize: parameters not supported by hardware");
    } else if (minBufferSizeInBytes == AudioRecord.ERROR) {
        // Log.e("SpeechRecord.getMinBufferSize: unable to query hardware for output properties");
        minBufferSizeInBytes = mSampleRate * (120 / 1000) * RESOLUTION_IN_BYTES * CHANNELS;
    }
    //buffer_size_multiplier=4 ka reason nhi pata hai
    int bufferSize = BUFFER_SIZE_MUTLIPLIER * minBufferSizeInBytes;
    //Log.i("SpeechRecord buffer size: " + bufferSize + ", min size = " + minBufferSizeInBytes);
    return bufferSize;
}
 
Example 3
Source File: AudioProcessor.java    From guitar-tuner with Apache License 2.0 5 votes vote down vote up
public void init() {
    int bufSize = 16384;
    int avalaibleSampleRates = SAMPLE_RATES.length;
    int i = 0;
    do {
        int sampleRate = SAMPLE_RATES[i];
        int minBufSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
        if (minBufSize != AudioRecord.ERROR_BAD_VALUE && minBufSize != AudioRecord.ERROR) {
            mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, Math.max(bufSize, minBufSize * 4));
        }
        i++;
    }
    while (i < avalaibleSampleRates && (mAudioRecord == null || mAudioRecord.getState() != AudioRecord.STATE_INITIALIZED));
}
 
Example 4
Source File: SampleRateCalculator.java    From voice-pitch-analyzer with GNU Affero General Public License v3.0 5 votes vote down vote up
public static int getMaxSupportedSampleRate() {
/*
 * Valid Audio Sample rates
 *
 * @see <a
 * href="http://en.wikipedia.org/wiki/Sampling_%28signal_processing%29"
 * >Wikipedia</a>
 */
    final int validSampleRates[] = new int[]{
            47250, 44100, 44056, 37800, 32000, 22050, 16000, 11025, 4800, 8000,};
/*
 * Selecting default audio input source for recording since
 * AudioFormat.CHANNEL_CONFIGURATION_DEFAULT is deprecated and selecting
 * default encoding format.
 */
    for (int i = 0; i < validSampleRates.length; i++) {
        int result = AudioRecord.getMinBufferSize(validSampleRates[i],
                android.media.AudioFormat.CHANNEL_IN_MONO,
                android.media.AudioFormat.ENCODING_PCM_16BIT);
        if (result != AudioRecord.ERROR
                && result != AudioRecord.ERROR_BAD_VALUE && result > 0) {
            // return the mininum supported audio sample rate
            return validSampleRates[i];
        }
    }
    // If none of the sample rates are supported return -1 handle it in
    // calling method
    return -1;
}
 
Example 5
Source File: SampleRateCalculator.java    From voice-pitch-analyzer with GNU Affero General Public License v3.0 5 votes vote down vote up
public static ArrayList<Integer> getAllSupportedSampleRates() {
/*
 *get all supported sample rates
 *
 * @see <a
 * href="http://en.wikipedia.org/wiki/Sampling_%28signal_processing%29"
 * >Wikipedia</a>
 */
    final int validSampleRates[] = new int[]{ 5644800, 2822400, 352800, 192000, 176400, 96000,
            88200, 50400, 50000, 4800, 47250, 44100, 44056, 37800, 32000, 22050, 16000, 11025, 8000, };
/*
 * Selecting default audio input source for recording since
 * AudioFormat.CHANNEL_CONFIGURATION_DEFAULT is deprecated and selecting
 * default encoding format.
 */

    ArrayList<Integer> supportedSampleRates = new ArrayList<Integer>();
    for (int i = 0; i < validSampleRates.length; i++) {
        int result = AudioRecord.getMinBufferSize(validSampleRates[i],
                android.media.AudioFormat.CHANNEL_IN_MONO,
                android.media.AudioFormat.ENCODING_PCM_16BIT);
        if (result != AudioRecord.ERROR
                && result != AudioRecord.ERROR_BAD_VALUE && result > 0) {
            // return the mininum supported audio sample rate
            supportedSampleRates.add(validSampleRates[i]);
        }
    }
    // If none of the sample rates are supported return -1 handle it in
    // calling method
    return supportedSampleRates;
}
 
Example 6
Source File: AudioRecorder.java    From connectivity-samples with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean validSize(int size) {
  return size != AudioRecord.ERROR && size != AudioRecord.ERROR_BAD_VALUE;
}
 
Example 7
Source File: AudioRecorder.java    From react-native-google-nearby-connection with MIT License 4 votes vote down vote up
@Override
protected boolean validSize(int size) {
	return size != AudioRecord.ERROR && size != AudioRecord.ERROR_BAD_VALUE;
}
 
Example 8
Source File: SaiyRecorder.java    From Saiy-PS with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Calculate the buffer size.
 *
 * @return the calculated buffer size
 */
private int calculateBufferSize() {

    framePeriod = sampleRateInHz * TIMER_INTERVAL / 1000;
    int bufferSize = framePeriod * 2 * bSamples * nChannels / 8;

    if (DEBUG) {
        MyLog.i(CLS_NAME, "bufferSize: " + bufferSize);
    }

    final int minBuff = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);

    switch (minBuff) {

        case AudioRecord.ERROR:
        case AudioRecord.ERROR_BAD_VALUE:
            if (DEBUG) {
                MyLog.w(CLS_NAME, "AudioRecord.ERROR/ERROR_BAD_VALUE");
            }
            break;
        default:

            if (DEBUG) {
                MyLog.i(CLS_NAME, "minBuff: " + minBuff);
            }

            if (bufferSize < minBuff) {
                bufferSize = minBuff;

                // Unused for now
                framePeriod = bufferSize / (2 * bSamples * nChannels / 8);
            }

            break;
    }


    if (DEBUG) {
        MyLog.i(CLS_NAME, "bufferSize returning: " + bufferSize);
    }

    return bufferSize;
}
 
Example 9
Source File: MicOpusRecorder.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
/**
 * 音声をレコードして、MediaCodec に渡します.
 */
private void recordAudio() throws NativeInterfaceException {
    int samplingRate = mSamplingRate.getValue();
    int channels = mChannels == 1 ? AudioFormat.CHANNEL_IN_MONO : AudioFormat.CHANNEL_IN_STEREO;
    int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
    int bufferSize = AudioRecord.getMinBufferSize(samplingRate, channels, audioFormat) * 4;
    int oneFrameDataCount = mSamplingRate.getValue() / mFrameSize.getFps();

    mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT,
            samplingRate,
            channels,
            audioFormat,
            bufferSize);

    if (mAudioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
        if (mAudioRecordCallback != null) {
            mAudioRecordCallback.onEncoderError();
        }
        return;
    }

    if (mUseAEC && AcousticEchoCanceler.isAvailable()) {
        // ノイズキャンセラー
        mEchoCanceler = AcousticEchoCanceler.create(mAudioRecord.getAudioSessionId());
        if (mEchoCanceler != null) {
            int ret = mEchoCanceler.setEnabled(true);
            if (ret != AudioEffect.SUCCESS) {
                if (DEBUG) {
                    Log.w(TAG, "AcousticEchoCanceler is not supported.");
                }
            }
        }
    }

    OpusEncoder opusEncoder = null;

    try {
        opusEncoder = new OpusEncoder(mSamplingRate, mChannels, mFrameSize, mBitRate, mApplication);

        mAudioRecord.startRecording();

        short[] emptyBuffer = new short[oneFrameDataCount];
        short[] pcmBuffer = new short[oneFrameDataCount];
        byte[] opusFrameBuffer = opusEncoder.bufferAllocate();
        while (!mStopFlag) {
            int readSize = mAudioRecord.read(pcmBuffer, 0, oneFrameDataCount);
            if (readSize > 0) {
                int opusFrameBufferLength;
                if (isMute()) {
                    opusFrameBufferLength = opusEncoder.encode(emptyBuffer, readSize, opusFrameBuffer);
                } else {
                    opusFrameBufferLength = opusEncoder.encode(pcmBuffer, readSize, opusFrameBuffer);
                }

                if (opusFrameBufferLength > 0 && mAudioRecordCallback != null) {
                    mAudioRecordCallback.onPeriodicNotification(opusFrameBuffer, opusFrameBufferLength);
                }
            } else if (readSize == AudioRecord.ERROR_INVALID_OPERATION) {
                if (DEBUG) {
                    Log.e(TAG, "Invalid operation error.");
                }
                break;
            } else if (readSize == AudioRecord.ERROR_BAD_VALUE) {
                if (DEBUG) {
                    Log.e(TAG, "Bad value error.");
                }
                break;
            } else if (readSize == AudioRecord.ERROR) {
                if (DEBUG) {
                    Log.e(TAG, "Unknown error.");
                }
                break;
            }
        }
    } finally {
        if (mEchoCanceler != null) {
            mEchoCanceler.release();
            mEchoCanceler = null;
        }

        if (opusEncoder != null) {
            opusEncoder.release();
        }
    }
}