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