Skip to content

Instantly share code, notes, and snippets.

@nesrak1
Created August 13, 2019 16:18
Show Gist options
  • Select an option

  • Save nesrak1/660c125004b46373843838cf1b22370c to your computer and use it in GitHub Desktop.

Select an option

Save nesrak1/660c125004b46373843838cf1b22370c to your computer and use it in GitHub Desktop.
var snd = {
//buffer
dat: "",
//playback
pos: 0,
stop: 0,
//song data
tickWait: 120, //ms between each tick
startLoop: 0, //start of loop
endLoop: 1, //end of loop
tpm: 16, //ticks per measure
instruments: [], //which sound is played for each instrument
//buffers: [],
notes: [], //notes with pitches and values
};
function readSong(data) {
snd.dat = data;
snd.pos = 0; //also used as playback pos
snd.tickWait = readNum(3);
snd.startLoop = readNum(2);
snd.endLoop = readNum(3);
snd.tpm = readNum(2);
var instCount = readNum(1);
for (var _ of loop(instCount)) {
snd.instruments.push(readNum(1));
//snd.buffers.push(A.createBuffer(2,1e6,44100));
}
for (var instIdx of loop(instCount)) {
var noteCount = readNum(3);
var curNotes = [];
var i = 0;
var curX = 0;
if (snd.instruments[instIdx] == 0) { //note
var curY = readNum(3);
while (i < noteCount) {
var mode = readNum(1);
//0 = rest 2len
//1 = rest 1len
//2 = 1off up 2len
//3 = 1off down 2len
//4 = 2off up 2len
//5 = 2off down 2len
//6 = 1off up 1len
//7 = 1off down 1len
//8 = 2off up 1len
//9 = 2off down 1len
var offLen = [0,0,1,1,2,2,1,1,2,2][mode];
var lenLen = [2,1,2,2,2,2,1,1,1,1][mode];
var len = readNum(lenLen);
if (offLen != 0) {
var off = readNum(offLen) * [1,-1][mode%2];
curY += off;
curNotes[curX] = [curY,len];
i++;
}
curX += len;
}
} else { //drum
while (i < noteCount) {
var mode = readNum(1);
var dist;
if (mode <= 7) { //regular distance
dist = mode;
} else if (mode == 8) {
dist = readNum(1) + 8;
} else if (mode == 9) {
dist = readNum(2) + 18;
}
curX += dist;
curNotes[curX] = 1;
i++;
}
}
snd.notes.push(curNotes);
}
}
function playSong() {
snd.pos = 0;
var id = setInterval(function() {
snd.notes.forEach(function(i, idx) {
var j = i[snd.pos];
if (j !== undefined) {
if (snd.instruments[idx] == 0) {
playPiano(Math.pow(1.05946309436, j[0] - 45) * 440, j[1] * snd.tickWait / 1000 + 0.2/*, snd.buffers[idx]*/);
} else {
playDrum();
}
}
});
snd.pos++;
if (snd.pos > snd.endLoop * snd.tpm) {
snd.pos = snd.startLoop * snd.tpm;
//clearInterval(id);
}
}, snd.tickWait);
}
function readNum(len) {
return parseInt(read(len));
}
function read(len) {
var tex = snd.dat.substr(snd.pos, len);
snd.pos += len;
return tex;
}
function loop(i) {
return [...Array(i).keys()];
}
var A = new AudioContext();
var b = (i,s,f,x) => Math.sin((6.28)*(i/s)*f+x);
var w = (i,frequency) => Math.sin(6.28*((i/44100)*frequency)+b(i,44100,frequency,0)**2+(.75*b(i,44100,frequency,.25))+(.1*b(i,44100,frequency,.5)));
function playPiano(frequency, time/*, m*/) {
var D = [];
var t = 44100 * time;
for (var i = 0; i < t; i++){
D[i] = i < 88
? (i / (44100 * .002)) * w(i,frequency)
: (1-((i-(44100*.002))/(44100*(time-.002))))**((.5*Math.log((frequency*1e4)/44100))**2) * w(i,frequency);
}
playSample(D, t+1000);
}
function playDrum() {
D = [];
var t = 10400;
for (var i = 0; i < t; i++) {
D[i] = Math.sin(i*(-1/12300*i+1.9)/(t/(2*Math.PI))*11)*(-1/t*i+1)*0.8;
}
playSample(D, t);
}
function playSample(samp, samps) {
var m=A.createBuffer(2,samps,44100);
m.getChannelData(0).set(samp);
m.getChannelData(1).set(samp);
var s=A.createBufferSource();
s.buffer=m;
s.connect(A.destination);
s.start();
setTimeout(function() {
s.stop();
s.disconnect();
s = null;
}, samps/44100 + 600);
}
function drum(x) {
//var noise = Math.sin(1/(((x%0.54)+0.01)/10000))/1000;
var pitch = ((-1/12300)*x+1.9);
var main = Math.sin(((x*pitch)/(10400/(2*Math.PI)))*11);
var volume = (-1/10400)*x+1;
return (main*volume)*0.8;
}
readSong("092040612450000146403273063773763773763673663673663573563573563473462271273063773763773763673663673663573563563573173213931263763573573763763573593116377376377356377376377356376357357376376357359311637737637735637737631631637635735737637635735931163773763773563773763773563763573573763763573593116377376377356377376316346377376377386387386387396397396399310638738638739637737637737637737637738637737637737637737637732637737637738638738638739639739639931063873863873963763573573763763573573863773763773763773763773063773763773763673663673663573563573563473462271273063773763773763673663673663573563563573173213737637737637737636736636736635735635735634734622712730637737637737636736636736635735635635731732139312637635735737637635735931163773763773563773763773563763573573763763573563813931963773563773763163163763573573763763573593116377376377356377376377356376357357376376357356351393166377356377376316346377376377386387386387396397396399310638738638637139316637737637737637638139316637737637737637732637737637738638738638739639739639931063873863873963763573573763763573573863773763773763773763773063773763773763673663673663573563573563473462271273063773763773763673663673663573563563573173213737637737637737636736636736635735635735634734622712730637737637737636736636736635735635635731732139316634633634137307346341373073463413730734634931363463363413730734634137307346341373073463473963463363413730734634137307346341373073463493136346336341373073463413731734634137327346342420510990457306121171062161271211711732136236127121171173273461711710721712632734627717631048631612117106216127121171173213623612712117117327346171171072171263273462771763105162771162171162171163101272171262271262271262201073213721710621612632136317211173013631731633732199310631632632137306327330990990637326121171062161271211711732136236127121171173273461711710721712632734627717631048631612117106216127121171173213623612712117117327346171171072171263273462771763105162771162171162171163101272171262271262271262201073213721710621612632136317211173013631731633732199310631632632137306327330991683106357376257117327101171763772761362371072171262274262961263172171262271263792196126217116216126377256137317126116116217137226127376336227156157127136257156157121162461371371161471171371161471171331233120312031203122312031203120212231203120312031223120312176176222494330517306357356357366367366367376377376377386387386216116316357356357366367366367376377376376337317321373561211710621612712117117321362361271211711732734617117107217126327346277107330487326121171062161271211711732136236127121171173273461711710721712632734627710733048635137226116216116321363272211730136327327321373563263101873073119733137301363513722611621611632136327221173013632732635730199314632633634137307326320219310635735635736636736636737637737637738638728710621611631635735635736636736636737637737637633731732137356357356357366367366367376377376377386387287106216116316357356357366367366367376377376376337317321373561211710621612712117117321362361271211711732734617117107217126327346277107330487326121171062161271211711732136236127121171173273461711710721712632734627710733048635137226116216116321363272211730136327327321373563263101873073119733137301363513722611621611632136327221173013632732635730199314632633634137307326320219310635735635736636736636737637737637738638728710621611631635735635736636736636737637737637633731732137356357356357366367366367376377376377386387287106216116316357356357366367366367376377376377151161371371161471171371161471171362761262161213652717623612622714624117207101264572771272171862161362971072261272271012740720612621710127307116217111271762573371272261272671062171062371062471072011625117251162873071172271262211726611625117221172111761731732622611730711611611731711611611634711611614647711711759117201172011723117101171072211622366012042730621611661730723613731730730724614731157307108310731732737632167326321673272217730622147336336221773262217732722177306327337101171063772217732622177327221773062211627117201172011725177326221773276273163162281116211172311735661730735664730734663730733662764663760760730634667760961276066573073566473073466373073366276466376076073063466776096127606677607607607607607607386216116361373013730137301373013730136337317327376371373013730137301373013730137301373862161163613730137301373013730137301363373173273762217732622177327221773062214733633622177326221773272217730632733710117106377221773262217732722177306221162711720117201172517732622177327627316316228111621117231173566173073566473073466373073366276466376076073063466776096127606657307356647307346637307336627646637607607306346677609612760667760760760730137301373013738621611636137301373013730137301373013633731732727710637137301373013730137301373013730137287106216116361373013730137301373013730136337317327277106277126227126227727236136227126227126227727236136227126227126227427207126227126227727236136227126227126227727236136277136227127227106357276246236247247127151271072071262271262261263273272071262271262771162171162171173172371262271262272282127136237136237237196297106231172011720117236136231172111721117211171412726127371279342392421216801368012312421351213638313531351213651351332313512136114803163380136218333133380136338013651351365162136338013651531363651363380111123138012436801513363212463801237380124818012124333515153180121282351213638313531351231632180133321351246113180136338013621411215133338013665133816513651513363831366531363651363831111231380121381801212823321213831321212711180121381801212736513818012121512121891212181621212189121218135121218212121211215333312121891212162114122");
playSong();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment