Play musical note through JavaScript

5

I need to create a code that reproduces the sound according to the musical notes informed ...

function music(){
     var partitura = '';
     var escala = 'CDEFGABC';
     var notas = escala.split('');
     for (var i = 0; i < notas.length; i++){
         reproduzir(notas[i]);
     }
}

For the time being, I found this library , which I could not understand enough to fit into my code.

    
asked by anonymous 02.01.2018 / 15:06

1 answer

6

The API you found does 99% of the work. If you are not going to use it, either you will use another API, or you will have to reimplement it anyway.

To use your notes with the API, you would do the following:

// importe a biblioteca
let player = new Retro.Player();
let minhaSingelaCancao = JSON.stringify({
    title : "mi-mi-re-do",
    tempo : 60,
    time_signature : "4/4",
    score : [{
        instrument : "oscillator-square",
        volume : 0.5,
        sheet : [ "E.4", "E.4", "D.4", "C.4", "C.4", "D.4", "E.4", "F.4", "G.4", "G.4", "F.4", "E.4", "E.4", "D.4", "D.4" ]
    }]
});

player.load(mysong);
player.play();

Some comments: If you want to play music that way, you will have to learn a minimum of notation and musical theory. For example, if you do not understand time and time, you simply will not be able to do what you want to do satisfactorily.

Also note that your sample notes do not include the octave or the length of each note - if you do not inform them, by default the library will assume that all notes are semibreves and the fourth octave. In other words, your musical piece will sound staccato depending on how the library interprets. For example, your grades begin with two lás . If the intention was this, beauty, otherwise ... Unfortunately the library does not support notation soon, but you can reduce the time in half (or change the measure) and then mark all other notes as minimal.

    
02.01.2018 / 15:29