2 * Copyright (C) 2010, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "AudioContext.h"
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>
42 void JSAudioContext::visitChildren(SlotVisitor& visitor)
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);
51 EncodedJSValue JSC_HOST_CALL JSAudioContextConstructor::constructJSAudioContext(ExecState* exec)
53 JSAudioContextConstructor* jsConstructor = static_cast<JSAudioContextConstructor*>(exec->callee());
55 return throwVMError(exec, createReferenceError(exec, "AudioContext constructor callee is unavailable"));
57 ScriptExecutionContext* scriptExecutionContext = jsConstructor->scriptExecutionContext();
58 if (!scriptExecutionContext)
59 return throwVMError(exec, createReferenceError(exec, "AudioContext constructor script execution context is unavailable"));
61 if (!scriptExecutionContext->isDocument())
62 return throwVMError(exec, createReferenceError(exec, "AudioContext constructor called in a script execution context which is not a document"));
64 Document* document = static_cast<Document*>(scriptExecutionContext);
66 RefPtr<AudioContext> audioContext;
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"));
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"));
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);
83 if (numberOfChannels <= 0 || numberOfChannels > 10)
84 return throwVMError(exec, createSyntaxError(exec, "Invalid number of channels"));
86 if (numberOfFrames <= 0)
87 return throwVMError(exec, createSyntaxError(exec, "Invalid number of frames"));
90 return throwVMError(exec, createSyntaxError(exec, "Invalid sample rate"));
93 audioContext = AudioContext::createOfflineContext(document, numberOfChannels, numberOfFrames, sampleRate, ec);
95 setDOMException(exec, ec);
96 return throwVMError(exec, createSyntaxError(exec, "Error creating OfflineAudioContext"));
100 if (!audioContext.get())
101 return throwVMError(exec, createReferenceError(exec, "Error creating AudioContext"));
103 return JSValue::encode(asObject(toJS(exec, jsConstructor->globalObject(), audioContext.get())));
106 JSValue JSAudioContext::createBuffer(ExecState* exec)
108 if (exec->argumentCount() < 2)
109 return throwError(exec, createSyntaxError(exec, "Not enough arguments"));
111 AudioContext* audioContext = static_cast<AudioContext*>(impl());
112 ASSERT(audioContext);
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);
120 bool mixToMono = exec->argument(1).toBoolean(exec);
122 RefPtr<AudioBuffer> audioBuffer = audioContext->createBuffer(arrayBuffer, mixToMono);
123 if (!audioBuffer.get())
124 return throwError(exec, createSyntaxError(exec, "Error decoding audio file data"));
126 return toJS(exec, globalObject(), audioBuffer.get());
129 return jsUndefined();
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"));
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);
140 if (numberOfChannels <= 0 || numberOfChannels > 10)
141 return throwError(exec, createSyntaxError(exec, "Invalid number of channels"));
143 if (numberOfFrames <= 0)
144 return throwError(exec, createSyntaxError(exec, "Invalid number of frames"));
147 return throwError(exec, createSyntaxError(exec, "Invalid sample rate"));
149 RefPtr<AudioBuffer> audioBuffer = audioContext->createBuffer(numberOfChannels, numberOfFrames, sampleRate);
150 if (!audioBuffer.get())
151 return throwError(exec, createSyntaxError(exec, "Error creating AudioBuffer"));
153 return toJS(exec, globalObject(), audioBuffer.get());
156 } // namespace WebCore
158 #endif // ENABLE(WEB_AUDIO)