Is there a way to select a web audio node with a variable in place of the nodes name?












1















I have a bank of 5 oscillator nodes and want to be able to select the waveshape of specific nodes. All oscillators hare names osc1, osc2, osc3 etc. and I want to be able to select which one I'm changing with the elements id (1, 2, 3 etc.)



This is what I thought would work:



var oscNum = ("osc"+eleNum)

if (element.value == 1)
{
oscNum.type = 'square';
}

else if (element.value == 2)
{
oscNum.type = "sawtooth";
}

else if (element.value == 3)
{
oscNum.type = "triangle";
}

else if (element.value == 0)
{
oscNum.type = "sine";
}


However it doesn't and I'm wondering if there's any way it's possible to use var.type instead of osc1.type to select a node.










share|improve this question



























    1















    I have a bank of 5 oscillator nodes and want to be able to select the waveshape of specific nodes. All oscillators hare names osc1, osc2, osc3 etc. and I want to be able to select which one I'm changing with the elements id (1, 2, 3 etc.)



    This is what I thought would work:



    var oscNum = ("osc"+eleNum)

    if (element.value == 1)
    {
    oscNum.type = 'square';
    }

    else if (element.value == 2)
    {
    oscNum.type = "sawtooth";
    }

    else if (element.value == 3)
    {
    oscNum.type = "triangle";
    }

    else if (element.value == 0)
    {
    oscNum.type = "sine";
    }


    However it doesn't and I'm wondering if there's any way it's possible to use var.type instead of osc1.type to select a node.










    share|improve this question

























      1












      1








      1








      I have a bank of 5 oscillator nodes and want to be able to select the waveshape of specific nodes. All oscillators hare names osc1, osc2, osc3 etc. and I want to be able to select which one I'm changing with the elements id (1, 2, 3 etc.)



      This is what I thought would work:



      var oscNum = ("osc"+eleNum)

      if (element.value == 1)
      {
      oscNum.type = 'square';
      }

      else if (element.value == 2)
      {
      oscNum.type = "sawtooth";
      }

      else if (element.value == 3)
      {
      oscNum.type = "triangle";
      }

      else if (element.value == 0)
      {
      oscNum.type = "sine";
      }


      However it doesn't and I'm wondering if there's any way it's possible to use var.type instead of osc1.type to select a node.










      share|improve this question














      I have a bank of 5 oscillator nodes and want to be able to select the waveshape of specific nodes. All oscillators hare names osc1, osc2, osc3 etc. and I want to be able to select which one I'm changing with the elements id (1, 2, 3 etc.)



      This is what I thought would work:



      var oscNum = ("osc"+eleNum)

      if (element.value == 1)
      {
      oscNum.type = 'square';
      }

      else if (element.value == 2)
      {
      oscNum.type = "sawtooth";
      }

      else if (element.value == 3)
      {
      oscNum.type = "triangle";
      }

      else if (element.value == 0)
      {
      oscNum.type = "sine";
      }


      However it doesn't and I'm wondering if there's any way it's possible to use var.type instead of osc1.type to select a node.







      javascript html5 web-audio web-audio-api






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 19 at 16:21









      Custard320Custard320

      205




      205
























          1 Answer
          1






          active

          oldest

          votes


















          1














          In your example the first line of code is computing a string. Let's say that eleNum equals 3. Then the value of oscNum would be just the string "osc3".



          If you want to select your oscillators by their index you can for example use an array to store them.



          const oscillators = [ osc0, osc1, osc2, osc3 ];


          Later on you can select an individual oscillator by its index and set the type.



          oscillators[eleNum].type = "triangle";





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54269063%2fis-there-a-way-to-select-a-web-audio-node-with-a-variable-in-place-of-the-nodes%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            In your example the first line of code is computing a string. Let's say that eleNum equals 3. Then the value of oscNum would be just the string "osc3".



            If you want to select your oscillators by their index you can for example use an array to store them.



            const oscillators = [ osc0, osc1, osc2, osc3 ];


            Later on you can select an individual oscillator by its index and set the type.



            oscillators[eleNum].type = "triangle";





            share|improve this answer




























              1














              In your example the first line of code is computing a string. Let's say that eleNum equals 3. Then the value of oscNum would be just the string "osc3".



              If you want to select your oscillators by their index you can for example use an array to store them.



              const oscillators = [ osc0, osc1, osc2, osc3 ];


              Later on you can select an individual oscillator by its index and set the type.



              oscillators[eleNum].type = "triangle";





              share|improve this answer


























                1












                1








                1







                In your example the first line of code is computing a string. Let's say that eleNum equals 3. Then the value of oscNum would be just the string "osc3".



                If you want to select your oscillators by their index you can for example use an array to store them.



                const oscillators = [ osc0, osc1, osc2, osc3 ];


                Later on you can select an individual oscillator by its index and set the type.



                oscillators[eleNum].type = "triangle";





                share|improve this answer













                In your example the first line of code is computing a string. Let's say that eleNum equals 3. Then the value of oscNum would be just the string "osc3".



                If you want to select your oscillators by their index you can for example use an array to store them.



                const oscillators = [ osc0, osc1, osc2, osc3 ];


                Later on you can select an individual oscillator by its index and set the type.



                oscillators[eleNum].type = "triangle";






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 19 at 17:14









                chrisguttandinchrisguttandin

                1313




                1313






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54269063%2fis-there-a-way-to-select-a-web-audio-node-with-a-variable-in-place-of-the-nodes%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Liquibase includeAll doesn't find base path

                    How to use setInterval in EJS file?

                    Petrus Granier-Deferre