tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

AudioBlock.h (4602B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 #ifndef MOZILLA_AUDIOBLOCK_H_
      7 #define MOZILLA_AUDIOBLOCK_H_
      8 
      9 #include "AudioSegment.h"
     10 
     11 namespace mozilla {
     12 
     13 /**
     14 * An AudioChunk whose buffer contents need to be valid only for one
     15 * processing block iteration, after which contents can be overwritten if the
     16 * buffer has not been passed to longer term storage or to another thread,
     17 * which may happen though AsAudioChunk() or AsMutableChunk().
     18 *
     19 * Use on graph thread only.
     20 */
     21 class AudioBlock : private AudioChunk {
     22 public:
     23  AudioBlock() {
     24    mDuration = WEBAUDIO_BLOCK_SIZE;
     25    mBufferFormat = AUDIO_FORMAT_SILENCE;
     26  }
     27  // No effort is made in constructors to ensure that mBufferIsDownstreamRef
     28  // is set because the block is expected to be a temporary and so the
     29  // reference will be released before the next iteration.
     30  // The custom copy constructor is required so as not to set
     31  // mBufferIsDownstreamRef without notifying AudioBlockBuffer.
     32  AudioBlock(const AudioBlock& aBlock) : AudioChunk(aBlock.AsAudioChunk()) {}
     33  explicit AudioBlock(const AudioChunk& aChunk) : AudioChunk(aChunk) {
     34    MOZ_ASSERT(aChunk.mDuration == WEBAUDIO_BLOCK_SIZE);
     35  }
     36  ~AudioBlock();
     37 
     38  using AudioChunk::ChannelCount;
     39  using AudioChunk::ChannelData;
     40  using AudioChunk::GetDuration;
     41  using AudioChunk::IsNull;
     42  using AudioChunk::SizeOfExcludingThis;
     43  using AudioChunk::SizeOfExcludingThisIfUnshared;
     44  // mDuration is not exposed.  Use GetDuration().
     45  // mBuffer is not exposed.  Use Get/SetBuffer().
     46  using AudioChunk::mBufferFormat;
     47  using AudioChunk::mChannelData;
     48  using AudioChunk::mVolume;
     49 
     50  const AudioChunk& AsAudioChunk() const { return *this; }
     51  AudioChunk* AsMutableChunk() {
     52    ClearDownstreamMark();
     53    return this;
     54  }
     55 
     56  /**
     57   * Allocates, if necessary, aChannelCount buffers of WEBAUDIO_BLOCK_SIZE float
     58   * samples for writing.
     59   */
     60  void AllocateChannels(uint32_t aChannelCount);
     61 
     62  /**
     63   * ChannelFloatsForWrite() should only be used when the buffers have been
     64   * created with AllocateChannels().
     65   */
     66  float* ChannelFloatsForWrite(size_t aChannel) {
     67    MOZ_ASSERT(mBufferFormat == AUDIO_FORMAT_FLOAT32);
     68    MOZ_ASSERT(CanWrite());
     69    return static_cast<float*>(const_cast<void*>(mChannelData[aChannel]));
     70  }
     71 
     72  ThreadSharedObject* GetBuffer() const { return mBuffer; }
     73  void SetBuffer(ThreadSharedObject* aNewBuffer);
     74  void SetNull(TrackTime aDuration) {
     75    MOZ_ASSERT(aDuration == WEBAUDIO_BLOCK_SIZE);
     76    SetBuffer(nullptr);
     77    mChannelData.Clear();
     78    mVolume = 1.0f;
     79    mBufferFormat = AUDIO_FORMAT_SILENCE;
     80  }
     81 
     82  AudioBlock& operator=(const AudioBlock& aBlock) {
     83    // Instead of just copying, mBufferIsDownstreamRef must be first cleared
     84    // if set.  It is set again for the new mBuffer if possible.  This happens
     85    // in SetBuffer().
     86    return *this = aBlock.AsAudioChunk();
     87  }
     88  AudioBlock& operator=(const AudioChunk& aChunk) {
     89    MOZ_ASSERT(aChunk.mDuration == WEBAUDIO_BLOCK_SIZE);
     90    SetBuffer(aChunk.mBuffer);
     91    mChannelData = aChunk.mChannelData;
     92    mVolume = aChunk.mVolume;
     93    mBufferFormat = aChunk.mBufferFormat;
     94    return *this;
     95  }
     96 
     97  bool IsMuted() const { return mVolume == 0.0f; }
     98 
     99  bool IsSilentOrSubnormal() const {
    100    if (!mBuffer) {
    101      return true;
    102    }
    103 
    104    for (uint32_t i = 0, length = mChannelData.Length(); i < length; ++i) {
    105      const float* channel = static_cast<const float*>(mChannelData[i]);
    106      for (TrackTime frame = 0; frame < mDuration; ++frame) {
    107        if (fabs(channel[frame]) >= FLT_MIN) {
    108          return false;
    109        }
    110      }
    111    }
    112 
    113    return true;
    114  }
    115 
    116 private:
    117  void ClearDownstreamMark();
    118  bool CanWrite();
    119 
    120  // mBufferIsDownstreamRef is set only when mBuffer references an
    121  // AudioBlockBuffer created in a different AudioBlock.  That can happen when
    122  // this AudioBlock is on a node downstream from the node which created the
    123  // buffer.  When this is set, the AudioBlockBuffer is notified that this
    124  // reference does not prevent the upstream node from re-using the buffer next
    125  // iteration and modifying its contents.  The AudioBlockBuffer is also
    126  // notified when mBuffer releases this reference.
    127  bool mBufferIsDownstreamRef = false;
    128 };
    129 
    130 }  // namespace mozilla
    131 
    132 MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(mozilla::AudioBlock)
    133 
    134 #endif  // MOZILLA_AUDIOBLOCK_H_