Builds a simple AudioSpecificConfig, as defined in ISO 14496-3 1.6.2.1 - Android Media

Android examples for Media:Audio

Description

Builds a simple AudioSpecificConfig, as defined in ISO 14496-3 1.6.2.1

Demo Code

/*// w  ww .  ja va2s. c o m
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

public class Main {
    private static final int[] AUDIO_SPECIFIC_CONFIG_SAMPLING_RATE_TABLE = new int[] {
            96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000,
            12000, 11025, 8000, 7350 };
    private static final int[] AUDIO_SPECIFIC_CONFIG_CHANNEL_COUNT_TABLE = new int[] {
            0, 1, 2, 3, 4, 5, 6, 8 };

    /**
     * Builds a simple AudioSpecificConfig, as defined in ISO 14496-3 1.6.2.1
     *
     * @param audioObjectType The audio object type.
     * @param sampleRateIndex The sample rate index.
     * @param channelConfig The channel configuration.
     * @return The AudioSpecificConfig.
     */
    public static byte[] buildAudioSpecificConfig(int audioObjectType,
            int sampleRateIndex, int channelConfig) {
        byte[] audioSpecificConfig = new byte[2];
        audioSpecificConfig[0] = (byte) ((audioObjectType << 3) & 0xF8 | (sampleRateIndex >> 1) & 0x07);
        audioSpecificConfig[1] = (byte) ((sampleRateIndex << 7) & 0x80 | (channelConfig << 3) & 0x78);
        return audioSpecificConfig;
    }

    /**
     * Builds a simple HE-AAC LC AudioSpecificConfig, as defined in ISO 14496-3 1.6.2.1
     *
     * @param sampleRate The sample rate in Hz.
     * @param numChannels The number of channels.
     * @return The AudioSpecificConfig.
     */
    public static byte[] buildAudioSpecificConfig(int sampleRate,
            int numChannels) {
        int sampleRateIndex = -1;
        for (int i = 0; i < AUDIO_SPECIFIC_CONFIG_SAMPLING_RATE_TABLE.length; ++i) {
            if (sampleRate == AUDIO_SPECIFIC_CONFIG_SAMPLING_RATE_TABLE[i]) {
                sampleRateIndex = i;
            }
        }
        int channelConfig = -1;
        for (int i = 0; i < AUDIO_SPECIFIC_CONFIG_CHANNEL_COUNT_TABLE.length; ++i) {
            if (numChannels == AUDIO_SPECIFIC_CONFIG_CHANNEL_COUNT_TABLE[i]) {
                channelConfig = i;
            }
        }
        // The full specification for AudioSpecificConfig is stated in ISO 14496-3 Section 1.6.2.1
        byte[] csd = new byte[2];
        csd[0] = (byte) ((2 /* AAC LC */<< 3) | (sampleRateIndex >> 1));
        csd[1] = (byte) (((sampleRateIndex & 0x1) << 7) | (channelConfig << 3));
        return csd;
    }
}

Related Tutorials