OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_AUDIO_SIMPLE_SOURCES_H_ | 5 #ifndef MEDIA_AUDIO_SIMPLE_SOURCES_H_ |
6 #define MEDIA_AUDIO_SIMPLE_SOURCES_H_ | 6 #define MEDIA_AUDIO_SIMPLE_SOURCES_H_ |
7 | 7 |
| 8 #include "base/files/file_path.h" |
8 #include "base/synchronization/lock.h" | 9 #include "base/synchronization/lock.h" |
9 #include "media/audio/audio_io.h" | 10 #include "media/audio/audio_io.h" |
| 11 #include "media/base/audio_converter.h" |
10 #include "media/base/seekable_buffer.h" | 12 #include "media/base/seekable_buffer.h" |
11 | 13 |
12 namespace media { | 14 namespace media { |
13 | 15 |
| 16 class WavAudioHandler; |
| 17 |
14 // An audio source that produces a pure sinusoidal tone. | 18 // An audio source that produces a pure sinusoidal tone. |
15 class MEDIA_EXPORT SineWaveAudioSource | 19 class MEDIA_EXPORT SineWaveAudioSource |
16 : public AudioOutputStream::AudioSourceCallback { | 20 : public AudioOutputStream::AudioSourceCallback { |
17 public: | 21 public: |
18 // |channels| is the number of audio channels, |freq| is the frequency in | 22 // |channels| is the number of audio channels, |freq| is the frequency in |
19 // hertz and it has to be less than half of the sampling frequency | 23 // hertz and it has to be less than half of the sampling frequency |
20 // |sample_freq| or else you will get aliasing. | 24 // |sample_freq| or else you will get aliasing. |
21 SineWaveAudioSource(int channels, double freq, double sample_freq); | 25 SineWaveAudioSource(int channels, double freq, double sample_freq); |
22 ~SineWaveAudioSource() override {} | 26 ~SineWaveAudioSource() override; |
23 | 27 |
24 // Return up to |cap| samples of data via OnMoreData(). Use Reset() to | 28 // Return up to |cap| samples of data via OnMoreData(). Use Reset() to |
25 // allow more data to be served. | 29 // allow more data to be served. |
26 void CapSamples(int cap); | 30 void CapSamples(int cap); |
27 void Reset(); | 31 void Reset(); |
28 | 32 |
29 // Implementation of AudioSourceCallback. | 33 // Implementation of AudioSourceCallback. |
30 int OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) override; | 34 int OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) override; |
31 void OnError(AudioOutputStream* stream) override; | 35 void OnError(AudioOutputStream* stream) override; |
32 | 36 |
33 // The number of OnMoreData() and OnError() calls respectively. | 37 // The number of OnMoreData() and OnError() calls respectively. |
34 int callbacks() { return callbacks_; } | 38 int callbacks() { return callbacks_; } |
35 int errors() { return errors_; } | 39 int errors() { return errors_; } |
36 | 40 |
37 protected: | 41 protected: |
38 int channels_; | 42 int channels_; |
39 double f_; | 43 double f_; |
40 int time_state_; | 44 int time_state_; |
41 int cap_; | 45 int cap_; |
42 int callbacks_; | 46 int callbacks_; |
43 int errors_; | 47 int errors_; |
44 base::Lock time_lock_; | 48 base::Lock time_lock_; |
45 }; | 49 }; |
46 | 50 |
| 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; |
| 57 |
| 58 // Implementation of AudioSourceCallback. |
| 59 int OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) override; |
| 60 void OnError(AudioOutputStream* stream) override; |
| 61 |
| 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 |
| 70 // Provides audio data from wav_audio_handler_ into the file audio converter. |
| 71 double ProvideInput(AudioBus* audio_bus, |
| 72 base::TimeDelta buffer_delay) override; |
| 73 |
| 74 // Loads the wav file on the first OnMoreData invocation. |
| 75 void LoadWavFile(const base::FilePath& path_to_wav_file); |
| 76 }; |
| 77 |
| 78 class BeepingSource : public AudioOutputStream::AudioSourceCallback { |
| 79 public: |
| 80 BeepingSource(const AudioParameters& params); |
| 81 ~BeepingSource() override; |
| 82 |
| 83 // Implementation of AudioSourceCallback. |
| 84 int OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) override; |
| 85 void OnError(AudioOutputStream* stream) override; |
| 86 |
| 87 static void BeepOnce(); |
| 88 private: |
| 89 int buffer_size_; |
| 90 scoped_ptr<uint8[]> buffer_; |
| 91 AudioParameters params_; |
| 92 base::TimeTicks last_callback_time_; |
| 93 base::TimeDelta interval_from_last_beep_; |
| 94 int beep_duration_in_buffers_; |
| 95 int beep_generated_in_buffers_; |
| 96 int beep_period_in_frames_; |
| 97 }; |
| 98 |
47 } // namespace media | 99 } // namespace media |
48 | 100 |
49 #endif // MEDIA_AUDIO_SIMPLE_SOURCES_H_ | 101 #endif // MEDIA_AUDIO_SIMPLE_SOURCES_H_ |
OLD | NEW |