|
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 #include "PeriodicWave.h" |
|
8 #include "AudioContext.h" |
|
9 #include "mozilla/dom/PeriodicWaveBinding.h" |
|
10 |
|
11 namespace mozilla { |
|
12 namespace dom { |
|
13 |
|
14 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(PeriodicWave, mContext) |
|
15 |
|
16 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(PeriodicWave, AddRef) |
|
17 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(PeriodicWave, Release) |
|
18 |
|
19 PeriodicWave::PeriodicWave(AudioContext* aContext, |
|
20 const float* aRealData, |
|
21 const float* aImagData, |
|
22 const uint32_t aLength, |
|
23 ErrorResult& aRv) |
|
24 : mContext(aContext) |
|
25 { |
|
26 MOZ_ASSERT(aContext); |
|
27 SetIsDOMBinding(); |
|
28 |
|
29 // Caller should have checked this and thrown. |
|
30 MOZ_ASSERT(aLength > 0); |
|
31 MOZ_ASSERT(aLength <= 4096); |
|
32 mLength = aLength; |
|
33 |
|
34 // Copy coefficient data. The two arrays share an allocation. |
|
35 mCoefficients = new ThreadSharedFloatArrayBufferList(2); |
|
36 float* buffer = static_cast<float*>(malloc(aLength*sizeof(float)*2)); |
|
37 if (buffer == nullptr) { |
|
38 aRv.Throw(NS_ERROR_OUT_OF_MEMORY); |
|
39 return; |
|
40 } |
|
41 PodCopy(buffer, aRealData, aLength); |
|
42 mCoefficients->SetData(0, buffer, buffer); |
|
43 PodCopy(buffer+aLength, aImagData, aLength); |
|
44 mCoefficients->SetData(1, nullptr, buffer+aLength); |
|
45 } |
|
46 |
|
47 size_t |
|
48 PeriodicWave::SizeOfExcludingThisIfNotShared(MallocSizeOf aMallocSizeOf) const |
|
49 { |
|
50 // Not owned: |
|
51 // - mContext |
|
52 size_t amount = 0; |
|
53 if (!mCoefficients->IsShared()) { |
|
54 amount += mCoefficients->SizeOfIncludingThis(aMallocSizeOf); |
|
55 } |
|
56 |
|
57 return amount; |
|
58 } |
|
59 |
|
60 JSObject* |
|
61 PeriodicWave::WrapObject(JSContext* aCx) |
|
62 { |
|
63 return PeriodicWaveBinding::Wrap(aCx, this); |
|
64 } |
|
65 |
|
66 } // namespace dom |
|
67 } // namespace mozilla |
|
68 |