2012-08-31 13:59:37 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "AudioContext.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "mozilla/dom/AudioContextBinding.h"
|
2012-09-18 16:07:33 -07:00
|
|
|
#include "AudioDestinationNode.h"
|
|
|
|
#include "AudioBufferSourceNode.h"
|
2012-09-21 15:42:14 -07:00
|
|
|
#include "AudioBuffer.h"
|
2012-10-31 12:09:32 -07:00
|
|
|
#include "GainNode.h"
|
2012-10-31 17:26:03 -07:00
|
|
|
#include "DelayNode.h"
|
2012-11-05 18:14:13 -08:00
|
|
|
#include "PannerNode.h"
|
2012-11-05 16:26:03 -08:00
|
|
|
#include "AudioListener.h"
|
2012-11-06 17:01:11 -08:00
|
|
|
#include "DynamicsCompressorNode.h"
|
2012-11-07 17:59:14 -08:00
|
|
|
#include "BiquadFilterNode.h"
|
2012-08-31 13:59:37 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
2012-09-07 15:13:26 -07:00
|
|
|
namespace dom {
|
2012-08-31 13:59:37 -07:00
|
|
|
|
2012-11-27 15:08:22 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_3(AudioContext,
|
|
|
|
mWindow, mDestination, mListener)
|
|
|
|
|
2012-10-09 11:50:26 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(AudioContext, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(AudioContext, Release)
|
2012-08-31 13:59:37 -07:00
|
|
|
|
|
|
|
AudioContext::AudioContext(nsIDOMWindow* aWindow)
|
|
|
|
: mWindow(aWindow)
|
2012-09-18 16:07:33 -07:00
|
|
|
, mDestination(new AudioDestinationNode(this))
|
2012-11-29 19:31:39 -08:00
|
|
|
, mSampleRate(44100) // hard-code for now
|
2012-08-31 13:59:37 -07:00
|
|
|
{
|
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioContext::~AudioContext()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
AudioContext::WrapObject(JSContext* aCx, JSObject* aScope,
|
|
|
|
bool* aTriedToWrap)
|
|
|
|
{
|
2013-01-22 17:12:21 -08:00
|
|
|
return AudioContextBinding::Wrap(aCx, aScope, this, aTriedToWrap);
|
2012-08-31 13:59:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<AudioContext>
|
2012-12-03 08:07:49 -08:00
|
|
|
AudioContext::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
|
2012-08-31 13:59:37 -07:00
|
|
|
{
|
2012-12-03 08:07:49 -08:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.Get());
|
2012-08-31 13:59:37 -07:00
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioContext* object = new AudioContext(window);
|
|
|
|
NS_ADDREF(object);
|
2012-11-23 15:33:41 -08:00
|
|
|
window->AddAudioContext(object);
|
2012-08-31 13:59:37 -07:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2012-09-18 16:07:33 -07:00
|
|
|
already_AddRefed<AudioBufferSourceNode>
|
|
|
|
AudioContext::CreateBufferSource()
|
|
|
|
{
|
|
|
|
nsRefPtr<AudioBufferSourceNode> bufferNode =
|
|
|
|
new AudioBufferSourceNode(this);
|
|
|
|
return bufferNode.forget();
|
|
|
|
}
|
|
|
|
|
2012-09-21 15:42:14 -07:00
|
|
|
already_AddRefed<AudioBuffer>
|
|
|
|
AudioContext::CreateBuffer(JSContext* aJSContext, uint32_t aNumberOfChannels,
|
|
|
|
uint32_t aLength, float aSampleRate,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsRefPtr<AudioBuffer> buffer = new AudioBuffer(this, aLength, aSampleRate);
|
|
|
|
if (!buffer->InitializeBuffers(aNumberOfChannels, aJSContext)) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return buffer.forget();
|
|
|
|
}
|
|
|
|
|
2012-10-31 12:09:32 -07:00
|
|
|
already_AddRefed<GainNode>
|
|
|
|
AudioContext::CreateGain()
|
|
|
|
{
|
|
|
|
nsRefPtr<GainNode> gainNode = new GainNode(this);
|
|
|
|
return gainNode.forget();
|
|
|
|
}
|
|
|
|
|
2012-10-31 17:26:03 -07:00
|
|
|
already_AddRefed<DelayNode>
|
2012-11-19 12:52:29 -08:00
|
|
|
AudioContext::CreateDelay(double aMaxDelayTime, ErrorResult& aRv)
|
2012-10-31 17:26:03 -07:00
|
|
|
{
|
2012-11-19 12:52:29 -08:00
|
|
|
if (aMaxDelayTime > 0. && aMaxDelayTime < 3.) {
|
2012-11-15 17:48:04 -08:00
|
|
|
nsRefPtr<DelayNode> delayNode = new DelayNode(this, aMaxDelayTime);
|
|
|
|
return delayNode.forget();
|
2012-11-08 10:17:22 -08:00
|
|
|
}
|
2012-11-15 17:48:04 -08:00
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return nullptr;
|
2012-10-31 17:26:03 -07:00
|
|
|
}
|
|
|
|
|
2012-11-05 18:14:13 -08:00
|
|
|
already_AddRefed<PannerNode>
|
|
|
|
AudioContext::CreatePanner()
|
|
|
|
{
|
|
|
|
nsRefPtr<PannerNode> pannerNode = new PannerNode(this);
|
|
|
|
return pannerNode.forget();
|
|
|
|
}
|
|
|
|
|
2012-11-06 17:01:11 -08:00
|
|
|
already_AddRefed<DynamicsCompressorNode>
|
|
|
|
AudioContext::CreateDynamicsCompressor()
|
|
|
|
{
|
|
|
|
nsRefPtr<DynamicsCompressorNode> compressorNode =
|
|
|
|
new DynamicsCompressorNode(this);
|
|
|
|
return compressorNode.forget();
|
|
|
|
}
|
|
|
|
|
2012-11-07 17:59:14 -08:00
|
|
|
already_AddRefed<BiquadFilterNode>
|
|
|
|
AudioContext::CreateBiquadFilter()
|
|
|
|
{
|
|
|
|
nsRefPtr<BiquadFilterNode> filterNode =
|
|
|
|
new BiquadFilterNode(this);
|
|
|
|
return filterNode.forget();
|
|
|
|
}
|
|
|
|
|
2012-11-05 16:26:03 -08:00
|
|
|
AudioListener*
|
|
|
|
AudioContext::Listener()
|
|
|
|
{
|
|
|
|
if (!mListener) {
|
|
|
|
mListener = new AudioListener(this);
|
|
|
|
}
|
|
|
|
return mListener;
|
|
|
|
}
|
|
|
|
|
2012-08-31 13:59:37 -07:00
|
|
|
}
|
2012-09-07 15:13:26 -07:00
|
|
|
}
|
2012-08-31 13:59:37 -07:00
|
|
|
|