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
|
|
|
|
2017-08-03 05:58:23 +00:00
|
|
|
#include "AudioSegment.h"
|
2012-09-21 22:42:14 +00:00
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
2016-04-01 11:36:41 +00:00
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "mozilla/StaticMutex.h"
|
2012-09-21 22:42:14 +00:00
|
|
|
#include "nsTArray.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"
|
2017-01-13 15:41:03 +00:00
|
|
|
#include "mozilla/dom/TypedArray.h"
|
2017-08-08 03:38:02 +00:00
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsIWeakReferenceUtils.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 {
|
|
|
|
|
2016-12-15 18:24:42 +00:00
|
|
|
struct AudioBufferOptions;
|
2013-08-15 19:44:14 +00:00
|
|
|
|
2013-02-04 23:07:25 +00:00
|
|
|
/**
|
|
|
|
* An AudioBuffer keeps its data either in the mJSChannels objects, which
|
2016-01-26 02:10:22 +00:00
|
|
|
* are Float32Arrays, or in mSharedChannels if the mJSChannels objects' buffers
|
|
|
|
* are detached.
|
2013-02-04 23:07:25 +00:00
|
|
|
*/
|
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>
|
2017-01-10 21:30:28 +00:00
|
|
|
Create(nsPIDOMWindowInner* aWindow, uint32_t aNumberOfChannels,
|
2014-05-15 21:23:27 +00:00
|
|
|
uint32_t aLength, float aSampleRate,
|
2015-08-28 06:30:49 +00:00
|
|
|
already_AddRefed<ThreadSharedFloatArrayBufferList> aInitialContents,
|
2016-03-11 21:43:31 +00:00
|
|
|
ErrorResult& aRv);
|
2012-09-21 22:42:14 +00:00
|
|
|
|
2015-08-28 06:30:49 +00:00
|
|
|
static already_AddRefed<AudioBuffer>
|
2017-01-10 21:30:28 +00:00
|
|
|
Create(nsPIDOMWindowInner* aWindow, uint32_t aNumberOfChannels,
|
2015-08-28 06:30:49 +00:00
|
|
|
uint32_t aLength, float aSampleRate,
|
2016-03-11 21:43:31 +00:00
|
|
|
ErrorResult& aRv)
|
2015-08-28 06:30:49 +00:00
|
|
|
{
|
2017-01-10 21:30:28 +00:00
|
|
|
return Create(aWindow, aNumberOfChannels, aLength, aSampleRate,
|
2016-03-11 21:43:31 +00:00
|
|
|
nullptr, aRv);
|
2015-08-28 06:30:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-16 05:45:48 +00:00
|
|
|
// Non-unit AudioChunk::mVolume is not supported
|
|
|
|
static already_AddRefed<AudioBuffer>
|
|
|
|
Create(nsPIDOMWindowInner* aWindow, float aSampleRate,
|
|
|
|
AudioChunk&& aInitialContents);
|
|
|
|
|
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
|
|
|
|
2016-12-15 18:24:42 +00:00
|
|
|
static already_AddRefed<AudioBuffer>
|
2017-01-10 21:30:28 +00:00
|
|
|
Constructor(const GlobalObject& aGlobal,
|
2016-12-15 18:24:42 +00:00
|
|
|
const AudioBufferOptions& aOptions, ErrorResult& aRv);
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
nsPIDOMWindowInner* GetParentObject() const
|
2012-09-21 22:42:14 +00:00
|
|
|
{
|
2016-01-30 17:05:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> parentObject = do_QueryReferent(mOwnerWindow);
|
2015-08-17 23:48:38 +00:00
|
|
|
return parentObject;
|
2012-09-21 22:42:14 +00:00
|
|
|
}
|
|
|
|
|
2016-01-18 03:22:51 +00:00
|
|
|
JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
2012-09-21 22:42:14 +00:00
|
|
|
|
|
|
|
float SampleRate() const
|
|
|
|
{
|
|
|
|
return mSampleRate;
|
|
|
|
}
|
|
|
|
|
2016-04-20 16:32:17 +00:00
|
|
|
uint32_t Length() const
|
2012-09-21 22:42:14 +00:00
|
|
|
{
|
2017-08-03 05:58:23 +00:00
|
|
|
return mSharedChannels.mDuration;
|
2012-09-21 22:42:14 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 20:52:29 +00:00
|
|
|
double Duration() const
|
2012-09-21 22:42:14 +00:00
|
|
|
{
|
2017-08-03 05:58:23 +00:00
|
|
|
return Length() / 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
|
|
|
/**
|
2017-08-09 04:39:40 +00:00
|
|
|
* Returns a reference to an AudioChunk containing the sample data.
|
|
|
|
* The AudioChunk can have a null buffer if there is no data.
|
2013-02-04 23:07:25 +00:00
|
|
|
*/
|
2017-08-09 04:39:40 +00:00
|
|
|
const AudioChunk& GetThreadSharedChannelsForRate(JSContext* aContext);
|
2013-02-04 23:07:25 +00:00
|
|
|
|
|
|
|
protected:
|
2017-01-10 21:30:28 +00:00
|
|
|
AudioBuffer(nsPIDOMWindowInner* aWindow, uint32_t aNumberOfChannels,
|
2017-08-16 05:38:43 +00:00
|
|
|
uint32_t aLength, float aSampleRate, ErrorResult& aRv);
|
2014-05-15 21:23:27 +00:00
|
|
|
~AudioBuffer();
|
|
|
|
|
2017-08-03 05:58:23 +00:00
|
|
|
void
|
|
|
|
SetSharedChannels(already_AddRefed<ThreadSharedFloatArrayBufferList> aBuffer);
|
|
|
|
|
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();
|
|
|
|
|
2013-02-04 23:07:25 +00:00
|
|
|
// Float32Arrays
|
2016-02-02 15:36:30 +00:00
|
|
|
AutoTArray<JS::Heap<JSObject*>, 2> mJSChannels;
|
2013-02-04 23:07:25 +00:00
|
|
|
// mSharedChannels aggregates the data from mJSChannels. This is non-null
|
2017-08-03 05:58:23 +00:00
|
|
|
// if and only if the mJSChannels' buffers are detached, but its mDuration
|
|
|
|
// member keeps the buffer length regardless of whether the buffer is
|
|
|
|
// provided by mJSChannels or mSharedChannels.
|
|
|
|
AudioChunk mSharedChannels;
|
2013-02-04 23:07:25 +00:00
|
|
|
|
2017-08-03 05:58:23 +00:00
|
|
|
nsWeakPtr mOwnerWindow;
|
2012-09-21 22:42:14 +00:00
|
|
|
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
|