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 "AudioBuffer.h" 32#include "Document.h" 33#include "JSAudioBuffer.h" 34#include "JSAudioContext.h" 35#include "JSDOMBinding.h" 36#include "JSOfflineAudioContext.h" 37#include "OfflineAudioContext.h" 38#include <runtime/ArrayBuffer.h> 39#include <runtime/Error.h> 40#include <runtime/JSArrayBuffer.h> 41 42using namespace JSC; 43 44namespace WebCore { 45 46EncodedJSValue JSC_HOST_CALL constructJSAudioContext(ExecState* exec) 47{ 48 DOMConstructorObject* jsConstructor = jsCast<DOMConstructorObject*>(exec->callee()); 49 if (!jsConstructor) 50 return throwVMError(exec, createReferenceError(exec, "AudioContext constructor callee is unavailable")); 51 52 ScriptExecutionContext* scriptExecutionContext = jsConstructor->scriptExecutionContext(); 53 if (!scriptExecutionContext) 54 return throwVMError(exec, createReferenceError(exec, "AudioContext constructor script execution context is unavailable")); 55 56 if (!scriptExecutionContext->isDocument()) 57 return throwVMError(exec, createReferenceError(exec, "AudioContext constructor called in a script execution context which is not a document")); 58 59 Document& document = toDocument(*scriptExecutionContext); 60 61 RefPtr<AudioContext> audioContext; 62 63 if (!exec->argumentCount()) { 64 // Constructor for default AudioContext which talks to audio hardware. 65 ExceptionCode ec = 0; 66 audioContext = AudioContext::create(document, ec); 67 if (ec) { 68 setDOMException(exec, ec); 69 return JSValue::encode(JSValue()); 70 } 71 if (!audioContext.get()) 72 return throwVMError(exec, createSyntaxError(exec, "audio resources unavailable for AudioContext construction")); 73 } else { 74#if ENABLE(LEGACY_WEB_AUDIO) 75 // Constructor for offline (render-target) AudioContext which renders into an AudioBuffer. 76 // new AudioContext(in unsigned long numberOfChannels, in unsigned long numberOfFrames, in float sampleRate); 77 document.addConsoleMessage(MessageSource::JS, MessageLevel::Warning, ASCIILiteral("Deprecated AudioContext constructor: use OfflineAudioContext instead")); 78 79 if (exec->argumentCount() < 3) 80 return throwVMError(exec, createNotEnoughArgumentsError(exec)); 81 82 int32_t numberOfChannels = exec->argument(0).toInt32(exec); 83 int32_t numberOfFrames = exec->argument(1).toInt32(exec); 84 float sampleRate = exec->argument(2).toFloat(exec); 85 86 if (numberOfChannels <= 0 || numberOfChannels > 10) 87 return throwVMError(exec, createSyntaxError(exec, "Invalid number of channels")); 88 89 if (numberOfFrames <= 0) 90 return throwVMError(exec, createSyntaxError(exec, "Invalid number of frames")); 91 92 if (sampleRate <= 0) 93 return throwVMError(exec, createSyntaxError(exec, "Invalid sample rate")); 94 95 96 ExceptionCode ec = 0; 97 audioContext = OfflineAudioContext::create(document, numberOfChannels, numberOfFrames, sampleRate, ec); 98 if (ec) { 99 setDOMException(exec, ec); 100 return throwVMError(exec, createSyntaxError(exec, "Error creating OfflineAudioContext")); 101 } 102#else 103 return throwVMError(exec, createSyntaxError(exec, "Illegal AudioContext constructor")); 104#endif 105 } 106 107 if (!audioContext.get()) 108 return throwVMError(exec, createReferenceError(exec, "Error creating AudioContext")); 109 110 return JSValue::encode(CREATE_DOM_WRAPPER(jsConstructor->globalObject(), AudioContext, audioContext.get())); 111} 112 113} // namespace WebCore 114 115#endif // ENABLE(WEB_AUDIO) 116