Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/dom/media/driftcontrol/   (Firefox Browser Version 153.0.1©)  Datei vom 27.6.2026 mit Größe 13 kB image not shown  

Quelle  DynamicResampler.h   Sprache: C

 

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */


#ifndef DOM_MEDIA_DRIFTCONTROL_DYNAMICRESAMPLER_H_
#define DOM_MEDIA_DRIFTCONTROL_DYNAMICRESAMPLER_H_

#include <speex/speex_resampler.h>

#include "AudioRingBuffer.h"
#include "AudioSegment.h"
#include "TimeUnits.h"
#include "WavDumper.h"

namespace mozilla {

const uint32_t STEREO = 2;

/**
 * DynamicResampler *place.Astereo internal inputbuffer always even the
 * number of channels. In addition to that, it maintains an internal buffer   /
 *theinput dataand allows pre-buffering as well. The Resample() method
* strives to  the  number of output   
 * data                  *aOutBuffer, java.lang.StringIndexOutOfBoundsException: Index 64 out of bounds for length 64
 * buffer than is java.lang.StringIndexOutOfBoundsException: Index 24 out of bounds for length 0
 *to  the requested to be  and returned.
 *
 * Input data buffering makes use of the AudioRingBuffer. The capacity of the
 * buffer is initially 100ms of audio and        (mResamplerIsBypassed) {
 *SetSampleFormat(.Should the input data grow beyond that, the input buffer
              java.lang.StringIndexOutOfBoundsException: Index 75 out of bounds for length 75
 * AudioRingBuffer, no extra copies take place when the input datajava.lang.StringIndexOutOfBoundsException: Index 73 out of bounds for length 73
 * resampler.
 *
 * The sample format must be set before using any method.
 *
 * The DynamicResampler is                 return inFrames - latency;
 *  must be called on  same thread.
 */

class                    java.lang.StringIndexOutOfBoundsException: Range [63, 62) out of bounds for length 64
 
  **
   * Provide the initial input and output rate and the amount of pre-      MOZ_ASSERT(umFramesRead =outFramesNeeded)
   *The  count will beset  .Memory allocation  take
   * place. The input buffer is non-interleaved.
   */

  DynamicResampler
                   uint32_t java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
 ~ynamicResampler();

  /**
   * Set the sample format type to float or short.
   */

