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.

234 lines
6.2 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_coding/test/PCMFile.h"
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "rtc_base/checks.h"
  15. #include "test/gtest.h"
  16. namespace webrtc {
  17. #define MAX_FILE_NAME_LENGTH_BYTE 500
  18. PCMFile::PCMFile()
  19. : pcm_file_(NULL),
  20. samples_10ms_(160),
  21. frequency_(16000),
  22. end_of_file_(false),
  23. auto_rewind_(false),
  24. rewinded_(false),
  25. read_stereo_(false),
  26. save_stereo_(false) {
  27. timestamp_ =
  28. (((uint32_t)rand() & 0x0000FFFF) << 16) | ((uint32_t)rand() & 0x0000FFFF);
  29. }
  30. PCMFile::PCMFile(uint32_t timestamp)
  31. : pcm_file_(NULL),
  32. samples_10ms_(160),
  33. frequency_(16000),
  34. end_of_file_(false),
  35. auto_rewind_(false),
  36. rewinded_(false),
  37. read_stereo_(false),
  38. save_stereo_(false) {
  39. timestamp_ = timestamp;
  40. }
  41. PCMFile::~PCMFile() {
  42. if (pcm_file_) {
  43. fclose(pcm_file_);
  44. }
  45. }
  46. int16_t PCMFile::ChooseFile(std::string* file_name,
  47. int16_t max_len,
  48. uint16_t* frequency_hz) {
  49. char tmp_name[MAX_FILE_NAME_LENGTH_BYTE];
  50. EXPECT_TRUE(fgets(tmp_name, MAX_FILE_NAME_LENGTH_BYTE, stdin) != NULL);
  51. tmp_name[MAX_FILE_NAME_LENGTH_BYTE - 1] = '\0';
  52. int16_t n = 0;
  53. // Removing trailing spaces.
  54. while ((isspace(tmp_name[n]) || iscntrl(tmp_name[n])) && (tmp_name[n] != 0) &&
  55. (n < MAX_FILE_NAME_LENGTH_BYTE)) {
  56. n++;
  57. }
  58. if (n > 0) {
  59. memmove(tmp_name, &tmp_name[n], MAX_FILE_NAME_LENGTH_BYTE - n);
  60. }
  61. // Removing trailing spaces.
  62. n = (int16_t)(strlen(tmp_name) - 1);
  63. if (n >= 0) {
  64. while ((isspace(tmp_name[n]) || iscntrl(tmp_name[n])) && (n >= 0)) {
  65. n--;
  66. }
  67. }
  68. if (n >= 0) {
  69. tmp_name[n + 1] = '\0';
  70. }
  71. int16_t len = (int16_t)strlen(tmp_name);
  72. if (len > max_len) {
  73. return -1;
  74. }
  75. if (len > 0) {
  76. std::string tmp_string(tmp_name, len + 1);
  77. *file_name = tmp_string;
  78. }
  79. printf("Enter the sampling frequency (in Hz) of the above file [%u]: ",
  80. *frequency_hz);
  81. EXPECT_TRUE(fgets(tmp_name, 10, stdin) != NULL);
  82. uint16_t tmp_frequency = (uint16_t)atoi(tmp_name);
  83. if (tmp_frequency > 0) {
  84. *frequency_hz = tmp_frequency;
  85. }
  86. return 0;
  87. }
  88. void PCMFile::Open(const std::string& file_name,
  89. uint16_t frequency,
  90. const char* mode,
  91. bool auto_rewind) {
  92. if ((pcm_file_ = fopen(file_name.c_str(), mode)) == NULL) {
  93. printf("Cannot open file %s.\n", file_name.c_str());
  94. ADD_FAILURE() << "Unable to read file";
  95. }
  96. frequency_ = frequency;
  97. samples_10ms_ = (uint16_t)(frequency_ / 100);
  98. auto_rewind_ = auto_rewind;
  99. end_of_file_ = false;
  100. rewinded_ = false;
  101. }
  102. int32_t PCMFile::SamplingFrequency() const {
  103. return frequency_;
  104. }
  105. uint16_t PCMFile::PayloadLength10Ms() const {
  106. return samples_10ms_;
  107. }
  108. int32_t PCMFile::Read10MsData(AudioFrame& audio_frame) {
  109. uint16_t channels = 1;
  110. if (read_stereo_) {
  111. channels = 2;
  112. }
  113. int32_t payload_size =
  114. (int32_t)fread(audio_frame.mutable_data(), sizeof(uint16_t),
  115. samples_10ms_ * channels, pcm_file_);
  116. if (payload_size < samples_10ms_ * channels) {
  117. int16_t* frame_data = audio_frame.mutable_data();
  118. for (int k = payload_size; k < samples_10ms_ * channels; k++) {
  119. frame_data[k] = 0;
  120. }
  121. if (auto_rewind_) {
  122. rewind(pcm_file_);
  123. rewinded_ = true;
  124. } else {
  125. end_of_file_ = true;
  126. }
  127. }
  128. audio_frame.samples_per_channel_ = samples_10ms_;
  129. audio_frame.sample_rate_hz_ = frequency_;
  130. audio_frame.num_channels_ = channels;
  131. audio_frame.timestamp_ = timestamp_;
  132. timestamp_ += samples_10ms_;
  133. ++blocks_read_;
  134. if (num_10ms_blocks_to_read_ && blocks_read_ >= *num_10ms_blocks_to_read_)
  135. end_of_file_ = true;
  136. return samples_10ms_;
  137. }
  138. void PCMFile::Write10MsData(const AudioFrame& audio_frame) {
  139. if (audio_frame.num_channels_ == 1) {
  140. if (!save_stereo_) {
  141. if (fwrite(audio_frame.data(), sizeof(uint16_t),
  142. audio_frame.samples_per_channel_, pcm_file_) !=
  143. static_cast<size_t>(audio_frame.samples_per_channel_)) {
  144. return;
  145. }
  146. } else {
  147. const int16_t* frame_data = audio_frame.data();
  148. int16_t* stereo_audio = new int16_t[2 * audio_frame.samples_per_channel_];
  149. for (size_t k = 0; k < audio_frame.samples_per_channel_; k++) {
  150. stereo_audio[k << 1] = frame_data[k];
  151. stereo_audio[(k << 1) + 1] = frame_data[k];
  152. }
  153. if (fwrite(stereo_audio, sizeof(int16_t),
  154. 2 * audio_frame.samples_per_channel_, pcm_file_) !=
  155. static_cast<size_t>(2 * audio_frame.samples_per_channel_)) {
  156. return;
  157. }
  158. delete[] stereo_audio;
  159. }
  160. } else {
  161. if (fwrite(audio_frame.data(), sizeof(int16_t),
  162. audio_frame.num_channels_ * audio_frame.samples_per_channel_,
  163. pcm_file_) !=
  164. static_cast<size_t>(audio_frame.num_channels_ *
  165. audio_frame.samples_per_channel_)) {
  166. return;
  167. }
  168. }
  169. }
  170. void PCMFile::Write10MsData(const int16_t* playout_buffer,
  171. size_t length_smpls) {
  172. if (fwrite(playout_buffer, sizeof(uint16_t), length_smpls, pcm_file_) !=
  173. length_smpls) {
  174. return;
  175. }
  176. }
  177. void PCMFile::Close() {
  178. fclose(pcm_file_);
  179. pcm_file_ = NULL;
  180. blocks_read_ = 0;
  181. }
  182. void PCMFile::FastForward(int num_10ms_blocks) {
  183. const int channels = read_stereo_ ? 2 : 1;
  184. long num_bytes_to_move =
  185. num_10ms_blocks * sizeof(int16_t) * samples_10ms_ * channels;
  186. int error = fseek(pcm_file_, num_bytes_to_move, SEEK_CUR);
  187. RTC_DCHECK_EQ(error, 0);
  188. }
  189. void PCMFile::Rewind() {
  190. rewind(pcm_file_);
  191. end_of_file_ = false;
  192. blocks_read_ = 0;
  193. }
  194. bool PCMFile::Rewinded() {
  195. return rewinded_;
  196. }
  197. void PCMFile::SaveStereo(bool is_stereo) {
  198. save_stereo_ = is_stereo;
  199. }
  200. void PCMFile::ReadStereo(bool is_stereo) {
  201. read_stereo_ = is_stereo;
  202. }
  203. void PCMFile::SetNum10MsBlocksToRead(int value) {
  204. num_10ms_blocks_to_read_ = value;
  205. }
  206. } // namespace webrtc