summaryrefslogtreecommitdiffstats
path: root/media/audio/simple_sources.h
blob: 7d5c3c4b87a1b57aec7c284ed993c62cf24b6822 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_AUDIO_SIMPLE_SOURCES_H_
#define MEDIA_AUDIO_SIMPLE_SOURCES_H_

#include "base/files/file_path.h"
#include "base/synchronization/lock.h"
#include "media/audio/audio_io.h"
#include "media/base/audio_converter.h"
#include "media/base/seekable_buffer.h"

namespace media {

class WavAudioHandler;

// An audio source that produces a pure sinusoidal tone.
class MEDIA_EXPORT SineWaveAudioSource
    : public AudioOutputStream::AudioSourceCallback {
 public:
  // |channels| is the number of audio channels, |freq| is the frequency in
  // hertz and it has to be less than half of the sampling frequency
  // |sample_freq| or else you will get aliasing.
  SineWaveAudioSource(int channels, double freq, double sample_freq);
  ~SineWaveAudioSource() override;

  // Return up to |cap| samples of data via OnMoreData().  Use Reset() to
  // allow more data to be served.
  void CapSamples(int cap);
  void Reset();

  // Implementation of AudioSourceCallback.
  int OnMoreData(AudioBus* audio_bus,
                 uint32_t total_bytes_delay,
                 uint32_t frames_skipped) override;
  void OnError(AudioOutputStream* stream) override;

  // The number of OnMoreData() and OnError() calls respectively.
  int callbacks() { return callbacks_; }
  int errors() { return errors_; }

 protected:
  int channels_;
  double f_;
  int time_state_;
  int cap_;
  int callbacks_;
  int errors_;
  base::Lock time_lock_;
};

class MEDIA_EXPORT FileSource : public AudioOutputStream::AudioSourceCallback,
                                public AudioConverter::InputCallback {
 public:
  FileSource(const AudioParameters& params,
             const base::FilePath& path_to_wav_file);
  ~FileSource() override;

  // Implementation of AudioSourceCallback.
  int OnMoreData(AudioBus* audio_bus,
                 uint32_t total_bytes_delay,
                 uint32_t frames_skipped) override;
  void OnError(AudioOutputStream* stream) override;

 private:
  AudioParameters params_;
  base::FilePath path_to_wav_file_;

  // The WAV data at |path_to_wav_file_| is read into memory and kept here.
  // This memory needs to survive for the lifetime of |wav_audio_handler_|,
  // so declare it first. Do not access this member directly.
  scoped_ptr<char[]> raw_wav_data_;

  scoped_ptr<WavAudioHandler> wav_audio_handler_;
  scoped_ptr<AudioConverter> file_audio_converter_;
  int wav_file_read_pos_;
  bool load_failed_;

  // Provides audio data from wav_audio_handler_ into the file audio converter.
  double ProvideInput(AudioBus* audio_bus,
                      base::TimeDelta buffer_delay) override;

  // Loads the wav file on the first OnMoreData invocation.
  void LoadWavFile(const base::FilePath& path_to_wav_file);
};

class BeepingSource : public AudioOutputStream::AudioSourceCallback {
 public:
  BeepingSource(const AudioParameters& params);
  ~BeepingSource() override;

  // Implementation of AudioSourceCallback.
  int OnMoreData(AudioBus* audio_bus,
                 uint32_t total_bytes_delay,
                 uint32_t frames_skipped) override;
  void OnError(AudioOutputStream* stream) override;

  static void BeepOnce();
 private:
  int buffer_size_;
  scoped_ptr<uint8_t[]> buffer_;
  AudioParameters params_;
  base::TimeTicks last_callback_time_;
  base::TimeDelta interval_from_last_beep_;
  int beep_duration_in_buffers_;
  int beep_generated_in_buffers_;
  int beep_period_in_frames_;
};

}  // namespace media

#endif  // MEDIA_AUDIO_SIMPLE_SOURCES_H_