Hi, so I am trying to complete a project for one of my classes and I decided to us the script part of the analog discovery; however, there is not enough comments on the code to explain what it is specifically doing. I found an example code that is titled "Wavegenpiano". I would like to know if someone can explain what that code is doing., ie, what each part of the code is doing. I would like to use that code associated with a buzzer to output a song.


How can I use the script part of the analog discovery to have a buzzer output out a sound?

Share this post

Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Hi @josh1441

Here you have some more comments:

const notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
const octaveUp = 2; // how many octaves are reserved upward
const rhythm = 6; // 6 Hz notes/second 
const damping = 0.9; // 90 % damping factor during one pause step 

// Decodes notes and configures the Wavegen to play the given song using FM/AM (amplitude/frequency modulation).
// Expected signs: CC#DD#.. notes, space pause, vertical line 4 pauses, plus or minus change octave up or down. 
function play(text){
    if(!('Wavegen1' in this)) {
        throw("Please open a Wavegen instrument");
    var rgFM = []; // FM array
    var rgAM = []; // AM array
    var fm = 1; // frequency modulation
    var am = 1; // amplitude modulation, damping
    var octave = -octaveUp; // initial octave 
    var pause = 0;
    // process text input to fill AM/FM buffers 
    for(var i = 0; i < text.length; i++){
        if(pause>0){// earlier pause did not expire 
            am *= damping;  // damping by amplitude modulation
        }else { 
            var sign = text[i]; // current sign
                pause = 4; // pause of 4 rhythms
                octave = -octaveUp; // set initial octave
            }else if(sign=='+'){ 
                octave++; // change octave up, double frequency
            }else if(sign=='-'){ 
                octave--; // change octave down, half of frequency
            }else if(text[i+1]=='#'){ 
                sign += "#"; // sharp note
            // note index, see notes
            var note = notes.indexOf(sign);
            if(note<0) {
                // other signs (like space) are considered as one pause 
                am *= damping;  // damping by amplitude modulation
                // octave changes frequency by factor of two
                // there are 12 note steps between two octaves
                fm = pow(2, octave + (note-9)/12); 
                am = 1; // 100% 
        // normalize FM value (0..1) to -1..1 required by Wavegen
        if(fm>1) fm = 1; 
        // normalize AM value (0..1) to -1..1
        if(am>1) am = 1; 
    Wavegen1.Channel1.Mode.text = "Modulation";
    Wavegen1.Custom.set("FM", rgFM);
    Wavegen1.Custom.set("AM", rgAM);
    var hz = rhythm/rgFM.length; // modulation frequency is the song length
    Wavegen1.Channel1.Advanced.Carrier.Frequency.value = 440.0*pow(2, octaveUp);
    Wavegen1.Channel1.Advanced.FM.Type.text = "FM";
    Wavegen1.Channel1.Advanced.FM.Frequency.value = hz;
    Wavegen1.Channel1.Advanced.FM.Amplitude.value = 100;
    Wavegen1.Channel1.Advanced.AM.Type.text = "AM";
    Wavegen1.Channel1.Advanced.AM.Amplitude.value = 50;
    Wavegen1.Channel1.Advanced.AM.Offset.value = -50; // 0 to 100% output
    Wavegen1.Channel1.Advanced.AM.Frequency.value = hz;;

For instance the play("CC#DD#EFF#GG#AA#B++C-C-C-C      ") looks like this:i1.thumb.png.a41fe679eaf505c12922dd423098db27.png


Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Answer this question...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.