Android AudioRecord Supported Sampling Rates

前端 未结 8 659
悲&欢浪女
悲&欢浪女 2020-11-28 22:47

I\'m trying to figure out what sampling rates are supported for phones running Android 2.2 and greater. We\'d like to sample at a rate lower than 44.1kHz and not have to re

相关标签:
8条回答
  • 2020-11-28 23:04

    Unfortunately not even all phones support the supposedly guaranteed 44.1kHz rate :(

    I' ve been testing a Samsung GalaxyY (GT-S5360L) and if you record from the Camcorder source (ambience microphone), the only supported rates are 8kHz and 16kHz. Recording @ 44.1kHz produces utter garbage and @ 11.025kHz produces a pitch-altered recording with slightly less duration than the original sound.

    Moreover, both strategies suggested by @Yahma and @Tom fail on this particular phone, as it is possible to receive a positive, minimum-buffer size from an unsupported configuration, and worse, I've been forced to reset the phone to get the audio stack working again, after attempting to use an AudioRecord class initialized from parameters that produce a supposedly valid, (non-exception raising) AudioTrack or AudioRecord instance.

    I'm frankly a little bit worried at the problems I envision when releasing a sound-app to the wild. In our case, we are being forced to introduce a costly sample-rate-conversion layer if we expect to reuse our algorithms (expecting a 44.1kHz recording rate)on this particular phone model.

    :(

    0 讨论(0)
  • 2020-11-28 23:07

    Just some updated information here. I spent some time trying to get access to recording from the microphone to work with Android 6 (4.4 KitKat was fine). The error shown was the same as I got for 4.4 when using the wrong settings for sample rate/pcm etc. But my problem was in fact that the Permissions in AndroidManifest.xml are no longer sufficient to request access to the Microphone and in fact this now needs to be done run time: https://developer.android.com/training/permissions/requesting.html

    0 讨论(0)
  • 2020-11-28 23:09

    Android has AudioManager.getProperty() function to acquire minimum buffer size and get the preferred sample rate for audio record and playback. But yes of course, AudioManager.getProperty() is not available on API level < 17. Here's an example code sample on how to use this API.

    // To get preferred buffer size and sampling rate.
    AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
    String rate = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
    String size = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
    Log.d("Buffer Size and sample rate", "Size :" + size + " & Rate: " + rate);
    

    Though its a late answer, I thought this might be useful.

    0 讨论(0)
  • 2020-11-28 23:11

    I have a phone (Acer Z3) where I get a positive buffer size returned from AudioRecord.getMinBufferSize(...) when testing 11025 Hz. However, if I subsequently run

    audioRecord = new AudioRecord(...);
    int state = audioRecord.getState();
    if (state != AudioRecord.STATE_INITIALIZED) ...
    

    I can see that this sampling rate in fact does not represent a valid configuration (as pointed out by user1222021 on Jun 5 '12). So my solution is to run both tests to find a valid sampling rate.

    0 讨论(0)
  • 2020-11-28 23:19

    I'd like to provide an alternative to Yahma's answer.

    I agree with his/her proposition that it must be tested (though presumably it varies according to the model, not the device), but using getMinBufferSize seems a bit indirect to me.

    In order to test whether a desired sample rate is supported I suggest attempting to construct an AudioTrack instance with the desired sample rate - if the specified sample rate is not supported you will get an exception of the form:

    "java.lang.IllegalArgumentException: 2756Hz is not a supported sample rate"

    0 讨论(0)
  • 2020-11-28 23:23

    This method gives the minimum audio sample rate supported by your device.

    NOTE : You may reverse the for loop to get the maximum sample rate supported by your device (Don't forget to change the method name).

    NOTE 2 : Though android doc says upto 48000(48khz) sample rate is supported ,I have added all the possible sampling rates (as in wikipedia) since who know new devices may record UHD audio in higher (sampling) framerates.

    private int getMinSupportedSampleRate() {
        /*
         * Valid Audio Sample rates
         * 
         * @see <a
         * href="http://en.wikipedia.org/wiki/Sampling_%28signal_processing%29"
         * >Wikipedia</a>
         */
        final int validSampleRates[] = new int[] { 8000, 11025, 16000, 22050,
                32000, 37800, 44056, 44100, 47250, 48000, 50000, 50400, 88200,
                96000, 176400, 192000, 352800, 2822400, 5644800 };
        /*
         * 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],
                    AudioFormat.CHANNEL_IN_DEFAULT,
                    AudioFormat.ENCODING_DEFAULT);
            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;
    }
    
    0 讨论(0)
提交回复
热议问题