Is there a way to select a web audio node with a variable in place of the nodes name?
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
add a comment |
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
add a comment |
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
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
javascript html5 web-audio web-audio-api
asked Jan 19 at 16:21
Custard320Custard320
205
205
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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";
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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";
add a comment |
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";
add a comment |
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";
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";
answered Jan 19 at 17:14
chrisguttandinchrisguttandin
1313
1313
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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