tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

wav_header.h (3235B)


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