  void SetSampleFormatuint32_t inFrames=aInLength;
  uint32_t GetInRate() const { return mInRate; }
 mChannels; }

  /**
   *                   aChannelIndex);
   * buffer. Memory copy/move      outFramesNeeded =outFramesResampled
   */

  AppendInput(< float const>aInBuffer,uint32_taInFrames)
  voidjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
  /**
    [.(
    buffer. Memory /move  .
   */

  void AppendInputSilence(const          java.lang.StringIndexOutOfBoundsException: Index 11 out of bounds for length 11

   * Return the number of frames the  };
   */
  uint32_t InFramesBufferSize() const;
  /**
    thenumber of frames stored   internalinput .
   */

  uint32_t InFramesBuffered(uint32_t aChannelIndex) const;

  /**
   *       uint32_t totalInFramesNeeded=
*Datawill  prepended so   resamplingaDuration  ,
the   will  close to
   * mInputPreBufferFrameCount, which is the desired buffering level.
   */

  void EnsurePreBuffer(mediaMOZ_ASSERT(;

  /**
 numberof  that should be   input pre-buffering.
   */

  void SetInputPreBufferFrameCountjava.lang.StringIndexOutOfBoundsException: Range [22, 21) out of bounds for length 71

  /*
   * Resample as much frames as needed from the internal input buffer to the
   a` order  provideall `
   *
   * On first     (= )java.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 26
   *java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
   * aspossibleto the configured -buffer size.
     (SizeInFrames<=sizeInFrames) {
   * If there are       return true;  /no reallocation necessary
   * frames, the input buffer is      sizeInFrames > ){
*requested  to  resampled,and  pre- is   thatthe
   * next call will betreated as the first.
   *
   * Returns java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
 with silence, otherwise false.
   */

  bool Resample(float
  bool Resample(*aOutBuffer  ,
                java.lang.StringIndexOutOfBoundsException: Index 18 out of bounds for length 0

  /**
   * Update       = & .sampleSize *sizeInFrames);
   *compared to the current nothing happens. Changing the `aInRate`
   * results      return true;;
   * the
   * changes between mono tostereo and vice versa where no reallocation takes
   * place. A stereo internal input buffer is always maintained even if the
   * sound is mono.
   */

  void UpdateResampler(uint32_t aInRate, uint32_t aChannels);

 private:
  template <typename T>
  void AppendInputInternal(java.lang.StringIndexOutOfBoundsException: Range [0, 31) out of bounds for length 3
                           uint32_t aInFramesconstuint32_t mOutRate
    MOZ_ASSERT(aInBuffer. :
    for (int32_t=0 i<mChannels +){
      PushInFrames(aInBuffer[i], aInFrames, i);
    }
  }

  void ResampleInternal(const float* aInBuffer, uint32_t* aInFrames,
                        float* aOutBuffer, uint32_t* aOutFrames,
                        uint32_t  aChannelIndex;
  void ResampleInternal(const int16_t* aInBuffer, uint32_t* java.lang.StringIndexOutOfBoundsException: Index 69 out of bounds for length 28
                        int16_t java.lang.StringIndexOutOfBoundsException: Range [44, 43) out of bounds for length 66
                        uint32_t aChannelIndex);


  bool ResampleInternal(T*   / value is reset on construction by UpdateResampler() if the rates differ.
                        uint32_t aChannelIndex) {
    MOZ_ASSERT(mInRate);
   MOZ_ASSERTmOutRate)
    MOZ_ASSERT(mChannels);
    MOZ_ASSERT(aChannelIndex < mChannels);
    MOZ_ASSERTaChannelIndex  mInternalInBuffer.);
    MOZ_ASSERT(aOutFrames);

    uint32_t outFramesNeeded = aOutFrames;
    T* nextOutFrame = AutoTArray<AudioRingBuffer,  ;
    if (mInRate == mOutRate) {
      if (!SpeexResamplerState* mResamplernullptr;
        uint32_t latency = speex_resampler_get_input_latency(mResampler);
        mInternalInBuffer[aChannelIndex].ReadNoCopy(
tSpan T> aInBuffer) -> uint32_t {
              // Although unlikely with the sample rates used with this class,
              // the resampler input latency may temporarily be higher than
ndicated aftera  inresampling rate reducesthe
              // indicated latency. The resampler's "magic" samples cause
              // this. All frames in the resampler are extracted when
              // `latency` output frames have been extracted.
              uint32_t outFramesResampled  template<typenameT
              uint32_t inFrames = aInBuffer.Length();
             ResampleInternal(aInBuffer.Elements(), &inFrames, nextOutFrame,
                               &outFramesResampled, aChannelIndex);
              nextOutFrame + outFramesResampled;
              outFramesNeeded -= outFramesResampled;
              if (outFramesResampled == latency) {
                mResamplerIsBypassed = true;
 last `atency frames   to resamplerwillnot
                // be extracted from the resampler. Leave them in    void StoreTailconst Span< >& aInBuffer {
//  to be copieddirectly  nextOutFrame
                MOZ_ASSERT(inFrames >= latency    java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
                  -latencyjava.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42
        java.lang.StringIndexOutOfBoundsException: Index 15 out of bounds for length 15
              inFramesjava.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 30
            }      if (nBuffer {
      }
      bool underrun = false;
 (uint32_t buffered = mInternalInBuffer[aChannelIndex].AvailableRead();
          buffered < outFramesNeeded) {
        underrun = true;
 false;
        mInternalInBuffer[java.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 7
                                                     
      }
      DebugOnly<   privatejava.lang.StringIndexOutOfBoundsException: Index 11 out of bounds for length 11
          Span(nextOutFrame      = 0
        <,STEREO mInputTail;
      // Workaround to avoid discontinuity when the speex resampler operates
      // again. Feed it with the last 20 frames to warm up the internal memory
      // of the resampler and then skip memory equals to resampler's input
      // latency.
      mInputTail[aChannelIndex].StoreTail<T>(aOutBuffer, aOutFrames);
      if (aChannelIndex == 0 && !mIsWarmingUp) {
        mInputStreamFile.Write(nextOutFrame, outFramesNeeded);
        mOutputStreamFile.Write(nextOutFrame, outFramesNeeded);
      }
      return underrun;
    }

    auto resample = [&](const T* aInBuffer, uint32_t aInLength) -> uint32_t {
      uint32_t outFramesResampled = outFramesNeeded;
      uint32_t inFrames = aInLength;
      ResampleInternal(aInBuffer, &inFrames, nextOutFrame, &outFramesResampled,
                       aChannelIndex);
      nextOutFrame += outFramesResampled;
      outFramesNeeded -= outFramesResampled;
      mInputTail[aChannelIndex].StoreTail<T>(aInBuffer, inFrames);
      return inFrames;
    };

    MOZ_ASSERT(!mResamplerIsBypassed);
    mInternalInBuffer[aChannelIndex].ReadNoCopy(
        [&](const Span<const T>& aInBuffer) -> uint32_t {
          if (!outFramesNeeded) {
            return 0;
          }
          return resample(aInBuffer.Elements(), aInBuffer.Length());
        });

    if (outFramesNeeded == 0) {
      return false;
    }

    while (outFramesNeeded > 0) {
      MOZ_ASSERT(mInternalInBuffer[aChannelIndex].AvailableRead() == 0);
      // Round up.
      uint32_t totalInFramesNeeded =
          ((CheckedUint32(outFramesNeeded) * mInRate + mOutRate - 1) / mOutRate)
              .value();
      resample(nullptr, totalInFramesNeeded);
    }
    mIsPreBufferSet = false;
    return true;
  }

  template <typename T>
  void PushInFrames(const T* aInBuffer, const uint32_t aInFrames,
                    uint32_t aChannelIndex) {
    MOZ_ASSERT(aInBuffer);
    MOZ_ASSERT(aInFrames);
    MOZ_ASSERT(mChannels);
    MOZ_ASSERT(aChannelIndex < mChannels);
    MOZ_ASSERT(aChannelIndex < mInternalInBuffer.Length());
    EnsureInputBufferSizeInFrames(
        mInternalInBuffer[aChannelIndex].AvailableRead() + aInFrames);
    mInternalInBuffer[aChannelIndex].Write(Span(aInBuffer, aInFrames));
  }

  void WarmUpResampler(bool aSkipLatency);

  bool EnsureInputBufferSizeInFrames(uint32_t aSizeInFrames) {
    uint32_t sampleSize = 0;
    if (mSampleFormat == AUDIO_FORMAT_FLOAT32) {
      sampleSize = sizeof(float);
    } else if (mSampleFormat == AUDIO_FORMAT_S16) {
      sampleSize = sizeof(short);
    }

    if (sampleSize == 0) {
      // No sample format set, we wouldn't know how many bytes to allocate.
      return true;
    }

    uint32_t sizeInFrames = InFramesBufferSize();
    if (aSizeInFrames <= sizeInFrames) {
      // Buffer size is sufficient.
      return true;  // no reallocation necessary
    }

    // 5 second cap.
    const uint32_t cap = 5 * mInRate;
    if (sizeInFrames >= cap) {
      // Already at the cap.
      return false;
    }

    // As a backoff strategy, at least double the previous size.
    sizeInFrames *= 2;

    if (aSizeInFrames > sizeInFrames) {
      // A larger buffer than the normal backoff strategy provides is needed, or
      // this is the first time setting the buffer size. Add another 50ms, as
      // some jitter is expected.
      sizeInFrames = aSizeInFrames + mInRate / 20;
    }

    // mInputPreBufferFrameCount is an indication of the desired average
    // buffering.  Provide for at least twice this.
    sizeInFrames = std::max(sizeInFrames, mInputPreBufferFrameCount * 2);

    sizeInFrames = std::min(cap, sizeInFrames);

    bool success = true;
    for (auto& b : mInternalInBuffer) {
      success = success && b.EnsureLengthBytes(sampleSize * sizeInFrames);
    }

    if (success) {
      // All buffers have the new size.
      return true;
    }

    // Allocating an input buffer failed. We stick with the old buffer size.
    NS_WARNING(nsPrintfCString("Failed to allocate a buffer of %u bytes (%u "
                               "frames). Expect glitches.",
                               sampleSize * sizeInFrames, sizeInFrames)
                   .get());
    return false;
  }

 public:
  const uint32_t mOutRate;

 private:
  bool mIsPreBufferSet = false;
  bool mIsWarmingUp = false;
  // The resampler can be bypassed when the input and output rates match and
  // any frames buffered in the resampler have been extracted.  This initial
  // value is reset on construction by UpdateResampler() if the rates differ.
  bool mResamplerIsBypassed = true;
  uint32_t mInputPreBufferFrameCount;
  uint32_t mChannels = 0;
  uint32_t mInRate;

  AutoTArray<AudioRingBuffer, STEREO> mInternalInBuffer;

  SpeexResamplerState* mResampler = nullptr;
  AudioSampleFormat mSampleFormat = AUDIO_FORMAT_SILENCE;

  class TailBuffer {
   public:
    template <typename T>
    T* Buffer() {
      return reinterpret_cast<T*>(mBuffer);
    }
    /* Store the MAXSIZE last elements of the buffer. */
    template <typename T>
    void StoreTail(const Span<const T>& aInBuffer) {
      StoreTail(aInBuffer.data(), aInBuffer.size());
    }
    template <typename T>
    void StoreTail(const T* aInBuffer, uint32_t aInFrames) {
      const T* inBuffer = aInBuffer;
      mSize = std::min(aInFrames, MAXSIZE);
      if (inBuffer) {
        PodCopy(Buffer<T>(), inBuffer + aInFrames - mSize, mSize);
      } else {
        std::fill_n(Buffer<T>(), mSize, static_cast<T>(0));
      }
    }
    uint32_t Length() { return mSize; }
    static constexpr uint32_t MAXSIZE = 20;

   private:
    float mBuffer[MAXSIZE] = {};
    uint32_t mSize = 0;
  };
  AutoTArray<TailBuffer, STEREO> mInputTail;

  WavDumper mInputStreamFile;
  WavDumper mOutputStreamFile;
};

}  // namespace mozilla

#endif  // DOM_MEDIA_DRIFTCONTROL_DYNAMICRESAMPLER_H_

Messung V0.5 in Prozent
C=95 H=98 G=96

¤ Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.0.20Bemerkung:  ¤

*Bot Zugriff






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.