【问题标题】:Having multiple Oscillators play together让多个振荡器一起播放
【发布时间】:2019-02-02 16:33:00
【问题描述】:

我构建了一个振荡器,带有用于频率、失谐、增益、波形的滑块和一个用于打开和关闭振荡器的按钮。就其本身而言,它完全按照我的预期工作并希望它工作。然而,当我复制代码以制作新的振荡器供它播放时,即使我使用的是osc.disconnect(),振荡器也只播放一次,而且波形滑块也很挑剔。据我所知,问题出在我在点击时调用 osc 时的播放/暂停按钮。

这是代码:我的播放按钮:

$('#play_pause_osc1').click(function () {
    if ($(this).val() == "Play Osc1") {
        $(this).val("Pause");
        osc1();
    } else {
        $(this).val("Play Osc1");
        osc1.disconnect();
    }
});

第一次点击后我得到的错误是:

未捕获的类型错误:对象 [object Object] 的属性 'osc1' 不是函数

考虑到它们只是第一个重复的 osc,我也得到了其他 osc 的相同错误。

这是我的代码: http://jsfiddle.net/ryanhagz/YRsE7/1/

【问题讨论】:

    标签: javascript jquery html jquery-ui web-audio-api


    【解决方案1】:

    试试这个:- http://jsfiddle.net/adiioo7/YRsE7/3/

    您正在使用ocs1 作为函数以及正在创建冲突的振荡器对象。

    JS:-

    //WEB AUDIO SET UP//
    //used start web audio
    var ctx = new webkitAudioContext();
    speakers = ctx.destination;
    var osc1= ctx.createOscillator();
    var osc2= ctx.createOscillator();
    var osc3= ctx.createOscillator();
    
    $(document).ready(function () {
    
        //WAVEFORM OBJECTS - used to set the value of "cur_wave_osc" under Waveform sliders.//
        var wF = {
            0: "Sine",
            1: "Square",
            2: "Sawtooth",
            3: "Triangle"
        };
        //PLAY_PAUSE BUTTONS - used to play & pause the oscillators.//
        //OSC1 PLAY_PAUSE//
        $('#play_pause_osc1').click(function () {
            if ($(this).val() == "Play Osc1") {
                $(this).val("Pause");
                oscillator1Start();
            } else {
                $(this).val("Play Osc1");
                osc1.disconnect();
            }
        });
        //OSC2 PLAY_PAUSE//
        $('#play_pause_osc2').click(function () {
            if ($(this).val() == "Play Osc2") {
                $(this).val("Pause");
                oscillator2Start();
            } else {
                $(this).val("Play Osc2");
                osc2.disconnect();
            }
        });
        //OSC3 PLAY_PAUSE//
        $('#play_pause_osc3').click(function () {
            if ($(this).val() == "Play Osc3") {
                $(this).val("Pause");
                oscillator3Start();
            } else {
                $(this).val("Play Osc3");
                osc3.disconnect();
            }
        });
        //GAIN SLIDERS - used for controlling osc volume.//
        //OSC1_GAIN//
        $(function () {
            $("#osc1_vol").slider({
                min: 0,
                max: 1,
                value: 0.5,
                step: 0.01,
                slide: function (event, ui) {
                    $("#cur_vol_osc1").val(ui.value);
                    gainNode1.gain.value = $("#cur_vol_osc1").val();
                }
            });
            $("#cur_vol_osc1").val($("#osc1_vol").slider("value"));
        });
        //OSC2_GAIN//
        $(function () {
            $("#osc2_vol").slider({
                min: 0,
                max: 1,
                value: 0.5,
                step: 0.01,
                slide: function (event, ui) {
                    $("#cur_vol_osc2").val(ui.value);
                    gainNode2.gain.value = $("#cur_vol_osc2").val();
                }
            });
            $("#cur_vol_osc2").val($("#osc2_vol").slider("value"));
        });
        //OSC3_GAIN//
        $(function () {
            $("#osc3_vol").slider({
                min: 0,
                max: 1,
                value: 0.5,
                step: 0.01,
                slide: function (event, ui) {
                    $("#cur_vol_osc3").val(ui.value);
                    gainNode3.gain.value = $("#cur_vol_osc3").val();
                }
            });
            $("#cur_vol_osc3").val($("#osc3_vol").slider("value"));
        });
        //PITCH SLIDERS - used for controlling osc pitch.//
        //OSC1_PITCH//
        $(function () {
            $("#osc1_pitch").slider({
                min: 0,
                max: 25000,
                value: 440,
                step: 0.01,
                slide: function (event, ui) {
                    $("#cur_pitch_osc1").val(ui.value);
                    osc1.frequency.value = $("#cur_pitch_osc1").val();
                }
            });
            $("#cur_pitch_osc1").val($("#osc1_pitch").slider("value"));
        });
        //OSC2_PITCH//
        $(function () {
            $("#osc2_pitch").slider({
                min: 0,
                max: 25000,
                value: 440,
                step: 0.01,
                slide: function (event, ui) {
                    $("#cur_pitch_osc2").val(ui.value);
                    osc2.frequency.value = $("#cur_pitch_osc2").val();
                }
            });
            $("#cur_pitch_osc2").val($("#osc2_pitch").slider("value"));
        });
        //OSC3_PITCH//
        $(function () {
            $("#osc3_pitch").slider({
                min: 0,
                max: 25000,
                value: 440,
                step: 0.01,
                slide: function (event, ui) {
                    $("#cur_pitch_osc3").val(ui.value);
                    osc3.frequency.value = $("#cur_pitch_osc3").val();
                }
            });
            $("#cur_pitch_osc3").val($("#osc3_pitch").slider("value"));
        });
        //DETUNE SLIDER - used for controlling osc detune.//
        //OSC1_DETUNE//
        $(function () {
            $("#osc1_detune").slider({
                min: -4800,
                max: 4800,
                value: 0,
                step: 0.01,
                slide: function (event, ui) {
                    $("#cur_detune_osc1").val(ui.value);
                    osc1.detune.value = $("#cur_detune_osc1").val();
                }
            });
            $("#cur_detune_osc1").val($("#osc1_detune").slider("value"));
        });
        //OSC2_DETUNE//
        $(function () {
            $("#osc2_detune").slider({
                min: -4800,
                max: 4800,
                value: 0,
                step: 0.01,
                slide: function (event, ui) {
                    $("#cur_detune_osc2").val(ui.value);
                    osc2.detune.value = $("#cur_detune_osc2").val();
                }
            });
            $("#cur_detune_osc2").val($("#osc2_detune").slider("value"));
        });
        //OSC3_DETUNE//
        $(function () {
            $("#osc3_detune").slider({
                min: -4800,
                max: 4800,
                value: 0,
                step: 0.01,
                slide: function (event, ui) {
                    $("#cur_detune_osc3").val(ui.value);
                    osc3.detune.value = $("#cur_detune_osc3").val();
                }
            });
            $("#cur_detune_osc3").val($("#osc3_detune").slider("value"));
        });
        //WAVEFORM SLIDERS - used for selecting osc waveform.//
        //OSC1_WAVEFORM//
        $(function () {
            $("#osc1_wave").slider({
                min: 0,
                max: 3,
                value: 0,
                step: 0.01,
                slide: function (event, ui) {
                    $("#cur_wave_osc1").val(wF[ui.value]);
                }
            });
            $("#cur_wave_osc1").val("Sine");
            osc1.type = $("#osc1_wave").val();
        });
        //OSC2_WAVEFORM//
        $(function () {
            $("#osc2_wave").slider({
                min: 0,
                max: 3,
                value: 0,
                step: 0.01,
                slide: function (event, ui) {
                    $("#cur_wave_osc2").val(wF[ui.value]);
                }
            });
            $("#cur_wave_osc2").val("Sine");
            osc2.type = $("#osc2_wave").val();
        });
        //OSC3_WAVEFORM//
        $(function () {
            $("#osc3_wave").slider({
                min: 0,
                max: 3,
                value: 0,
                step: 0.01,
                slide: function (event, ui) {
                    $("#cur_wave_osc3").val(wF[ui.value]);
                }
            });
            $("#cur_wave_osc3").val("Sine");
            osc3.type = $("#osc3_wave").val();
        });
    });
    
    //CREATE OSCILLATORS//
    //OSC1//
    function oscillator1Start() {
        //creates the osc
        osc1 = ctx.createOscillator();
        //sets waveform
        osc1.type = $("#osc1_wave").slider("value"); //0 = sine, 1 = square, 2 = saw, 3 = triangle, 4 = custom
        //sets frequency
        osc1.frequency.value;
        //sets detune
        osc1.detune.value;
        //creates a gain node
        gainNode1 = ctx.createGainNode();
        //connects osc to gain node
        osc1.connect(gainNode1);
        //connects gain node to speakers
        gainNode1.connect(speakers);
        //sets gain value
        gainNode1.gain.value;
        //plays the osc
        osc1.start(0);
    }
    //OSC2//
    function oscillator2Start() {
        //creates the osc
        osc2 = ctx.createOscillator();
        //sets waveform
        osc2.type; //0 = sine, 1 = square, 2 = saw, 3 = triangle, 4 = custom
        //sets frequency
        osc2.frequency.value;
        //sets detune
        osc2.detune.value;
        //creates a gain node
        gainNode2 = ctx.createGainNode();
        //connects osc to gain node
        osc2.connect(gainNode2);
        //connects gain node to speakers
        gainNode2.connect(speakers);
        //sets gain value
        gainNode2.gain.value;
        //plays the osc
        osc2.start(0);
    }
    //OSC3//
    function oscillator3Start() {
        //creates the osc
        osc3 = ctx.createOscillator();
        //sets waveform
        osc3.type; //0 = sine, 1 = square, 2 = saw, 3 = triangle, 4 = custom
        //sets frequency
        osc3.frequency.value;
        //sets detune
        osc3.detune.value;
        //creates a gain node
        gainNode3 = ctx.createGainNode();
        //connects osc to gain node
        osc3.connect(gainNode3);
        //connects gain node to speakers
        gainNode3.connect(speakers);
        //sets gain value
        gainNode3.gain.value;
        //plays the osc
        osc3.start(0);
    }
    

    【讨论】:

    • 好人,谢谢。这解决了一个问题。我遇到的另一个问题是,如果你看一下小提琴,第二个和第三个波形滑块没有出现。它们与第一个有效的代码相同,而且它们的名称没有冲突,所以我不明白。
    • 你知道为什么我的另外两个波形滑块没有显示吗?
    • @ryanhagz - 这是因为这些滑块的步长值不正确。
    • 我也这么认为,但我在 15 分钟前修复了它,没有任何区别。所有步骤都已设置回 1。我已将小提琴更新为我现在拥有的代码:jsfiddle.net/ryanhagz/YRsE7/6
    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多