Swapnil
Swapnil

Reputation: 2051

Getting bit rate or bit depth of an audio wav file

I am using AudioTrack to play a .wav audio file. Everything is fine but I for now I have hard coded the bit depth of the audio file while initializing the AudioTrack object in STATIC_MODE.

mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, mSampleRate, mChannels,
            AudioFormat.ENCODING_PCM_16BIT, dataSize, AudioTrack.MODE_STATIC);

I want to get the bit-depth/bitrate of the .wav file programmatically and then set the encoding in the AudioTrack object. I have tried to use MediaExtractor and MediaFormat but it gives me only the following information:

mediaFormat:{mime=audio/raw, durationUs=10080000, channel-count=1, channel-mask=0, sample-rate=16000}

In the documentation of MediaFormat, it says that KEY_BIT_RATE is encoder-only. Does that mean that I can only use this option while encoding raw PCM bits. If yes, what can be any other way to read the bitrate/bit-depth programmatically? I have already tried getting the information for the same file on the terminal using the mediainfo binary and it gives me the correct bit depth.

Upvotes: 0

Views: 2882

Answers (3)

mikeLundquist
mikeLundquist

Reputation: 1009

You could always look at the 34th and 35th bytes of the wav file's header. See this resource.

Upvotes: 1

Big Bear
Big Bear

Reputation: 1

        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        int bitDepth;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.N){
            bitDepth = format.getInteger("pcm-encoding");
        } else{
            bitDepth = format.getInteger("bit-width");

and the format above android 7.0 like

 mime: string(audio/raw), channel-count: int32(2), sample-rate: int32(48000), pcm-encoding: int32(2)}

below android 7.0 like

mime: string(audio/raw), channel-count: int32(2), sample-rate: int32(48000), bit-width: int32(16), what: int32(1869968451)}

https://developer.android.com/reference/android/media/MediaFormat.html#KEY_PCM_ENCODING

Upvotes: 0

BiggySmallz
BiggySmallz

Reputation: 1

    MediaExtractor mediaExtractor = new MediaExtractor();
    try {
        mediaExtractor.setDataSource(path);
        return mediaExtractor.getTrackFormat(0).getInteger("bit-per-sample");
    } catch (Exception e) {
        e.printStackTrace();
    }

Upvotes: 0

Related Questions