1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/AudioBuffer.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,125 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef AudioBuffer_h_ 1.11 +#define AudioBuffer_h_ 1.12 + 1.13 +#include "nsWrapperCache.h" 1.14 +#include "nsCycleCollectionParticipant.h" 1.15 +#include "mozilla/Attributes.h" 1.16 +#include "nsAutoPtr.h" 1.17 +#include "nsTArray.h" 1.18 +#include "AudioContext.h" 1.19 +#include "js/TypeDecls.h" 1.20 +#include "mozilla/MemoryReporting.h" 1.21 + 1.22 +namespace mozilla { 1.23 + 1.24 +class ErrorResult; 1.25 +class ThreadSharedFloatArrayBufferList; 1.26 + 1.27 +namespace dom { 1.28 + 1.29 +class AudioContext; 1.30 + 1.31 +/** 1.32 + * An AudioBuffer keeps its data either in the mJSChannels objects, which 1.33 + * are Float32Arrays, or in mSharedChannels if the mJSChannels objects have 1.34 + * been neutered. 1.35 + */ 1.36 +class AudioBuffer MOZ_FINAL : public nsWrapperCache 1.37 +{ 1.38 +public: 1.39 + static already_AddRefed<AudioBuffer> 1.40 + Create(AudioContext* aContext, uint32_t aNumberOfChannels, 1.41 + uint32_t aLength, float aSampleRate, 1.42 + JSContext* aJSContext, ErrorResult& aRv); 1.43 + 1.44 + size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; 1.45 + 1.46 + NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(AudioBuffer) 1.47 + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(AudioBuffer) 1.48 + 1.49 + AudioContext* GetParentObject() const 1.50 + { 1.51 + return mContext; 1.52 + } 1.53 + 1.54 + virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; 1.55 + 1.56 + float SampleRate() const 1.57 + { 1.58 + return mSampleRate; 1.59 + } 1.60 + 1.61 + int32_t Length() const 1.62 + { 1.63 + return mLength; 1.64 + } 1.65 + 1.66 + double Duration() const 1.67 + { 1.68 + return mLength / static_cast<double> (mSampleRate); 1.69 + } 1.70 + 1.71 + uint32_t NumberOfChannels() const 1.72 + { 1.73 + return mJSChannels.Length(); 1.74 + } 1.75 + 1.76 + /** 1.77 + * If mSharedChannels is non-null, copies its contents to 1.78 + * new Float32Arrays in mJSChannels. Returns a Float32Array. 1.79 + */ 1.80 + void GetChannelData(JSContext* aJSContext, uint32_t aChannel, 1.81 + JS::MutableHandle<JSObject*> aRetval, 1.82 + ErrorResult& aRv); 1.83 + 1.84 + void CopyFromChannel(const Float32Array& aDestination, uint32_t aChannelNumber, 1.85 + uint32_t aStartInChannel, ErrorResult& aRv); 1.86 + void CopyToChannel(JSContext* aJSContext, const Float32Array& aSource, 1.87 + uint32_t aChannelNumber, uint32_t aStartInChannel, 1.88 + ErrorResult& aRv); 1.89 + 1.90 + /** 1.91 + * Returns a ThreadSharedFloatArrayBufferList containing the sample data. 1.92 + * Can return null if there is no data. 1.93 + */ 1.94 + ThreadSharedFloatArrayBufferList* GetThreadSharedChannelsForRate(JSContext* aContext); 1.95 + 1.96 + // This replaces the contents of the JS array for the given channel. 1.97 + // This function needs to be called on an AudioBuffer which has not been 1.98 + // handed off to the content yet, and right after the object has been 1.99 + // initialized. 1.100 + void SetRawChannelContents(JSContext* aJSContext, 1.101 + uint32_t aChannel, 1.102 + float* aContents); 1.103 + 1.104 +protected: 1.105 + AudioBuffer(AudioContext* aContext, uint32_t aNumberOfChannels, 1.106 + uint32_t aLength, float aSampleRate); 1.107 + ~AudioBuffer(); 1.108 + 1.109 + bool RestoreJSChannelData(JSContext* aJSContext); 1.110 + void ClearJSChannels(); 1.111 + 1.112 + nsRefPtr<AudioContext> mContext; 1.113 + // Float32Arrays 1.114 + nsAutoTArray<JS::Heap<JSObject*>, 2> mJSChannels; 1.115 + 1.116 + // mSharedChannels aggregates the data from mJSChannels. This is non-null 1.117 + // if and only if the mJSChannels are neutered. 1.118 + nsRefPtr<ThreadSharedFloatArrayBufferList> mSharedChannels; 1.119 + 1.120 + uint32_t mLength; 1.121 + float mSampleRate; 1.122 +}; 1.123 + 1.124 +} 1.125 +} 1.126 + 1.127 +#endif 1.128 +