initial import
[vuplus_webkit] / Source / WebCore / bindings / js / JSAudioContextCustom.cpp
1 /*
2  * Copyright (C) 2010, Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1.  Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "config.h"
26
27 #if ENABLE(WEB_AUDIO)
28
29 #include "AudioContext.h"
30
31 #include "ArrayBuffer.h"
32 #include "AudioBuffer.h"
33 #include "JSArrayBuffer.h"
34 #include "JSAudioBuffer.h"
35 #include "JSAudioContext.h"
36 #include <runtime/Error.h>
37
38 using namespace JSC;
39
40 namespace WebCore {
41
42 void JSAudioContext::visitChildren(SlotVisitor& visitor)
43 {
44     ASSERT_GC_OBJECT_INHERITS(this, &s_info);
45     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
46     ASSERT(structure()->typeInfo().overridesVisitChildren());
47     Base::visitChildren(visitor);
48     m_impl->visitJSEventListeners(visitor);
49 }
50
51 EncodedJSValue JSC_HOST_CALL JSAudioContextConstructor::constructJSAudioContext(ExecState* exec)
52 {
53     JSAudioContextConstructor* jsConstructor = static_cast<JSAudioContextConstructor*>(exec->callee());
54     if (!jsConstructor)
55         return throwVMError(exec, createReferenceError(exec, "AudioContext constructor callee is unavailable"));
56
57     ScriptExecutionContext* scriptExecutionContext = jsConstructor->scriptExecutionContext();
58     if (!scriptExecutionContext)
59         return throwVMError(exec, createReferenceError(exec, "AudioContext constructor script execution context is unavailable"));
60         
61     if (!scriptExecutionContext->isDocument())
62         return throwVMError(exec, createReferenceError(exec, "AudioContext constructor called in a script execution context which is not a document"));
63
64     Document* document = static_cast<Document*>(scriptExecutionContext);
65
66     RefPtr<AudioContext> audioContext;
67     
68     if (!exec->argumentCount()) {
69         // Constructor for default AudioContext which talks to audio hardware.
70         audioContext = AudioContext::create(document);
71         if (!audioContext.get())
72             return throwVMError(exec, createSyntaxError(exec, "audio resources unavailable for AudioContext construction"));
73     } else {
74         // Constructor for offline (render-target) AudioContext which renders into an AudioBuffer.
75         // new AudioContext(in unsigned long numberOfChannels, in unsigned long numberOfFrames, in float sampleRate);
76         if (exec->argumentCount() < 3)
77             return throwVMError(exec, createSyntaxError(exec, "Not enough arguments"));
78
79         int32_t numberOfChannels = exec->argument(0).toInt32(exec);
80         int32_t numberOfFrames = exec->argument(1).toInt32(exec);
81         float sampleRate = exec->argument(2).toFloat(exec);
82         
83         if (numberOfChannels <= 0 || numberOfChannels > 10)
84             return throwVMError(exec, createSyntaxError(exec, "Invalid number of channels"));
85
86         if (numberOfFrames <= 0)
87             return throwVMError(exec, createSyntaxError(exec, "Invalid number of frames"));
88
89         if (sampleRate <= 0)
90             return throwVMError(exec, createSyntaxError(exec, "Invalid sample rate"));
91
92         ExceptionCode ec = 0;
93         audioContext = AudioContext::createOfflineContext(document, numberOfChannels, numberOfFrames, sampleRate, ec);
94         if (ec) {
95             setDOMException(exec, ec);
96             return throwVMError(exec, createSyntaxError(exec, "Error creating OfflineAudioContext"));
97         }
98     }
99
100     if (!audioContext.get())
101         return throwVMError(exec, createReferenceError(exec, "Error creating AudioContext"));
102
103     return JSValue::encode(asObject(toJS(exec, jsConstructor->globalObject(), audioContext.get())));
104 }
105
106 JSValue JSAudioContext::createBuffer(ExecState* exec)
107 {
108     if (exec->argumentCount() < 2)
109         return throwError(exec, createSyntaxError(exec, "Not enough arguments"));
110
111     AudioContext* audioContext = static_cast<AudioContext*>(impl());
112     ASSERT(audioContext);
113
114     // AudioBuffer createBuffer(in ArrayBuffer buffer, in boolean mixToMono);
115     JSValue val = exec->argument(0);
116     if (val.inherits(&JSArrayBuffer::s_info)) {
117         ArrayBuffer* arrayBuffer = toArrayBuffer(val);
118         ASSERT(arrayBuffer);
119         if (arrayBuffer) {
120             bool mixToMono = exec->argument(1).toBoolean(exec);
121
122             RefPtr<AudioBuffer> audioBuffer = audioContext->createBuffer(arrayBuffer, mixToMono);
123             if (!audioBuffer.get())
124                 return throwError(exec, createSyntaxError(exec, "Error decoding audio file data"));
125
126             return toJS(exec, globalObject(), audioBuffer.get());
127         }
128
129         return jsUndefined();
130     }
131     
132     // AudioBuffer createBuffer(in unsigned long numberOfChannels, in unsigned long numberOfFrames, in float sampleRate);
133     if (exec->argumentCount() < 3)
134         return throwError(exec, createSyntaxError(exec, "Not enough arguments"));
135     
136     int32_t numberOfChannels = exec->argument(0).toInt32(exec);
137     int32_t numberOfFrames = exec->argument(1).toInt32(exec);
138     float sampleRate = exec->argument(2).toFloat(exec);
139
140     if (numberOfChannels <= 0 || numberOfChannels > 10)
141         return throwError(exec, createSyntaxError(exec, "Invalid number of channels"));
142
143     if (numberOfFrames <= 0)
144         return throwError(exec, createSyntaxError(exec, "Invalid number of frames"));
145
146     if (sampleRate <= 0)
147         return throwError(exec, createSyntaxError(exec, "Invalid sample rate"));
148
149     RefPtr<AudioBuffer> audioBuffer = audioContext->createBuffer(numberOfChannels, numberOfFrames, sampleRate);
150     if (!audioBuffer.get())
151         return throwError(exec, createSyntaxError(exec, "Error creating AudioBuffer"));
152
153     return toJS(exec, globalObject(), audioBuffer.get());
154 }
155
156 } // namespace WebCore
157
158 #endif // ENABLE(WEB_AUDIO)