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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 template<class Format, class Fixed, int bias> | 44 template<class Format, class Fixed, int bias> |
45 static void AdjustVolume(Format* buf_out, | 45 static void AdjustVolume(Format* buf_out, |
46 int sample_count, | 46 int sample_count, |
47 int fixed_volume) { | 47 int fixed_volume) { |
48 for (int i = 0; i < sample_count; ++i) { | 48 for (int i = 0; i < sample_count; ++i) { |
49 buf_out[i] = static_cast<Format>(ScaleChannel<Fixed>(buf_out[i] - bias, | 49 buf_out[i] = static_cast<Format>(ScaleChannel<Fixed>(buf_out[i] - bias, |
50 fixed_volume) + bias); | 50 fixed_volume) + bias); |
51 } | 51 } |
52 } | 52 } |
53 | 53 |
54 // Type is the datatype of a data point in the waveform (i.e. uint8, int16, | |
55 // int32, etc). | |
56 template <class Type> | |
57 static void DoCrossfade(int bytes_to_crossfade, int number_of_channels, | |
58 int bytes_per_channel, const Type* src, Type* dest) { | |
59 DCHECK_EQ(sizeof(Type), static_cast<size_t>(bytes_per_channel)); | |
60 int number_of_samples = | |
61 bytes_to_crossfade / (bytes_per_channel * number_of_channels); | |
62 | |
63 const Type* dest_end = dest + number_of_samples * number_of_channels; | |
64 const Type* src_end = src + number_of_samples * number_of_channels; | |
65 | |
66 for (int i = 0; i < number_of_samples; ++i) { | |
67 double crossfade_ratio = static_cast<double>(i) / number_of_samples; | |
68 for (int j = 0; j < number_of_channels; ++j) { | |
69 DCHECK_LT(dest, dest_end); | |
70 DCHECK_LT(src, src_end); | |
71 *dest = (*dest) * (1.0 - crossfade_ratio) + (*src) * crossfade_ratio; | |
72 ++src; | |
73 ++dest; | |
74 } | |
75 } | |
76 } | |
77 | |
78 static const int kChannel_L = 0; | 54 static const int kChannel_L = 0; |
79 static const int kChannel_R = 1; | 55 static const int kChannel_R = 1; |
80 static const int kChannel_C = 2; | 56 static const int kChannel_C = 2; |
81 | 57 |
82 template<class Fixed, int min_value, int max_value> | 58 template<class Fixed, int min_value, int max_value> |
83 static int AddChannel(int val, int adder) { | 59 static int AddChannel(int val, int adder) { |
84 Fixed sum = static_cast<Fixed>(val) + static_cast<Fixed>(adder); | 60 Fixed sum = static_cast<Fixed>(val) + static_cast<Fixed>(adder); |
85 if (sum > max_value) | 61 if (sum > max_value) |
86 return max_value; | 62 return max_value; |
87 if (sum < min_value) | 63 if (sum < min_value) |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 #if defined(OS_WIN) | 384 #if defined(OS_WIN) |
409 | 385 |
410 bool IsWASAPISupported() { | 386 bool IsWASAPISupported() { |
411 // Note: that function correctly returns that Windows Server 2003 does not | 387 // Note: that function correctly returns that Windows Server 2003 does not |
412 // support WASAPI. | 388 // support WASAPI. |
413 return base::win::GetVersion() >= base::win::VERSION_VISTA; | 389 return base::win::GetVersion() >= base::win::VERSION_VISTA; |
414 } | 390 } |
415 | 391 |
416 #endif | 392 #endif |
417 | 393 |
418 void Crossfade(int bytes_to_crossfade, int number_of_channels, | |
419 int bytes_per_channel, const uint8* src, uint8* dest) { | |
420 // TODO(vrk): The type punning below is no good! | |
421 switch (bytes_per_channel) { | |
422 case 4: | |
423 DoCrossfade(bytes_to_crossfade, number_of_channels, bytes_per_channel, | |
424 reinterpret_cast<const int32*>(src), | |
425 reinterpret_cast<int32*>(dest)); | |
426 break; | |
427 case 2: | |
428 DoCrossfade(bytes_to_crossfade, number_of_channels, bytes_per_channel, | |
429 reinterpret_cast<const int16*>(src), | |
430 reinterpret_cast<int16*>(dest)); | |
431 break; | |
432 case 1: | |
433 DoCrossfade(bytes_to_crossfade, number_of_channels, bytes_per_channel, | |
434 src, dest); | |
435 break; | |
436 default: | |
437 NOTREACHED() << "Unsupported audio bit depth in crossfade."; | |
438 } | |
439 } | |
440 | |
441 // The minimum number of samples in a hardware packet. | |
442 // This value is selected so that we can handle down to 5khz sample rate. | |
443 static const int kMinSamplesPerHardwarePacket = 1024; | |
444 | |
445 // The maximum number of samples in a hardware packet. | |
446 // This value is selected so that we can handle up to 192khz sample rate. | |
447 static const int kMaxSamplesPerHardwarePacket = 64 * 1024; | |
448 | |
449 // This constant governs the hardware audio buffer size, this value should be | |
450 // chosen carefully. | |
451 // This value is selected so that we have 8192 samples for 48khz streams. | |
452 static const int kMillisecondsPerHardwarePacket = 170; | |
453 | |
454 uint32 SelectSamplesPerPacket(int sample_rate) { | |
455 // Select the number of samples that can provide at least | |
456 // |kMillisecondsPerHardwarePacket| worth of audio data. | |
457 int samples = kMinSamplesPerHardwarePacket; | |
458 while (samples <= kMaxSamplesPerHardwarePacket && | |
459 samples * base::Time::kMillisecondsPerSecond < | |
460 sample_rate * kMillisecondsPerHardwarePacket) { | |
461 samples *= 2; | |
462 } | |
463 return samples; | |
464 } | |
465 | |
466 } // namespace media | 394 } // namespace media |
OLD | NEW |