/src/mozilla-central/dom/media/webaudio/WebAudioUtils.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "WebAudioUtils.h" |
8 | | #include "AudioNodeStream.h" |
9 | | #include "blink/HRTFDatabaseLoader.h" |
10 | | |
11 | | #include "nsContentUtils.h" |
12 | | #include "nsIConsoleService.h" |
13 | | #include "nsIScriptError.h" |
14 | | #include "AudioEventTimeline.h" |
15 | | |
16 | | namespace mozilla { |
17 | | |
18 | | LazyLogModule gWebAudioAPILog("WebAudioAPI"); |
19 | | |
20 | | namespace dom { |
21 | | |
22 | | void WebAudioUtils::ConvertAudioTimelineEventToTicks(AudioTimelineEvent& aEvent, |
23 | | AudioNodeStream* aDest) |
24 | 0 | { |
25 | 0 | aEvent.SetTimeInTicks( |
26 | 0 | aDest->SecondsToNearestStreamTime(aEvent.Time<double>())); |
27 | 0 | aEvent.mTimeConstant *= aDest->SampleRate(); |
28 | 0 | aEvent.mDuration *= aDest->SampleRate(); |
29 | 0 | } |
30 | | |
31 | | void |
32 | | WebAudioUtils::Shutdown() |
33 | 0 | { |
34 | 0 | WebCore::HRTFDatabaseLoader::shutdown(); |
35 | 0 | } |
36 | | |
37 | | int |
38 | | WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler, |
39 | | uint32_t aChannel, |
40 | | const float* aIn, uint32_t* aInLen, |
41 | | float* aOut, uint32_t* aOutLen) |
42 | 0 | { |
43 | | #ifdef MOZ_SAMPLE_TYPE_S16 |
44 | | AutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp1; |
45 | | AutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp2; |
46 | | tmp1.SetLength(*aInLen); |
47 | | tmp2.SetLength(*aOutLen); |
48 | | ConvertAudioSamples(aIn, tmp1.Elements(), *aInLen); |
49 | | int result = speex_resampler_process_int(aResampler, aChannel, tmp1.Elements(), aInLen, tmp2.Elements(), aOutLen); |
50 | | ConvertAudioSamples(tmp2.Elements(), aOut, *aOutLen); |
51 | | return result; |
52 | | #else |
53 | 0 | return speex_resampler_process_float(aResampler, aChannel, aIn, aInLen, aOut, aOutLen); |
54 | 0 | #endif |
55 | 0 | } |
56 | | |
57 | | int |
58 | | WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler, |
59 | | uint32_t aChannel, |
60 | | const int16_t* aIn, uint32_t* aInLen, |
61 | | float* aOut, uint32_t* aOutLen) |
62 | 0 | { |
63 | 0 | AutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp; |
64 | | #ifdef MOZ_SAMPLE_TYPE_S16 |
65 | | tmp.SetLength(*aOutLen); |
66 | | int result = speex_resampler_process_int(aResampler, aChannel, aIn, aInLen, tmp.Elements(), aOutLen); |
67 | | ConvertAudioSamples(tmp.Elements(), aOut, *aOutLen); |
68 | | return result; |
69 | | #else |
70 | | tmp.SetLength(*aInLen); |
71 | 0 | ConvertAudioSamples(aIn, tmp.Elements(), *aInLen); |
72 | 0 | int result = speex_resampler_process_float(aResampler, aChannel, tmp.Elements(), aInLen, aOut, aOutLen); |
73 | 0 | return result; |
74 | 0 | #endif |
75 | 0 | } |
76 | | |
77 | | int |
78 | | WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler, |
79 | | uint32_t aChannel, |
80 | | const int16_t* aIn, uint32_t* aInLen, |
81 | | int16_t* aOut, uint32_t* aOutLen) |
82 | 0 | { |
83 | | #ifdef MOZ_SAMPLE_TYPE_S16 |
84 | | return speex_resampler_process_int(aResampler, aChannel, aIn, aInLen, aOut, aOutLen); |
85 | | #else |
86 | | AutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp1; |
87 | 0 | AutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp2; |
88 | 0 | tmp1.SetLength(*aInLen); |
89 | 0 | tmp2.SetLength(*aOutLen); |
90 | 0 | ConvertAudioSamples(aIn, tmp1.Elements(), *aInLen); |
91 | 0 | int result = speex_resampler_process_float(aResampler, aChannel, tmp1.Elements(), aInLen, tmp2.Elements(), aOutLen); |
92 | 0 | ConvertAudioSamples(tmp2.Elements(), aOut, *aOutLen); |
93 | 0 | return result; |
94 | 0 | #endif |
95 | 0 | } |
96 | | |
97 | | void |
98 | | WebAudioUtils::LogToDeveloperConsole(uint64_t aWindowID, const char* aKey) |
99 | 0 | { |
100 | 0 | // This implementation is derived from dom/media/VideoUtils.cpp, but we |
101 | 0 | // use a windowID so that the message is delivered to the developer console. |
102 | 0 | // It is similar to ContentUtils::ReportToConsole, but also works off main |
103 | 0 | // thread. |
104 | 0 | if (!NS_IsMainThread()) { |
105 | 0 | nsCOMPtr<nsIRunnable> task = NS_NewRunnableFunction( |
106 | 0 | "dom::WebAudioUtils::LogToDeveloperConsole", |
107 | 0 | [aWindowID, aKey] { LogToDeveloperConsole(aWindowID, aKey); }); |
108 | 0 | SystemGroup::Dispatch(TaskCategory::Other, task.forget()); |
109 | 0 | return; |
110 | 0 | } |
111 | 0 |
|
112 | 0 | nsCOMPtr<nsIConsoleService> console( |
113 | 0 | do_GetService("@mozilla.org/consoleservice;1")); |
114 | 0 | if (!console) { |
115 | 0 | NS_WARNING("Failed to log message to console."); |
116 | 0 | return; |
117 | 0 | } |
118 | 0 |
|
119 | 0 | nsAutoCString spec; |
120 | 0 | uint32_t aLineNumber, aColumnNumber; |
121 | 0 | JSContext *cx = nsContentUtils::GetCurrentJSContext(); |
122 | 0 | if (cx) { |
123 | 0 | nsJSUtils::GetCallingLocation(cx, spec, &aLineNumber, &aColumnNumber); |
124 | 0 | } |
125 | 0 |
|
126 | 0 | nsresult rv; |
127 | 0 | nsCOMPtr<nsIScriptError> errorObject = |
128 | 0 | do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv); |
129 | 0 | if (!errorObject) { |
130 | 0 | NS_WARNING("Failed to log message to console."); |
131 | 0 | return; |
132 | 0 | } |
133 | 0 |
|
134 | 0 | nsAutoString result; |
135 | 0 | rv = nsContentUtils::GetLocalizedString(nsContentUtils::eDOM_PROPERTIES, |
136 | 0 | aKey, result); |
137 | 0 |
|
138 | 0 | if (NS_FAILED(rv)) { |
139 | 0 | NS_WARNING("Failed to log message to console."); |
140 | 0 | return; |
141 | 0 | } |
142 | 0 |
|
143 | 0 | errorObject->InitWithWindowID(result, |
144 | 0 | NS_ConvertUTF8toUTF16(spec), |
145 | 0 | EmptyString(), |
146 | 0 | aLineNumber, aColumnNumber, |
147 | 0 | nsIScriptError::warningFlag, "Web Audio", |
148 | 0 | aWindowID); |
149 | 0 | console->LogMessage(errorObject); |
150 | 0 | } |
151 | | |
152 | | } // namespace dom |
153 | | } // namespace mozilla |