tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

WebAudioUtils.cpp (3536B)


      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 
      9 #include "blink/HRTFDatabaseLoader.h"
     10 #include "mozilla/SchedulerGroup.h"
     11 #include "nsComponentManagerUtils.h"
     12 #include "nsContentUtils.h"
     13 #include "nsIConsoleService.h"
     14 #include "nsIScriptError.h"
     15 #include "nsJSUtils.h"
     16 #include "nsServiceManagerUtils.h"
     17 
     18 namespace mozilla {
     19 
     20 LazyLogModule gWebAudioAPILog("WebAudioAPI");
     21 
     22 namespace dom {
     23 
     24 void WebAudioUtils::Shutdown() { WebCore::HRTFDatabaseLoader::shutdown(); }
     25 
     26 int WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler,
     27                                         uint32_t aChannel, const float* aIn,
     28                                         uint32_t* aInLen, float* aOut,
     29                                         uint32_t* aOutLen) {
     30  return speex_resampler_process_float(aResampler, aChannel, aIn, aInLen, aOut,
     31                                       aOutLen);
     32 }
     33 
     34 int WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler,
     35                                         uint32_t aChannel, const int16_t* aIn,
     36                                         uint32_t* aInLen, float* aOut,
     37                                         uint32_t* aOutLen) {
     38  AutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE * 4> tmp;
     39  tmp.SetLength(*aInLen);
     40  ConvertAudioSamples(aIn, tmp.Elements(), *aInLen);
     41  int result = speex_resampler_process_float(
     42      aResampler, aChannel, tmp.Elements(), aInLen, aOut, aOutLen);
     43  return result;
     44 }
     45 
     46 int WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler,
     47                                         uint32_t aChannel, const int16_t* aIn,
     48                                         uint32_t* aInLen, int16_t* aOut,
     49                                         uint32_t* aOutLen) {
     50  AutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE * 4> tmp1;
     51  AutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE * 4> tmp2;
     52  tmp1.SetLength(*aInLen);
     53  tmp2.SetLength(*aOutLen);
     54  ConvertAudioSamples(aIn, tmp1.Elements(), *aInLen);
     55  int result = speex_resampler_process_float(
     56      aResampler, aChannel, tmp1.Elements(), aInLen, tmp2.Elements(), aOutLen);
     57  ConvertAudioSamples(tmp2.Elements(), aOut, *aOutLen);
     58  return result;
     59 }
     60 
     61 void WebAudioUtils::LogToDeveloperConsole(uint64_t aWindowID,
     62                                          const char* aKey) {
     63  // This implementation is derived from dom/media/VideoUtils.cpp, but we
     64  // use a windowID so that the message is delivered to the developer console.
     65  // It is similar to ContentUtils::ReportToConsole, but also works off main
     66  // thread.
     67  if (!NS_IsMainThread()) {
     68    nsCOMPtr<nsIRunnable> task = NS_NewRunnableFunction(
     69        "dom::WebAudioUtils::LogToDeveloperConsole",
     70        [aWindowID, aKey] { LogToDeveloperConsole(aWindowID, aKey); });
     71    SchedulerGroup::Dispatch(task.forget());
     72    return;
     73  }
     74 
     75  nsAutoString result;
     76  nsresult rv = nsContentUtils::GetLocalizedString(
     77      nsContentUtils::eDOM_PROPERTIES, aKey, result);
     78 
     79  if (NS_FAILED(rv)) {
     80    NS_WARNING("Failed to log message to console.");
     81    return;
     82  }
     83 
     84  nsContentUtils::ReportToConsoleByWindowID(result, nsIScriptError::warningFlag,
     85                                            "Web Audio"_ns, aWindowID);
     86 }
     87 
     88 }  // namespace dom
     89 }  // namespace mozilla