Java Code Examples for com.google.android.exoplayer2.util.Util#getPcmFrameSize()

The following examples show how to use com.google.android.exoplayer2.util.Util#getPcmFrameSize() . 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: TeeAudioProcessor.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
private void writeFileHeader(RandomAccessFile randomAccessFile) throws IOException {
  // Write the start of the header as big endian data.
  randomAccessFile.writeInt(WavUtil.RIFF_FOURCC);
  randomAccessFile.writeInt(-1);
  randomAccessFile.writeInt(WavUtil.WAVE_FOURCC);
  randomAccessFile.writeInt(WavUtil.FMT_FOURCC);

  // Write the rest of the header as little endian data.
  scratchByteBuffer.clear();
  scratchByteBuffer.putInt(16);
  scratchByteBuffer.putShort((short) WavUtil.getTypeForEncoding(encoding));
  scratchByteBuffer.putShort((short) channelCount);
  scratchByteBuffer.putInt(sampleRateHz);
  int bytesPerSample = Util.getPcmFrameSize(encoding, channelCount);
  scratchByteBuffer.putInt(bytesPerSample * sampleRateHz);
  scratchByteBuffer.putShort((short) bytesPerSample);
  scratchByteBuffer.putShort((short) (8 * bytesPerSample / channelCount));
  randomAccessFile.write(scratchBuffer, 0, scratchByteBuffer.position());

  // Write the start of the data chunk as big endian data.
  randomAccessFile.writeInt(WavUtil.DATA_FOURCC);
  randomAccessFile.writeInt(-1);
}
 
Example 2
Source File: TeeAudioProcessor.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
private void writeFileHeader(RandomAccessFile randomAccessFile) throws IOException {
  // Write the start of the header as big endian data.
  randomAccessFile.writeInt(WavUtil.RIFF_FOURCC);
  randomAccessFile.writeInt(-1);
  randomAccessFile.writeInt(WavUtil.WAVE_FOURCC);
  randomAccessFile.writeInt(WavUtil.FMT_FOURCC);

  // Write the rest of the header as little endian data.
  scratchByteBuffer.clear();
  scratchByteBuffer.putInt(16);
  scratchByteBuffer.putShort((short) WavUtil.getTypeForEncoding(encoding));
  scratchByteBuffer.putShort((short) channelCount);
  scratchByteBuffer.putInt(sampleRateHz);
  int bytesPerSample = Util.getPcmFrameSize(encoding, channelCount);
  scratchByteBuffer.putInt(bytesPerSample * sampleRateHz);
  scratchByteBuffer.putShort((short) bytesPerSample);
  scratchByteBuffer.putShort((short) (8 * bytesPerSample / channelCount));
  randomAccessFile.write(scratchBuffer, 0, scratchByteBuffer.position());

  // Write the start of the data chunk as big endian data.
  randomAccessFile.writeInt(WavUtil.DATA_FOURCC);
  randomAccessFile.writeInt(-1);
}
 
Example 3
Source File: TrimmingAudioProcessor.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean configure(int sampleRateHz, int channelCount, @C.PcmEncoding int encoding)
    throws UnhandledFormatException {
  if (encoding != OUTPUT_ENCODING) {
    throw new UnhandledFormatException(sampleRateHz, channelCount, encoding);
  }
  if (endBufferSize > 0) {
    trimmedFrameCount += endBufferSize / bytesPerFrame;
  }
  bytesPerFrame = Util.getPcmFrameSize(OUTPUT_ENCODING, channelCount);
  endBuffer = new byte[trimEndFrames * bytesPerFrame];
  endBufferSize = 0;
  pendingTrimStartBytes = trimStartFrames * bytesPerFrame;
  boolean wasActive = isActive;
  isActive = trimStartFrames != 0 || trimEndFrames != 0;
  receivedInputSinceConfigure = false;
  setInputFormat(sampleRateHz, channelCount, encoding);
  return wasActive != isActive;
}
 
