Java Code Examples for android.media.AudioFormat#CHANNEL_OUT_5POINT1

The following examples show how to use android.media.AudioFormat#CHANNEL_OUT_5POINT1 . 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: MediaCodecBridge.java    From 365browser with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private int getAudioFormat(int channelCount) {
    switch (channelCount) {
        case 1:
            return AudioFormat.CHANNEL_OUT_MONO;
        case 2:
            return AudioFormat.CHANNEL_OUT_STEREO;
        case 4:
            return AudioFormat.CHANNEL_OUT_QUAD;
        case 6:
            return AudioFormat.CHANNEL_OUT_5POINT1;
        case 8:
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND;
            } else {
                return AudioFormat.CHANNEL_OUT_7POINT1;
            }
        default:
            return AudioFormat.CHANNEL_OUT_DEFAULT;
    }
}
 
Example 2
Source File: SonicAudioPlayer.java    From AntennaPod-AudioPlayer with Apache License 2.0 6 votes vote down vote up
private static int findFormatFromChannels(int numChannels) {
    switch (numChannels) {
        case 1:
            return AudioFormat.CHANNEL_OUT_MONO;
        case 2:
            return AudioFormat.CHANNEL_OUT_STEREO;
        case 3:
            return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER;
        case 4:
            return AudioFormat.CHANNEL_OUT_QUAD;
        case 5:
            return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER;
        case 6:
            return AudioFormat.CHANNEL_OUT_5POINT1;
        case 7:
            return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER;
        case 8:
            if (Build.VERSION.SDK_INT >= 23) {
                return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND;
            } else {
                return -1;
            }
        default:
            return -1; // Error
    }
}
 
