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