Added Sine City's source code

This commit is contained in:
Emmanuel BENOîT 2014-10-13 07:33:40 +02:00
parent 2a19d8403b
commit 270260cf74
23 changed files with 8309 additions and 0 deletions

11
201410_-_Sine_City/README Normal file
View file

@ -0,0 +1,11 @@
Source code for "Sine City"
A JavaScript 8K intro
On Pouet: http://www.pouet.net/prod.php?which=64233
Notes:
* It is likely not to work on your end.
* Tested on Linux using Chromium and Firefox with a NVidia GPU
* Known to work on some Windows boxes w/ native GL enabled
* The "original" version is not the code that was shown at DemoJS,
I adjusted quite a few things in the final, packed version.

View file

@ -0,0 +1,2 @@
// in case I need it again
C=V.getContext('2d');for(C.drawImage(this,0,_='');$--;(A=C.getImageData($%X,$/X,1,1).data[0])&&(_+=String.fromCharCode(A)));(1,eval)(_)

159
201410_-_Sine_City/music.js Normal file
View file

@ -0,0 +1,159 @@
var song = {
songData: [
{
i: [ 0, 198, 128, 0, 3, 192, 128, 1, 0, 0, 96, 128, 28, 0, 0, 0, 0, 2, 63, 61, 13, 16, 44, 8, 0, 0 ],
p: [1,2,3,4,5,2,3,4,6,7,1,2,3,4,5,2,3,4,5,2,3,4,1,2,3,8,9],
c: [
{n: [115,,,,,,,,,,,,117,,,,,,,,,,,,113],
f: [12,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,96]},
{n: [115,,,,,,,,,,,,,,,110],
f: [12,,,,,,,,,,,,,,,12,,,,,,,,,,,,,,,,,,,,,115,,,,,,,,,,,,,,,148]},
{n: [115,,,,,,,,,,,,117,,,,,,,,,,,,118],
f: [12,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,96]},
{n: [122,,,,,,,,,,,,,,,123,,,,,115],
f: [12,,,,,,,,,,,,,,,12,,,,,12,,,,,,,,,,,,,,,,115,,,,,,,,,,,,,,,8,,,,,128]},
{n: [118,,,,,,,,,,,,117,,,,,,,,,,,,113],
f: [12,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,96]},
{n: [115],
f: [12,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,255]},
{n: [,,,,,,,,,,,,,,,,118,118,118],
f: [,,,,,,,,,,,,,,,,12,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,128]},
{n: [122,,,,,,,,,,,,,,,123,,,,,115],
f: [12,,,,,,,,,,,,,,,12,,,,,13,12,,,,,,,,,,,,,,,115,,,,,,,,,,,,,,,8,,,,,95,61]},
{n: [],
f: []}
]
},
{
i: [ 0, 255, 116, 1, 0, 255, 101, 0, 0, 15, 1, 4, 45, 0, 13, 6, 1, 2, 62, 27, 60, 48, 0, 0, 44, 2 ],
p: [,1,,2,,1,,2,,3,4,4,5,4,5,4,5,4,5,4,5,4,5,4,3,6],
c: [
{n: [,,,,,139,,,139,139,,,,,,139,,,,,,,,139,,,139,139,,,,,139,139,139],
f: []},
{n: [,,139,139,,,,,139,139,,,,,139,139,,,,,,,,,,139,139,139,,,,,139,139,139],
f: []},
{n: [,,,139,,,,,139,139,,,,,,139,,,,,139,139,,,,,,139,,,,139,139,139],
f: []},
{n: [139,,,,139,,139,,,139,,,139,,,,139,,139,,,139,,,139,,,,139,,139,,,139],
f: []},
{n: [139,,,139,139,,139,,,139,,,139,,,139,139,,139,,,139,,,139,,,139,139,,139,,,139],
f: []},
{n: [139,,,139,139,,139,,,139,,,139,,,139,139,,139,,,139,139],
f: []}
]
},
{
i: [ 0, 0, 140, 0, 0, 0, 140, 0, 0, 128, 4, 10, 34, 0, 187, 5, 0, 1, 239, 135, 34, 19, 108, 8, 8, 4 ],
p: [,,1,,1,,3,,1,3,2,2,2,2,2,3,2,3,2,3,2,3,2,3,1],
c: [
{n: [,,,,,,,,,,127,,,,,,,,,,,,127,,,,,,,,,,,,127],
f: []},
{n: [,,,139,,,,,,139,139,,,,,139,,,,,,139,139,,,,,139,,,,,139,,139],
f: []},
{n: [,,127,,,,,,,,127,,,,127,,,,,,,,127,,,,127,,,,,,,,127],
f: []}
]
},
{
i: [ 1, 192, 128, 0, 3, 201, 128, 0, 0, 93, 5, 6, 58, 3, 195, 6, 1, 3, 35, 63, 14, 17, 11, 10, 61, 6 ],
p: [,,,,1,2,3,4,5,6,7,5,8,9,10,11,8,9,10,11,12,13,14,15,16],
c: [
{n: [,,,,,,,,147,146,145,,,,,,,,,,145,147,146,,,,,,,,,,149,148,147],
f: []},
{n: [,,,,,,151,151,151,,,,,,,146,142,138,,,,,,,,,,,,,,,147,150,147],
f: []},
{n: [,,146,,,146,,,,,,,,,147,146,145,,,,,,,,,,146,145,142,,,142,,,142],
f: []},
{n: [,,141,,,,,,,,,,,,141,140,139,,,146,,,146,,,127],
f: []},
{n: [,139,,151,,,,,,,,,,151,,139,,,,,,,,,,139,,151],
f: []},
{n: [,139,,151,150,149,,,,,,,,139,,149,148,147,,,,,,,,139,,,146,,,145,,,144],
f: []},
{n: [,139,,,139,142,,,142,144,146,,144,,,,146,147,,,147,146,141,,137,,,,142,141,,141,,137],
f: []},
{n: [139,,127,,142,,,130,,,144,142,141,,129,,141,,,144,,,141,144,142,,,,149,,,146,,,151],
f: []},
{n: [,149,,149,,146,144,142,,,141,,142,,,139,,,,151,147,,,,146,147,,144,146,,142,144,141,146],
f: []},
{n: [,,,,,,,,147,146,145,,,,,141,144,147,146,,144,,137,,,144,146,142,144,,,142,144,141,146],
f: []},
{n: [,146,,151,,146,,,,144,142,144,146,,,,142,,,,,139,,,,,151,146,139,144,139,142,141,142,151],
f: []},
{n: [,,147,,,149,146,,,142,,142,141,,,144,,144,,,144,146,142,144,142,,,,151,150,149,,137,,149],
f: []},
{n: [,151,,,147,151,,,146,151,,,,,144,151,,149,147,149,151,,146,,,144,143,142,,,139,,151,139],
f: []},
{n: [,,134,,,134,,,134,,,146,144,,,141,144,,,146,147,146,,,144,,139,140,141,,,153,,,141],
f: []},
{n: [,142,,151,,,142,143,144,,,,151,,,139,,,137,,149,,137,,,,125,,,141,142,141,,,146],
f: []},
{n: [139,,127,139,,127,139,,127,139,127],
f: []}
]
},
{
i: [ 2, 160, 128, 1, 0, 160, 128, 0, 1, 60, 4, 7, 41, 0, 60, 4, 1, 3, 14, 0, 35, 32, 31, 12, 89, 1 ],
p: [,,,,,,,,,,,,1,2,1,2,1,2,1,2,1,2,1,2,1,3],
c: [
{n: [,,139,,,,,,,,,,,,139,,,,,,,,,,,,139,,,,,139,139,,,139],
f: []},
{n: [,,139,,,151,,,139,,,151,,,139,,,151,,,139,,,151,,,139,,,151,,,146,146,146,146],
f: []},
{n: [,,139,,,151,,,139,,,151,,,139,,139,139,139,139,127],
f: []}
]
},
{
i: [ 2, 100, 128, 0, 3, 201, 128, 0, 0, 0, 0, 6, 29, 0, 195, 6, 1, 3, 28, 229, 119, 77, 147, 6, 61, 2 ],
p: [,,,,,,,,,6,1,2,3,4,5,2,3,4,1,2,3,4,1,2,3,7],
c: [
{n: [115,115,115,115,115,115,115,115,115,115,115,115,117,117,117,117,117,117,117,117,117,117,117,117,113,113,113,113,113,113,113,113,113,113,113,113],
f: []},
{n: [115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110],
f: []},
{n: [115,115,115,115,115,115,115,115,115,115,115,115,117,117,117,117,117,117,117,117,117,117,117,117,118,118,118,118,118,118,118,118,118,118,118,118],
f: []},
{n: [110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,111,111,111,111,111,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115],
f: []},
{n: [118,118,118,118,118,118,118,118,118,118,118,118,117,117,117,117,117,117,117,117,117,117,117,117,113,113,113,113,113,113,113,113,113,113,113,113],
f: []},
{n: [,,,,,,,,,,,,,,,,,,130,,130,,130,,130,,130,,130,118,130,118,130,118,130,118],
f: [,,,,,,,,,,,,,,,,,,26,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,2]},
{n: [110,122,,122,134,,110,122,,122,134,,110,122,,111,123,,111,123,115,127,139,127,115],
f: [,,,,,,,,,,,,,,,,,,,,26,25,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,8,147]}
]
},
{
i: [ 2, 100, 128, 0, 3, 201, 128, 0, 0, 0, 5, 6, 58, 0, 195, 6, 1, 2, 135, 0, 0, 32, 147, 6, 121, 6 ],
p: [],
c: [
]
},
{
i: [ 2, 100, 128, 0, 3, 201, 128, 0, 0, 0, 5, 6, 58, 0, 195, 6, 1, 2, 135, 0, 0, 32, 147, 6, 121, 6 ],
p: [],
c: [
]
}
],
rowLen: 5513,
patternLen: 36,
endPattern: 28
};

