Java Code Examples for android.media.AudioFormat#CHANNEL_INVALID

The following examples show how to use android.media.AudioFormat#CHANNEL_INVALID . 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: 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 2
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 3
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 4
Source File: DefaultAudioSink.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@Override
public void configure(
    @C.Encoding int inputEncoding,
    int inputChannelCount,
    int inputSampleRate,
    int specifiedBufferSize,
    @Nullable int[] outputChannels,
    int trimStartFrames,
    int trimEndFrames)
    throws ConfigurationException {
  if (Util.SDK_INT < 21 && inputChannelCount == 8 && outputChannels == null) {
    // AudioTrack doesn't support 8 channel output before Android L. Discard the last two (side)
    // channels to give a 6 channel stream that is supported.
    outputChannels = new int[6];
    for (int i = 0; i < outputChannels.length; i++) {
      outputChannels[i] = i;
    }
  }

  boolean isInputPcm = Util.isEncodingLinearPcm(inputEncoding);
  boolean processingEnabled = isInputPcm && inputEncoding != C.ENCODING_PCM_FLOAT;
  int sampleRate = inputSampleRate;
  int channelCount = inputChannelCount;
  @C.Encoding int encoding = inputEncoding;
  boolean shouldConvertHighResIntPcmToFloat =
      enableConvertHighResIntPcmToFloat
          && supportsOutput(inputChannelCount, C.ENCODING_PCM_FLOAT)
          && Util.isEncodingHighResolutionIntegerPcm(inputEncoding);
  AudioProcessor[] availableAudioProcessors =
      shouldConvertHighResIntPcmToFloat
          ? toFloatPcmAvailableAudioProcessors
          : toIntPcmAvailableAudioProcessors;
  if (processingEnabled) {
    trimmingAudioProcessor.setTrimFrameCount(trimStartFrames, trimEndFrames);
    channelMappingAudioProcessor.setChannelMap(outputChannels);
    AudioProcessor.AudioFormat inputAudioFormat =
        new AudioProcessor.AudioFormat(sampleRate, channelCount, encoding);
    AudioProcessor.AudioFormat outputAudioFormat = inputAudioFormat;
    for (AudioProcessor audioProcessor : availableAudioProcessors) {
      try {
        outputAudioFormat = audioProcessor.configure(inputAudioFormat);
      } catch (UnhandledAudioFormatException e) {
        throw new ConfigurationException(e);
      }
      if (audioProcessor.isActive()) {
        inputAudioFormat = outputAudioFormat;
      }
    }
    sampleRate = outputAudioFormat.sampleRate;
    channelCount = outputAudioFormat.channelCount;
    encoding = outputAudioFormat.encoding;
  }

  int outputChannelConfig = getChannelConfig(channelCount, isInputPcm);
  if (outputChannelConfig == AudioFormat.CHANNEL_INVALID) {
    throw new ConfigurationException("Unsupported channel count: " + channelCount);
  }

  int inputPcmFrameSize =
      isInputPcm ? Util.getPcmFrameSize(inputEncoding, inputChannelCount) : C.LENGTH_UNSET;
  int outputPcmFrameSize =
      isInputPcm ? Util.getPcmFrameSize(encoding, channelCount) : C.LENGTH_UNSET;
  boolean canApplyPlaybackParameters = processingEnabled && !shouldConvertHighResIntPcmToFloat;
  Configuration pendingConfiguration =
      new Configuration(
          isInputPcm,
          inputPcmFrameSize,
          inputSampleRate,
          outputPcmFrameSize,
          sampleRate,
          outputChannelConfig,
          encoding,
          specifiedBufferSize,
          processingEnabled,
          canApplyPlaybackParameters,
          availableAudioProcessors);
  if (isInitialized()) {
    this.pendingConfiguration = pendingConfiguration;
  } else {
    configuration = pendingConfiguration;
  }
}
 
