Selecting One of Two Global Variables within a Procedure?

I wanted to create a project to generate and combine tones:

I approached this by generating a table with a specified frequency and length for a tone. I then assign the tone generated to one of two tones: Tone 1 and Tone 2. The two tones generated can then be combined.

I haven't followed or understood all of the discussion about appropriate ways to use (or not to use variables), but it would appear that the method that that I have used to assign the tone generated to "Tone 1" or "Tone 2" is not the preferred way. I'm happy to do this in a different way if someone will explain how to do this in the preferred manner.


BTW, I've been using this method since it was first demonstrated in Logo. However, if Snap! has new, better affordances, I'm happy to adopt them.

Can you provide a link to your project please?

Here's a link to the project:

What is it your to trying to achieve?

append the two list of samples into one long one or produce a mix of the two?

Why the 12000 sample rate BTW?

I want to produce a mix of the two tones.

The playback rate of 12000 Hz was chosen for convenience. A 1000 Hz tone with 12 samples per cycle requires a playback rate of 12000 Hz. Once that value is arbitrarily chosen, a 100 Hz tone will have 120 samples per cycle. I wanted a result that sounds reasonably good and provides sufficient resolution in the graph.


I wanted to reconstruct a utility, SoundScope, that we originally created in JavaScript:

Sure, just make a list with the two tones (or any number of tones, an extension you get for free!) and use the tone number as an index into the list.

I can create a list of tones. But then how do I use "Item Number of Tone List" to assign a frequency table to one of the two variables (Tone 1 or Tone 2)?


List of list does not fit your needs?
untitled script pic - 2022-01-08T201558.753

Used as a list item
untitled script pic - 2022-01-08T202022.699

untitled script pic - 2022-01-08T202108.469
Stage (25)

That seems to be redundant.
untitled script pic - 2022-01-08T203400.492
untitled script pic - 2022-01-08T203252.784

It is not that the list of lists does not meet my needs; it is just that I don't know how to use it to assign a list of numbers (i.e., the frequency table) to one of two variables. ... If someone could provide an example demonstrating how this is accomplished, I would do it that way.

P.S. I initialize the variable by setting to a list because an error is generated if I try to assign a list of numbers to a variable that is not a list. Once it is initialized the first time, it is redundant, so I could probably remove it.

PS If you insist on a pulldown input, put

tone 1=1
tone 2=2

in the input options.

OK ... I see the general strategy now. (Thanks!)

I'm off by one level of abstraction. I would actually like the variable "Tone 1" (formerly containing the list [a b]) to be replaced with the list [f g]. However, Item 1 of Tone List is actually replaced with [f g], not the variable Tone List. (It seems to have done what I told it to do, not what I wanted it to do.) Is there a way to shift the pointer so that it is directed at Tone 1, and not Item 1 of Tone List?

Just delete the variables Tone 1 and Tone 2, and stop thinking in those terms! (If you want to display JOIN [Tone] TONE-NUMBER to the user, go for it.) The one variable TONE LIST should be the only repository of tones. You can write a selector
if you want.

Finally! ... Got it. (and Thank You!)

I revised the tone generator and placed each waveform in a tone repository table as suggested. This worked well. I appreciate the suggestions and help.

This raised a question regarding documentation. When I used the variable name "Tone 1" for Tone 1 and "Tone 2" for Tone 2, the name of the variable provided guidance regarding the contents. When a table is used in place of individual variables, the contents of each item in the table are not obvious. I could add a header at the beginning of each line in the table, but then the code would become more convoluted. Any suggestions regarding the best way to indicate to a user that the first item in the table is "Tone 1", the second item is "Tone 2", etc.? (Thanks)


I should think that would be pretty obvious! But feel free to make a block
untitled script pic
I made it Variables color so that untitled script pic (1) will look like untitled script pic (2).

Our students (pre-service teachers with no prior coding experience) generally find lists to be intuitive. Lists of lists seem less intuitive. For someone with prior experience, it may be apparent that the first item is Tone 1, the second item is Tone 2, etc. That may not necessarily be the case for a novice.


Therefore we were considering the possibility of adding a header at the beginning of each item to make the contents more apparent.


Oh I see, the "1+2" row makes what you're saying much more sensible than it would be if (as I was imagining) row 3 would be Tone 3!

The capability of hyperblocks to add two lists is fantastic! I have a colleague who is a math educator. He believes that it is useful for teachers to understand that, in his words, "A function can be thought of not only as a process, but also as an object that can be multiplied by another object."

Joe used to introduce the concept by asking students to multiply one function by another by hand; this could take most of a class period. Hyperblocks can do this in a single click ... and, in the case of tones, you can hear the result. (The illustration below involves addition, but Joe immediately used the same capability to multiply two tones, etc.)


So the next step is FM synthesis!