Example 3
Source File: Util.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the audio track channel configuration for the given channel count, or {@link
 * AudioFormat#CHANNEL_INVALID} if output is not poossible.
 *
 * @param channelCount The number of channels in the input audio.
 * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not
 *     possible.
 */
public static int getAudioTrackChannelConfig(int channelCount) {
  switch (channelCount) {
    case 1:
      return AudioFormat.CHANNEL_OUT_MONO;
    case 2:
      return AudioFormat.CHANNEL_OUT_STEREO;
    case 3:
      return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER;
    case 4:
      return AudioFormat.CHANNEL_OUT_QUAD;
    case 5:
      return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER;
    case 6:
      return AudioFormat.CHANNEL_OUT_5POINT1;
    case 7:
      return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER;
    case 8:
      if (Util.SDK_INT >= 23) {
        return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND;
      } else if (Util.SDK_INT >= 21) {
        // Equal to AudioFormat.CHANNEL_OUT_7POINT1_SURROUND, which is hidden before Android M.
        return AudioFormat.CHANNEL_OUT_5POINT1
            | AudioFormat.CHANNEL_OUT_SIDE_LEFT
            | AudioFormat.CHANNEL_OUT_SIDE_RIGHT;
      } else {
        // 8 ch output is not supported before Android L.
        return AudioFormat.CHANNEL_INVALID;
      }
    default:
      return AudioFormat.CHANNEL_INVALID;
  }
}
 
Example 4
Source File: Util.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the audio track channel configuration for the given channel count, or {@link
 * AudioFormat#CHANNEL_INVALID} if output is not poossible.
 *
 * @param channelCount The number of channels in the input audio.
 * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not
 *     possible.
 */
public static int getAudioTrackChannelConfig(int channelCount) {
  switch (channelCount) {
    case 1:
      return AudioFormat.CHANNEL_OUT_MONO;
    case 2:
      return AudioFormat.CHANNEL_OUT_STEREO;
    case 3:
      return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER;
    case 4:
      return AudioFormat.CHANNEL_OUT_QUAD;
    case 5:
      return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER;
    case 6:
      return AudioFormat.CHANNEL_OUT_5POINT1;
    case 7:
      return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER;
    case 8:
      if (Util.SDK_INT >= 23) {
        return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND;
      } else if (Util.SDK_INT >= 21) {
        // Equal to AudioFormat.CHANNEL_OUT_7POINT1_SURROUND, which is hidden before Android M.
        return AudioFormat.CHANNEL_OUT_5POINT1
            | AudioFormat.CHANNEL_OUT_SIDE_LEFT
            | AudioFormat.CHANNEL_OUT_SIDE_RIGHT;
      } else {
        // 8 ch output is not supported before Android L.
        return AudioFormat.CHANNEL_INVALID;
      }
    default:
      return AudioFormat.CHANNEL_INVALID;
  }
}
 
Example 5
Source File: Util.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the audio track channel configuration for the given channel count, or {@link
 * AudioFormat#CHANNEL_INVALID} if output is not poossible.
 *
 * @param channelCount The number of channels in the input audio.
 * @return The channel configuration or {@link AudioFormat#CHANNEL_INVALID} if output is not
 *     possible.
 */
public static int getAudioTrackChannelConfig(int channelCount) {
  switch (channelCount) {
    case 1:
      return AudioFormat.CHANNEL_OUT_MONO;
    case 2:
      return AudioFormat.CHANNEL_OUT_STEREO;
    case 3:
      return AudioFormat.CHANNEL_OUT_STEREO | AudioFormat.CHANNEL_OUT_FRONT_CENTER;
    case 4:
      return AudioFormat.CHANNEL_OUT_QUAD;
    case 5:
      return AudioFormat.CHANNEL_OUT_QUAD | AudioFormat.CHANNEL_OUT_FRONT_CENTER;
    case 6:
      return AudioFormat.CHANNEL_OUT_5POINT1;
    case 7:
      return AudioFormat.CHANNEL_OUT_5POINT1 | AudioFormat.CHANNEL_OUT_BACK_CENTER;
    case 8:
      if (Util.SDK_INT >= 23) {
        return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND;
      } else if (Util.SDK_INT >= 21) {
        // Equal to AudioFormat.CHANNEL_OUT_7POINT1_SURROUND, which is hidden before Android M.
        return AudioFormat.CHANNEL_OUT_5POINT1
            | AudioFormat.CHANNEL_OUT_SIDE_LEFT
            | AudioFormat.CHANNEL_OUT_SIDE_RIGHT;
      } else {
        // 8 ch output is not supported before Android L.
        return AudioFormat.CHANNEL_INVALID;
      }
    default:
      return AudioFormat.CHANNEL_INVALID;
  }
}
 
Example 6
Source File: AudioTrack.java    From Exoplayer_VLC with Apache License 2.0 4 votes vote down vote up
/**
 * Reconfigures the audio track to play back media in {@code format}. Buffers passed to
 * {@link #handleBuffer} must using the specified {@code encoding}, which should be a constant
 * from {@link AudioFormat}.
 *
 * @param format Specifies the channel count and sample rate to play back.
 * @param encoding The format in which audio is represented.
 * @param specifiedBufferSize A specific size for the playback buffer in bytes, or 0 to use a
 *     size inferred from the format.
 */
@SuppressLint("InlinedApi")
public void reconfigure(MediaFormat format, int encoding, int specifiedBufferSize) {
  int channelCount = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
  int channelConfig;
  switch (channelCount) {
    case 1:
      channelConfig = AudioFormat.CHANNEL_OUT_MONO;
      break;
    case 2:
      channelConfig = AudioFormat.CHANNEL_OUT_STEREO;
      break;
    case 6:
      channelConfig = AudioFormat.CHANNEL_OUT_5POINT1;
      break;
    case 8:
      channelConfig = AudioFormat.CHANNEL_OUT_7POINT1;
      break;
    default:
      throw new IllegalArgumentException("Unsupported channel count: " + channelCount);
  }

  int sampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);

  // TODO: Does channelConfig determine channelCount?
  boolean isAc3 = false;//encoding == AudioFormat.ENCODING_AC3 || encoding == AudioFormat.ENCODING_E_AC3;
  if (isInitialized() && this.sampleRate == sampleRate && this.channelConfig == channelConfig
      && !this.isAc3 && !isAc3) {
    // We already have an existing audio track with the correct sample rate and channel config.
    return;
  }

  reset();

  this.encoding = encoding;
  this.sampleRate = sampleRate;
  this.channelConfig = channelConfig;
  this.isAc3 = isAc3;
  ac3Bitrate = UNKNOWN_AC3_BITRATE; // Calculated on receiving the first buffer if isAc3 is true.
  frameSize = 2 * channelCount; // 2 bytes per 16 bit sample * number of channels.
  minBufferSize = android.media.AudioTrack.getMinBufferSize(sampleRate, channelConfig, encoding);

  if (specifiedBufferSize != 0) {
    bufferSize = specifiedBufferSize;
  } else {
    int multipliedBufferSize = minBufferSize * BUFFER_MULTIPLICATION_FACTOR;
    int minAppBufferSize = (int) durationUsToFrames(MIN_BUFFER_DURATION_US) * frameSize;
    int maxAppBufferSize = (int) Math.max(minBufferSize,
        durationUsToFrames(MAX_BUFFER_DURATION_US) * frameSize);
    bufferSize = multipliedBufferSize < minAppBufferSize ? minAppBufferSize
        : multipliedBufferSize > maxAppBufferSize ? maxAppBufferSize
        : multipliedBufferSize;
  }
}
 
Example 7
Source File: AudioPlayback.java    From MediaPlayer-Extended with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes or reinitializes the audio track with the supplied format for playback
 * while keeping the playstate. Keeps the current configuration and skips reinitialization
 * if the new format is the same as the current format.
 */
public void init(MediaFormat format) {
    Log.d(TAG, "init");

    boolean playing = false;

    if(isInitialized()) {
        if(!checkIfReinitializationRequired(format)) {
            // Set new format that equals the old one (in case we compare references somewhere)
            mAudioFormat = format;
            return;
        }

        playing = isPlaying();
        pause();
        stopAndRelease(false);
    } else {
        // deferred creation of the audio thread until its first use
        mAudioThread = new AudioThread();
        mAudioThread.setPaused(true);
        mAudioThread.start();
    }

    mAudioFormat = format;

    int channelCount = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
    int bytesPerSample = 2;
    mFrameSize = bytesPerSample * channelCount;
    mSampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);

    int channelConfig = AudioFormat.CHANNEL_OUT_DEFAULT;
    switch(channelCount) {
        case 1:
            channelConfig = AudioFormat.CHANNEL_OUT_MONO;
            break;
        case 2:
            channelConfig = AudioFormat.CHANNEL_OUT_STEREO;
            break;
        case 4:
            channelConfig = AudioFormat.CHANNEL_OUT_QUAD;
            break;
        case 6:
            channelConfig = AudioFormat.CHANNEL_OUT_5POINT1;
            break;
        case 8:
            channelConfig = AudioFormat.CHANNEL_OUT_7POINT1;
    }

    mPlaybackBufferSize = mFrameChunkSize * channelCount;

    mAudioTrack = new AudioTrack(
            mAudioStreamType,
            mSampleRate,
            channelConfig,
            AudioFormat.ENCODING_PCM_16BIT,
            mPlaybackBufferSize, // at least twice the size to enable double buffering (according to docs)
            AudioTrack.MODE_STREAM, mAudioSessionId);

    if(mAudioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
        stopAndRelease();
        throw new IllegalStateException("audio track init failed");
    }

    mAudioSessionId = mAudioTrack.getAudioSessionId();
    mAudioStreamType = mAudioTrack.getStreamType();
    setStereoVolume(mVolumeLeft, mVolumeRight);
    mPresentationTimeOffsetUs = PTS_NOT_SET;

    if(playing) {
        play();
    }
}