wav_header.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_HEADER_H_
  11. #define COMMON_AUDIO_WAV_HEADER_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <algorithm>
  15. #include "rtc_base/checks.h"
  16. namespace webrtc {
  17. // Interface providing header reading functionality.
  18. class WavHeaderReader {
  19. public:
  20. // Returns the number of bytes read.
  21. virtual size_t Read(void* buf, size_t num_bytes) = 0;
  22. virtual bool SeekForward(uint32_t num_bytes) = 0;
  23. virtual ~WavHeaderReader() = default;
  24. virtual int64_t GetPosition() = 0;
  25. };
  26. // Possible WAV formats.
  27. enum class WavFormat {
  28. kWavFormatPcm = 1, // PCM, each sample of size bytes_per_sample.
  29. kWavFormatIeeeFloat = 3, // IEEE float.
  30. kWavFormatALaw = 6, // 8-bit ITU-T G.711 A-law.
  31. kWavFormatMuLaw = 7, // 8-bit ITU-T G.711 mu-law.
  32. };
  33. // Header sizes for supported WAV formats.
  34. constexpr size_t kPcmWavHeaderSize = 44;
  35. constexpr size_t kIeeeFloatWavHeaderSize = 58;
  36. // Returns the size of the WAV header for the specified format.
  37. constexpr size_t WavHeaderSize(WavFormat format) {
  38. if (format == WavFormat::kWavFormatPcm) {
  39. return kPcmWavHeaderSize;
  40. }
  41. RTC_CHECK_EQ(format, WavFormat::kWavFormatIeeeFloat);
  42. return kIeeeFloatWavHeaderSize;
  43. }
  44. // Returns the maximum size of the supported WAV formats.
  45. constexpr size_t MaxWavHeaderSize() {
  46. return std::max(WavHeaderSize(WavFormat::kWavFormatPcm),
  47. WavHeaderSize(WavFormat::kWavFormatIeeeFloat));
  48. }
  49. // Return true if the given parameters will make a well-formed WAV header.
  50. bool CheckWavParameters(size_t num_channels,
  51. int sample_rate,
  52. WavFormat format,
  53. size_t num_samples);
  54. // Write a kWavHeaderSize bytes long WAV header to buf. The payload that
  55. // follows the header is supposed to have the specified number of interleaved
  56. // channels and contain the specified total number of samples of the specified
  57. // type. The size of the header is returned in header_size. CHECKs the input
  58. // parameters for validity.
  59. void WriteWavHeader(size_t num_channels,
  60. int sample_rate,
  61. WavFormat format,
  62. size_t num_samples,
  63. uint8_t* buf,
  64. size_t* header_size);
  65. // Read a WAV header from an implemented WavHeaderReader and parse the values
  66. // into the provided output parameters. WavHeaderReader is used because the
  67. // header can be variably sized. Returns false if the header is invalid.
  68. bool ReadWavHeader(WavHeaderReader* readable,
  69. size_t* num_channels,
  70. int* sample_rate,
  71. WavFormat* format,
  72. size_t* bytes_per_sample,
  73. size_t* num_samples,
  74. int64_t* data_start_pos);
  75. } // namespace webrtc
  76. #endif // COMMON_AUDIO_WAV_HEADER_H_