| 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 |
| 18 class MEDIA_EXPORT SimpleSource |
| 19 : public AudioOutputStream::AudioSourceCallback { |
| 20 public: |
| 21 ~SimpleSource() override {}; |
| 22 }; |
| 23 |
| 14 // An audio source that produces a pure sinusoidal tone. | 24 // An audio source that produces a pure sinusoidal tone. |
| 15 class MEDIA_EXPORT SineWaveAudioSource | 25 class MEDIA_EXPORT SineWaveAudioSource : public SimpleSource { |
| 16 : public AudioOutputStream::AudioSourceCallback { | |
| 17 public: | 26 public: |
| 18 // |channels| is the number of audio channels, |freq| is the frequency in | 27 // |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 | 28 // hertz and it has to be less than half of the sampling frequency |
| 20 // |sample_freq| or else you will get aliasing. | 29 // |sample_freq| or else you will get aliasing. |
| 21 SineWaveAudioSource(int channels, double freq, double sample_freq); | 30 SineWaveAudioSource(int channels, double freq, double sample_freq); |
| 22 ~SineWaveAudioSource() override {} | 31 ~SineWaveAudioSource() override; |
| 23 | 32 |
| 24 // Return up to |cap| samples of data via OnMoreData(). Use Reset() to | 33 // Return up to |cap| samples of data via OnMoreData(). Use Reset() to |
| 25 // allow more data to be served. | 34 // allow more data to be served. |
| 26 void CapSamples(int cap); | 35 void CapSamples(int cap); |
| 27 void Reset(); | 36 void Reset(); |
| 28 | 37 |
| 29 // Implementation of AudioSourceCallback. | 38 // Implementation of AudioSourceCallback. |
| 30 int OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) override; | 39 int OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) override; |
| 31 void OnError(AudioOutputStream* stream) override; | 40 void OnError(AudioOutputStream* stream) override; |
| 32 | 41 |
| 33 // The number of OnMoreData() and OnError() calls respectively. | 42 // The number of OnMoreData() and OnError() calls respectively. |
| 34 int callbacks() { return callbacks_; } | 43 int callbacks() { return callbacks_; } |
| 35 int errors() { return errors_; } | 44 int errors() { return errors_; } |
| 36 | 45 |
| 37 protected: | 46 protected: |
| 38 int channels_; | 47 int channels_; |
| 39 double f_; | 48 double f_; |
| 40 int time_state_; | 49 int time_state_; |
| 41 int cap_; | 50 int cap_; |
| 42 int callbacks_; | 51 int callbacks_; |
| 43 int errors_; | 52 int errors_; |
| 44 base::Lock time_lock_; | 53 base::Lock time_lock_; |
| 45 }; | 54 }; |
| 46 | 55 |
| 56 class FileSource : public SimpleSource, |
| 57 public AudioConverter::InputCallback { |
| 58 public: |
| 59 FileSource(const AudioParameters& params, |
| 60 const base::FilePath& path_to_wav_file); |
| 61 ~FileSource() override; |
| 62 |
| 63 // Implementation of AudioSourceCallback. |
| 64 int OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) override; |
| 65 void OnError(AudioOutputStream* stream) override; |
| 66 |
| 67 private: |
| 68 AudioParameters params_; |
| 69 base::FilePath path_to_wav_file_; |
| 70 scoped_ptr<uint8[]> wav_file_data_; |
| 71 scoped_ptr<WavAudioHandler> wav_audio_handler_; |
| 72 scoped_ptr<AudioConverter> file_audio_converter_; |
| 73 int wav_file_read_pos_; |
| 74 |
| 75 // Provides audio data from wav_audio_handler_ into the file audio converter. |
| 76 double ProvideInput(AudioBus* audio_bus, |
| 77 base::TimeDelta buffer_delay) override; |
| 78 |
| 79 // Loads the wav file on the first OnMoreData invocation. |
| 80 void LoadWavFile(const base::FilePath& path_to_wav_file); |
| 81 }; |
| 82 |
| 83 class BeepingSource : public SimpleSource { |
| 84 public: |
| 85 BeepingSource(const AudioParameters& params); |
| 86 ~BeepingSource() override; |
| 87 |
| 88 // Implementation of AudioSourceCallback. |
| 89 int OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) override; |
| 90 void OnError(AudioOutputStream* stream) override; |
| 91 |
| 92 static void BeepOnce(); |
| 93 private: |
| 94 scoped_ptr<uint8[]> buffer_; |
| 95 int buffer_size_; |
| 96 AudioParameters params_; |
| 97 base::TimeTicks last_callback_time_; |
| 98 base::TimeDelta interval_from_last_beep_; |
| 99 int beep_duration_in_buffers_; |
| 100 int beep_generated_in_buffers_; |
| 101 int beep_period_in_frames_; |
| 102 }; |
| 103 |
| 47 } // namespace media | 104 } // namespace media |
| 48 | 105 |
| 49 #endif // MEDIA_AUDIO_SIMPLE_SOURCES_H_ | 106 #endif // MEDIA_AUDIO_SIMPLE_SOURCES_H_ |
| OLD | NEW |