Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Software adjust volume of samples, allows each audio stream its own | 5 // Software adjust volume of samples, allows each audio stream its own |
| 6 // volume without impacting master volume for chrome and other applications. | 6 // volume without impacting master volume for chrome and other applications. |
| 7 | 7 |
| 8 // Implemented as templates to allow 8, 16 and 32 bit implementations. | 8 // Implemented as templates to allow 8, 16 and 32 bit implementations. |
| 9 // 8 bit is unsigned and biased by 128. | 9 // 8 bit is unsigned and biased by 128. |
| 10 | 10 |
| 11 #include <algorithm> | 11 #include <algorithm> |
| 12 | 12 |
| 13 #include "base/atomicops.h" | 13 #include "base/atomicops.h" |
| 14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/shared_memory.h" | 16 #include "base/shared_memory.h" |
| 17 #if defined(OS_WIN) | 17 #if defined(OS_WIN) |
| 18 #include "base/win/windows_version.h" | 18 #include "base/win/windows_version.h" |
| 19 #endif | 19 #endif |
| 20 #include "media/audio/audio_util.h" | 20 #include "media/audio/audio_util.h" |
| 21 #if defined(OS_MACOSX) | 21 #if defined(OS_MACOSX) |
| 22 #include "media/audio/mac/audio_low_latency_input_mac.h" | 22 #include "media/audio/mac/audio_low_latency_input_mac.h" |
| 23 #include "media/audio/mac/audio_low_latency_output_mac.h" | 23 #include "media/audio/mac/audio_low_latency_output_mac.h" |
| 24 #endif | 24 #endif |
| 25 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
| 26 #include "media/audio/win/audio_low_latency_input_win.h" | 26 #include "media/audio/win/audio_low_latency_input_win.h" |
| 27 #include "media/audio/win/audio_low_latency_output_win.h" | |
| 27 #endif | 28 #endif |
| 28 | 29 |
| 29 using base::subtle::Atomic32; | 30 using base::subtle::Atomic32; |
| 30 | 31 |
| 31 const uint32 kUnknownDataSize = static_cast<uint32>(-1); | 32 const uint32 kUnknownDataSize = static_cast<uint32>(-1); |
| 32 | 33 |
| 33 namespace media { | 34 namespace media { |
| 34 | 35 |
| 35 // TODO(fbarchard): Convert to intrinsics for better efficiency. | 36 // TODO(fbarchard): Convert to intrinsics for better efficiency. |
| 36 template<class Fixed> | 37 template<class Fixed> |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 234 | 235 |
| 235 destination[j * channels + i] = static_cast<int16>(sample); | 236 destination[j * channels + i] = static_cast<int16>(sample); |
| 236 } | 237 } |
| 237 } | 238 } |
| 238 } | 239 } |
| 239 | 240 |
| 240 double GetAudioHardwareSampleRate() { | 241 double GetAudioHardwareSampleRate() { |
| 241 #if defined(OS_MACOSX) | 242 #if defined(OS_MACOSX) |
| 242 // Hardware sample-rate on the Mac can be configured, so we must query. | 243 // Hardware sample-rate on the Mac can be configured, so we must query. |
| 243 return AUAudioOutputStream::HardwareSampleRate(); | 244 return AUAudioOutputStream::HardwareSampleRate(); |
| 245 #elif defined(OS_WIN) | |
| 246 if (base::win::GetVersion() <= base::win::VERSION_XP) { | |
| 247 // Fall back to Windows Wave implementation on Windows XP or lower | |
| 248 // and use 48kHz as default input sample rate. | |
| 249 return 48000.0; | |
| 250 } else { | |
|
scherkus (not reviewing)
2011/11/08 23:52:21
nit: no need for else statement if you return
henrika (OOO until Aug 14)
2011/11/09 13:59:22
Done.
| |
| 251 // Hardware sample-rate on Windows can be configured, so we must query. | |
| 252 // TODO(henrika): improve possibility to specify audio endpoint. | |
| 253 // Use the default device (same as for Wave) for now to be compatible. | |
| 254 return WASAPIAudioOutputStream::HardwareSampleRate(eConsole); | |
| 255 } | |
| 244 #else | 256 #else |
| 245 // Hardware for Windows and Linux is nearly always 48KHz. | 257 // Hardware for Linux is nearly always 48KHz. |
| 246 // TODO(crogers) : return correct value in rare non-48KHz cases. | 258 // TODO(crogers) : return correct value in rare non-48KHz cases. |
| 247 return 48000.0; | 259 return 48000.0; |
| 248 #endif | 260 #endif |
| 249 } | 261 } |
| 250 | 262 |
| 251 double GetAudioInputHardwareSampleRate() { | 263 double GetAudioInputHardwareSampleRate() { |
| 252 #if defined(OS_MACOSX) | 264 #if defined(OS_MACOSX) |
| 253 // Hardware sample-rate on the Mac can be configured, so we must query. | 265 // Hardware sample-rate on the Mac can be configured, so we must query. |
| 254 return AUAudioInputStream::HardwareSampleRate(); | 266 return AUAudioInputStream::HardwareSampleRate(); |
| 255 #elif defined(OS_WIN) | 267 #elif defined(OS_WIN) |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 268 // TODO(henrika): return correct value in rare non-48KHz cases. | 280 // TODO(henrika): return correct value in rare non-48KHz cases. |
| 269 return 48000.0; | 281 return 48000.0; |
| 270 #endif | 282 #endif |
| 271 } | 283 } |
| 272 | 284 |
| 273 size_t GetAudioHardwareBufferSize() { | 285 size_t GetAudioHardwareBufferSize() { |
| 274 // The sizes here were determined by experimentation and are roughly | 286 // The sizes here were determined by experimentation and are roughly |
| 275 // the lowest value (for low latency) that still allowed glitch-free | 287 // the lowest value (for low latency) that still allowed glitch-free |
| 276 // audio under high loads. | 288 // audio under high loads. |
| 277 // | 289 // |
| 278 // For Mac OS X the chromium audio backend uses a low-latency | 290 // For Mac OS X and Windows the chromium audio backend uses a low-latency |
| 279 // CoreAudio API, so a low buffer size is possible. For other OSes, | 291 // Core Audio API, so a low buffer size is possible. For Linux, further |
| 280 // further tuning may be needed. | 292 // tuning may be needed. |
| 293 // TODO(crogers): figure out the best possible buffer sizes for WebAudio. | |
| 281 #if defined(OS_MACOSX) | 294 #if defined(OS_MACOSX) |
| 282 return 128; | 295 return 128; |
| 283 #elif defined(OS_LINUX) | 296 #elif defined(OS_WIN) |
| 284 return 2048; | 297 int mixing_sample_rate = |
| 298 static_cast<int>(WASAPIAudioOutputStream::HardwareSampleRate(eConsole)); | |
| 299 if (mixing_sample_rate == 48000 || mixing_sample_rate == 44100) | |
| 300 return 1024; | |
| 301 else if (mixing_sample_rate == 96000) | |
|
scherkus (not reviewing)
2011/11/08 23:52:21
ditto
henrika (OOO until Aug 14)
2011/11/09 13:59:22
Done.
| |
| 302 return 2048; | |
| 303 else | |
| 304 return 2048; | |
| 285 #else | 305 #else |
| 286 return 2048; | 306 return 2048; |
| 287 #endif | 307 #endif |
| 288 } | 308 } |
| 289 | 309 |
| 290 // When transferring data in the shared memory, first word is size of data | 310 // When transferring data in the shared memory, first word is size of data |
| 291 // in bytes. Actual data starts immediately after it. | 311 // in bytes. Actual data starts immediately after it. |
| 292 | 312 |
| 293 uint32 TotalSharedMemorySizeInBytes(uint32 packet_size) { | 313 uint32 TotalSharedMemorySizeInBytes(uint32 packet_size) { |
| 294 // Need to reserve extra 4 bytes for size of data. | 314 // Need to reserve extra 4 bytes for size of data. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 331 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; | 351 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; |
| 332 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); | 352 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); |
| 333 | 353 |
| 334 // Actual data size stored at the end of the buffer. | 354 // Actual data size stored at the end of the buffer. |
| 335 uint32 actual_data_size = | 355 uint32 actual_data_size = |
| 336 base::subtle::Acquire_Load(reinterpret_cast<volatile Atomic32*>(ptr)); | 356 base::subtle::Acquire_Load(reinterpret_cast<volatile Atomic32*>(ptr)); |
| 337 return actual_data_size == kUnknownDataSize; | 357 return actual_data_size == kUnknownDataSize; |
| 338 } | 358 } |
| 339 | 359 |
| 340 } // namespace media | 360 } // namespace media |
| OLD | NEW |