Line data Source code
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 :
15 : namespace mozilla {
16 :
17 : LazyLogModule gWebAudioAPILog("WebAudioAPI");
18 :
19 : namespace dom {
20 :
21 0 : void WebAudioUtils::ConvertAudioTimelineEventToTicks(AudioTimelineEvent& aEvent,
22 : AudioNodeStream* aDest)
23 : {
24 0 : aEvent.SetTimeInTicks(
25 0 : aDest->SecondsToNearestStreamTime(aEvent.Time<double>()));
26 0 : aEvent.mTimeConstant *= aDest->SampleRate();
27 0 : aEvent.mDuration *= aDest->SampleRate();
28 0 : }
29 :
30 : void
31 0 : WebAudioUtils::Shutdown()
32 : {
33 0 : WebCore::HRTFDatabaseLoader::shutdown();
34 0 : }
35 :
36 : int
37 0 : WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler,
38 : uint32_t aChannel,
39 : const float* aIn, uint32_t* aInLen,
40 : float* aOut, uint32_t* aOutLen)
41 : {
42 : #ifdef MOZ_SAMPLE_TYPE_S16
43 : AutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp1;
44 : AutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp2;
45 : tmp1.SetLength(*aInLen);
46 : tmp2.SetLength(*aOutLen);
47 : ConvertAudioSamples(aIn, tmp1.Elements(), *aInLen);
48 : int result = speex_resampler_process_int(aResampler, aChannel, tmp1.Elements(), aInLen, tmp2.Elements(), aOutLen);
49 : ConvertAudioSamples(tmp2.Elements(), aOut, *aOutLen);
50 : return result;
51 : #else
52 0 : return speex_resampler_process_float(aResampler, aChannel, aIn, aInLen, aOut, aOutLen);
53 : #endif
54 : }
55 :
56 : int
57 0 : WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler,
58 : uint32_t aChannel,
59 : const int16_t* aIn, uint32_t* aInLen,
60 : float* aOut, uint32_t* aOutLen)
61 : {
62 0 : AutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp;
63 : #ifdef MOZ_SAMPLE_TYPE_S16
64 : tmp.SetLength(*aOutLen);
65 : int result = speex_resampler_process_int(aResampler, aChannel, aIn, aInLen, tmp.Elements(), aOutLen);
66 : ConvertAudioSamples(tmp.Elements(), aOut, *aOutLen);
67 : return result;
68 : #else
69 0 : tmp.SetLength(*aInLen);
70 0 : ConvertAudioSamples(aIn, tmp.Elements(), *aInLen);
71 0 : int result = speex_resampler_process_float(aResampler, aChannel, tmp.Elements(), aInLen, aOut, aOutLen);
72 0 : return result;
73 : #endif
74 : }
75 :
76 : int
77 0 : WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler,
78 : uint32_t aChannel,
79 : const int16_t* aIn, uint32_t* aInLen,
80 : int16_t* aOut, uint32_t* aOutLen)
81 : {
82 : #ifdef MOZ_SAMPLE_TYPE_S16
83 : return speex_resampler_process_int(aResampler, aChannel, aIn, aInLen, aOut, aOutLen);
84 : #else
85 0 : AutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp1;
86 0 : AutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp2;
87 0 : tmp1.SetLength(*aInLen);
88 0 : tmp2.SetLength(*aOutLen);
89 0 : ConvertAudioSamples(aIn, tmp1.Elements(), *aInLen);
90 0 : int result = speex_resampler_process_float(aResampler, aChannel, tmp1.Elements(), aInLen, tmp2.Elements(), aOutLen);
91 0 : ConvertAudioSamples(tmp2.Elements(), aOut, *aOutLen);
92 0 : return result;
93 : #endif
94 : }
95 :
96 : void
97 0 : WebAudioUtils::LogToDeveloperConsole(uint64_t aWindowID, const char* aKey)
98 : {
99 : // This implementation is derived from dom/media/VideoUtils.cpp, but we
100 : // use a windowID so that the message is delivered to the developer console.
101 : // It is similar to ContentUtils::ReportToConsole, but also works off main
102 : // thread.
103 0 : if (!NS_IsMainThread()) {
104 0 : nsCOMPtr<nsIRunnable> task = NS_NewRunnableFunction(
105 : "dom::WebAudioUtils::LogToDeveloperConsole",
106 0 : [aWindowID, aKey]() { LogToDeveloperConsole(aWindowID, aKey); });
107 0 : NS_DispatchToMainThread(task.forget(), NS_DISPATCH_NORMAL);
108 0 : return;
109 : }
110 :
111 : nsCOMPtr<nsIConsoleService> console(
112 0 : do_GetService("@mozilla.org/consoleservice;1"));
113 0 : if (!console) {
114 0 : NS_WARNING("Failed to log message to console.");
115 0 : return;
116 : }
117 :
118 0 : nsAutoCString spec;
119 : uint32_t aLineNumber, aColumnNumber;
120 0 : JSContext *cx = nsContentUtils::GetCurrentJSContext();
121 0 : if (cx) {
122 0 : nsJSUtils::GetCallingLocation(cx, spec, &aLineNumber, &aColumnNumber);
123 : }
124 :
125 : nsresult rv;
126 : nsCOMPtr<nsIScriptError> errorObject =
127 0 : do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv);
128 0 : if (!errorObject) {
129 0 : NS_WARNING("Failed to log message to console.");
130 0 : return;
131 : }
132 :
133 0 : nsXPIDLString result;
134 0 : rv = nsContentUtils::GetLocalizedString(nsContentUtils::eDOM_PROPERTIES,
135 : aKey, result);
136 :
137 0 : if (NS_FAILED(rv)) {
138 0 : NS_WARNING("Failed to log message to console.");
139 0 : return;
140 : }
141 :
142 0 : errorObject->InitWithWindowID(result,
143 0 : NS_ConvertUTF8toUTF16(spec),
144 0 : EmptyString(),
145 : aLineNumber, aColumnNumber,
146 : nsIScriptError::warningFlag, "Web Audio",
147 0 : aWindowID);
148 0 : console->LogMessage(errorObject);
149 : }
150 :
151 : } // namespace dom
152 : } // namespace mozilla
|