| 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 // MSVC++ requires this to be set before any other includes to get M_PI. | 4 // MSVC++ requires this to be set before any other includes to get M_PI. |
| 5 #define _USE_MATH_DEFINES | 5 #define _USE_MATH_DEFINES |
| 6 | 6 |
| 7 #include "media/audio/simple_sources.h" | 7 #include "media/audio/simple_sources.h" |
| 8 | 8 |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| 11 #include <algorithm> | 11 #include <algorithm> |
| 12 #include <cmath> | 12 #include <cmath> |
| 13 | 13 |
| 14 #include "base/files/file.h" | 14 #include "base/files/file.h" |
| 15 #include "base/lazy_instance.h" | |
| 16 #include "base/logging.h" | 15 #include "base/logging.h" |
| 17 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 18 #include "media/audio/sounds/wav_audio_handler.h" | 17 #include "media/audio/sounds/wav_audio_handler.h" |
| 19 #include "media/base/audio_bus.h" | 18 #include "media/base/audio_bus.h" |
| 20 | 19 |
| 21 namespace media { | 20 namespace media { |
| 22 namespace { | 21 namespace { |
| 23 // Opens |wav_filename|, reads it and loads it as a wav file. This function will | 22 // Opens |wav_filename|, reads it and loads it as a wav file. This function will |
| 24 // return a null pointer if we can't read the file or if it's malformed. The | 23 // return a null pointer if we can't read the file or if it's malformed. The |
| 25 // caller takes ownership of the returned data. The size of the data is stored | 24 // caller takes ownership of the returned data. The size of the data is stored |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 base::AutoLock auto_lock(lock_); | 86 base::AutoLock auto_lock(lock_); |
| 88 return automatic_beep_; | 87 return automatic_beep_; |
| 89 } | 88 } |
| 90 | 89 |
| 91 private: | 90 private: |
| 92 mutable base::Lock lock_; | 91 mutable base::Lock lock_; |
| 93 bool beep_once_; | 92 bool beep_once_; |
| 94 bool automatic_beep_; | 93 bool automatic_beep_; |
| 95 }; | 94 }; |
| 96 | 95 |
| 97 static base::LazyInstance<BeepContext>::Leaky g_beep_context = | 96 BeepContext* GetBeepContext() { |
| 98 LAZY_INSTANCE_INITIALIZER; | 97 static BeepContext* context = new BeepContext(); |
| 98 return context; |
| 99 } |
| 100 |
| 99 } // namespace | 101 } // namespace |
| 100 | 102 |
| 101 ////////////////////////////////////////////////////////////////////////////// | 103 ////////////////////////////////////////////////////////////////////////////// |
| 102 // SineWaveAudioSource implementation. | 104 // SineWaveAudioSource implementation. |
| 103 | 105 |
| 104 SineWaveAudioSource::SineWaveAudioSource(int channels, | 106 SineWaveAudioSource::SineWaveAudioSource(int channels, |
| 105 double freq, double sample_freq) | 107 double freq, double sample_freq) |
| 106 : channels_(channels), | 108 : channels_(channels), |
| 107 f_(freq / sample_freq), | 109 f_(freq / sample_freq), |
| 108 time_state_(0), | 110 time_state_(0), |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 | 265 |
| 264 int BeepingSource::OnMoreData(base::TimeDelta /* delay */, | 266 int BeepingSource::OnMoreData(base::TimeDelta /* delay */, |
| 265 base::TimeTicks /* delay_timestamp */, | 267 base::TimeTicks /* delay_timestamp */, |
| 266 int /* prior_frames_skipped */, | 268 int /* prior_frames_skipped */, |
| 267 AudioBus* dest) { | 269 AudioBus* dest) { |
| 268 // Accumulate the time from the last beep. | 270 // Accumulate the time from the last beep. |
| 269 interval_from_last_beep_ += base::TimeTicks::Now() - last_callback_time_; | 271 interval_from_last_beep_ += base::TimeTicks::Now() - last_callback_time_; |
| 270 | 272 |
| 271 memset(buffer_.get(), 0, buffer_size_); | 273 memset(buffer_.get(), 0, buffer_size_); |
| 272 bool should_beep = false; | 274 bool should_beep = false; |
| 273 BeepContext* beep_context = g_beep_context.Pointer(); | 275 BeepContext* beep_context = GetBeepContext(); |
| 274 if (beep_context->automatic_beep()) { | 276 if (beep_context->automatic_beep()) { |
| 275 base::TimeDelta delta = interval_from_last_beep_ - | 277 base::TimeDelta delta = interval_from_last_beep_ - |
| 276 base::TimeDelta::FromMilliseconds(kAutomaticBeepIntervalInMs); | 278 base::TimeDelta::FromMilliseconds(kAutomaticBeepIntervalInMs); |
| 277 if (delta > base::TimeDelta()) { | 279 if (delta > base::TimeDelta()) { |
| 278 should_beep = true; | 280 should_beep = true; |
| 279 interval_from_last_beep_ = delta; | 281 interval_from_last_beep_ = delta; |
| 280 } | 282 } |
| 281 } else { | 283 } else { |
| 282 should_beep = beep_context->beep_once(); | 284 should_beep = beep_context->beep_once(); |
| 283 beep_context->SetBeepOnce(false); | 285 beep_context->SetBeepOnce(false); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 310 last_callback_time_ = base::TimeTicks::Now(); | 312 last_callback_time_ = base::TimeTicks::Now(); |
| 311 dest->FromInterleaved(buffer_.get(), dest->frames(), | 313 dest->FromInterleaved(buffer_.get(), dest->frames(), |
| 312 params_.bits_per_sample() / 8); | 314 params_.bits_per_sample() / 8); |
| 313 return dest->frames(); | 315 return dest->frames(); |
| 314 } | 316 } |
| 315 | 317 |
| 316 void BeepingSource::OnError(AudioOutputStream* stream) { | 318 void BeepingSource::OnError(AudioOutputStream* stream) { |
| 317 } | 319 } |
| 318 | 320 |
| 319 void BeepingSource::BeepOnce() { | 321 void BeepingSource::BeepOnce() { |
| 320 g_beep_context.Pointer()->SetBeepOnce(true); | 322 GetBeepContext()->SetBeepOnce(true); |
| 321 } | 323 } |
| 322 | 324 |
| 323 } // namespace media | 325 } // namespace media |
| OLD | NEW |