pico.js

日本語

INTRODUCTION

What is?

Pico.js is a JavaScript library for real-time audio processing that runs a browser and node.js.

Support

USAGE

Tutorial

  1. Make an object implemented a function process(L, R)

    Arguments L and R are instanceof Float32Array

  2. Start processing: pico.play(gen);

    Write signal into L and R in gen.process(L, R)

  3. Stop processing: pico.pause();

    The silence come

ex.01

// Generate whitenoise
var noise = {
    process: function(L, R) {
        for (var i = 0; i < L.length; i++) {
            L[i] = R[i] = Math.random() * 0.25;
        }
    }
};
pico.play(noise);

Others features

  1. Change the samplerate

    pico.setup({samplerate:24000})

    You can choose some one [8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000]

  2. Flash fallback

    Put in the 'pico.swf' to in the same folder as 'pico.js'

  3. node.js support

    You can install with npm.

    $ npm install node-pico

    See: repository demo/node-demo.js

ex.02

// Generate sinetone
function sinetone(freq) {
    var phase = 0,
        phaseStep = freq / pico.samplerate;
    return {
        process: function(L, R) {
            for (var i = 0; i < L.length; i++) {
                L[i] = R[i] = Math.sin(6.28318 * phase) * 0.25;
                phase += phaseStep;
            }
        }
    };
}
pico.play(sinetone(880));

REFERENCE

Methods

  1. pico.play(gen)

    Start processing

  2. pico.pause()

    Stop processing

  3. pico.setup(opts)

    Setup samplerate and cellsize

  4. pico.bind(PlayerClass, opts)

    Use another PlayerClass (for example via Flash)

Properties

  • pico.isPlaying

    Return true if processing

  • pico.env

    Return environment (webkit, moz, flash, nop=no operation)

  • pico.samplerate

    Return samplerate

  • pico.channels

    Return channels (always return 2)

  • pico.cellsize

    Return length of samples to be processed

ex.03

// Generate music from one-liner function
window.f1 = function(t) {
    return Math.sin(t*(0.001+Math.sin(t>>10)))*64;
};
window.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;
    var dlyL = new pico.DelayNode({time:225, feedback:0.8});
    var dlyR = new pico.DelayNode({time:225, feedback:0.8});
    return {
        process: function(L, R) {
            for (var i = 0; i < L.length; i++) {
                L[i] = (window.f1(t|0) % 256) / 512;
                R[i] = (window.f2(t|0) % 256) / 512;
                t += dt;
            }
            dlyL.process(L, true);
            dlyR.process(R, true);
        }
    };
}
pico.play(oneliner());

OTHERS

Source

License

  • MIT
このエントリーをはてなブックマークに追加

ex.04

// Demo song of MML and FM synthesis
// TO MAKE THE END OF BATTLE/Ys2
//     Copyright© Nihon Falcom Corporation
// 
// Original MML Data
// http://d.hatena.ne.jp/mosshm/20071013/p1

pico.play(demo());