Binary file not shown.

View file

@ -0,0 +1,336 @@
/* -*- mode: javascript; tab-width: 4; indent-tabs-mode: nil; -*-
*
* Copyright (c) 2011-2013 Marcus Geelnard
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*
*/
"use strict";
var CPlayer = function() {
//--------------------------------------------------------------------------
// Private methods
//--------------------------------------------------------------------------
// Oscillators
var osc_sin = function (value) {
return Math.sin(value * 6.283184);
};
var osc_saw = function (value) {
return 2 * (value % 1) - 1;
};
var osc_square = function (value) {
return (value % 1) < 0.5 ? 1 : -1;
};
var osc_tri = function (value) {
var v2 = (value % 1) * 4;
if(v2 < 2) return v2 - 1;
return 3 - v2;
};
var getnotefreq = function (n) {
// 174.61.. / 44100 = 0.003959503758 (F3)
return 0.003959503758 * Math.pow(2, (n - 128) / 12);
};
var createNote = function (instr, n) {
var osc1 = mOscillators[instr.i[0]],
o1vol = instr.i[1],
o1xenv = instr.i[3],
osc2 = mOscillators[instr.i[4]],
o2vol = instr.i[5],
o2xenv = instr.i[8],
noiseVol = instr.i[9],
attack = instr.i[10] * instr.i[10] * 4,
sustain = instr.i[11] * instr.i[11] * 4,
release = instr.i[12] * instr.i[12] * 4,
releaseInv = 1 / release;
var noteBuf = new Int32Array(attack + sustain + release);
// Calculate note frequencies for the oscillators
var o1t = getnotefreq(n + instr.i[2] - 128);
var o2t = getnotefreq(n + instr.i[6] - 128) * (1 + 0.0008 * instr.i[7]);
// Re-trig oscillators
var c1 = 0, c2 = 0;
// Local variables.
var j, e, t, rsample;
// Generate one note (attack + sustain + release)
for (j = 0; j < attack + sustain + release; j++) {
// Envelope
e = 1;
if (j < attack) {
e = j / attack;
} else if (j >= attack + sustain) {
e -= (j - attack - sustain) * releaseInv;
}
// Oscillator 1
t = o1t;
if (o1xenv) {
t *= e * e;
}
c1 += t;
rsample = osc1(c1) * o1vol;
// Oscillator 2
t = o2t;
if (o2xenv) {
t *= e * e;
}
c2 += t;
rsample += osc2(c2) * o2vol;
// Noise oscillator
if (noiseVol) {
rsample += (2 * Math.random() - 1) * noiseVol;
}
// Add to (mono) channel buffer
noteBuf[j] = (80 * rsample * e) | 0;
}
return noteBuf;
};
//--------------------------------------------------------------------------
// Private members
//--------------------------------------------------------------------------
// Array of oscillator functions
var mOscillators = [
osc_sin,
osc_square,
osc_saw,
osc_tri
];
// Private variables set up by init()
var mSong, mLastRow, mCurrentCol, mNumWords, mMixBuf;
//--------------------------------------------------------------------------
// Initialization
//--------------------------------------------------------------------------
this.init = function (song) {
// Define the song
mSong = song;
// Init iteration state variables
mLastRow = song.endPattern - 2;
mCurrentCol = 0;
// Prepare song info
mNumWords = song.rowLen * song.patternLen * (mLastRow + 1) * 2;
// Create work buffer (initially cleared)
mMixBuf = new Int32Array(mNumWords);
};
//--------------------------------------------------------------------------
// Public methods
//--------------------------------------------------------------------------
// Generate audio data for a single track
this.generate = function () {
// Local variables
var i, j, b, p, row, col, n, cp,
k, t, lfor, e, x, rsample, rowStartSample, f, da;
// Put performance critical items in local variables
var chnBuf = new Int32Array(mNumWords),
instr = mSong.songData[mCurrentCol],
rowLen = mSong.rowLen,
patternLen = mSong.patternLen;
// Clear effect state
var low = 0, band = 0, high;
var lsample, filterActive = false;
// Clear note cache.
var noteCache = [];
// Patterns
for (p = 0; p <= mLastRow; ++p) {
cp = instr.p[p];
// Pattern rows
for (row = 0; row < patternLen; ++row) {
// Execute effect command.
var cmdNo = cp ? instr.c[cp - 1].f[row] : 0;
if (cmdNo) {
instr.i[cmdNo - 1] = instr.c[cp - 1].f[row + patternLen] || 0;
// Clear the note cache since the instrument has changed.
if (cmdNo < 14) {
noteCache = [];
}
}
// Put performance critical instrument properties in local variables
var oscLFO = mOscillators[instr.i[13]],
lfoAmt = instr.i[14] / 512,
lfoFreq = Math.pow(2, instr.i[15] - 9) / rowLen,
fxLFO = instr.i[16],
fxFilter = instr.i[17],
fxFreq = instr.i[18] * 43.23529 * 3.141592 / 44100,
q = 1 - instr.i[19] / 255,
dist = instr.i[20] * 1e-5,
drive = instr.i[21] / 32,
panAmt = instr.i[22] / 512,
panFreq = 6.283184 * Math.pow(2, instr.i[23] - 9) / rowLen,
dlyAmt = instr.i[24] / 255,
dly = instr.i[25] * rowLen;
// Calculate start sample number for this row in the pattern
rowStartSample = (p * patternLen + row) * rowLen;
// Generate notes for this pattern row
for (col = 0; col < 4; ++col) {
n = cp ? instr.c[cp - 1].n[row + col * patternLen] : 0;
if (n) {
if (!noteCache[n]) {
noteCache[n] = createNote(instr, n);
}
// Copy note from the note cache
var noteBuf = noteCache[n];
for (j = 0, i = rowStartSample * 2; j < noteBuf.length; j++, i += 2) {
chnBuf[i] += noteBuf[j];
}
}
}
// Perform effects for this pattern row
for (j = 0; j < rowLen; j++) {
// Dry mono-sample
k = (rowStartSample + j) * 2;
rsample = chnBuf[k];
// We only do effects if we have some sound input
if (rsample || filterActive) {
// State variable filter
f = fxFreq;
if (fxLFO) {
f *= oscLFO(lfoFreq * k) * lfoAmt + 0.5;
}
f = 1.5 * Math.sin(f);
low += f * band;
high = q * (rsample - band) - low;
band += f * high;
rsample = fxFilter == 3 ? band : fxFilter == 1 ? high : low;
// Distortion
if (dist) {
rsample *= dist;
rsample = rsample < 1 ? rsample > -1 ? osc_sin(rsample*.25) : -1 : 1;
rsample /= dist;
}
// Drive
rsample *= drive;
// Is the filter active (i.e. still audiable)?
filterActive = rsample * rsample > 1e-5;
// Panning
t = Math.sin(panFreq * k) * panAmt + 0.5;
lsample = rsample * (1 - t);
rsample *= t;
} else {
lsample = 0;
}
// Delay is always done, since it does not need sound input
if (k >= dly) {
// Left channel = left + right[-p] * t
lsample += chnBuf[k-dly+1] * dlyAmt;
// Right channel = right + left[-p] * t
rsample += chnBuf[k-dly] * dlyAmt;
}
// Store in stereo channel buffer (needed for the delay effect)
chnBuf[k] = lsample | 0;
chnBuf[k+1] = rsample | 0;
// ...and add to stereo mix buffer
mMixBuf[k] += lsample | 0;
mMixBuf[k+1] += rsample | 0;
}
}
}
// Next iteration. Return progress (1.0 == done!).
mCurrentCol++;
return mCurrentCol / 8;
};
// Create a WAVE formatted Uint8Array from the generated audio data
this.createWave = function() {
// Create WAVE header
var l1 = mNumWords * 2 - 8;
var l2 = l1 - 36;
var headerLen = 44;
var wave = new Uint8Array(headerLen + mNumWords * 2);
wave.set(
[82,73,70,70,
l1 & 255,(l1 >> 8) & 255,(l1 >> 16) & 255,(l1 >> 24) & 255,
87,65,86,69,102,109,116,32,16,0,0,0,1,0,2,0,
68,172,0,0,16,177,2,0,4,0,16,0,100,97,116,97,
l2 & 255,(l2 >> 8) & 255,(l2 >> 16) & 255,(l2 >> 24) & 255]
);
// Append actual wave data
for (var i = 0, idx = headerLen; i < mNumWords; ++i) {
// Note: We clamp here
var y = mMixBuf[i];
y = y < -32767 ? -32767 : (y > 32767 ? 32767 : y);
wave[idx++] = y & 255;
wave[idx++] = (y >> 8) & 255;
}
// Return the WAVE formatted typed array
return wave;
};
// Get n samples of wave data at time t [s]. Wave data in range [-2,2].
this.getData = function(t, n) {
var i = 2 * Math.floor(t * 44100);
var d = new Array(n);
for (var j = 0; j < 2*n; j += 1) {
var k = i + j;
d[j] = t > 0 && k < mMixBuf.length ? mMixBuf[k] / 32768 : 0;
}
return d;
};
};

