Play musical note by Javascript

Asked

Viewed 672 times

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 now, I found this library, I couldn’t understand enough to adapt to my code.

1 answer

5


The API you found does 99% of the work. If you’re not going to use it, you’ll either use another API, or you’ll 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();

A few 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 don’t understand what time and time are (in English, team Signature), you simply won’t be able to do what you want to do satisfactorily.

Note also that your note example does not include the octave nor the duration of each note - if you do not inform them, by default the library will assume that all notes are semibrief and the fourth octave. In other words, your musical excerpt will sound stale depending on how the library will interpret. For example, your notes start with two lás. If that was the intention, fine, otherwise... Unfortunately the library does not support short notation, but you can reduce the time by half (or change the bar) and then mark all other notes as minimal.

  • For each API file, it appears in the Console: "Uncaught Referenceerror: Module is not defined". Why does this happen? And the music doesn’t play either...

  • @lucaswmolin If you do not load the files correctly, it is obvious that you will not touch... As for the error, it is of inclusion. You can open a new question about this, including your HTML code, so we can help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.