Example 5
Source File: DefaultAudioSink.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void configure(
    @C.Encoding int inputEncoding,
    int inputChannelCount,
    int inputSampleRate,
    int specifiedBufferSize,
    @Nullable int[] outputChannels,
    int trimStartFrames,
    int trimEndFrames)
    throws ConfigurationException {
  if (Util.SDK_INT < 21 && inputChannelCount == 8 && outputChannels == null) {
    // AudioTrack doesn't support 8 channel output before Android L. Discard the last two (side)
    // channels to give a 6 channel stream that is supported.
    outputChannels = new int[6];
    for (int i = 0; i < outputChannels.length; i++) {
      outputChannels[i] = i;
    }
  }

  boolean isInputPcm = Util.isEncodingLinearPcm(inputEncoding);
  boolean processingEnabled = isInputPcm && inputEncoding != C.ENCODING_PCM_FLOAT;
  int sampleRate = inputSampleRate;
  int channelCount = inputChannelCount;
  @C.Encoding int encoding = inputEncoding;
  boolean shouldConvertHighResIntPcmToFloat =
      enableConvertHighResIntPcmToFloat
          && supportsOutput(inputChannelCount, C.ENCODING_PCM_FLOAT)
          && Util.isEncodingHighResolutionIntegerPcm(inputEncoding);
  AudioProcessor[] availableAudioProcessors =
      shouldConvertHighResIntPcmToFloat
          ? toFloatPcmAvailableAudioProcessors
          : toIntPcmAvailableAudioProcessors;
  boolean flushAudioProcessors = false;
  if (processingEnabled) {
    trimmingAudioProcessor.setTrimFrameCount(trimStartFrames, trimEndFrames);
    channelMappingAudioProcessor.setChannelMap(outputChannels);
    for (AudioProcessor audioProcessor : availableAudioProcessors) {
      try {
        flushAudioProcessors |= audioProcessor.configure(sampleRate, channelCount, encoding);
      } catch (AudioProcessor.UnhandledFormatException e) {
        throw new ConfigurationException(e);
      }
      if (audioProcessor.isActive()) {
        channelCount = audioProcessor.getOutputChannelCount();
        sampleRate = audioProcessor.getOutputSampleRateHz();
        encoding = audioProcessor.getOutputEncoding();
      }
    }
  }

  int outputChannelConfig = getChannelConfig(channelCount, isInputPcm);
  if (outputChannelConfig == AudioFormat.CHANNEL_INVALID) {
    throw new ConfigurationException("Unsupported channel count: " + channelCount);
  }

  int inputPcmFrameSize =
      isInputPcm ? Util.getPcmFrameSize(inputEncoding, inputChannelCount) : C.LENGTH_UNSET;
  int outputPcmFrameSize =
      isInputPcm ? Util.getPcmFrameSize(encoding, channelCount) : C.LENGTH_UNSET;
  boolean canApplyPlaybackParameters = processingEnabled && !shouldConvertHighResIntPcmToFloat;
  Configuration pendingConfiguration =
      new Configuration(
          isInputPcm,
          inputPcmFrameSize,
          inputSampleRate,
          outputPcmFrameSize,
          sampleRate,
          outputChannelConfig,
          encoding,
          specifiedBufferSize,
          processingEnabled,
          canApplyPlaybackParameters,
          availableAudioProcessors);
  // If we have a pending configuration already, we always drain audio processors as the preceding
  // configuration may have required it (even if this one doesn't).
  boolean drainAudioProcessors = flushAudioProcessors || this.pendingConfiguration != null;
  if (isInitialized()
      && (!pendingConfiguration.canReuseAudioTrack(configuration) || drainAudioProcessors)) {
    this.pendingConfiguration = pendingConfiguration;
  } else {
    configuration = pendingConfiguration;
  }
}
 
Example 6
Source File: DefaultAudioSink.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void configure(
    @C.Encoding int inputEncoding,
    int inputChannelCount,
    int inputSampleRate,
    int specifiedBufferSize,
    @Nullable int[] outputChannels,
    int trimStartFrames,
    int trimEndFrames)
    throws ConfigurationException {
  if (Util.SDK_INT < 21 && inputChannelCount == 8 && outputChannels == null) {
    // AudioTrack doesn't support 8 channel output before Android L. Discard the last two (side)
    // channels to give a 6 channel stream that is supported.
    outputChannels = new int[6];
    for (int i = 0; i < outputChannels.length; i++) {
      outputChannels[i] = i;
    }
  }

  boolean isInputPcm = Util.isEncodingLinearPcm(inputEncoding);
  boolean processingEnabled = isInputPcm && inputEncoding != C.ENCODING_PCM_FLOAT;
  int sampleRate = inputSampleRate;
  int channelCount = inputChannelCount;
  @C.Encoding int encoding = inputEncoding;
  boolean shouldConvertHighResIntPcmToFloat =
      enableConvertHighResIntPcmToFloat
          && supportsOutput(inputChannelCount, C.ENCODING_PCM_FLOAT)
          && Util.isEncodingHighResolutionIntegerPcm(inputEncoding);
  AudioProcessor[] availableAudioProcessors =
      shouldConvertHighResIntPcmToFloat
          ? toFloatPcmAvailableAudioProcessors
          : toIntPcmAvailableAudioProcessors;
  boolean flushAudioProcessors = false;
  if (processingEnabled) {
    trimmingAudioProcessor.setTrimFrameCount(trimStartFrames, trimEndFrames);
    channelMappingAudioProcessor.setChannelMap(outputChannels);
    for (AudioProcessor audioProcessor : availableAudioProcessors) {
      try {
        flushAudioProcessors |= audioProcessor.configure(sampleRate, channelCount, encoding);
      } catch (AudioProcessor.UnhandledFormatException e) {
        throw new ConfigurationException(e);
      }
      if (audioProcessor.isActive()) {
        channelCount = audioProcessor.getOutputChannelCount();
        sampleRate = audioProcessor.getOutputSampleRateHz();
        encoding = audioProcessor.getOutputEncoding();
      }
    }
  }

  int outputChannelConfig = getChannelConfig(channelCount, isInputPcm);
  if (outputChannelConfig == AudioFormat.CHANNEL_INVALID) {
    throw new ConfigurationException("Unsupported channel count: " + channelCount);
  }

  int inputPcmFrameSize =
      isInputPcm ? Util.getPcmFrameSize(inputEncoding, inputChannelCount) : C.LENGTH_UNSET;
  int outputPcmFrameSize =
      isInputPcm ? Util.getPcmFrameSize(encoding, channelCount) : C.LENGTH_UNSET;
  boolean canApplyPlaybackParameters = processingEnabled && !shouldConvertHighResIntPcmToFloat;
  Configuration pendingConfiguration =
      new Configuration(
          isInputPcm,
          inputPcmFrameSize,
          inputSampleRate,
          outputPcmFrameSize,
          sampleRate,
          outputChannelConfig,
          encoding,
          specifiedBufferSize,
          processingEnabled,
          canApplyPlaybackParameters,
          availableAudioProcessors);
  // If we have a pending configuration already, we always drain audio processors as the preceding
  // configuration may have required it (even if this one doesn't).
  boolean drainAudioProcessors = flushAudioProcessors || this.pendingConfiguration != null;
  if (isInitialized()
      && (!pendingConfiguration.canReuseAudioTrack(configuration) || drainAudioProcessors)) {
    this.pendingConfiguration = pendingConfiguration;
  } else {
    configuration = pendingConfiguration;
  }
}