You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

398 lines
14 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2012 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. #include "modules/audio_processing/audio_buffer.h"
  11. #include <string.h>
  12. #include <cstdint>
  13. #include "common_audio/channel_buffer.h"
  14. #include "common_audio/include/audio_util.h"
  15. #include "common_audio/resampler/push_sinc_resampler.h"
  16. #include "modules/audio_processing/splitting_filter.h"
  17. #include "rtc_base/checks.h"
  18. namespace webrtc {
  19. namespace {
  20. constexpr size_t kSamplesPer32kHzChannel = 320;
  21. constexpr size_t kSamplesPer48kHzChannel = 480;
  22. constexpr size_t kMaxSamplesPerChannel = AudioBuffer::kMaxSampleRate / 100;
  23. size_t NumBandsFromFramesPerChannel(size_t num_frames) {
  24. if (num_frames == kSamplesPer32kHzChannel) {
  25. return 2;
  26. }
  27. if (num_frames == kSamplesPer48kHzChannel) {
  28. return 3;
  29. }
  30. return 1;
  31. }
  32. } // namespace
  33. AudioBuffer::AudioBuffer(size_t input_rate,
  34. size_t input_num_channels,
  35. size_t buffer_rate,
  36. size_t buffer_num_channels,
  37. size_t output_rate,
  38. size_t output_num_channels)
  39. : AudioBuffer(static_cast<int>(input_rate) / 100,
  40. input_num_channels,
  41. static_cast<int>(buffer_rate) / 100,
  42. buffer_num_channels,
  43. static_cast<int>(output_rate) / 100) {}
  44. AudioBuffer::AudioBuffer(size_t input_num_frames,
  45. size_t input_num_channels,
  46. size_t buffer_num_frames,
  47. size_t buffer_num_channels,
  48. size_t output_num_frames)
  49. : input_num_frames_(input_num_frames),
  50. input_num_channels_(input_num_channels),
  51. buffer_num_frames_(buffer_num_frames),
  52. buffer_num_channels_(buffer_num_channels),
  53. output_num_frames_(output_num_frames),
  54. output_num_channels_(0),
  55. num_channels_(buffer_num_channels),
  56. num_bands_(NumBandsFromFramesPerChannel(buffer_num_frames_)),
  57. num_split_frames_(rtc::CheckedDivExact(buffer_num_frames_, num_bands_)),
  58. data_(
  59. new ChannelBuffer<float>(buffer_num_frames_, buffer_num_channels_)) {
  60. RTC_DCHECK_GT(input_num_frames_, 0);
  61. RTC_DCHECK_GT(buffer_num_frames_, 0);
  62. RTC_DCHECK_GT(output_num_frames_, 0);
  63. RTC_DCHECK_GT(input_num_channels_, 0);
  64. RTC_DCHECK_GT(buffer_num_channels_, 0);
  65. RTC_DCHECK_LE(buffer_num_channels_, input_num_channels_);
  66. const bool input_resampling_needed = input_num_frames_ != buffer_num_frames_;
  67. const bool output_resampling_needed =
  68. output_num_frames_ != buffer_num_frames_;
  69. if (input_resampling_needed) {
  70. for (size_t i = 0; i < buffer_num_channels_; ++i) {
  71. input_resamplers_.push_back(std::unique_ptr<PushSincResampler>(
  72. new PushSincResampler(input_num_frames_, buffer_num_frames_)));
  73. }
  74. }
  75. if (output_resampling_needed) {
  76. for (size_t i = 0; i < buffer_num_channels_; ++i) {
  77. output_resamplers_.push_back(std::unique_ptr<PushSincResampler>(
  78. new PushSincResampler(buffer_num_frames_, output_num_frames_)));
  79. }
  80. }
  81. if (num_bands_ > 1) {
  82. split_data_.reset(new ChannelBuffer<float>(
  83. buffer_num_frames_, buffer_num_channels_, num_bands_));
  84. splitting_filter_.reset(new SplittingFilter(
  85. buffer_num_channels_, num_bands_, buffer_num_frames_));
  86. }
  87. }
  88. AudioBuffer::~AudioBuffer() {}
  89. void AudioBuffer::set_downmixing_to_specific_channel(size_t channel) {
  90. downmix_by_averaging_ = false;
  91. RTC_DCHECK_GT(input_num_channels_, channel);
  92. channel_for_downmixing_ = std::min(channel, input_num_channels_ - 1);
  93. }
  94. void AudioBuffer::set_downmixing_by_averaging() {
  95. downmix_by_averaging_ = true;
  96. }
  97. void AudioBuffer::CopyFrom(const float* const* data,
  98. const StreamConfig& stream_config) {
  99. RTC_DCHECK_EQ(stream_config.num_frames(), input_num_frames_);
  100. RTC_DCHECK_EQ(stream_config.num_channels(), input_num_channels_);
  101. RestoreNumChannels();
  102. const bool downmix_needed = input_num_channels_ > 1 && num_channels_ == 1;
  103. const bool resampling_needed = input_num_frames_ != buffer_num_frames_;
  104. if (downmix_needed) {
  105. RTC_DCHECK_GE(kMaxSamplesPerChannel, input_num_frames_);
  106. std::array<float, kMaxSamplesPerChannel> downmix;
  107. if (downmix_by_averaging_) {
  108. const float kOneByNumChannels = 1.f / input_num_channels_;
  109. for (size_t i = 0; i < input_num_frames_; ++i) {
  110. float value = data[0][i];
  111. for (size_t j = 1; j < input_num_channels_; ++j) {
  112. value += data[j][i];
  113. }
  114. downmix[i] = value * kOneByNumChannels;
  115. }
  116. }
  117. const float* downmixed_data =
  118. downmix_by_averaging_ ? downmix.data() : data[channel_for_downmixing_];
  119. if (resampling_needed) {
  120. input_resamplers_[0]->Resample(downmixed_data, input_num_frames_,
  121. data_->channels()[0], buffer_num_frames_);
  122. }
  123. const float* data_to_convert =
  124. resampling_needed ? data_->channels()[0] : downmixed_data;
  125. FloatToFloatS16(data_to_convert, buffer_num_frames_, data_->channels()[0]);
  126. } else {
  127. if (resampling_needed) {
  128. for (size_t i = 0; i < num_channels_; ++i) {
  129. input_resamplers_[i]->Resample(data[i], input_num_frames_,
  130. data_->channels()[i],
  131. buffer_num_frames_);
  132. FloatToFloatS16(data_->channels()[i], buffer_num_frames_,
  133. data_->channels()[i]);
  134. }
  135. } else {
  136. for (size_t i = 0; i < num_channels_; ++i) {
  137. FloatToFloatS16(data[i], buffer_num_frames_, data_->channels()[i]);
  138. }
  139. }
  140. }
  141. }
  142. void AudioBuffer::CopyTo(const StreamConfig& stream_config,
  143. float* const* data) {
  144. RTC_DCHECK_EQ(stream_config.num_frames(), output_num_frames_);
  145. const bool resampling_needed = output_num_frames_ != buffer_num_frames_;
  146. if (resampling_needed) {
  147. for (size_t i = 0; i < num_channels_; ++i) {
  148. FloatS16ToFloat(data_->channels()[i], buffer_num_frames_,
  149. data_->channels()[i]);
  150. output_resamplers_[i]->Resample(data_->channels()[i], buffer_num_frames_,
  151. data[i], output_num_frames_);
  152. }
  153. } else {
  154. for (size_t i = 0; i < num_channels_; ++i) {
  155. FloatS16ToFloat(data_->channels()[i], buffer_num_frames_, data[i]);
  156. }
  157. }
  158. for (size_t i = num_channels_; i < stream_config.num_channels(); ++i) {
  159. memcpy(data[i], data[0], output_num_frames_ * sizeof(**data));
  160. }
  161. }
  162. void AudioBuffer::CopyTo(AudioBuffer* buffer) const {
  163. RTC_DCHECK_EQ(buffer->num_frames(), output_num_frames_);
  164. const bool resampling_needed = output_num_frames_ != buffer_num_frames_;
  165. if (resampling_needed) {
  166. for (size_t i = 0; i < num_channels_; ++i) {
  167. output_resamplers_[i]->Resample(data_->channels()[i], buffer_num_frames_,
  168. buffer->channels()[i],
  169. buffer->num_frames());
  170. }
  171. } else {
  172. for (size_t i = 0; i < num_channels_; ++i) {
  173. memcpy(buffer->channels()[i], data_->channels()[i],
  174. buffer_num_frames_ * sizeof(**buffer->channels()));
  175. }
  176. }
  177. for (size_t i = num_channels_; i < buffer->num_channels(); ++i) {
  178. memcpy(buffer->channels()[i], buffer->channels()[0],
  179. output_num_frames_ * sizeof(**buffer->channels()));
  180. }
  181. }
  182. void AudioBuffer::RestoreNumChannels() {
  183. num_channels_ = buffer_num_channels_;
  184. data_->set_num_channels(buffer_num_channels_);
  185. if (split_data_.get()) {
  186. split_data_->set_num_channels(buffer_num_channels_);
  187. }
  188. }
  189. void AudioBuffer::set_num_channels(size_t num_channels) {
  190. RTC_DCHECK_GE(buffer_num_channels_, num_channels);
  191. num_channels_ = num_channels;
  192. data_->set_num_channels(num_channels);
  193. if (split_data_.get()) {
  194. split_data_->set_num_channels(num_channels);
  195. }
  196. }
  197. // The resampler is only for supporting 48kHz to 16kHz in the reverse stream.
  198. void AudioBuffer::CopyFrom(const AudioFrame* frame) {
  199. RTC_DCHECK_EQ(frame->num_channels_, input_num_channels_);
  200. RTC_DCHECK_EQ(frame->samples_per_channel_, input_num_frames_);
  201. RestoreNumChannels();
  202. const bool resampling_required = input_num_frames_ != buffer_num_frames_;
  203. const int16_t* interleaved = frame->data();
  204. if (num_channels_ == 1) {
  205. if (input_num_channels_ == 1) {
  206. if (resampling_required) {
  207. std::array<float, kMaxSamplesPerChannel> float_buffer;
  208. S16ToFloatS16(interleaved, input_num_frames_, float_buffer.data());
  209. input_resamplers_[0]->Resample(float_buffer.data(), input_num_frames_,
  210. data_->channels()[0],
  211. buffer_num_frames_);
  212. } else {
  213. S16ToFloatS16(interleaved, input_num_frames_, data_->channels()[0]);
  214. }
  215. } else {
  216. std::array<float, kMaxSamplesPerChannel> float_buffer;
  217. float* downmixed_data =
  218. resampling_required ? float_buffer.data() : data_->channels()[0];
  219. if (downmix_by_averaging_) {
  220. for (size_t j = 0, k = 0; j < input_num_frames_; ++j) {
  221. int32_t sum = 0;
  222. for (size_t i = 0; i < input_num_channels_; ++i, ++k) {
  223. sum += interleaved[k];
  224. }
  225. downmixed_data[j] = sum / static_cast<int16_t>(input_num_channels_);
  226. }
  227. } else {
  228. for (size_t j = 0, k = channel_for_downmixing_; j < input_num_frames_;
  229. ++j, k += input_num_channels_) {
  230. downmixed_data[j] = interleaved[k];
  231. }
  232. }
  233. if (resampling_required) {
  234. input_resamplers_[0]->Resample(downmixed_data, input_num_frames_,
  235. data_->channels()[0],
  236. buffer_num_frames_);
  237. }
  238. }
  239. } else {
  240. auto deinterleave_channel = [](size_t channel, size_t num_channels,
  241. size_t samples_per_channel, const int16_t* x,
  242. float* y) {
  243. for (size_t j = 0, k = channel; j < samples_per_channel;
  244. ++j, k += num_channels) {
  245. y[j] = x[k];
  246. }
  247. };
  248. if (resampling_required) {
  249. std::array<float, kMaxSamplesPerChannel> float_buffer;
  250. for (size_t i = 0; i < num_channels_; ++i) {
  251. deinterleave_channel(i, num_channels_, input_num_frames_, interleaved,
  252. float_buffer.data());
  253. input_resamplers_[i]->Resample(float_buffer.data(), input_num_frames_,
  254. data_->channels()[i],
  255. buffer_num_frames_);
  256. }
  257. } else {
  258. for (size_t i = 0; i < num_channels_; ++i) {
  259. deinterleave_channel(i, num_channels_, input_num_frames_, interleaved,
  260. data_->channels()[i]);
  261. }
  262. }
  263. }
  264. }
  265. void AudioBuffer::CopyTo(AudioFrame* frame) const {
  266. RTC_DCHECK(frame->num_channels_ == num_channels_ || num_channels_ == 1);
  267. RTC_DCHECK_EQ(frame->samples_per_channel_, output_num_frames_);
  268. const bool resampling_required = buffer_num_frames_ != output_num_frames_;
  269. int16_t* interleaved = frame->mutable_data();
  270. if (num_channels_ == 1) {
  271. std::array<float, kMaxSamplesPerChannel> float_buffer;
  272. if (resampling_required) {
  273. output_resamplers_[0]->Resample(data_->channels()[0], buffer_num_frames_,
  274. float_buffer.data(), output_num_frames_);
  275. }
  276. const float* deinterleaved =
  277. resampling_required ? float_buffer.data() : data_->channels()[0];
  278. if (frame->num_channels_ == 1) {
  279. for (size_t j = 0; j < output_num_frames_; ++j) {
  280. interleaved[j] = FloatS16ToS16(deinterleaved[j]);
  281. }
  282. } else {
  283. for (size_t i = 0, k = 0; i < output_num_frames_; ++i) {
  284. float tmp = FloatS16ToS16(deinterleaved[i]);
  285. for (size_t j = 0; j < frame->num_channels_; ++j, ++k) {
  286. interleaved[k] = tmp;
  287. }
  288. }
  289. }
  290. } else {
  291. auto interleave_channel = [](size_t channel, size_t num_channels,
  292. size_t samples_per_channel, const float* x,
  293. int16_t* y) {
  294. for (size_t k = 0, j = channel; k < samples_per_channel;
  295. ++k, j += num_channels) {
  296. y[j] = FloatS16ToS16(x[k]);
  297. }
  298. };
  299. if (resampling_required) {
  300. for (size_t i = 0; i < num_channels_; ++i) {
  301. std::array<float, kMaxSamplesPerChannel> float_buffer;
  302. output_resamplers_[i]->Resample(data_->channels()[i],
  303. buffer_num_frames_, float_buffer.data(),
  304. output_num_frames_);
  305. interleave_channel(i, frame->num_channels_, output_num_frames_,
  306. float_buffer.data(), interleaved);
  307. }
  308. } else {
  309. for (size_t i = 0; i < num_channels_; ++i) {
  310. interleave_channel(i, frame->num_channels_, output_num_frames_,
  311. data_->channels()[i], interleaved);
  312. }
  313. }
  314. for (size_t i = num_channels_; i < frame->num_channels_; ++i) {
  315. for (size_t j = 0, k = i, n = num_channels_; j < output_num_frames_;
  316. ++j, k += frame->num_channels_, n += frame->num_channels_) {
  317. interleaved[k] = interleaved[n];
  318. }
  319. }
  320. }
  321. }
  322. void AudioBuffer::SplitIntoFrequencyBands() {
  323. splitting_filter_->Analysis(data_.get(), split_data_.get());
  324. }
  325. void AudioBuffer::MergeFrequencyBands() {
  326. splitting_filter_->Synthesis(split_data_.get(), data_.get());
  327. }
  328. void AudioBuffer::ExportSplitChannelData(size_t channel,
  329. int16_t* const* split_band_data) {
  330. for (size_t k = 0; k < num_bands(); ++k) {
  331. const float* band_data = split_bands(channel)[k];
  332. RTC_DCHECK(split_band_data[k]);
  333. RTC_DCHECK(band_data);
  334. for (size_t i = 0; i < num_frames_per_band(); ++i) {
  335. split_band_data[k][i] = FloatS16ToS16(band_data[i]);
  336. }
  337. }
  338. }
  339. void AudioBuffer::ImportSplitChannelData(
  340. size_t channel,
  341. const int16_t* const* split_band_data) {
  342. for (size_t k = 0; k < num_bands(); ++k) {
  343. float* band_data = split_bands(channel)[k];
  344. RTC_DCHECK(split_band_data[k]);
  345. RTC_DCHECK(band_data);
  346. for (size_t i = 0; i < num_frames_per_band(); ++i) {
  347. band_data[i] = split_band_data[k][i];
  348. }
  349. }
  350. }
  351. } // namespace webrtc