michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef AudioBuffer_h_ michael@0: #define AudioBuffer_h_ michael@0: michael@0: #include "nsWrapperCache.h" michael@0: #include "nsCycleCollectionParticipant.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsTArray.h" michael@0: #include "AudioContext.h" michael@0: #include "js/TypeDecls.h" michael@0: #include "mozilla/MemoryReporting.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: class ErrorResult; michael@0: class ThreadSharedFloatArrayBufferList; michael@0: michael@0: namespace dom { michael@0: michael@0: class AudioContext; michael@0: michael@0: /** michael@0: * An AudioBuffer keeps its data either in the mJSChannels objects, which michael@0: * are Float32Arrays, or in mSharedChannels if the mJSChannels objects have michael@0: * been neutered. michael@0: */ michael@0: class AudioBuffer MOZ_FINAL : public nsWrapperCache michael@0: { michael@0: public: michael@0: static already_AddRefed michael@0: Create(AudioContext* aContext, uint32_t aNumberOfChannels, michael@0: uint32_t aLength, float aSampleRate, michael@0: JSContext* aJSContext, ErrorResult& aRv); michael@0: michael@0: size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; michael@0: michael@0: NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(AudioBuffer) michael@0: NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(AudioBuffer) michael@0: michael@0: AudioContext* GetParentObject() const michael@0: { michael@0: return mContext; michael@0: } michael@0: michael@0: virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; michael@0: michael@0: float SampleRate() const michael@0: { michael@0: return mSampleRate; michael@0: } michael@0: michael@0: int32_t Length() const michael@0: { michael@0: return mLength; michael@0: } michael@0: michael@0: double Duration() const michael@0: { michael@0: return mLength / static_cast (mSampleRate); michael@0: } michael@0: michael@0: uint32_t NumberOfChannels() const michael@0: { michael@0: return mJSChannels.Length(); michael@0: } michael@0: michael@0: /** michael@0: * If mSharedChannels is non-null, copies its contents to michael@0: * new Float32Arrays in mJSChannels. Returns a Float32Array. michael@0: */ michael@0: void GetChannelData(JSContext* aJSContext, uint32_t aChannel, michael@0: JS::MutableHandle aRetval, michael@0: ErrorResult& aRv); michael@0: michael@0: void CopyFromChannel(const Float32Array& aDestination, uint32_t aChannelNumber, michael@0: uint32_t aStartInChannel, ErrorResult& aRv); michael@0: void CopyToChannel(JSContext* aJSContext, const Float32Array& aSource, michael@0: uint32_t aChannelNumber, uint32_t aStartInChannel, michael@0: ErrorResult& aRv); michael@0: michael@0: /** michael@0: * Returns a ThreadSharedFloatArrayBufferList containing the sample data. michael@0: * Can return null if there is no data. michael@0: */ michael@0: ThreadSharedFloatArrayBufferList* GetThreadSharedChannelsForRate(JSContext* aContext); michael@0: michael@0: // This replaces the contents of the JS array for the given channel. michael@0: // This function needs to be called on an AudioBuffer which has not been michael@0: // handed off to the content yet, and right after the object has been michael@0: // initialized. michael@0: void SetRawChannelContents(JSContext* aJSContext, michael@0: uint32_t aChannel, michael@0: float* aContents); michael@0: michael@0: protected: michael@0: AudioBuffer(AudioContext* aContext, uint32_t aNumberOfChannels, michael@0: uint32_t aLength, float aSampleRate); michael@0: ~AudioBuffer(); michael@0: michael@0: bool RestoreJSChannelData(JSContext* aJSContext); michael@0: void ClearJSChannels(); michael@0: michael@0: nsRefPtr mContext; michael@0: // Float32Arrays michael@0: nsAutoTArray, 2> mJSChannels; michael@0: michael@0: // mSharedChannels aggregates the data from mJSChannels. This is non-null michael@0: // if and only if the mJSChannels are neutered. michael@0: nsRefPtr mSharedChannels; michael@0: michael@0: uint32_t mLength; michael@0: float mSampleRate; michael@0: }; michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif michael@0: