wav_file.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 COMMON_AUDIO_WAV_FILE_H_
  11. #define COMMON_AUDIO_WAV_FILE_H_
  12. #include <stdint.h>
  13. #include <cstddef>
  14. #include <string>
  15. #include "common_audio/wav_header.h"
  16. #include "rtc_base/system/file_wrapper.h"
  17. namespace webrtc {
  18. // Interface to provide access WAV file parameters.
  19. class WavFile {
  20. public:
  21. enum class SampleFormat { kInt16, kFloat };
  22. virtual ~WavFile() {}
  23. virtual int sample_rate() const = 0;
  24. virtual size_t num_channels() const = 0;
  25. virtual size_t num_samples() const = 0;
  26. };
  27. // Simple C++ class for writing 16-bit integer and 32 bit floating point PCM WAV
  28. // files. All error handling is by calls to RTC_CHECK(), making it unsuitable
  29. // for anything but debug code.
  30. class WavWriter final : public WavFile {
  31. public:
  32. // Opens a new WAV file for writing.
  33. WavWriter(const std::string& filename,
  34. int sample_rate,
  35. size_t num_channels,
  36. SampleFormat sample_format = SampleFormat::kInt16);
  37. WavWriter(FileWrapper file,
  38. int sample_rate,
  39. size_t num_channels,
  40. SampleFormat sample_format = SampleFormat::kInt16);
  41. // Closes the WAV file, after writing its header.
  42. ~WavWriter() { Close(); }
  43. WavWriter(const WavWriter&) = delete;
  44. WavWriter& operator=(const WavWriter&) = delete;
  45. // Write additional samples to the file. Each sample is in the range
  46. // [-32768.0,32767.0], and there must be the previously specified number of
  47. // interleaved channels.
  48. void WriteSamples(const float* samples, size_t num_samples);
  49. void WriteSamples(const int16_t* samples, size_t num_samples);
  50. int sample_rate() const override { return sample_rate_; }
  51. size_t num_channels() const override { return num_channels_; }
  52. size_t num_samples() const override { return num_samples_written_; }
  53. private:
  54. void Close();
  55. const int sample_rate_;
  56. const size_t num_channels_;
  57. size_t num_samples_written_;
  58. WavFormat format_;
  59. FileWrapper file_;
  60. };
  61. // Follows the conventions of WavWriter.
  62. class WavReader final : public WavFile {
  63. public:
  64. // Opens an existing WAV file for reading.
  65. explicit WavReader(const std::string& filename);
  66. explicit WavReader(FileWrapper file);
  67. // Close the WAV file.
  68. ~WavReader() { Close(); }
  69. WavReader(const WavReader&) = delete;
  70. WavReader& operator=(const WavReader&) = delete;
  71. // Resets position to the beginning of the file.
  72. void Reset();
  73. // Returns the number of samples read. If this is less than requested,
  74. // verifies that the end of the file was reached.
  75. size_t ReadSamples(size_t num_samples, float* samples);
  76. size_t ReadSamples(size_t num_samples, int16_t* samples);
  77. int sample_rate() const override { return sample_rate_; }
  78. size_t num_channels() const override { return num_channels_; }
  79. size_t num_samples() const override { return num_samples_in_file_; }
  80. private:
  81. void Close();
  82. int sample_rate_;
  83. size_t num_channels_;
  84. WavFormat format_;
  85. size_t num_samples_in_file_;
  86. size_t num_unread_samples_;
  87. FileWrapper file_;
  88. int64_t
  89. data_start_pos_; // Position in the file immediately after WAV header.
  90. };
  91. } // namespace webrtc
  92. #endif // COMMON_AUDIO_WAV_FILE_H_