2012-09-21 22:42:14 +00: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/. */
|
|
|
|
|
2012-10-30 21:39:38 +00:00
|
|
|
#ifndef AudioBuffer_h_
|
|
|
|
#define AudioBuffer_h_
|
2012-09-21 22:42:14 +00:00
|
|
|
|
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "AudioContext.h"
|
2013-08-28 02:59:14 +00:00
|
|
|
#include "js/TypeDecls.h"
|
2014-01-04 18:15:41 +00:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2012-09-21 22:42:14 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class ErrorResult;
|
2013-08-15 19:44:14 +00:00
|
|
|
class ThreadSharedFloatArrayBufferList;
|
2012-09-21 22:42:14 +00:00
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
2013-08-15 19:44:14 +00:00
|
|
|
class AudioContext;
|
|
|
|
|
2013-02-04 23:07:25 +00:00
|
|
|
/**
|
|
|
|
* An AudioBuffer keeps its data either in the mJSChannels objects, which
|
|
|
|
* are Float32Arrays, or in mSharedChannels if the mJSChannels objects have
|
|
|
|
* been neutered.
|
|
|
|
*/
|
2015-03-21 16:28:04 +00:00
|
|
|
class AudioBuffer final : public nsWrapperCache
|
2012-09-21 22:42:14 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-08-28 06:30:49 +00:00
|
|
|
// If non-null, aInitialContents must have number of channels equal to
|
|
|
|
// aNumberOfChannels and their lengths must be at least aLength.
|
2014-05-15 21:23:27 +00:00
|
|
|
static already_AddRefed<AudioBuffer>
|
|
|
|
Create(AudioContext* aContext, uint32_t aNumberOfChannels,
|
|
|
|
uint32_t aLength, float aSampleRate,
|
2015-08-28 06:30:49 +00:00
|
|
|
already_AddRefed<ThreadSharedFloatArrayBufferList> aInitialContents,
|
2014-05-15 21:23:27 +00:00
|
|
|
JSContext* aJSContext, ErrorResult& aRv);
|
2012-09-21 22:42:14 +00:00
|
|
|
|
2015-08-28 06:30:49 +00:00
|
|
|
static already_AddRefed<AudioBuffer>
|
|
|
|
Create(AudioContext* aContext, uint32_t aNumberOfChannels,
|
|
|
|
uint32_t aLength, float aSampleRate,
|
|
|
|
JSContext* aJSContext, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return Create(aContext, aNumberOfChannels, aLength, aSampleRate,
|
|
|
|
nullptr, aJSContext, aRv);
|
|
|
|
}
|
|
|
|
|
2014-01-04 18:15:41 +00:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
|
|
|
|
2013-07-05 20:21:52 +00:00
|
|
|
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(AudioBuffer)
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(AudioBuffer)
|
2012-09-21 22:42:14 +00:00
|
|
|
|
2015-08-17 23:48:38 +00:00
|
|
|
nsPIDOMWindow* GetParentObject() const
|
2012-09-21 22:42:14 +00:00
|
|
|
{
|
2015-08-17 23:48:38 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindow> parentObject = do_QueryReferent(mOwnerWindow);
|
|
|
|
return parentObject;
|
2012-09-21 22:42:14 +00:00
|
|
|
}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
2012-09-21 22:42:14 +00:00
|
|
|
|
|
|
|
float SampleRate() const
|
|
|
|
{
|
|
|
|
return mSampleRate;
|
|
|
|
}
|
|
|
|
|
2013-02-04 23:07:25 +00:00
|
|
|
int32_t Length() const
|
2012-09-21 22:42:14 +00:00
|
|
|
{
|
|
|
|
return mLength;
|
|
|
|
}
|
|
|
|
|
2012-11-19 20:52:29 +00:00
|
|
|
double Duration() const
|
2012-09-21 22:42:14 +00:00
|
|
|
{
|
2012-11-19 20:52:29 +00:00
|
|
|
return mLength / static_cast<double> (mSampleRate);
|
2012-09-21 22:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t NumberOfChannels() const
|
|
|
|
{
|
2013-02-04 23:07:25 +00:00
|
|
|
return mJSChannels.Length();
|
2012-09-21 22:42:14 +00:00
|
|
|
}
|
|
|
|
|
2013-02-04 23:07:25 +00:00
|
|
|
/**
|
|
|
|
* If mSharedChannels is non-null, copies its contents to
|
|
|
|
* new Float32Arrays in mJSChannels. Returns a Float32Array.
|
|
|
|
*/
|
2014-06-11 20:26:52 +00:00
|
|
|
void GetChannelData(JSContext* aJSContext, uint32_t aChannel,
|
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
|
|
|
ErrorResult& aRv);
|
2013-02-04 23:07:25 +00:00
|
|
|
|
2013-09-17 11:30:32 +00:00
|
|
|
void CopyFromChannel(const Float32Array& aDestination, uint32_t aChannelNumber,
|
|
|
|
uint32_t aStartInChannel, ErrorResult& aRv);
|
|
|
|
void CopyToChannel(JSContext* aJSContext, const Float32Array& aSource,
|
|
|
|
uint32_t aChannelNumber, uint32_t aStartInChannel,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2013-02-04 23:07:25 +00:00
|
|
|
/**
|
2013-04-22 12:12:45 +00:00
|
|
|
* Returns a ThreadSharedFloatArrayBufferList containing the sample data.
|
2013-06-25 16:01:07 +00:00
|
|
|
* Can return null if there is no data.
|
2013-02-04 23:07:25 +00:00
|
|
|
*/
|
2013-04-22 12:12:45 +00:00
|
|
|
ThreadSharedFloatArrayBufferList* GetThreadSharedChannelsForRate(JSContext* aContext);
|
2013-02-04 23:07:25 +00:00
|
|
|
|
|
|
|
protected:
|
2014-05-15 21:23:27 +00:00
|
|
|
AudioBuffer(AudioContext* aContext, uint32_t aNumberOfChannels,
|
2015-08-28 06:30:49 +00:00
|
|
|
uint32_t aLength, float aSampleRate,
|
|
|
|
already_AddRefed<ThreadSharedFloatArrayBufferList>
|
|
|
|
aInitialContents);
|
2014-05-15 21:23:27 +00:00
|
|
|
~AudioBuffer();
|
|
|
|
|
2013-05-04 21:40:20 +00:00
|
|
|
bool RestoreJSChannelData(JSContext* aJSContext);
|
2015-08-24 20:51:55 +00:00
|
|
|
|
|
|
|
already_AddRefed<ThreadSharedFloatArrayBufferList>
|
|
|
|
StealJSArrayDataIntoSharedChannels(JSContext* aJSContext);
|
|
|
|
|
2013-01-25 21:21:22 +00:00
|
|
|
void ClearJSChannels();
|
|
|
|
|
2015-08-17 23:48:38 +00:00
|
|
|
nsWeakPtr mOwnerWindow;
|
2013-02-04 23:07:25 +00:00
|
|
|
// Float32Arrays
|
2014-05-15 21:23:27 +00:00
|
|
|
nsAutoTArray<JS::Heap<JSObject*>, 2> mJSChannels;
|
2013-02-04 23:07:25 +00:00
|
|
|
|
|
|
|
// mSharedChannels aggregates the data from mJSChannels. This is non-null
|
|
|
|
// if and only if the mJSChannels are neutered.
|
|
|
|
nsRefPtr<ThreadSharedFloatArrayBufferList> mSharedChannels;
|
|
|
|
|
2012-09-21 22:42:14 +00:00
|
|
|
uint32_t mLength;
|
|
|
|
float mSampleRate;
|
|
|
|
};
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2012-09-21 22:42:14 +00:00
|
|
|
|
2012-10-30 21:39:38 +00:00
|
|
|
#endif
|
|
|
|
|