node.js ã§é³ãåºã
node.js ã§ãä¿¡å·å¦çç°¡åã¤ã³ã¿ã¼ãã§ã¤ã¹ pico.js ã使ããããã«ãªãã¾ããã ãã¨ãã¨Macçã®ã¿å¯¾å¿ãã¦ããã®ã ãã©ã便å©ãªã¢ã¸ã¥ã¼ã«ãæãã¦ããã£ã¦ã¯ãã¹ãã©ãããã©ã¼ã 対å¿ã§ãã(Macã¨Ubuntuã§ãã確èªãã¦ããªããã©)ãnpmã§ã¤ã³ã¹ãã¼ã«ã§ãã¾ãã
$ npm install node-pico
ãã©ã¦ã¶ã§é³ãåºãããã°ã©ã ãæ¸ãã¨ãã«ãã³ã¼ããæ¸ãã¦ãã©ã¦ã¶ã«ç§»åãã¦åèªã¿è¾¼ã¿ãã¦ãã¿ã³ãæ¼ããã¿ãããªããæ¹ããã¦ãã¦ã ããã£ãã®ã ãã©ãããªã楽ã«ãªã£ãã
node-dev ã¯ã³ã¼ããæ´æ°ããããã³ã«åèµ·åãã¦ãããã®ã§ãã¨ãã£ã¿ã§ã³ã¼ããä¿®æ£ãã¦ä¿åãããã³ã«èªåã§åçãå§ã¾ããåçãå§ã¾ãã¨å°ãã¨ãã¯ã module.exports ãã³ã¡ã³ãã¢ã¦ããããsong.js ã¯ãã©ã¦ã¶ã§ã使ããããã«ãã¦ããã¨å¾ã ã楽ã§è¯ãã
ä¸ã®ã³ã¼ãã«ã¯æ¸ãã¦ããªããã©ãå¿ ç¶çã«UIã¨é³æºãåé¢ãããã®ãè¯ãã¨æãã
$ node-dev main.js
// main.js var pico = require("node-pico"); var song = require("./song"); if (typeof song === "function") pico.play(song());
// song.js (function() { "use strict"; var f1 = function(t) { return Math.sin(t*(0.001+Math.sin(t>>10)))*64; }; var f2 = function(t) { return (t>>9)&((t<<5)|(Math.sin(t*1.4142)*3000))+(t>>3); }; function oneliner() { var t = 0, dt = 8000 / pico.samplerate; return { process: function(L, R) { for (var i = 0; i < L.length; i++) { L[i] = (f1(t|0) % 256) / 512; R[i] = (f2(t|0) % 256) / 512; t += dt; } } }; } var exports = function() { return oneliner(); }; if (typeof module !== "undefined" && module.exports) { module.exports = exports; } else { window.song = exports; } })();
// browser $("#play").on("click", function() { pico.play(song()); });