Jump to content
  • 0

Analog Discovery 2 - Javascript


josh1441

Question

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.

TL;DR

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

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

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 
            pause--;
            i--;
            am *= damping;  // damping by amplitude modulation
        }else { 
            var sign = text[i]; // current sign
            if(sign=='|'){
                pause = 4; // pause of 4 rhythms
                octave = -octaveUp; // set initial octave
            }else if(sign=='+'){ 
                octave++; // change octave up, double frequency
                continue;
            }else if(sign=='-'){ 
                octave--; // change octave down, half of frequency
                continue;
            }else if(text[i+1]=='#'){ 
                sign += "#"; // sharp note
                i++;
            }
            // 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
            }else{
                // 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; 
        rgFM.push(2.0*fm-1);
        // normalize AM value (0..1) to -1..1
        if(am>1) am = 1; 
        rgAM.push(2.0*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;
    Wavegen1.run();
}

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

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...