tor-browser

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

AudioGenerator.h (2006B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      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 https://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef DOM_MEDIA_GTEST_AUDIO_GENERATOR_H_
      8 #define DOM_MEDIA_GTEST_AUDIO_GENERATOR_H_
      9 
     10 #include "AudioSegment.h"
     11 #include "SineWaveGenerator.h"
     12 #include "prtime.h"
     13 
     14 namespace mozilla {
     15 
     16 template <typename Sample>
     17 class AudioGenerator {
     18 public:
     19  AudioGenerator(uint32_t aChannels, uint32_t aSampleRate,
     20                 uint32_t aFrequency = 1000)
     21      : mSampleRate(aSampleRate),
     22        mFrequency(aFrequency),
     23        mChannelCount(aChannels),
     24        mGenerator(aSampleRate, aFrequency) {}
     25 
     26  void Generate(mozilla::AudioSegment& aSegment, uint32_t aFrameCount) {
     27    CheckedInt<size_t> bufferSize(sizeof(Sample));
     28    bufferSize *= aFrameCount;
     29    RefPtr<SharedBuffer> buffer = SharedBuffer::Create(bufferSize);
     30    Sample* dest = static_cast<Sample*>(buffer->Data());
     31    mGenerator.generate(dest, aFrameCount);
     32    AutoTArray<const Sample*, 1> channels;
     33    for (uint32_t i = 0; i < mChannelCount; ++i) {
     34      channels.AppendElement(dest);
     35    }
     36    aSegment.AppendFrames(buffer.forget(), channels, aFrameCount,
     37                          PRINCIPAL_HANDLE_NONE);
     38  }
     39 
     40  void GenerateInterleaved(Sample* aSamples, uint32_t aFrameCount) {
     41    mGenerator.generate(aSamples, aFrameCount, mChannelCount);
     42  }
     43 
     44  void SetChannelsCount(uint32_t aChannelCount) {
     45    mChannelCount = aChannelCount;
     46  }
     47 
     48  uint32_t ChannelCount() const { return mChannelCount; }
     49 
     50  static float Amplitude() {
     51    return mozilla::SineWaveGenerator<Sample>::Amplitude();
     52  }
     53 
     54  const uint32_t mSampleRate;
     55  const uint32_t mFrequency;
     56 
     57 private:
     58  uint32_t mChannelCount;
     59  mozilla::SineWaveGenerator<Sample> mGenerator;
     60 };
     61 
     62 }  // namespace mozilla
     63 
     64 #endif  // DOM_MEDIA_GTEST_AUDIO_GENERATOR_H_