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.

65 lines
2.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. namespace webrtc {
  15. static const size_t kWavHeaderSize = 44;
  16. class ReadableWav {
  17. public:
  18. // Returns the number of bytes read.
  19. virtual size_t Read(void* buf, size_t num_bytes) = 0;
  20. virtual bool SeekForward(uint32_t num_bytes) = 0;
  21. virtual ~ReadableWav() = default;
  22. };
  23. enum WavFormat {
  24. kWavFormatPcm = 1, // PCM, each sample of size bytes_per_sample
  25. kWavFormatALaw = 6, // 8-bit ITU-T G.711 A-law
  26. kWavFormatMuLaw = 7, // 8-bit ITU-T G.711 mu-law
  27. };
  28. // Return true if the given parameters will make a well-formed WAV header.
  29. bool CheckWavParameters(size_t num_channels,
  30. int sample_rate,
  31. WavFormat format,
  32. size_t bytes_per_sample,
  33. size_t num_samples);
  34. // Write a kWavHeaderSize bytes long WAV header to buf. The payload that
  35. // follows the header is supposed to have the specified number of interleaved
  36. // channels and contain the specified total number of samples of the specified
  37. // type. CHECKs the input parameters for validity.
  38. void WriteWavHeader(uint8_t* buf,
  39. size_t num_channels,
  40. int sample_rate,
  41. WavFormat format,
  42. size_t bytes_per_sample,
  43. size_t num_samples);
  44. // Read a WAV header from an implemented ReadableWav and parse the values into
  45. // the provided output parameters. ReadableWav is used because the header can
  46. // be variably sized. Returns false if the header is invalid.
  47. bool ReadWavHeader(ReadableWav* readable,
  48. size_t* num_channels,
  49. int* sample_rate,
  50. WavFormat* format,
  51. size_t* bytes_per_sample,
  52. size_t* num_samples);
  53. } // namespace webrtc
  54. #endif // COMMON_AUDIO_WAV_HEADER_H_