1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85

media / audio / wav_audio_handler.h [blame]

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_AUDIO_WAV_AUDIO_HANDLER_H_
#define MEDIA_AUDIO_WAV_AUDIO_HANDLER_H_

#include <stddef.h>
#include <stdint.h>

#include <memory>

#include "base/memory/raw_span.h"
#include "base/time/time.h"
#include "media/audio/audio_handler.h"
#include "media/base/media_export.h"

namespace media {

class AudioBus;

// This class provides the input from wav file format.  See
// https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
class MEDIA_EXPORT WavAudioHandler : public AudioHandler {
 public:
  // Supported audio format.
  enum class AudioFormat : uint16_t {
    kAudioFormatPCM = 0x0001,
    kAudioFormatFloat = 0x0003,
    kAudioFormatExtensible = 0xfffe
  };

  WavAudioHandler(const WavAudioHandler&) = delete;
  WavAudioHandler& operator=(const WavAudioHandler&) = delete;

  ~WavAudioHandler() override;

  // Create a WavAudioHandler using |wav_data|. If |wav_data| cannot be parsed
  // correctly, the returned scoped_ptr will be null. The underlying memory for
  // wav_data must survive for the lifetime of this class.
  static std::unique_ptr<WavAudioHandler> Create(
      base::span<const uint8_t> wav_data);

  // AudioHandler:
  bool Initialize() override;
  int GetNumChannels() const override;
  int GetSampleRate() const override;
  base::TimeDelta GetDuration() const override;
  bool AtEnd() const override;
  bool CopyTo(AudioBus* bus, size_t* frames_written) override;
  void Reset() override;

  // Accessors.
  base::span<const uint8_t> data() const { return audio_data_; }
  AudioFormat audio_format() const { return audio_format_; }

  int total_frames_for_testing() const {
    return static_cast<int>(total_frames_);
  }
  int bits_per_sample_for_testing() const {
    return static_cast<int>(bits_per_sample_);
  }

 private:
  // Note: It is preferred to pass |audio_data| by value here.
  WavAudioHandler(base::span<const uint8_t> audio_data,
                  uint16_t num_channels,
                  uint32_t sample_rate,
                  uint16_t bits_per_sample,
                  AudioFormat audio_format);

  // Data part of the |wav_data_|.
  const base::raw_span<const uint8_t> audio_data_;
  const uint16_t num_channels_;
  const uint32_t sample_rate_;
  const uint16_t bits_per_sample_;
  const AudioFormat audio_format_;
  uint32_t total_frames_;

  size_t cursor_ = 0;
};

}  // namespace media

#endif  // MEDIA_AUDIO_WAV_AUDIO_HANDLER_H_