test_utils.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_PROCESSING_TEST_TEST_UTILS_H_
  11. #define MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_
  12. #include <math.h>
  13. #include <iterator>
  14. #include <limits>
  15. #include <memory>
  16. #include <sstream> // no-presubmit-check TODO(webrtc:8982)
  17. #include <string>
  18. #include <vector>
  19. #include "common_audio/channel_buffer.h"
  20. #include "common_audio/wav_file.h"
  21. #include "modules/audio_processing/include/audio_processing.h"
  22. #include "rtc_base/constructor_magic.h"
  23. namespace webrtc {
  24. static const AudioProcessing::Error kNoErr = AudioProcessing::kNoError;
  25. #define EXPECT_NOERR(expr) EXPECT_EQ(kNoErr, (expr))
  26. class RawFile final {
  27. public:
  28. explicit RawFile(const std::string& filename);
  29. ~RawFile();
  30. void WriteSamples(const int16_t* samples, size_t num_samples);
  31. void WriteSamples(const float* samples, size_t num_samples);
  32. private:
  33. FILE* file_handle_;
  34. RTC_DISALLOW_COPY_AND_ASSIGN(RawFile);
  35. };
  36. // Encapsulates samples and metadata for an integer frame.
  37. struct Int16FrameData {
  38. // Max data size that matches the data size of the AudioFrame class, providing
  39. // storage for 8 channels of 96 kHz data.
  40. static const int kMaxDataSizeSamples = 7680;
  41. Int16FrameData() {
  42. sample_rate_hz = 0;
  43. num_channels = 0;
  44. samples_per_channel = 0;
  45. data.fill(0);
  46. }
  47. void CopyFrom(const Int16FrameData& src) {
  48. samples_per_channel = src.samples_per_channel;
  49. sample_rate_hz = src.sample_rate_hz;
  50. num_channels = src.num_channels;
  51. const size_t length = samples_per_channel * num_channels;
  52. RTC_CHECK_LE(length, kMaxDataSizeSamples);
  53. memcpy(data.data(), src.data.data(), sizeof(int16_t) * length);
  54. }
  55. std::array<int16_t, kMaxDataSizeSamples> data;
  56. int32_t sample_rate_hz;
  57. size_t num_channels;
  58. size_t samples_per_channel;
  59. };
  60. // Reads ChannelBuffers from a provided WavReader.
  61. class ChannelBufferWavReader final {
  62. public:
  63. explicit ChannelBufferWavReader(std::unique_ptr<WavReader> file);
  64. ~ChannelBufferWavReader();
  65. // Reads data from the file according to the |buffer| format. Returns false if
  66. // a full buffer can't be read from the file.
  67. bool Read(ChannelBuffer<float>* buffer);
  68. private:
  69. std::unique_ptr<WavReader> file_;
  70. std::vector<float> interleaved_;
  71. RTC_DISALLOW_COPY_AND_ASSIGN(ChannelBufferWavReader);
  72. };
  73. // Writes ChannelBuffers to a provided WavWriter.
  74. class ChannelBufferWavWriter final {
  75. public:
  76. explicit ChannelBufferWavWriter(std::unique_ptr<WavWriter> file);
  77. ~ChannelBufferWavWriter();
  78. void Write(const ChannelBuffer<float>& buffer);
  79. private:
  80. std::unique_ptr<WavWriter> file_;
  81. std::vector<float> interleaved_;
  82. RTC_DISALLOW_COPY_AND_ASSIGN(ChannelBufferWavWriter);
  83. };
  84. // Takes a pointer to a vector. Allows appending the samples of channel buffers
  85. // to the given vector, by interleaving the samples and converting them to float
  86. // S16.
  87. class ChannelBufferVectorWriter final {
  88. public:
  89. explicit ChannelBufferVectorWriter(std::vector<float>* output);
  90. ChannelBufferVectorWriter(const ChannelBufferVectorWriter&) = delete;
  91. ChannelBufferVectorWriter& operator=(const ChannelBufferVectorWriter&) =
  92. delete;
  93. ~ChannelBufferVectorWriter();
  94. // Creates an interleaved copy of |buffer|, converts the samples to float S16
  95. // and appends the result to output_.
  96. void Write(const ChannelBuffer<float>& buffer);
  97. private:
  98. std::vector<float> interleaved_buffer_;
  99. std::vector<float>* output_;
  100. };
  101. void WriteIntData(const int16_t* data,
  102. size_t length,
  103. WavWriter* wav_file,
  104. RawFile* raw_file);
  105. void WriteFloatData(const float* const* data,
  106. size_t samples_per_channel,
  107. size_t num_channels,
  108. WavWriter* wav_file,
  109. RawFile* raw_file);
  110. // Exits on failure; do not use in unit tests.
  111. FILE* OpenFile(const std::string& filename, const char* mode);
  112. size_t SamplesFromRate(int rate);
  113. void SetFrameSampleRate(Int16FrameData* frame, int sample_rate_hz);
  114. template <typename T>
  115. void SetContainerFormat(int sample_rate_hz,
  116. size_t num_channels,
  117. Int16FrameData* frame,
  118. std::unique_ptr<ChannelBuffer<T> >* cb) {
  119. SetFrameSampleRate(frame, sample_rate_hz);
  120. frame->num_channels = num_channels;
  121. cb->reset(new ChannelBuffer<T>(frame->samples_per_channel, num_channels));
  122. }
  123. AudioProcessing::ChannelLayout LayoutFromChannels(size_t num_channels);
  124. template <typename T>
  125. float ComputeSNR(const T* ref, const T* test, size_t length, float* variance) {
  126. float mse = 0;
  127. float mean = 0;
  128. *variance = 0;
  129. for (size_t i = 0; i < length; ++i) {
  130. T error = ref[i] - test[i];
  131. mse += error * error;
  132. *variance += ref[i] * ref[i];
  133. mean += ref[i];
  134. }
  135. mse /= length;
  136. *variance /= length;
  137. mean /= length;
  138. *variance -= mean * mean;
  139. float snr = 100; // We assign 100 dB to the zero-error case.
  140. if (mse > 0)
  141. snr = 10 * log10(*variance / mse);
  142. return snr;
  143. }
  144. // Returns a vector<T> parsed from whitespace delimited values in to_parse,
  145. // or an empty vector if the string could not be parsed.
  146. template <typename T>
  147. std::vector<T> ParseList(const std::string& to_parse) {
  148. std::vector<T> values;
  149. std::istringstream str(to_parse);
  150. std::copy(
  151. std::istream_iterator<T>(str), // no-presubmit-check TODO(webrtc:8982)
  152. std::istream_iterator<T>(), // no-presubmit-check TODO(webrtc:8982)
  153. std::back_inserter(values));
  154. return values;
  155. }
  156. } // namespace webrtc
  157. #endif // MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_