| 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 // 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 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 template<class Format, class Fixed, int bias> | 42 template<class Format, class Fixed, int bias> |
| 43 static void AdjustVolume(Format* buf_out, | 43 static void AdjustVolume(Format* buf_out, |
| 44 int sample_count, | 44 int sample_count, |
| 45 int fixed_volume) { | 45 int fixed_volume) { |
| 46 for (int i = 0; i < sample_count; ++i) { | 46 for (int i = 0; i < sample_count; ++i) { |
| 47 buf_out[i] = static_cast<Format>(ScaleChannel<Fixed>(buf_out[i] - bias, | 47 buf_out[i] = static_cast<Format>(ScaleChannel<Fixed>(buf_out[i] - bias, |
| 48 fixed_volume) + bias); | 48 fixed_volume) + bias); |
| 49 } | 49 } |
| 50 } | 50 } |
| 51 | 51 |
| 52 // Type is the datatype of a data point in the waveform (i.e. uint8, int16, | |
| 53 // int32, etc). | |
| 54 template <class Type> | |
| 55 static void DoCrossfade(int bytes_to_crossfade, int number_of_channels, | |
| 56 int bytes_per_channel, const Type* src, Type* dest) { | |
| 57 DCHECK_EQ(sizeof(Type), static_cast<size_t>(bytes_per_channel)); | |
| 58 int number_of_samples = | |
| 59 bytes_to_crossfade / (bytes_per_channel * number_of_channels); | |
| 60 | |
| 61 const Type* dest_end = dest + number_of_samples * number_of_channels; | |
| 62 const Type* src_end = src + number_of_samples * number_of_channels; | |
| 63 | |
| 64 for (int i = 0; i < number_of_samples; ++i) { | |
| 65 double crossfade_ratio = static_cast<double>(i) / number_of_samples; | |
| 66 for (int j = 0; j < number_of_channels; ++j) { | |
| 67 DCHECK_LT(dest, dest_end); | |
| 68 DCHECK_LT(src, src_end); | |
| 69 *dest = (*dest) * (1.0 - crossfade_ratio) + (*src) * crossfade_ratio; | |
| 70 ++src; | |
| 71 ++dest; | |
| 72 } | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 static const int kChannel_L = 0; | 52 static const int kChannel_L = 0; |
| 77 static const int kChannel_R = 1; | 53 static const int kChannel_R = 1; |
| 78 static const int kChannel_C = 2; | 54 static const int kChannel_C = 2; |
| 79 | 55 |
| 80 template<class Fixed, int min_value, int max_value> | 56 template<class Fixed, int min_value, int max_value> |
| 81 static int AddChannel(int val, int adder) { | 57 static int AddChannel(int val, int adder) { |
| 82 Fixed sum = static_cast<Fixed>(val) + static_cast<Fixed>(adder); | 58 Fixed sum = static_cast<Fixed>(val) + static_cast<Fixed>(adder); |
| 83 if (sum > max_value) | 59 if (sum > max_value) |
| 84 return max_value; | 60 return max_value; |
| 85 if (sum < min_value) | 61 if (sum < min_value) |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 #if defined(OS_WIN) | 382 #if defined(OS_WIN) |
| 407 | 383 |
| 408 bool IsWASAPISupported() { | 384 bool IsWASAPISupported() { |
| 409 // Note: that function correctly returns that Windows Server 2003 does not | 385 // Note: that function correctly returns that Windows Server 2003 does not |
| 410 // support WASAPI. | 386 // support WASAPI. |
| 411 return base::win::GetVersion() >= base::win::VERSION_VISTA; | 387 return base::win::GetVersion() >= base::win::VERSION_VISTA; |
| 412 } | 388 } |
| 413 | 389 |
| 414 #endif | 390 #endif |
| 415 | 391 |
| 416 void Crossfade(int bytes_to_crossfade, int number_of_channels, | |
| 417 int bytes_per_channel, const uint8* src, uint8* dest) { | |
| 418 // TODO(vrk): The type punning below is no good! | |
| 419 switch (bytes_per_channel) { | |
| 420 case 4: | |
| 421 DoCrossfade(bytes_to_crossfade, number_of_channels, bytes_per_channel, | |
| 422 reinterpret_cast<const int32*>(src), | |
| 423 reinterpret_cast<int32*>(dest)); | |
| 424 break; | |
| 425 case 2: | |
| 426 DoCrossfade(bytes_to_crossfade, number_of_channels, bytes_per_channel, | |
| 427 reinterpret_cast<const int16*>(src), | |
| 428 reinterpret_cast<int16*>(dest)); | |
| 429 break; | |
| 430 case 1: | |
| 431 DoCrossfade(bytes_to_crossfade, number_of_channels, bytes_per_channel, | |
| 432 src, dest); | |
| 433 break; | |
| 434 default: | |
| 435 NOTREACHED() << "Unsupported audio bit depth in crossfade."; | |
| 436 } | |
| 437 } | |
| 438 | |
| 439 } // namespace media | 392 } // namespace media |
| OLD | NEW |