content/media/webaudio/AudioBuffer.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef AudioBuffer_h_
michael@0 8 #define AudioBuffer_h_
michael@0 9
michael@0 10 #include "nsWrapperCache.h"
michael@0 11 #include "nsCycleCollectionParticipant.h"
michael@0 12 #include "mozilla/Attributes.h"
michael@0 13 #include "nsAutoPtr.h"
michael@0 14 #include "nsTArray.h"
michael@0 15 #include "AudioContext.h"
michael@0 16 #include "js/TypeDecls.h"
michael@0 17 #include "mozilla/MemoryReporting.h"
michael@0 18
michael@0 19 namespace mozilla {
michael@0 20
michael@0 21 class ErrorResult;
michael@0 22 class ThreadSharedFloatArrayBufferList;
michael@0 23
michael@0 24 namespace dom {
michael@0 25
michael@0 26 class AudioContext;
michael@0 27
michael@0 28 /**
michael@0 29 * An AudioBuffer keeps its data either in the mJSChannels objects, which
michael@0 30 * are Float32Arrays, or in mSharedChannels if the mJSChannels objects have
michael@0 31 * been neutered.
michael@0 32 */
michael@0 33 class AudioBuffer MOZ_FINAL : public nsWrapperCache
michael@0 34 {
michael@0 35 public:
michael@0 36 static already_AddRefed<AudioBuffer>
michael@0 37 Create(AudioContext* aContext, uint32_t aNumberOfChannels,
michael@0 38 uint32_t aLength, float aSampleRate,
michael@0 39 JSContext* aJSContext, ErrorResult& aRv);
michael@0 40
michael@0 41 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
michael@0 42
michael@0 43 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(AudioBuffer)
michael@0 44 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(AudioBuffer)
michael@0 45
michael@0 46 AudioContext* GetParentObject() const
michael@0 47 {
michael@0 48 return mContext;
michael@0 49 }
michael@0 50
michael@0 51 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
michael@0 52
michael@0 53 float SampleRate() const
michael@0 54 {
michael@0 55 return mSampleRate;
michael@0 56 }
michael@0 57
michael@0 58 int32_t Length() const
michael@0 59 {
michael@0 60 return mLength;
michael@0 61 }
michael@0 62
michael@0 63 double Duration() const
michael@0 64 {
michael@0 65 return mLength / static_cast<double> (mSampleRate);
michael@0 66 }
michael@0 67
michael@0 68 uint32_t NumberOfChannels() const
michael@0 69 {
michael@0 70 return mJSChannels.Length();
michael@0 71 }
michael@0 72
michael@0 73 /**
michael@0 74 * If mSharedChannels is non-null, copies its contents to
michael@0 75 * new Float32Arrays in mJSChannels. Returns a Float32Array.
michael@0 76 */
michael@0 77 void GetChannelData(JSContext* aJSContext, uint32_t aChannel,
michael@0 78 JS::MutableHandle<JSObject*> aRetval,
michael@0 79 ErrorResult& aRv);
michael@0 80
michael@0 81 void CopyFromChannel(const Float32Array& aDestination, uint32_t aChannelNumber,
michael@0 82 uint32_t aStartInChannel, ErrorResult& aRv);
michael@0 83 void CopyToChannel(JSContext* aJSContext, const Float32Array& aSource,
michael@0 84 uint32_t aChannelNumber, uint32_t aStartInChannel,
michael@0 85 ErrorResult& aRv);
michael@0 86
michael@0 87 /**
michael@0 88 * Returns a ThreadSharedFloatArrayBufferList containing the sample data.
michael@0 89 * Can return null if there is no data.
michael@0 90 */
michael@0 91 ThreadSharedFloatArrayBufferList* GetThreadSharedChannelsForRate(JSContext* aContext);
michael@0 92
michael@0 93 // This replaces the contents of the JS array for the given channel.
michael@0 94 // This function needs to be called on an AudioBuffer which has not been
michael@0 95 // handed off to the content yet, and right after the object has been
michael@0 96 // initialized.
michael@0 97 void SetRawChannelContents(JSContext* aJSContext,
michael@0 98 uint32_t aChannel,
michael@0 99 float* aContents);
michael@0 100
michael@0 101 protected:
michael@0 102 AudioBuffer(AudioContext* aContext, uint32_t aNumberOfChannels,
michael@0 103 uint32_t aLength, float aSampleRate);
michael@0 104 ~AudioBuffer();
michael@0 105
michael@0 106 bool RestoreJSChannelData(JSContext* aJSContext);
michael@0 107 void ClearJSChannels();
michael@0 108
michael@0 109 nsRefPtr<AudioContext> mContext;
michael@0 110 // Float32Arrays
michael@0 111 nsAutoTArray<JS::Heap<JSObject*>, 2> mJSChannels;
michael@0 112
michael@0 113 // mSharedChannels aggregates the data from mJSChannels. This is non-null
michael@0 114 // if and only if the mJSChannels are neutered.
michael@0 115 nsRefPtr<ThreadSharedFloatArrayBufferList> mSharedChannels;
michael@0 116
michael@0 117 uint32_t mLength;
michael@0 118 float mSampleRate;
michael@0 119 };
michael@0 120
michael@0 121 }
michael@0 122 }
michael@0 123
michael@0 124 #endif
michael@0 125

mercurial