tor-browser

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

test_audio_device.h (6116B)


      1 /*
      2 *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 #ifndef MODULES_AUDIO_DEVICE_INCLUDE_TEST_AUDIO_DEVICE_H_
     11 #define MODULES_AUDIO_DEVICE_INCLUDE_TEST_AUDIO_DEVICE_H_
     12 
     13 #include <cstddef>
     14 #include <cstdint>
     15 #include <memory>
     16 
     17 #include "absl/strings/string_view.h"
     18 #include "api/array_view.h"
     19 #include "api/audio/audio_device.h"
     20 #include "api/environment/environment.h"
     21 #include "api/scoped_refptr.h"
     22 #include "rtc_base/buffer.h"
     23 
     24 namespace webrtc {
     25 
     26 // This is test API and is in development, so it can be changed/removed without
     27 // notice.
     28 
     29 // This class exists for historical reasons. For now it only contains static
     30 // methods to create test AudioDeviceModule. Implementation details of that
     31 // module are considered private. This class isn't intended to be instantiated.
     32 class TestAudioDeviceModule {
     33 public:
     34  // Returns the number of samples that Capturers and Renderers with this
     35  // sampling frequency will work with every time Capture or Render is called.
     36  static size_t SamplesPerFrame(int sampling_frequency_in_hz);
     37 
     38  class Capturer {
     39   public:
     40    virtual ~Capturer() {}
     41    // Returns the sampling frequency in Hz of the audio data that this
     42    // capturer produces.
     43    virtual int SamplingFrequency() const = 0;
     44    // Returns the number of channels of captured audio data.
     45    virtual int NumChannels() const = 0;
     46    // Replaces the contents of `buffer` with 10ms of captured audio data
     47    // (see TestAudioDeviceModule::SamplesPerFrame). Returns true if the
     48    // capturer can keep producing data, or false when the capture finishes.
     49    virtual bool Capture(BufferT<int16_t>* buffer) = 0;
     50  };
     51 
     52  class Renderer {
     53   public:
     54    virtual ~Renderer() {}
     55    // Returns the sampling frequency in Hz of the audio data that this
     56    // renderer receives.
     57    virtual int SamplingFrequency() const = 0;
     58    // Returns the number of channels of audio data to be required.
     59    virtual int NumChannels() const = 0;
     60    // Renders the passed audio data and returns true if the renderer wants
     61    // to keep receiving data, or false otherwise.
     62    virtual bool Render(ArrayView<const int16_t> data) = 0;
     63  };
     64 
     65  // A fake capturer that generates pulses with random samples between
     66  // -max_amplitude and +max_amplitude.
     67  class PulsedNoiseCapturer : public Capturer {
     68   public:
     69    ~PulsedNoiseCapturer() override {}
     70 
     71    virtual void SetMaxAmplitude(int16_t amplitude) = 0;
     72  };
     73 
     74  // Creates a new TestAudioDeviceModule. When capturing or playing, 10 ms audio
     75  // frames will be processed every 10ms / `speed`.
     76  // `capturer` is an object that produces audio data. Can be nullptr if this
     77  // device is never used for recording.
     78  // `renderer` is an object that receives audio data that would have been
     79  // played out. Can be nullptr if this device is never used for playing.
     80  // Use one of the Create... functions to get these instances.
     81  static scoped_refptr<AudioDeviceModule> Create(
     82      const Environment& env,
     83      std::unique_ptr<Capturer> capturer,
     84      std::unique_ptr<Renderer> renderer,
     85      float speed = 1);
     86 
     87  // Returns a Capturer instance that generates a signal of `num_channels`
     88  // channels where every second frame is zero and every second frame is evenly
     89  // distributed random noise with max amplitude `max_amplitude`.
     90  static std::unique_ptr<PulsedNoiseCapturer> CreatePulsedNoiseCapturer(
     91      int16_t max_amplitude,
     92      int sampling_frequency_in_hz,
     93      int num_channels = 1);
     94 
     95  // Returns a Renderer instance that does nothing with the audio data.
     96  static std::unique_ptr<Renderer> CreateDiscardRenderer(
     97      int sampling_frequency_in_hz,
     98      int num_channels = 1);
     99 
    100  // WavReader and WavWriter creation based on file name.
    101 
    102  // Returns a Capturer instance that gets its data from a WAV file. The sample
    103  // rate and channels will be checked against the Wav file.
    104  static std::unique_ptr<Capturer> CreateWavFileReader(
    105      absl::string_view filename,
    106      int sampling_frequency_in_hz,
    107      int num_channels = 1);
    108 
    109  // Returns a Capturer instance that gets its data from a file.
    110  // Automatically detects sample rate and num of channels.
    111  // `repeat` - if true, the file will be replayed from the start when we reach
    112  // the end of file.
    113  static std::unique_ptr<Capturer> CreateWavFileReader(
    114      absl::string_view filename,
    115      bool repeat = false);
    116 
    117  // Returns a Renderer instance that writes its data to a file.
    118  static std::unique_ptr<Renderer> CreateWavFileWriter(
    119      absl::string_view filename,
    120      int sampling_frequency_in_hz,
    121      int num_channels = 1);
    122 
    123  // Returns a Renderer instance that writes its data to a WAV file, cutting
    124  // off silence at the beginning (not necessarily perfect silence, see
    125  // kAmplitudeThreshold) and at the end (only actual 0 samples in this case).
    126  static std::unique_ptr<Renderer> CreateBoundedWavFileWriter(
    127      absl::string_view filename,
    128      int sampling_frequency_in_hz,
    129      int num_channels = 1);
    130 
    131  // Returns a Capturer instance that gets its data from a raw file (*.raw).
    132  static std::unique_ptr<Capturer> CreateRawFileReader(
    133      absl::string_view filename,
    134      int sampling_frequency_in_hz = 48000,
    135      int num_channels = 2,
    136      bool repeat = true);
    137 
    138  // Returns a Renderer instance that writes its data to a raw file (*.raw),
    139  // cutting off silence at the beginning (not necessarily perfect silence, see
    140  // kAmplitudeThreshold) and at the end (only actual 0 samples in this case).
    141  static std::unique_ptr<Renderer> CreateRawFileWriter(
    142      absl::string_view filename,
    143      int sampling_frequency_in_hz = 48000,
    144      int num_channels = 2);
    145 
    146 private:
    147  TestAudioDeviceModule() = default;
    148 };
    149 
    150 }  // namespace webrtc
    151 
    152 #endif  // MODULES_AUDIO_DEVICE_INCLUDE_TEST_AUDIO_DEVICE_H_