Skip to content

Commit

Permalink
rearrange some files
Browse files Browse the repository at this point in the history
  • Loading branch information
shicks committed Jan 8, 2016
1 parent 2ec8a4a commit 7851b8f
Show file tree
Hide file tree
Showing 15 changed files with 431 additions and 144 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
deps.d
*.compiled.*
*.srcmap
scraps/*
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ all: nes.compiled.js # test4.compiled.js

JSCOMP=java -jar ~/Downloads/compiler.jar

deps.d: deps.pl
./deps.pl
deps.d: deps.pl Makefile
./deps.pl '!' -path './scraps/\*'

-include deps.d
File renamed without changes.
138 changes: 2 additions & 136 deletions stepgeneratornode.js → audio/stepgeneratornode.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

import BufferedAudioNode from './bufferedaudionode';
import {nub} from '../util/util';
import {Complex, cisi} from '../util/math';

/** An audio node that takes a generator function as input. */
export default class StepGeneratorNode {
Expand Down Expand Up @@ -231,139 +233,3 @@ function lanczosKernel(radius, phases) {
}


/**
* @param {number} x
* @return {!Complex} Ci(x) + i*Si(x).
*/
function cisi(x) {
// NOTE: this comes from NR p301
const MAXIT = 100;
const EULER = 0.577215664901533; // euler's constant γ
const HALFPI = 1.570796326794897; // π/2
const TMIN = 2; // dividing line beween taylor series vs continued fraction
const EPS = 2.2204460492503131e-16; // relative error
const FPMIN = 8.9e-308; // close to smallest representable number
const BIG = 1.7e+308; // near machine overflow limit

const t = Math.abs(x);
let cs; // complex result
if (!t) return new Complex(-BIG);
if (t > TMIN) {
let b = new Complex(1, t);
let c = new Complex(BIG);
let d = b.inv();
let h = d;
let i;
for (i = 1; i < MAXIT; i++) {
const a = -i * i;
b = b.add(2);
d = d.mul(a).add(b).inv();
c = b.add(c.inv().mul(a));
const del = c.mul(d);
h = h.mul(del);
if (Math.abs(del.re - 1) + Math.abs(del.im) <= EPS) break;
}
if (i >= MAXIT) throw new Error('cf failed in cisi(' + x + ')');
h = new Complex(Math.cos(t), -Math.sin(t)).mul(h);
cs = new Complex(-h.re, h.im + HALFPI);
} else {
let sum, sumc, sums; // doubles
if (t < Math.sqrt(FPMIN)) {
sumc = 0.;
sums = t;
} else {
sum = sums = sumc = 0.;
let sign = 1.;
let fact = 1.;
let odd = true;
let k;
for (k = 1; k <= MAXIT; k++) {
fact *= t / k;
const term = fact / k;
sum += sign * term;
const err = term / Math.abs(sum);
if (odd) {
sign = -sign;
sums = sum;
sum = sumc;
} else {
sumc = sum;
sum = sums;
}
if (err < EPS) break;
odd = !odd;
}
if (k > MAXIT) throw('maxits exceeded in cisi(' + x + ')');
}
cs = new Complex(sumc + Math.log(t) + EULER, sums);
}
if (x < 0) cs = cs.conj();
return cs;
}

class Complex {
constructor(re, im = 0) {
this.re = re;
this.im = im;
Object.freeze(this);
}

add(z) {
return z instanceof Complex ?
new Complex(this.re + z.re, this.im + z.im) :
new Complex(this.re + z, this.im);
}

mul(z) {
return z instanceof Complex ?
new Complex(this.re * z.re - this.im * z.im,
this.re * z.im + this.im * z.re) :
new Complex(this.re * z, this.im * z);
}

sub(z) {
return z instanceof Complex ?
new Complex(this.re - z.re, this.im - z.im) :
new Complex(this.re - z, this.im);
}

inv() {
const mag2 = this.mag2();
return new Complex(this.re / mag2, -this.im / mag2);
}

div(z) {
return z instanceof Complex ?
this.mul(z.conj()).div(z.mag2()) :
new Complex(this.re / z, this.im / z);
}

conj() {
return new Complex(this.re, -this.im);
}

mag2() {
return this.re * this.re + this.im * this.im;
}

toString() {
return this.im == 0 ? String(this.re) :
this.im > 0 ? this.re + ' + ' + this.im + 'i' :
this.re + ' - ' + -this.im + 'i';
}
}

Complex.ONE = new Complex(1);
Complex.I = new Complex(0, 1);
Complex.ZERO = new Complex(0);
Object.freeze(Complex);


function nub(xs) {
if (!xs.length) return [];
const out = [xs[0]];
for (let x of xs) {
if (x != out[out.length - 1]) out.push(x);
}
return out;
}
6 changes: 3 additions & 3 deletions deps.pl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/perl

# Dependency and makefile management.
# Usage: ./deps.pl
# Usage: ./deps.pl FIND_ARGS...
# Result: Generates a makefile based on in-file markup.

# Input files:
Expand Down Expand Up @@ -31,7 +31,7 @@
use File::Basename qw/dirname/;

# Find all files recursively from current dir
open FIND, "find . -name '*.js' | sed 's+^./++' |";
open FIND, "find . -name '*.js' @ARGV | sed 's+^./++' |";
my @files = <FIND>;
close FIND;
map {chomp $_} @files;
Expand Down Expand Up @@ -70,7 +70,7 @@
my @q = ($file);
while (@q) {
my $cur = shift @q;
foreach (split / /, $deps{$cur}) {
foreach (split / /, ($deps{$cur} or '')) {
next unless $_;
next if defined $d{$_};
$d{$_} = 1;
Expand Down
2 changes: 1 addition & 1 deletion nsfplayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Clock from './clock';
import Cpu from './cpu';
import Memory from './mem';
import Nsf from './nsf';
import StepGeneratorNode from './stepgeneratornode';
import StepGeneratorNode from './audio/stepgeneratornode';

export default class NsfPlayer {

Expand Down
Loading

0 comments on commit 7851b8f

Please sign in to comment.