2012-09-21 15:42:14 -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/. */
|
|
|
|
|
2012-10-30 14:39:38 -07:00
|
|
|
#ifndef AudioBuffer_h_
|
|
|
|
#define AudioBuffer_h_
|
2012-09-21 15:42:14 -07:00
|
|
|
|
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "AudioContext.h"
|
2013-08-27 19:59:14 -07:00
|
|
|
#include "js/TypeDecls.h"
|
2014-01-04 10:15:41 -08:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2012-09-21 15:42:14 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class ErrorResult;
|
2013-08-15 12:44:14 -07:00
|
|
|
class ThreadSharedFloatArrayBufferList;
|
2012-09-21 15:42:14 -07:00
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
2013-08-15 12:44:14 -07:00
|
|
|
class AudioContext;
|
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
/**
|
|
|
|
* An AudioBuffer keeps its data either in the mJSChannels objects, which
|
|
|
|
* are Float32Arrays, or in mSharedChannels if the mJSChannels objects have
|
|
|
|
* been neutered.
|
|
|
|
*/
|
2014-02-06 05:36:46 -08:00
|
|
|
class AudioBuffer MOZ_FINAL : public nsWrapperCache
|
2012-09-21 15:42:14 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
AudioBuffer(AudioContext* aContext, uint32_t aLength,
|
2013-02-04 15:07:25 -08:00
|
|
|
float aSampleRate);
|
2012-09-21 15:42:14 -07:00
|
|
|
~AudioBuffer();
|
|
|
|
|
2014-01-04 10:15:41 -08:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
|
|
|
|
2012-09-21 15:42:14 -07:00
|
|
|
// This function needs to be called in order to allocate
|
|
|
|
// all of the channels. It is fallible!
|
|
|
|
bool InitializeBuffers(uint32_t aNumberOfChannels,
|
|
|
|
JSContext* aJSContext);
|
|
|
|
|
2013-07-05 13:21:52 -07:00
|
|
|
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(AudioBuffer)
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(AudioBuffer)
|
2012-09-21 15:42:14 -07:00
|
|
|
|
|
|
|
AudioContext* GetParentObject() const
|
|
|
|
{
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
2014-04-08 15:27:18 -07:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
|
2012-09-21 15:42:14 -07:00
|
|
|
|
|
|
|
float SampleRate() const
|
|
|
|
{
|
|
|
|
return mSampleRate;
|
|
|
|
}
|
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
int32_t Length() const
|
2012-09-21 15:42:14 -07:00
|
|
|
{
|
|
|
|
return mLength;
|
|
|
|
}
|
|
|
|
|
2012-11-19 12:52:29 -08:00
|
|
|
double Duration() const
|
2012-09-21 15:42:14 -07:00
|
|
|
{
|
2012-11-19 12:52:29 -08:00
|
|
|
return mLength / static_cast<double> (mSampleRate);
|
2012-09-21 15:42:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t NumberOfChannels() const
|
|
|
|
{
|
2013-02-04 15:07:25 -08:00
|
|
|
return mJSChannels.Length();
|
2012-09-21 15:42:14 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
/**
|
|
|
|
* If mSharedChannels is non-null, copies its contents to
|
|
|
|
* new Float32Arrays in mJSChannels. Returns a Float32Array.
|
|
|
|
*/
|
2012-09-21 15:42:14 -07:00
|
|
|
JSObject* GetChannelData(JSContext* aJSContext, uint32_t aChannel,
|
2013-02-04 15:07:25 -08:00
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2013-09-17 04:30:32 -07: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 15:07:25 -08:00
|
|
|
/**
|
2013-04-22 05:12:45 -07:00
|
|
|
* Returns a ThreadSharedFloatArrayBufferList containing the sample data.
|
2013-06-25 09:01:07 -07:00
|
|
|
* Can return null if there is no data.
|
2013-02-04 15:07:25 -08:00
|
|
|
*/
|
2013-04-22 05:12:45 -07:00
|
|
|
ThreadSharedFloatArrayBufferList* GetThreadSharedChannelsForRate(JSContext* aContext);
|
2013-02-04 15:07:25 -08:00
|
|
|
|
2013-04-13 18:37:04 -07:00
|
|
|
// This replaces the contents of the JS array for the given channel.
|
|
|
|
// This function needs to be called on an AudioBuffer which has not been
|
|
|
|
// handed off to the content yet, and right after the object has been
|
|
|
|
// initialized.
|
|
|
|
void SetRawChannelContents(JSContext* aJSContext,
|
|
|
|
uint32_t aChannel,
|
|
|
|
float* aContents);
|
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
protected:
|
2013-05-04 14:40:20 -07:00
|
|
|
bool RestoreJSChannelData(JSContext* aJSContext);
|
2013-01-25 13:21:22 -08:00
|
|
|
void ClearJSChannels();
|
|
|
|
|
2012-09-21 15:42:14 -07:00
|
|
|
nsRefPtr<AudioContext> mContext;
|
2013-02-04 15:07:25 -08:00
|
|
|
// Float32Arrays
|
2013-06-18 03:00:38 -07:00
|
|
|
AutoFallibleTArray<JS::Heap<JSObject*>, 2> mJSChannels;
|
2013-02-04 15:07:25 -08: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 15:42:14 -07:00
|
|
|
uint32_t mLength;
|
|
|
|
float mSampleRate;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-30 14:39:38 -07:00
|
|
|
#endif
|
|
|
|
|