online-feature.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * Copyright (c) 2022 Xiaomi Corporation (authors: Fangjun Kuang)
  3. *
  4. * See LICENSE for clarification regarding multiple authors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. // The content in this file is copied/modified from
  19. // This file is copied/modified from kaldi/src/feat/online-feature.cc
  20. #include "online-feature.h"
  21. #include <algorithm>
  22. #include <utility>
  23. #include <vector>
  24. #include "feature-window.h"
  25. #include "log.h"
  26. namespace knf {
  27. RecyclingVector::RecyclingVector(int32_t items_to_hold)
  28. : items_to_hold_(items_to_hold == 0 ? -1 : items_to_hold),
  29. first_available_index_(0) {}
  30. const float *RecyclingVector::At(int32_t index) const {
  31. if (index < first_available_index_) {
  32. KNF_LOG(FATAL) << "Attempted to retrieve feature vector that was "
  33. "already removed by the RecyclingVector (index = "
  34. << index << "; "
  35. << "first_available_index = " << first_available_index_
  36. << "; "
  37. << "size = " << Size() << ")";
  38. }
  39. // 'at' does size checking.
  40. return items_.at(index - first_available_index_).data();
  41. }
  42. void RecyclingVector::PushBack(std::vector<float> item) {
  43. // Note: -1 is a larger number when treated as unsigned
  44. if (items_.size() == static_cast<size_t>(items_to_hold_)) {
  45. items_.pop_front();
  46. ++first_available_index_;
  47. }
  48. items_.push_back(std::move(item));
  49. }
  50. int32_t RecyclingVector::Size() const {
  51. return first_available_index_ + static_cast<int32_t>(items_.size());
  52. }
  53. // discard the first n frames
  54. void RecyclingVector::Pop(int32_t n) {
  55. for (int32_t i = 0; i < n && !items_.empty(); ++i) {
  56. items_.pop_front();
  57. ++first_available_index_;
  58. }
  59. }
  60. template <class C>
  61. OnlineGenericBaseFeature<C>::OnlineGenericBaseFeature(
  62. const typename C::Options &opts)
  63. : computer_(opts),
  64. window_function_(computer_.GetFrameOptions()),
  65. input_finished_(false),
  66. waveform_offset_(0) {}
  67. template <class C>
  68. void OnlineGenericBaseFeature<C>::AcceptWaveform(float sampling_rate,
  69. const float *waveform,
  70. int32_t n) {
  71. if (n == 0) {
  72. return; // Nothing to do.
  73. }
  74. if (input_finished_) {
  75. KNF_LOG(FATAL) << "AcceptWaveform called after InputFinished() was called.";
  76. }
  77. KNF_CHECK_EQ(sampling_rate, computer_.GetFrameOptions().samp_freq);
  78. waveform_remainder_.insert(waveform_remainder_.end(), waveform, waveform + n);
  79. ComputeFeatures();
  80. }
  81. template <class C>
  82. void OnlineGenericBaseFeature<C>::InputFinished() {
  83. input_finished_ = true;
  84. ComputeFeatures();
  85. }
  86. template <class C>
  87. void OnlineGenericBaseFeature<C>::ComputeFeatures() {
  88. const FrameExtractionOptions &frame_opts = computer_.GetFrameOptions();
  89. int64_t num_samples_total = waveform_offset_ + waveform_remainder_.size();
  90. int32_t num_frames_old = features_.Size();
  91. int32_t num_frames_new =
  92. NumFrames(num_samples_total, frame_opts, input_finished_);
  93. KNF_CHECK_GE(num_frames_new, num_frames_old);
  94. // note: this online feature-extraction code does not support VTLN.
  95. float vtln_warp = 1.0;
  96. std::vector<float> window;
  97. bool need_raw_log_energy = computer_.NeedRawLogEnergy();
  98. for (int32_t frame = num_frames_old; frame < num_frames_new; ++frame) {
  99. std::fill(window.begin(), window.end(), 0);
  100. float raw_log_energy = 0.0;
  101. ExtractWindow(waveform_offset_, waveform_remainder_.data(), waveform_remainder_.size(),
  102. frame, frame_opts, window_function_, &window,
  103. need_raw_log_energy ? &raw_log_energy : nullptr);
  104. std::vector<float> this_feature(computer_.Dim());
  105. computer_.Compute(raw_log_energy, vtln_warp, &window, this_feature.data());
  106. features_.PushBack(std::move(this_feature));
  107. }
  108. // OK, we will now discard any portion of the signal that will not be
  109. // necessary to compute frames in the future.
  110. int64_t first_sample_of_next_frame =
  111. FirstSampleOfFrame(num_frames_new, frame_opts);
  112. int32_t samples_to_discard = first_sample_of_next_frame - waveform_offset_;
  113. if (samples_to_discard > 0) {
  114. // discard the leftmost part of the waveform that we no longer need.
  115. int32_t new_num_samples =
  116. static_cast<int32_t>(waveform_remainder_.size()) - samples_to_discard;
  117. if (new_num_samples <= 0) {
  118. // odd, but we'll try to handle it.
  119. waveform_offset_ += waveform_remainder_.size();
  120. waveform_remainder_.resize(0);
  121. } else {
  122. std::vector<float> new_remainder(new_num_samples);
  123. std::copy(waveform_remainder_.begin() + samples_to_discard,
  124. waveform_remainder_.end(), new_remainder.begin());
  125. waveform_offset_ += samples_to_discard;
  126. waveform_remainder_.swap(new_remainder);
  127. }
  128. }
  129. }
  130. template class OnlineGenericBaseFeature<FbankComputer>;
  131. } // namespace knf