Skip to content

Instantly share code, notes, and snippets.

View gliese1337's full-sized avatar

Logan Kearsley gliese1337

View GitHub Profile
import beamcoder from 'beamcoder';
import { Readable, Writable } from 'stream';
async function * transform(demuxer: beamcoder.Demuxer) {
const vdec = beamcoder.decoder({ name: 'h264' });
const adec = beamcoder.decoder({ name: 'aac' });
const venc = beamcoder.encoder({ name: 'h264' });
const aenc = beamcoder.encoder({ name: 'aac' });
@gliese1337
gliese1337 / Multimethod.js
Last active January 12, 2018 02:51
A JavaScript multimethod library.
const clauses = new Symbol();
function Multimethod(){
const self = !!new.target ? this : Object.create(Multimethod.prototype);
self[clauses] = [];
return new Proxy(self,{
apply: function(target, thisArg, args){
let curImpl = () => throw new Error("No Matching Implementation");
let curLen = -1;
for(const { thisPredicate, predicates, impl } of self[clauses]){
@gliese1337
gliese1337 / TM.js
Last active January 5, 2016 09:08
Basic Turing Machine simulator in JS
function TM(tape, alphabet, states, start){
var state, transition, symbol,
counter = 0, stateId = start, pos = 0;
while(true){
symbol = tape[pos];
if(typeof symbol === 'undefined'){
symbol = alphabet[0];
tape[pos] = symbol;
}
state = states[stateId];
@gliese1337
gliese1337 / earley.js
Last active November 20, 2015 00:55
Incremental, Back-Trackable Earley Recognizer for CFGs
var Earley = (function(){
function Grammar(rules) {
this.symbols = {};
rules.forEach(function(rule) {
// "A -> B C | D" -> ["A ", " B C | D"]
var parts = rule.split('->');
var lhs = parts[0].trim();
var rhss = parts[1].trim();
// "B C | D" -> ["B C", "D"]
@gliese1337
gliese1337 / FastFib.js
Created January 1, 2014 21:49
Fast Fibonacci Implementations
/* Looping version */
function fib(n){
var fk = 0, fkp = 1,
f2k, f2kp, b;
for(b = Math.floor(Math.log(n)/Math.LN2); b > 0; b--){
f2k = fk*(2*fkp-fk);
f2kp = fkp*fkp+fk*fk;
if(n&(1<<b)){
fk = f2kp;
@gliese1337
gliese1337 / submsNow.js
Created August 26, 2012 16:07 — forked from jeromeetienne/submsNow.js
a submillisecond version of Date.now() based on based on window.performance.now()
/**
* precise version of Date.now() -
* It provide submillisecond precision based on window.performance.now() when
* available, fall back on Date.now()
* see http://updates.html5rocks.com/2012/05/requestAnimationFrame-API-now-with-sub-millisecond-precision
*/
var nowSubms = (function(){
var p = window.performance || {};
if( p.now ) return function(){ return p.timing.navigationStart + p.now(); };
else if( p.mozNow ) return function(){ return p.timing.navigationStart + p.mozNow(); };
@gliese1337
gliese1337 / wavaudio.js
Created October 19, 2011 19:15
AudioData polyfill based on AudioLib
Audio = (function(){
return {
sampleRate:44100,
channelCount:1,
WriteAudio:function(soundData){
new Audio( 'data:audio/wav;base64,'+btoa(
audioLib.PCMData.encode({
data: soundData,
sampleRate: this.sampleRate,
channelCount: this.channelCount,
@gliese1337
gliese1337 / WebVTTParser.js
Created August 16, 2011 20:16
Non-incremental parser for WebVTT files.
/*
http://www.whatwg.org/specs/web-apps/current-work/webvtt.html
*/
function parseWebVTT(input){
"use strict";
var line,l,p,cue_list=[],
cue,cue_text,id,fields,
time_pat = /\s*(\d*:?[0-5]\d:[0-5]\d\.\d\d\d)\s*-->\s*(\d*:?[0-5]\d:[0-5]\d\.\d\d\d)\s*(.*)/;
//If the first character is a BYTE ORDER MARK (BOM) character, advance position to the next character in input.
@gliese1337
gliese1337 / StreamAccumulator.js
Created June 24, 2011 00:01
Accumulate the output of a stream into a string.
/*
* No sentinel results in reading the entire stream until it ends, a
* numeric sentinel reads fixed length chunks, and a string sentinel
* reads chunks bounded by the sentinel string
*/
exports.accumulate = function(stream,opts,callback){
stream.pause();
if(!callback){
callback = opts;
opts = {encoding:'utf8'};
@gliese1337
gliese1337 / MemoryStream.js
Created June 23, 2011 23:54
Wrapper to make a string or Buffer look like a Stream object.
function emit_string(obj,data,encoding){
obj.emit('data',(data instanceof Buffer
?data.toString(encoding)
:data[0])
);
}
function emit_buffer(obj,data){
obj.emit('data',(data instanceof Buffer
?data:new Buffer(data[0],data[1]))