Example 4
Source File: TeeAudioProcessor.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
private void writeFileHeader(RandomAccessFile randomAccessFile) throws IOException {
  // Write the start of the header as big endian data.
  randomAccessFile.writeInt(WavUtil.RIFF_FOURCC);
  randomAccessFile.writeInt(-1);
  randomAccessFile.writeInt(WavUtil.WAVE_FOURCC);
  randomAccessFile.writeInt(WavUtil.FMT_FOURCC);

  // Write the rest of the header as little endian data.
  scratchByteBuffer.clear();
  scratchByteBuffer.putInt(16);
  scratchByteBuffer.putShort((short) WavUtil.getTypeForEncoding(encoding));
  scratchByteBuffer.putShort((short) channelCount);
  scratchByteBuffer.putInt(sampleRateHz);
  int bytesPerSample = Util.getPcmFrameSize(encoding, channelCount);
  scratchByteBuffer.putInt(bytesPerSample * sampleRateHz);
  scratchByteBuffer.putShort((short) bytesPerSample);
  scratchByteBuffer.putShort((short) (8 * bytesPerSample / channelCount));
  randomAccessFile.write(scratchBuffer, 0, scratchByteBuffer.position());

  // Write the start of the data chunk as big endian data.
  randomAccessFile.writeInt(WavUtil.DATA_FOURCC);
  randomAccessFile.writeInt(-1);
}
 
Example 5
Source File: TrimmingAudioProcessor.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean configure(int sampleRateHz, int channelCount, @C.PcmEncoding int encoding)
    throws UnhandledFormatException {
  if (encoding != OUTPUT_ENCODING) {
    throw new UnhandledFormatException(sampleRateHz, channelCount, encoding);
  }
  if (endBufferSize > 0) {
    trimmedFrameCount += endBufferSize / bytesPerFrame;
  }
  bytesPerFrame = Util.getPcmFrameSize(OUTPUT_ENCODING, channelCount);
  endBuffer = new byte[trimEndFrames * bytesPerFrame];
  endBufferSize = 0;
  pendingTrimStartBytes = trimStartFrames * bytesPerFrame;
  boolean wasActive = isActive;
  isActive = trimStartFrames != 0 || trimEndFrames != 0;
  receivedInputSinceConfigure = false;
  setInputFormat(sampleRateHz, channelCount, encoding);
  return wasActive != isActive;
}
 
Example 6
Source File: AudioProcessor.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
public AudioFormat(int sampleRate, int channelCount, @C.PcmEncoding int encoding) {
  this.sampleRate = sampleRate;
  this.channelCount = channelCount;
  this.encoding = encoding;
  bytesPerFrame =
      Util.isEncodingLinearPcm(encoding)
          ? Util.getPcmFrameSize(encoding, channelCount)
          : Format.NO_VALUE;
}
 
Example 7
Source File: SilenceMediaSource.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
private static long getAudioByteCount(long durationUs) {
  long audioSampleCount = durationUs * SAMPLE_RATE_HZ / C.MICROS_PER_SECOND;
  return Util.getPcmFrameSize(ENCODING, CHANNEL_COUNT) * audioSampleCount;
}
 
Example 8
Source File: SilenceMediaSource.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
private static long getAudioPositionUs(long bytes) {
  long audioSampleCount = bytes / Util.getPcmFrameSize(ENCODING, CHANNEL_COUNT);
  return audioSampleCount * C.MICROS_PER_SECOND / SAMPLE_RATE_HZ;
}
 
Example 9
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 10
Source File: SilenceMediaSource.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private static long getAudioByteCount(long durationUs) {
  long audioSampleCount = durationUs * SAMPLE_RATE_HZ / C.MICROS_PER_SECOND;
  return Util.getPcmFrameSize(ENCODING, CHANNEL_COUNT) * audioSampleCount;
}
 
Example 11
Source File: SilenceMediaSource.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private static long getAudioPositionUs(long bytes) {
  long audioSampleCount = bytes / Util.getPcmFrameSize(ENCODING, CHANNEL_COUNT);
  return audioSampleCount * C.MICROS_PER_SECOND / SAMPLE_RATE_HZ;
}
 
Example 12
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 13
Source File: SilenceMediaSource.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
private static long getAudioByteCount(long durationUs) {
  long audioSampleCount = durationUs * SAMPLE_RATE_HZ / C.MICROS_PER_SECOND;
  return Util.getPcmFrameSize(ENCODING, CHANNEL_COUNT) * audioSampleCount;
}
 
Example 14
Source File: SilenceMediaSource.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
private static long getAudioPositionUs(long bytes) {
  long audioSampleCount = bytes / Util.getPcmFrameSize(ENCODING, CHANNEL_COUNT);
  return audioSampleCount * C.MICROS_PER_SECOND / SAMPLE_RATE_HZ;
}
 
Example 15
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;
  }
}