blocker.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2014 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_CODING_CODECS_OPUS_TEST_BLOCKER_H_
  11. #define MODULES_AUDIO_CODING_CODECS_OPUS_TEST_BLOCKER_H_
  12. #include <memory>
  13. #include "common_audio/channel_buffer.h"
  14. #include "modules/audio_coding/codecs/opus/test/audio_ring_buffer.h"
  15. namespace webrtc {
  16. // The callback function to process audio in the time domain. Input has already
  17. // been windowed, and output will be windowed. The number of input channels
  18. // must be >= the number of output channels.
  19. class BlockerCallback {
  20. public:
  21. virtual ~BlockerCallback() {}
  22. virtual void ProcessBlock(const float* const* input,
  23. size_t num_frames,
  24. size_t num_input_channels,
  25. size_t num_output_channels,
  26. float* const* output) = 0;
  27. };
  28. // The main purpose of Blocker is to abstract away the fact that often we
  29. // receive a different number of audio frames than our transform takes. For
  30. // example, most FFTs work best when the fft-size is a power of 2, but suppose
  31. // we receive 20ms of audio at a sample rate of 48000. That comes to 960 frames
  32. // of audio, which is not a power of 2. Blocker allows us to specify the
  33. // transform and all other necessary processing via the Process() callback
  34. // function without any constraints on the transform-size
  35. // (read: |block_size_|) or received-audio-size (read: |chunk_size_|).
  36. // We handle this for the multichannel audio case, allowing for different
  37. // numbers of input and output channels (for example, beamforming takes 2 or
  38. // more input channels and returns 1 output channel). Audio signals are
  39. // represented as deinterleaved floats in the range [-1, 1].
  40. //
  41. // Blocker is responsible for:
  42. // - blocking audio while handling potential discontinuities on the edges
  43. // of chunks
  44. // - windowing blocks before sending them to Process()
  45. // - windowing processed blocks, and overlap-adding them together before
  46. // sending back a processed chunk
  47. //
  48. // To use blocker:
  49. // 1. Impelment a BlockerCallback object |bc|.
  50. // 2. Instantiate a Blocker object |b|, passing in |bc|.
  51. // 3. As you receive audio, call b.ProcessChunk() to get processed audio.
  52. //
  53. // A small amount of delay is added to the first received chunk to deal with
  54. // the difference in chunk/block sizes. This delay is <= chunk_size.
  55. //
  56. // Ownership of window is retained by the caller. That is, Blocker makes a
  57. // copy of window and does not attempt to delete it.
  58. class Blocker {
  59. public:
  60. Blocker(size_t chunk_size,
  61. size_t block_size,
  62. size_t num_input_channels,
  63. size_t num_output_channels,
  64. const float* window,
  65. size_t shift_amount,
  66. BlockerCallback* callback);
  67. ~Blocker();
  68. void ProcessChunk(const float* const* input,
  69. size_t chunk_size,
  70. size_t num_input_channels,
  71. size_t num_output_channels,
  72. float* const* output);
  73. size_t initial_delay() const { return initial_delay_; }
  74. private:
  75. const size_t chunk_size_;
  76. const size_t block_size_;
  77. const size_t num_input_channels_;
  78. const size_t num_output_channels_;
  79. // The number of frames of delay to add at the beginning of the first chunk.
  80. const size_t initial_delay_;
  81. // The frame index into the input buffer where the first block should be read
  82. // from. This is necessary because shift_amount_ is not necessarily a
  83. // multiple of chunk_size_, so blocks won't line up at the start of the
  84. // buffer.
  85. size_t frame_offset_;
  86. // Since blocks nearly always overlap, there are certain blocks that require
  87. // frames from the end of one chunk and the beginning of the next chunk. The
  88. // input and output buffers are responsible for saving those frames between
  89. // calls to ProcessChunk().
  90. //
  91. // Both contain |initial delay| + |chunk_size| frames. The input is a fairly
  92. // standard FIFO, but due to the overlap-add it's harder to use an
  93. // AudioRingBuffer for the output.
  94. AudioRingBuffer input_buffer_;
  95. ChannelBuffer<float> output_buffer_;
  96. // Space for the input block (can't wrap because of windowing).
  97. ChannelBuffer<float> input_block_;
  98. // Space for the output block (can't wrap because of overlap/add).
  99. ChannelBuffer<float> output_block_;
  100. std::unique_ptr<float[]> window_;
  101. // The amount of frames between the start of contiguous blocks. For example,
  102. // |shift_amount_| = |block_size_| / 2 for a Hann window.
  103. size_t shift_amount_;
  104. BlockerCallback* callback_;
  105. };
  106. } // namespace webrtc
  107. #endif // MODULES_AUDIO_CODING_CODECS_OPUS_TEST_BLOCKER_H_