• Playing Piano Midi Notes on an Espruino Original Board

    Using the Espruino examples found here:

    Espruino Waveforms

    A recording of Middle C played on a piano was found here:

    Piano C4 Sample

    As per the example Audacity found here:

    was used to create an audio sample at 8 kHz rate in unsigned 8 bit format.

    The function get_freq(note) was constructed so that the playback rate of the sound sample can be adjusted to change the pitch of the note. The note is specified as a Midi note number.
    Middle C is Midi note number 60 (aka C4), C above middle C (aka C5) is midi note number 72.

    function get_freq(note){ //note is midi note
    var octave=Math.log(2);
    var halftone=octave/12;
    var tone=Math.exp(note*halftone);
    var freq=8000*tone; //C4 Middle C Midi note 60
    return freq;
    }// end get_freq

    The sample was converted to a string and coded into the program

    // 8 bit unsigned Piano C4 sampled at 8 kHz

    The chromatic scale is played by this code:

    var ww = new Waveform(Piano.length);//,{bits:16});
    analogWrite(A4, 0.5);
    // this event doesn't fire unless repeat is true
    ww.on('buffer',function(data) { 
      if (k>3) ww.stop();
    ww.on('finish',function(data) { console.log("F");//+data);
    if (note>72) note=60;//{
    ww.startOutput(Aout, get_freq(note));//,{repeat:true});


    Other instrument samples are widely available on the Web. (Orchestras)
    The tempo of the notes increases with pitch. How to solve this?
    With only 2 DACs, polyphony is limited. Using PWM can produce synthesized sounds.

    attached files

    Piano1 .js

    1 Attachment

  • This looks great! Do you have a recording of it in action anywhere?

    The tempo of the notes increases with pitch. How to solve this?

    You could have one sample per octave?

    With only 2 DACs, polyphony is limited. Using PWM can produce synthesized sounds.

    Waveform itself can do polyphony on one DAC channel! Give it a try - just start a few Waveforms on exactly the same output :)

    You also mentioned MIDI... I'd love to be able to import notes from a MIDI file into Espruino to play - it might help to make up for my lack of musical talent!

  • To create a Midi file using the computer keyboard or a USB (Piano like) keyboard and play it using the sound card. Reads and saves files as well. Records sound as wav file.
    Midi Editor

    To parse Midi at the byte level consult the Vogan poetry found here
    Midi Specification
    Midi Msg Summary

    or in human readable form convert a Midi file to text using:
    Midi to CSV and back

    a b c d e f g
    0 0 Header 1 7 384
    1 0 Start_track
    1 0 Text_t "Creator: GNU LilyPond "
    1 0 Text_t "Generated automatically by: GNU LilyPond "
    1 0 Text_t "at Mon Mar 6 20:16:27 2006 "
    1 0 Text_t "at Mon Mar 6 20:16:27 2006 "
    1 0 Title_t "Track 0"
    1 0 End_track
    2 0 Start_track
    2 0 Key_signature -1 "minor"
    2 0 Time_signature 2 1 18 8
    2 0 Title_t "\new"
    2 0 Tempo 500000
    2 6144 Note_on_c 0 69 127
    2 6912 Note_off_c 0 69 64
    2 6912 Note_on_c 0 74 127
    2 7680 Note_off_c 0 74 64

    One thought was to adapt the following to Espruino. It uses square, sine and triangle waves.
    Is there an easy way to get theses waveforms using PWM?

    Sonic Pi Mario Tune

    When using the DAC to play a sound sample it would be useful to be able to stop the sample output at some point mid stream. Is there a way to do this?

    As time allows I'll setup and record the sound from the code above and post it.

    Is there a synthesizer or sound chip that uses I2C or SPI that Espruino could control?

  • Here are links to sounds:



    Using PWM:


  • Thanks! I'll try the MIDI-CSV thing out - it'd be an easy way to test.

    There's no 'hardware' way of getting a saw wave/etc out of PWM - but that's why we have the Waveform stuff - it should be pretty easy to insert those types of wave into an ArrayBuffer for Waveform.

    You should be able to stop a waveform whenever you want - the only issue is if you get a 'glitch' if you haven't stopped the waveform at a zero crossing.

    I haven't come across a synth chip, but there must be one. I wouldn't be surprised if they took MIDI directly though :)

    If there's anything simple I can do to make Espruino better suited for this I'm up for adding it - I think this kind of functionality would be really handy, if only because I'm getting really tired of the same 3 songs on our baby's music box thing now :)

  • Hi @Gordon

    The following link has Ruby (sonic pi)code that takes a CSV Midi file as input and outputs CSV files for each Midi track as a Midi note number, note length, and velocity (amplitude).

    Playing Midi with Sonic Pi

    The conversion is a bit tricky as Midi CSV has column that contains a clock count ( Tempo cmds can alter the clock speed, followed by the Note On or Note off commands and the Velocity(Amplitude). A Note On with velocity zero is really a Note Off.
    Sonic Pi doesn't use a master clock just a note number and duration. Not a problem if there are no chords, but gets complicated when a Midi track contains chords, especially pyramid chords.

    Here is a sound using theses Midi translation techniques on a Sonic Pi platform:

    Widor Tocatta

    I'll give the waveform stop another try. I think you get an error message if you try to stop it before it finishes.

    As for synth or sound chips, Midi would be nice but I2C or SPIand Espruino would work as well.

    Adafruit has a bunch of Arduino Midi projects and kit as well.

    From the main Midi spec site, the Din plug goes to an optoisolator and some driver chips into a UART. My Midi Keyboard uses either Din or USB for connection. The USB driver shows a generic audio device driver. Some keyboards use Bluetooth as well. I intend to explore the HID requirements for this at some point. Of course more code to do the Midi in binary format would be needed. As a start, setup the HID for the Espruino USB, pair with the PC, then send some Midi phrases out the port and see if they show up in the Midi Editor program mentioned before. That could then expand into a button presses into the Espruino to send Midi canned commands ( a drum pad device)

  • @ClearMemory041063 cool, I want to play with MIDI thingies as well. after I finish the Siri integration first 😄

    as I don't want to "mess" with my Puck I will go from Puck to a web page via Web Bluetooth and then via Web MIDI to a device connected via USB. the latency will be "huge", but it is ok for start.

    ... maybe we can join forces on this journey

  • Hi @tbd. Lots of Midi possibilities. I'm just crawling around the basic Vogan poetry at this point, and will be glad to get USB Midi to connect with a Pico. Hopefully the HID info will transfer for a Bluetooth connection later on.

    Here are links I've glanced at today:
    Also looked at USB sniffer programs for a debug method.




  • USB MIDI might be too tricky to pull off on the Pico I'm afraid. I'm not sure you can customise the USB descriptors enough, but even if you could, you can only have the output endpoint (there isn't support for a USB input endpoint).

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

Playing Piano Midi Notes on an Original Espruino Board using the DAC

Posted by Avatar for ClearMemory041063 @ClearMemory041063