| 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 <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 | 11 |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
| 14 #include "base/time/time.h" |
| 14 #include "media/audio/audio_io.h" | 15 #include "media/audio/audio_io.h" |
| 15 #include "media/base/audio_converter.h" | 16 #include "media/base/audio_converter.h" |
| 16 #include "media/base/seekable_buffer.h" | 17 #include "media/base/seekable_buffer.h" |
| 17 | 18 |
| 18 namespace media { | 19 namespace media { |
| 19 | 20 |
| 20 class WavAudioHandler; | 21 class WavAudioHandler; |
| 21 | 22 |
| 22 // An audio source that produces a pure sinusoidal tone. | 23 // An audio source that produces a pure sinusoidal tone. |
| 23 class MEDIA_EXPORT SineWaveAudioSource | 24 class MEDIA_EXPORT SineWaveAudioSource |
| 24 : public AudioOutputStream::AudioSourceCallback { | 25 : public AudioOutputStream::AudioSourceCallback { |
| 25 public: | 26 public: |
| 26 // |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 |
| 27 // 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 |
| 28 // |sample_freq| or else you will get aliasing. | 29 // |sample_freq| or else you will get aliasing. |
| 29 SineWaveAudioSource(int channels, double freq, double sample_freq); | 30 SineWaveAudioSource(int channels, double freq, double sample_freq); |
| 30 ~SineWaveAudioSource() override; | 31 ~SineWaveAudioSource() override; |
| 31 | 32 |
| 32 // 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 |
| 33 // allow more data to be served. | 34 // allow more data to be served. |
| 34 void CapSamples(int cap); | 35 void CapSamples(int cap); |
| 35 void Reset(); | 36 void Reset(); |
| 36 | 37 |
| 37 // Implementation of AudioSourceCallback. | 38 // Implementation of AudioSourceCallback. |
| 38 int OnMoreData(AudioBus* audio_bus, | 39 int OnMoreData(base::TimeDelta delay, |
| 39 uint32_t total_bytes_delay, | 40 base::TimeTicks timestamp, |
| 40 uint32_t frames_skipped) override; | 41 int prior_frames_skipped, |
| 42 AudioBus* dest) override; |
| 41 void OnError(AudioOutputStream* stream) override; | 43 void OnError(AudioOutputStream* stream) override; |
| 42 | 44 |
| 43 // The number of OnMoreData() and OnError() calls respectively. | 45 // The number of OnMoreData() and OnError() calls respectively. |
| 44 int callbacks() { return callbacks_; } | 46 int callbacks() { return callbacks_; } |
| 45 int errors() { return errors_; } | 47 int errors() { return errors_; } |
| 46 | 48 |
| 47 protected: | 49 protected: |
| 48 int channels_; | 50 int channels_; |
| 49 double f_; | 51 double f_; |
| 50 int time_state_; | 52 int time_state_; |
| 51 int cap_; | 53 int cap_; |
| 52 int callbacks_; | 54 int callbacks_; |
| 53 int errors_; | 55 int errors_; |
| 54 base::Lock time_lock_; | 56 base::Lock time_lock_; |
| 55 }; | 57 }; |
| 56 | 58 |
| 57 class MEDIA_EXPORT FileSource : public AudioOutputStream::AudioSourceCallback, | 59 class MEDIA_EXPORT FileSource : public AudioOutputStream::AudioSourceCallback, |
| 58 public AudioConverter::InputCallback { | 60 public AudioConverter::InputCallback { |
| 59 public: | 61 public: |
| 60 FileSource(const AudioParameters& params, | 62 FileSource(const AudioParameters& params, |
| 61 const base::FilePath& path_to_wav_file, | 63 const base::FilePath& path_to_wav_file, |
| 62 bool loop); | 64 bool loop); |
| 63 ~FileSource() override; | 65 ~FileSource() override; |
| 64 | 66 |
| 65 // Implementation of AudioSourceCallback. | 67 // Implementation of AudioSourceCallback. |
| 66 int OnMoreData(AudioBus* audio_bus, | 68 int OnMoreData(base::TimeDelta delay, |
| 67 uint32_t total_bytes_delay, | 69 base::TimeTicks delay_timestamp, |
| 68 uint32_t frames_skipped) override; | 70 int prior_frames_skipped, |
| 71 AudioBus* dest) override; |
| 69 void OnError(AudioOutputStream* stream) override; | 72 void OnError(AudioOutputStream* stream) override; |
| 70 | 73 |
| 71 private: | 74 private: |
| 72 AudioParameters params_; | 75 AudioParameters params_; |
| 73 base::FilePath path_to_wav_file_; | 76 base::FilePath path_to_wav_file_; |
| 74 | 77 |
| 75 // The WAV data at |path_to_wav_file_| is read into memory and kept here. | 78 // The WAV data at |path_to_wav_file_| is read into memory and kept here. |
| 76 // This memory needs to survive for the lifetime of |wav_audio_handler_|, | 79 // This memory needs to survive for the lifetime of |wav_audio_handler_|, |
| 77 // so declare it first. Do not access this member directly. | 80 // so declare it first. Do not access this member directly. |
| 78 std::unique_ptr<char[]> raw_wav_data_; | 81 std::unique_ptr<char[]> raw_wav_data_; |
| 79 | 82 |
| 80 std::unique_ptr<WavAudioHandler> wav_audio_handler_; | 83 std::unique_ptr<WavAudioHandler> wav_audio_handler_; |
| 81 std::unique_ptr<AudioConverter> file_audio_converter_; | 84 std::unique_ptr<AudioConverter> file_audio_converter_; |
| 82 int wav_file_read_pos_; | 85 int wav_file_read_pos_; |
| 83 bool load_failed_; | 86 bool load_failed_; |
| 84 bool looping_; | 87 bool looping_; |
| 85 | 88 |
| 86 // Provides audio data from wav_audio_handler_ into the file audio converter. | 89 // Provides audio data from wav_audio_handler_ into the file audio converter. |
| 87 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; | 90 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; |
| 88 | 91 |
| 89 // Loads the wav file on the first OnMoreData invocation. | 92 // Loads the wav file on the first OnMoreData invocation. |
| 90 void LoadWavFile(const base::FilePath& path_to_wav_file); | 93 void LoadWavFile(const base::FilePath& path_to_wav_file); |
| 91 | 94 |
| 92 // Rewinds the player to the start of the loaded wav file. | 95 // Rewinds the player to the start of the loaded wav file. |
| 93 void Rewind(); | 96 void Rewind(); |
| 94 }; | 97 }; |
| 95 | 98 |
| 96 class BeepingSource : public AudioOutputStream::AudioSourceCallback { | 99 class BeepingSource : public AudioOutputStream::AudioSourceCallback { |
| 97 public: | 100 public: |
| 98 BeepingSource(const AudioParameters& params); | 101 explicit BeepingSource(const AudioParameters& params); |
| 99 ~BeepingSource() override; | 102 ~BeepingSource() override; |
| 100 | 103 |
| 101 // Implementation of AudioSourceCallback. | 104 // Implementation of AudioSourceCallback. |
| 102 int OnMoreData(AudioBus* audio_bus, | 105 int OnMoreData(base::TimeDelta delay, |
| 103 uint32_t total_bytes_delay, | 106 base::TimeTicks delay_timestamp, |
| 104 uint32_t frames_skipped) override; | 107 int prior_frames_skipped, |
| 108 AudioBus* dest) override; |
| 105 void OnError(AudioOutputStream* stream) override; | 109 void OnError(AudioOutputStream* stream) override; |
| 106 | 110 |
| 107 static void BeepOnce(); | 111 static void BeepOnce(); |
| 108 private: | 112 private: |
| 109 int buffer_size_; | 113 int buffer_size_; |
| 110 std::unique_ptr<uint8_t[]> buffer_; | 114 std::unique_ptr<uint8_t[]> buffer_; |
| 111 AudioParameters params_; | 115 AudioParameters params_; |
| 112 base::TimeTicks last_callback_time_; | 116 base::TimeTicks last_callback_time_; |
| 113 base::TimeDelta interval_from_last_beep_; | 117 base::TimeDelta interval_from_last_beep_; |
| 114 int beep_duration_in_buffers_; | 118 int beep_duration_in_buffers_; |
| 115 int beep_generated_in_buffers_; | 119 int beep_generated_in_buffers_; |
| 116 int beep_period_in_frames_; | 120 int beep_period_in_frames_; |
| 117 }; | 121 }; |
| 118 | 122 |
| 119 } // namespace media | 123 } // namespace media |
| 120 | 124 |
| 121 #endif // MEDIA_AUDIO_SIMPLE_SOURCES_H_ | 125 #endif // MEDIA_AUDIO_SIMPLE_SOURCES_H_ |
| OLD | NEW |