1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef MEDIA_AUDIO_SIMPLE_SOURCES_H_
6 #define MEDIA_AUDIO_SIMPLE_SOURCES_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 
12 #include "base/files/file_path.h"
13 #include "base/synchronization/lock.h"
14 #include "base/thread_annotations.h"
15 #include "base/time/time.h"
16 #include "media/audio/audio_io.h"
17 #include "media/base/audio_converter.h"
18 #include "media/base/seekable_buffer.h"
19 
20 namespace media {
21 
22 class WavAudioHandler;
23 
24 // An audio source that produces a pure sinusoidal tone.
25 class MEDIA_EXPORT SineWaveAudioSource
26     : public AudioOutputStream::AudioSourceCallback {
27  public:
28   // |channels| is the number of audio channels, |freq| is the frequency in
29   // hertz and it has to be less than half of the sampling frequency
30   // |sample_freq| or else you will get aliasing.
31   SineWaveAudioSource(int channels, double freq, double sample_freq);
32   ~SineWaveAudioSource() override;
33 
34   // Return up to |cap| samples of data via OnMoreData().  Use Reset() to
35   // allow more data to be served.
36   void CapSamples(int cap);
37   void Reset();
38 
39   // Sets a callback to be called in OnMoreData(). The callback should be set
40   // only once before SineWaveAudioSource is passed to AudioOutputStream. It
41   // will be called on the same thread on which the stream calls OnMoreData(),
42   // which is usually different from the thread where the source is created.
set_on_more_data_callback(base::RepeatingClosure on_more_data_callback)43   void set_on_more_data_callback(base::RepeatingClosure on_more_data_callback) {
44     on_more_data_callback_ = on_more_data_callback;
45   }
46 
47   // Implementation of AudioSourceCallback.
48   int OnMoreData(base::TimeDelta delay,
49                  base::TimeTicks timestamp,
50                  int prior_frames_skipped,
51                  AudioBus* dest) override;
52   void OnError(ErrorType type) override;
53 
54   // The number of OnMoreData() and OnError() calls respectively.
callbacks()55   int callbacks() {
56     base::AutoLock auto_lock(lock_);
57     return callbacks_;
58   }
pos_samples()59   int pos_samples() {
60     base::AutoLock auto_lock(lock_);
61     return pos_samples_;
62   }
errors()63   int errors() { return errors_; }
64 
65  protected:
66   const int channels_;
67   const double f_;
68 
69   base::RepeatingClosure on_more_data_callback_;
70 
71   base::Lock lock_;
72   int pos_samples_ GUARDED_BY(lock_) = 0;
73   int cap_ GUARDED_BY(lock_) = 0;
74   int callbacks_ GUARDED_BY(lock_) = 0;
75   int errors_ = 0;
76 };
77 
78 class MEDIA_EXPORT FileSource : public AudioOutputStream::AudioSourceCallback,
79                                 public AudioConverter::InputCallback {
80  public:
81   FileSource(const AudioParameters& params,
82              const base::FilePath& path_to_wav_file,
83              bool loop);
84   ~FileSource() override;
85 
86   // Implementation of AudioSourceCallback.
87   int OnMoreData(base::TimeDelta delay,
88                  base::TimeTicks delay_timestamp,
89                  int prior_frames_skipped,
90                  AudioBus* dest) override;
91   void OnError(ErrorType type) override;
92 
93  private:
94   AudioParameters params_;
95   base::FilePath path_to_wav_file_;
96 
97   // The WAV data at |path_to_wav_file_| is read into memory and kept here.
98   // This memory needs to survive for the lifetime of |wav_audio_handler_|,
99   // so declare it first. Do not access this member directly.
100   std::unique_ptr<char[]> raw_wav_data_;
101 
102   std::unique_ptr<WavAudioHandler> wav_audio_handler_;
103   std::unique_ptr<AudioConverter> file_audio_converter_;
104   int wav_file_read_pos_;
105   bool load_failed_;
106   bool looping_;
107 
108   // Provides audio data from wav_audio_handler_ into the file audio converter.
109   double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override;
110 
111   // Loads the wav file on the first OnMoreData invocation.
112   void LoadWavFile(const base::FilePath& path_to_wav_file);
113 
114   // Rewinds the player to the start of the loaded wav file.
115   void Rewind();
116 };
117 
118 class BeepingSource : public AudioOutputStream::AudioSourceCallback {
119  public:
120   explicit BeepingSource(const AudioParameters& params);
121   ~BeepingSource() override;
122 
123   // Implementation of AudioSourceCallback.
124   int OnMoreData(base::TimeDelta delay,
125                  base::TimeTicks delay_timestamp,
126                  int prior_frames_skipped,
127                  AudioBus* dest) override;
128   void OnError(ErrorType type) override;
129 
130   static void BeepOnce();
131  private:
132   int buffer_size_;
133   std::unique_ptr<uint8_t[]> buffer_;
134   AudioParameters params_;
135   base::TimeTicks last_callback_time_;
136   base::TimeDelta interval_from_last_beep_;
137   int beep_duration_in_buffers_;
138   int beep_generated_in_buffers_;
139   int beep_period_in_frames_;
140 };
141 
142 }  // namespace media
143 
144 #endif  // MEDIA_AUDIO_SIMPLE_SOURCES_H_
145