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 |
11 // TODO(vrk): This file has been running pretty wild and free, and it's likely | 11 // TODO(vrk): This file has been running pretty wild and free, and it's likely |
12 // that a lot of the functions can be simplified and made more elegant. Revisit | 12 // that a lot of the functions can be simplified and made more elegant. Revisit |
13 // after other audio cleanup is done. (crbug.com/120319) | 13 // after other audio cleanup is done. (crbug.com/120319) |
14 | 14 |
15 #include "media/audio/audio_util.h" | 15 #include "media/audio/audio_util.h" |
16 | 16 |
17 #include <algorithm> | |
18 #include <limits> | |
19 | |
20 #include "base/basictypes.h" | |
21 #include "base/command_line.h" | 17 #include "base/command_line.h" |
22 #include "base/logging.h" | |
23 #include "base/string_number_conversions.h" | 18 #include "base/string_number_conversions.h" |
24 #include "base/time.h" | 19 #include "base/time.h" |
25 #include "media/audio/audio_parameters.h" | |
26 #include "media/base/audio_bus.h" | |
27 #include "media/base/media_switches.h" | 20 #include "media/base/media_switches.h" |
28 | 21 |
29 #if defined(OS_MACOSX) | 22 #if defined(OS_WIN) |
30 #include "media/audio/mac/audio_low_latency_input_mac.h" | |
31 #include "media/audio/mac/audio_low_latency_output_mac.h" | |
32 #elif defined(OS_WIN) | |
33 #include "base/win/windows_version.h" | 23 #include "base/win/windows_version.h" |
34 #include "media/audio/audio_manager_base.h" | |
35 #include "media/audio/win/audio_low_latency_input_win.h" | |
36 #include "media/audio/win/audio_low_latency_output_win.h" | |
37 #include "media/audio/win/core_audio_util_win.h" | |
38 #include "media/base/limits.h" | |
39 #endif | 24 #endif |
40 | 25 |
41 namespace media { | 26 namespace media { |
42 | 27 |
43 // TODO(fbarchard): Convert to intrinsics for better efficiency. | |
44 template<class Fixed> | |
45 static int ScaleChannel(int channel, int volume) { | |
46 return static_cast<int>((static_cast<Fixed>(channel) * volume) >> 16); | |
47 } | |
48 | |
49 template<class Format, class Fixed, int bias> | |
50 static void AdjustVolume(Format* buf_out, | |
51 int sample_count, | |
52 int fixed_volume) { | |
53 for (int i = 0; i < sample_count; ++i) { | |
54 buf_out[i] = static_cast<Format>(ScaleChannel<Fixed>(buf_out[i] - bias, | |
55 fixed_volume) + bias); | |
56 } | |
57 } | |
58 | |
59 // AdjustVolume() does an in place audio sample change. | |
60 bool AdjustVolume(void* buf, | |
61 size_t buflen, | |
62 int channels, | |
63 int bytes_per_sample, | |
64 float volume) { | |
65 DCHECK(buf); | |
66 if (volume < 0.0f || volume > 1.0f) | |
67 return false; | |
68 if (volume == 1.0f) { | |
69 return true; | |
70 } else if (volume == 0.0f) { | |
71 memset(buf, 0, buflen); | |
72 return true; | |
73 } | |
74 if (channels > 0 && channels <= 8 && bytes_per_sample > 0) { | |
75 int sample_count = buflen / bytes_per_sample; | |
76 const int fixed_volume = static_cast<int>(volume * 65536); | |
77 if (bytes_per_sample == 1) { | |
78 AdjustVolume<uint8, int32, 128>(reinterpret_cast<uint8*>(buf), | |
79 sample_count, | |
80 fixed_volume); | |
81 return true; | |
82 } else if (bytes_per_sample == 2) { | |
83 AdjustVolume<int16, int32, 0>(reinterpret_cast<int16*>(buf), | |
84 sample_count, | |
85 fixed_volume); | |
86 return true; | |
87 } else if (bytes_per_sample == 4) { | |
88 AdjustVolume<int32, int64, 0>(reinterpret_cast<int32*>(buf), | |
89 sample_count, | |
90 fixed_volume); | |
91 return true; | |
92 } | |
93 } | |
94 return false; | |
95 } | |
96 | |
97 // Returns user buffer size as specified on the command line or 0 if no buffer | 28 // Returns user buffer size as specified on the command line or 0 if no buffer |
98 // size has been specified. | 29 // size has been specified. |
99 int GetUserBufferSize() { | 30 int GetUserBufferSize() { |
100 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | 31 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
101 int buffer_size = 0; | 32 int buffer_size = 0; |
102 std::string buffer_size_str(cmd_line->GetSwitchValueASCII( | 33 std::string buffer_size_str(cmd_line->GetSwitchValueASCII( |
103 switches::kAudioBufferSize)); | 34 switches::kAudioBufferSize)); |
104 if (base::StringToInt(buffer_size_str, &buffer_size) && buffer_size > 0) | 35 if (base::StringToInt(buffer_size_str, &buffer_size) && buffer_size > 0) |
105 return buffer_size; | 36 return buffer_size; |
106 | 37 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 // out performance was degraded compared to XP. | 90 // out performance was degraded compared to XP. |
160 // - The regression was fixed in Windows 7 and most configurations will work | 91 // - The regression was fixed in Windows 7 and most configurations will work |
161 // with 2, but some (e.g., some Sound Blasters) still need 3. | 92 // with 2, but some (e.g., some Sound Blasters) still need 3. |
162 // - Some XP configurations (even multi-processor ones) also need 3. | 93 // - Some XP configurations (even multi-processor ones) also need 3. |
163 return (base::win::GetVersion() == base::win::VERSION_VISTA) ? 4 : 3; | 94 return (base::win::GetVersion() == base::win::VERSION_VISTA) ? 4 : 3; |
164 } | 95 } |
165 | 96 |
166 #endif | 97 #endif |
167 | 98 |
168 } // namespace media | 99 } // namespace media |
OLD | NEW |