Making Pico Music in a fashion similar to Sonic PI

Posted on
  • There are a number of examples on this site for creating musical sounds. Thank you for the help in creating this post.
    When I bought a Raspberry Pi, I discovered the Sonic PI software that also runs on PC, Mac and Linux.
    Program uses a modified Ruby interpreter.

    Sonic PI

    I wondered if something like Sonic Pi could be implemented using Espruino, at least a small subset.
    Here is a first attempt:

    // 1 Nov 2017
    // Tested on a Pico
    // make music in a fashion similar to Sonic PI
    // Hardware
    // Connect an amplified speaker input to ground and the Speaker pin
    // as defined here
    var Speaker=A5; //A9;
    // use this to find a suitable pin
    Pico pins
    Suitable pins are:
    A0 A1 A2 A3 A5 A6 A7 A8 A9
    A10 A11 A15 B0 B1 B3 B4 B5 B6
    B7 B8 B9 B10 B13 B14 B15
    var Tempo =1.0; // changes tempo of the music
    // define some note lengths
    var Whole=1.0;
    var Half=Whole/2.0;
    var Quart=Half/2.0;
    var Eighth=Quart/2.0;
    // Use this table to write the song as a seried of Midi notes,
    // and durations
    // Tune is C4 Major scale C4 is middle C, Midi note 60
    var tune=[[60,Quart],
              [null,Half], // a musical rest
    var octave=Math.log(2);
    var halftone=octave/12;
    function freq(f) { 
      if (f===0) digitalWrite(Speaker,0);
      else analogWrite(Speaker, 0.5, { freq: f } );
    function soundoff(){
    function play(note){
      if(note===null)return; // a rest
    var tone=Math.exp(note*halftone);
    freq(261.63*tone); //C4 Middle C Midi note 60
    }//end play
    function wait(a,c){
    var b=getTime();
    if (b>=(a+c)) return b;
    return a;
    var a;
    var b;
    var i=0;
    setInterval(function () {
      if(i>=tune.length) clearInterval();
    }, 10);

    1 Attachment

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Making Pico Music in a fashion similar to Sonic PI

Posted by Avatar for ClearMemory041063 @ClearMemory041063