1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 Create an oscillator of each type and verify that the type is set correctly.
8 <link rel="stylesheet" href="../fast/js/resources/js-test-style.css"/>
9 <script type="text/javascript" src="resources/audio-testing.js"></script>
10 <script type="text/javascript" src="../fast/js/resources/js-test-pre.js"></script>
14 <div id="description"></div>
15 <div id="console"></div>
18 description("Basic test of setting Oscillator node types.");
20 var sampleRate = 44100;
21 var renderLengthSeconds = 0.25;
23 var oscTypes = [{type: "sine", integerType: 0, name: "SINE"},
24 {type: "square", integerType: 1, name: "SQUARE"},
25 {type: "sawtooth", integerType: 2, name: "SAWTOOTH"},
26 {type: "triangle", integerType: 3, name: "TRIANGLE"},
27 {type: "custom", integerType: 4, name: "CUSTOM"}];
31 if (window.testRunner) {
32 testRunner.dumpAsText();
33 testRunner.waitUntilDone();
36 window.jsTestIsAsync = true;
38 // Create offline audio context.
39 var context = new webkitOfflineAudioContext(2, sampleRate * renderLengthSeconds, sampleRate);
40 var osc = context.createOscillator();
42 // Set each possible oscillator type (except CUSTOM) and verify that the type is correct.
43 // Here we're setting the type using WebIDL enum values which are strings.
44 for (var k = 0; k < oscTypes.length - 1; ++k) {
45 osc.type = oscTypes[k].type;
46 if (osc.type == oscTypes[k].type)
47 testPassed("Oscillator correctly set to " + oscTypes[k].name + " type.");
49 testFailed("Oscillator set to " + oscTypes[k].name + " type, but returns " + oscTypes[osc.type].name + " type.");
52 // For legacy support, verify that we can set the type attribute as an integer value and
53 // verify that this translates correctly to the WebIDL enum value.
54 for (var k = 0; k < oscTypes.length - 1; ++k) {
55 osc.type = oscTypes[k].integerType;
56 if (osc.type == oscTypes[k].type)
57 testPassed("Oscillator correctly set to " + oscTypes[k].name + " type using legacy integer value.");
59 testFailed("Oscillator set to " + oscTypes[k].name + " type, but returns " + oscTypes[osc.type].name + " type using legacy integer value.");
62 // Now set a custom oscillator
63 var coeffA = new Float32Array([0, 1, 0.5]);
64 var coeffB = new Float32Array([0, 0, 0]);
65 var wavetable = context.createWaveTable(coeffA, coeffB);
66 osc.setWaveTable(wavetable);
67 if (osc.type == "custom")
68 testPassed("Oscillator correctly set to CUSTOM type using setWaveTable.");
70 testFailed("Oscillator set to CUSTOM type, but returns " + oscTypes[osc.type].name + " type.");
72 // Try setting some invalid types
75 testFailed("Directly setting oscillator type to CUSTOM did not throw exception.");
77 testPassed("Directly setting oscillator type to CUSTOM correctly throws exception.");
80 var oscType = osc.CUSTOM + 1;
83 testFailed("Setting oscillator to invalid type " + oscType + " did not throw exception.");
85 testPassed("Setting oscillator to invalid type " + oscType + " correctly throws exception.");
88 // Check specifically that we throw a TypeError.
89 shouldThrowTypeError(function() { osc.type = "xyz12349jfksd"; }, "Setting .type to illegal string value");
90 shouldThrowTypeError(function() { osc.type = new Float32Array(1); }, "Setting .type to illegal type of Float32Array");
96 successfullyParsed = true;
100 <script src="../fast/js/resources/js-test-post.js"></script>