Rename: Use "Register" Mojo Service/App in content client API.
[chromium-blink-merge.git] / media / audio / simple_sources.h
blobdcb04fd84534804913c4b827c51a77569ea46b1e
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.
5 #ifndef MEDIA_AUDIO_SIMPLE_SOURCES_H_
6 #define MEDIA_AUDIO_SIMPLE_SOURCES_H_
8 #include "base/files/file_path.h"
9 #include "base/synchronization/lock.h"
10 #include "media/audio/audio_io.h"
11 #include "media/base/audio_converter.h"
12 #include "media/base/seekable_buffer.h"
14 namespace media {
16 class WavAudioHandler;
18 // An audio source that produces a pure sinusoidal tone.
19 class MEDIA_EXPORT SineWaveAudioSource
20 : public AudioOutputStream::AudioSourceCallback {
21 public:
22 // |channels| is the number of audio channels, |freq| is the frequency in
23 // hertz and it has to be less than half of the sampling frequency
24 // |sample_freq| or else you will get aliasing.
25 SineWaveAudioSource(int channels, double freq, double sample_freq);
26 ~SineWaveAudioSource() override;
28 // Return up to |cap| samples of data via OnMoreData(). Use Reset() to
29 // allow more data to be served.
30 void CapSamples(int cap);
31 void Reset();
33 // Implementation of AudioSourceCallback.
34 int OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) override;
35 void OnError(AudioOutputStream* stream) override;
37 // The number of OnMoreData() and OnError() calls respectively.
38 int callbacks() { return callbacks_; }
39 int errors() { return errors_; }
41 protected:
42 int channels_;
43 double f_;
44 int time_state_;
45 int cap_;
46 int callbacks_;
47 int errors_;
48 base::Lock time_lock_;
51 class FileSource : public AudioOutputStream::AudioSourceCallback,
52 public AudioConverter::InputCallback {
53 public:
54 FileSource(const AudioParameters& params,
55 const base::FilePath& path_to_wav_file);
56 ~FileSource() override;
58 // Implementation of AudioSourceCallback.
59 int OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) override;
60 void OnError(AudioOutputStream* stream) override;
62 private:
63 AudioParameters params_;
64 base::FilePath path_to_wav_file_;
65 scoped_ptr<uint8[]> wav_file_data_;
66 scoped_ptr<WavAudioHandler> wav_audio_handler_;
67 scoped_ptr<AudioConverter> file_audio_converter_;
68 int wav_file_read_pos_;
69 bool load_failed_;
71 // Provides audio data from wav_audio_handler_ into the file audio converter.
72 double ProvideInput(AudioBus* audio_bus,
73 base::TimeDelta buffer_delay) override;
75 // Loads the wav file on the first OnMoreData invocation.
76 void LoadWavFile(const base::FilePath& path_to_wav_file);
79 class BeepingSource : public AudioOutputStream::AudioSourceCallback {
80 public:
81 BeepingSource(const AudioParameters& params);
82 ~BeepingSource() override;
84 // Implementation of AudioSourceCallback.
85 int OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) override;
86 void OnError(AudioOutputStream* stream) override;
88 static void BeepOnce();
89 private:
90 int buffer_size_;
91 scoped_ptr<uint8[]> buffer_;
92 AudioParameters params_;
93 base::TimeTicks last_callback_time_;
94 base::TimeDelta interval_from_last_beep_;
95 int beep_duration_in_buffers_;
96 int beep_generated_in_buffers_;
97 int beep_period_in_frames_;
100 } // namespace media
102 #endif // MEDIA_AUDIO_SIMPLE_SOURCES_H_