PCMFile.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #ifndef MODULES_AUDIO_CODING_TEST_PCMFILE_H_
  11. #define MODULES_AUDIO_CODING_TEST_PCMFILE_H_
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string>
  15. #include "absl/types/optional.h"
  16. #include "api/audio/audio_frame.h"
  17. namespace webrtc {
  18. class PCMFile {
  19. public:
  20. PCMFile();
  21. PCMFile(uint32_t timestamp);
  22. ~PCMFile();
  23. void Open(const std::string& filename,
  24. uint16_t frequency,
  25. const char* mode,
  26. bool auto_rewind = false);
  27. int32_t Read10MsData(AudioFrame& audio_frame);
  28. void Write10MsData(const int16_t* playout_buffer, size_t length_smpls);
  29. void Write10MsData(const AudioFrame& audio_frame);
  30. uint16_t PayloadLength10Ms() const;
  31. int32_t SamplingFrequency() const;
  32. void Close();
  33. bool EndOfFile() const { return end_of_file_; }
  34. // Moves forward the specified number of 10 ms blocks. If a limit has been set
  35. // with SetNum10MsBlocksToRead, fast-forwarding does not count towards this
  36. // limit.
  37. void FastForward(int num_10ms_blocks);
  38. void Rewind();
  39. static int16_t ChooseFile(std::string* file_name,
  40. int16_t max_len,
  41. uint16_t* frequency_hz);
  42. bool Rewinded();
  43. void SaveStereo(bool is_stereo = true);
  44. void ReadStereo(bool is_stereo = true);
  45. // If set, the reading will stop after the specified number of blocks have
  46. // been read. When that has happened, EndOfFile() will return true. Calling
  47. // Rewind() will reset the counter and start over.
  48. void SetNum10MsBlocksToRead(int value);
  49. private:
  50. FILE* pcm_file_;
  51. uint16_t samples_10ms_;
  52. int32_t frequency_;
  53. bool end_of_file_;
  54. bool auto_rewind_;
  55. bool rewinded_;
  56. uint32_t timestamp_;
  57. bool read_stereo_;
  58. bool save_stereo_;
  59. absl::optional<int> num_10ms_blocks_to_read_;
  60. int blocks_read_ = 0;
  61. };
  62. } // namespace webrtc
  63. #endif // MODULES_AUDIO_CODING_TEST_PCMFILE_H_