View file

@ -0,0 +1,5 @@
<body>
<script language="javascript" src="music.js"></script>
<script language="javascript" src="player-small.js"></script>
<script language="javascript" src="sine-city-ori.js"></script>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,5 @@
<body>
<script language="javascript" src="music.js"></script>
<script language="javascript" src="player-small.js"></script>
<script language="javascript" src="sine-city-packing-1-shaders.js"></script>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,5 @@
<body>
<script language="javascript" src="music.js"></script>
<script language="javascript" src="player-small.js"></script>
<script language="javascript" src="sine-city-packing-2-raymarchers.js"></script>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,5 @@
<body>
<script language="javascript" src="music.js"></script>
<script language="javascript" src="player-small.js"></script>
<script language="javascript" src="sine-city-packing-3-direction.js"></script>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,3 @@
<body>
<script language="javascript" src="sine-city-packing-4-sound.js"></script>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,3 @@
<body>
<script language="javascript" src="sine-city-packing-5-misc.js"></script>

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,3 @@
<body>
<script language="javascript" src="sine-city-packing-6-gl.js"></script>

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,3 @@
<body>
<script language="javascript" src="sine-city-packing-7-variables.js"></script>

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,3 @@
<body>
<script language="javascript" src="sine-city-packing-8-finalize.js"></script>

File diff suppressed because one or more lines are too long