Waveform music (multiple notes)

Posted on
  • Hi, I was just messing around, and realised I'd added the ability to play more than one Waveform at once on the same output a while back, but had never shown it in use.

    So...

    var pitches = {
      'A':440.00, 'B':493.88, 'C':523.25, 'D':587.33,
      'E':659.26, 'F':698.46, 'G':783.99, 'a':880
    };
    var tune = "E BCD CBA ACE DCB  CD E C A A    D Fa GFE  CE DCB BCD E C A A   ";
    
    var s = atob("f4GBgoODh5GtnmgkE1m435g/MobgzYJbd4­1YHkHD/7UoEZPysyscl/K5PSqLy5tLToqdfHGdrX­I1V7vUeiVIqceEUG2kmWVfj6qIWFuKpZFpXXiXkn­NthZB9cH6LgG5vgJKSdWF7oZVoXHuRioJ8c3iJjH­tweomLf3JygpCHdHOCiYJ8fYCBg4ODgn53d4CGiI­Z8cHGAjo1+dn6Jh3pzeoWHfXd8hYd8d3+Gg3t1e4­F9d36KiHp0fYZ+cneLlol3cnyEgXt8g4WBeXV8ho­qDdnOCjoNxc4aRhnd3f4J6cXmNlIJxd4mKeXJ5iI­2Denh9fX1/f4B/g4N2bnuSlYJzdoOIgXp7g4V+dn­mDhoF9gIF+eHqDioJyb3+Oi3xzeIOHgXp7gIB+fH­+DgHt8hIh/cnOEjoVzb32Mi3pxeYeLf3N2hIqBdX­aBhoB7fYSGfXZ7hYeAeXl9goJ9e36BgYGCgoF8e4­GDgHp6f4ODgHo=");
    var w = new Waveform(s.length);
    var w2 = new Waveform(s.length);
    var w3 = new Waveform(s.length);
    w.buffer.set(s);
    w2.buffer.set(s);
    w3.buffer.set(s);
    s=undefined;
    
    function step() {
      var ch = tune[pos];
      if (ch !== undefined) pos++; else pos=0;
      if (ch in pitches) {
        if (!w.running)
          w.startOutput(B0, 4000*pitches[ch]/2500);
        else if (!w2.running)
          w2.startOutput(B0, 4000*pitches[ch]/2500);
        else if (!w3.running)
          w3.startOutput(B0, 4000*pitches[ch]/2500);
      }
    }
    
    
    var pos=0;
    function onInit() {
      pinMode(B0, "af_opendrain");
      analogWrite(B0, 0.5, {freq:20000});
      setInterval(step, 150);
    }
    

    Tetris music - not hi-fi quality, but it sounds pretty old-school :)

    You might want to change pinMode(B0, "af_opendrain"); if you're not using a Pico (it was running on the FET output to get a bit of volume).

    It'd be great if someone wanted to add another track so it actually was properly polyphonic. At the moment it's just playing overlapping waveforms (rather than actual different notes).

  • Actually just any multi-track music would be great. You could do something like this:

    var track1 = "...";
    var track2 = "...";
    
    function note(ch) {  
      if (ch in pitches) {
        var f = 4000*pitches[ch]/1500;
        if (!w.running)
          w.startOutput(B0, f);
        else if (!w2.running)
          w2.startOutput(B0, f);
        else if (!w3.running)
          w3.startOutput(B0, f);
        else if (!w4.running)
          w4.startOutput(B0, f);
      }
    }
    
    function step() {
      var ch1 = track1[pos];
      var ch2 = track2[pos];
      if (ch1 !== undefined) pos++; else pos=0;
      note(ch1);
      note(ch2);
    }
    

    As you might have guessed by now I'm not very musical :)

  • Hi,
    The video here kickstarter.com/projects/gfw­/puckjs-the-ground-breaking-bluetooth-be­acon between about 35 secs and 45 secs shows that when user press puck button music is played on user android phone.

    So, could you please help me to achieve the similar functionality.

    Regards

  • See here: espruino.com/Puck.js+Keyboard­

    var controls = require("ble_hid_controls");
    NRF.setServices(undefined, { hid : controls.report });
    setWatch(function() {
      // Play/stop music
      controls.playpause();
    }, BTN, {repeat:true,edge:"rising",debounce:50})­;
    

    Should do it.

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

Waveform music (multiple notes)

Posted by Avatar for Gordon @Gordon

Actions