gecko/content/media/webaudio/AudioBuffer.h
Ehsan Akhgari 5366306b68 Bug 793294 - Implement AudioBuffer; r=bzbarsky,smaug
This is the full implementation of the AudioBuffer object.  There are
two ways to create these objects from an audio context and this patch
implements only one of them.

The construction of the AudioBuffer object is a two step process: the
object should be created with operator new first, and then
InitializeBuffers should be called on it.  InitializeBuffers is
fallible, because it uses the JS API to create the underlying typed
arrays, but that's fine, since the length of the buffers comes from web
content, and we don't want to use infallible allocations for those
anyways.

We hold on to the JS objects from the C++ implementation, and trace
through all of those objects, so that a GC does not kill those object
without us knowing.

The buffer should be possible to manipulate from both C++ and JS, and
the C++ object probably needs to support a set of methods for the C++
callers at some point.
2012-09-25 17:58:50 -04:00

84 lines
1.9 KiB
C++

/* -*- 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/. */
#pragma once
#include "nsWrapperCache.h"
#include "nsCycleCollectionParticipant.h"
#include "mozilla/Attributes.h"
#include "EnableWebAudioCheck.h"
#include "nsAutoPtr.h"
#include "nsTArray.h"
#include "AudioContext.h"
struct JSContext;
struct JSObject;
namespace mozilla {
class ErrorResult;
namespace dom {
class AudioBuffer MOZ_FINAL : public nsISupports,
public nsWrapperCache,
public EnableWebAudioCheck
{
public:
AudioBuffer(AudioContext* aContext, uint32_t aLength,
uint32_t aSampleRate);
~AudioBuffer();
// This function needs to be called in order to allocate
// all of the channels. It is fallible!
bool InitializeBuffers(uint32_t aNumberOfChannels,
JSContext* aJSContext);
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(AudioBuffer)
AudioContext* GetParentObject() const
{
return mContext;
}
virtual JSObject* WrapObject(JSContext* aCx, JSObject* aScope,
bool* aTriedToWrap);
float SampleRate() const
{
return mSampleRate;
}
uint32_t Length() const
{
return mLength;
}
float Duration() const
{
return mLength / mSampleRate;
}
uint32_t NumberOfChannels() const
{
return mChannels.Length();
}
JSObject* GetChannelData(JSContext* aJSContext, uint32_t aChannel,
ErrorResult& aRv) const;
private:
nsRefPtr<AudioContext> mContext;
FallibleTArray<JSObject*> mChannels;
uint32_t mLength;
float mSampleRate;
};
}
}