Skip to content

Commit

Permalink
可能であればconstを利用するよう変更
Browse files Browse the repository at this point in the history
  • Loading branch information
mizdra committed Nov 16, 2017
1 parent 0beb0af commit de4d283
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 57 deletions.
101 changes: 51 additions & 50 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,20 @@ const GRAD = 0.02444;
const MAX_FREQ = GRAD * MAX_RAND + MIN_FREQ;

function main() {
let canvas = document.createElement('canvas');
document.body.appendChild(canvas);
canvas.width = 1000;
canvas.height = 500;
let ctx = new AudioContext();
let analyser = ctx.createAnalyser();
const ctx = new AudioContext();
const analyser = ctx.createAnalyser();
// analyser.minDecibels = -90;
analyser.maxDecibels = -40;
// analyser.smoothingTimeConstant = 0.85;
let gainNode = ctx.createGain();
const gainNode = ctx.createGain();
gainNode.gain.value = 0;

navigator.mediaDevices.getUserMedia({audio: true}).then((stream: MediaStream) => {
let source = ctx.createMediaStreamSource(stream);
const source = ctx.createMediaStreamSource(stream);
source.connect(analyser);
source.connect(gainNode);
gainNode.connect(ctx.destination);
visualize(canvas, analyser, ctx);
visualize(analyser, ctx);
}).catch((err: MediaStreamError) => {
alert(err);
});
Expand All @@ -42,23 +38,23 @@ function main() {
}

function search() {
let inputTextarea = document.getElementById('input') as HTMLTextAreaElement;
let input = inputTextarea != null ? inputTextarea.value : '';
let outputTextarea = document.getElementById('output') as HTMLTextAreaElement;
let form = document.getElementById('form') as HTMLFormElement;
let radios = form.elements.namedItem('mode');
let mode = radios ? radios['value'] as string : '';
let iptSeed4gen = form.elements.namedItem('seed-4gen') as HTMLInputElement;
let iptFrame4gen = form.elements.namedItem('frame-4gen') as HTMLInputElement;
let iptSeed5gen = form.elements.namedItem('seed-5gen') as HTMLInputElement;
let iptFrame5gen = form.elements.namedItem('frame-5gen') as HTMLInputElement;
let seed4gen = iptSeed4gen ? parseInt(iptSeed4gen.value, 16) : 0;
let frame4gen = iptFrame4gen ? Number(iptFrame4gen.value) : 0;
let seed5gen = iptSeed5gen ? parseUint64(iptSeed5gen.value) : new Uint64(0, 0);
let frame5gen = iptFrame5gen ? Number(iptFrame5gen.value) : 0;

let freqs = (input.match(/\d+/g) || []).map(x => Number(x));
let results: string[] = [];
const inputTextarea = document.getElementById('input') as HTMLTextAreaElement;
const input = inputTextarea != null ? inputTextarea.value : '';
const outputTextarea = document.getElementById('output') as HTMLTextAreaElement;
const form = document.getElementById('form') as HTMLFormElement;
const radios = form.elements.namedItem('mode');
const mode = radios ? radios['value'] as string : '';
const iptSeed4gen = form.elements.namedItem('seed-4gen') as HTMLInputElement;
const iptFrame4gen = form.elements.namedItem('frame-4gen') as HTMLInputElement;
const iptSeed5gen = form.elements.namedItem('seed-5gen') as HTMLInputElement;
const iptFrame5gen = form.elements.namedItem('frame-5gen') as HTMLInputElement;
const seed4gen = iptSeed4gen ? parseInt(iptSeed4gen.value, 16) : 0;
const frame4gen = iptFrame4gen ? Number(iptFrame4gen.value) : 0;
const seed5gen = iptSeed5gen ? parseUint64(iptSeed5gen.value) : new Uint64(0, 0);
const frame5gen = iptFrame5gen ? Number(iptFrame5gen.value) : 0;

const freqs = (input.match(/\d+/g) || []).map(x => Number(x));
const results: string[] = [];

switch (mode) {
case '4gen-seed':
Expand All @@ -71,7 +67,7 @@ function search() {
}
break;
case '4gen-frame': {
let lcg = new LCG(seed4gen);
const lcg = new LCG(seed4gen);
for (let frame = 0; frame < frame4gen; frame ++) {
if (isValidSeed(new LCG(lcg.seed), freqs)) {
results.push(String(frame));
Expand All @@ -81,7 +77,7 @@ function search() {
break;
}
case '5gen-frame': {
let lcg = new Uint64LCG(seed5gen);
const lcg = new Uint64LCG(seed5gen);
for (let frame = 0; frame < frame5gen; frame ++) {
if (isValidSeed(new Uint64LCG(lcg.seed), freqs)) {
results.push(String(frame));
Expand All @@ -100,37 +96,42 @@ function search() {
}

function isValidSeed(lcg: AbstractLCG, freqs: number[]) {
for (let f of freqs) {
let got = (lcg.randMod(8192)) * GRAD + MIN_FREQ;
for (const f of freqs) {
const got = (lcg.randMod(8192)) * GRAD + MIN_FREQ;
if (Math.abs(f - got) >= 2) {
return false;
}
}
return true;
}

function visualize(canvas: HTMLCanvasElement, analyser: AnalyserNode, ctx: AudioContext) {
let WIDTH = canvas.width;
let HEIGHT = canvas.height;
let canvasCtx = canvas.getContext('2d') as CanvasRenderingContext2D;
function visualize(analyser: AnalyserNode, ctx: AudioContext) {
const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
canvas.width = 1000;
canvas.height = 500;

const WIDTH = canvas.width;
const HEIGHT = canvas.height;
const canvasCtx = canvas.getContext('2d') as CanvasRenderingContext2D;
analyser.fftSize = Math.min(32768, getMaxFftSize());
let bufferLength = 2000 * analyser.fftSize / ctx.sampleRate; // analyser.frequencyBinCount;
let dataArray = new Uint8Array(bufferLength);
const bufferLength = 2000 * analyser.fftSize / ctx.sampleRate; // analyser.frequencyBinCount;
const dataArray = new Uint8Array(bufferLength);
let contiguousBigPoints: number [][] = [];

canvasCtx.clearRect(0, 0, WIDTH, HEIGHT);

let drawAlt = function() {
const drawAlt = function() {
requestAnimationFrame(drawAlt);

analyser.getByteFrequencyData(dataArray);

let bigPoints = retrieveBig(dataArray);
let seen: Set<number> = new Set();
for (let pt of bigPoints) {
const bigPoints = retrieveBig(dataArray);
const seen: Set<number> = new Set();
for (const pt of bigPoints) {
let set = false;
for (let i = 0; i < contiguousBigPoints.length; i ++) {
let [pt2, count] = contiguousBigPoints[i];
const [pt2, count] = contiguousBigPoints[i];
if (Math.abs(pt - pt2) <= 2) {
contiguousBigPoints[i] = [pt, count + 1];
set = true;
Expand All @@ -141,12 +142,12 @@ function visualize(canvas: HTMLCanvasElement, analyser: AnalyserNode, ctx: Audio
}
seen.add(pt);
}
let newCbp: number[][] = [];
const newCbp: number[][] = [];
let chatotGrowling = false;
let addedPt: number|null = null;
contiguousBigPoints.forEach(([pt, count]) => {
let freq = pt * ctx.sampleRate / analyser.fftSize;
let isChatot = MIN_FREQ - 2 <= freq && freq < MAX_FREQ + 2;
const freq = pt * ctx.sampleRate / analyser.fftSize;
const isChatot = MIN_FREQ - 2 <= freq && freq < MAX_FREQ + 2;
if (seen.has(pt)) {
newCbp.push([pt, count]);
if (isChatot) {
Expand All @@ -165,7 +166,7 @@ function visualize(canvas: HTMLCanvasElement, analyser: AnalyserNode, ctx: Audio
let max = 0;
let maxFreq = 0;
for (let i = 0; i < bufferLength; i++) {
let mag = dataArray[i];
const mag = dataArray[i];
if (max < mag) {
max = mag;
maxFreq = i * ctx.sampleRate / analyser.fftSize;
Expand All @@ -174,11 +175,11 @@ function visualize(canvas: HTMLCanvasElement, analyser: AnalyserNode, ctx: Audio
canvasCtx.fillStyle = 'hsl(' + ((1 - mag / 256) * 240) + ',50%,50%)';
canvasCtx.fillRect(WIDTH - 1, (1 - i / bufferLength) * HEIGHT, WIDTH, (1 - (i + 1) / bufferLength) * HEIGHT);
}
let paragraph = document.getElementById('maxHz') as HTMLParagraphElement;
const paragraph = document.getElementById('maxHz') as HTMLParagraphElement;
paragraph.innerText = '♪ ' + String(Math.round(maxFreq)) + 'Hz';
paragraph.style.backgroundColor = chatotGrowling ? '#f9c94f' : 'white';
if (addedPt) {
let textarea = document.getElementById('input') as HTMLTextAreaElement;
const textarea = document.getElementById('input') as HTMLTextAreaElement;
if (textarea.value !== '') {
textarea.value += '\n';
}
Expand All @@ -190,8 +191,8 @@ function visualize(canvas: HTMLCanvasElement, analyser: AnalyserNode, ctx: Audio
}

function retrieveBig(data: Uint8Array) {
let length = data.length;
let res: number [][] = [];
const length = data.length;
const res: number [][] = [];
for (let i = 0; i < length; i ++) {
if (data[i] > 250) {
if (res.length > 0 && res[res.length - 1][1] === i - 1) {
Expand All @@ -207,7 +208,7 @@ function retrieveBig(data: Uint8Array) {
}
}
return res.map((tuple) => {
let [, , repr] = tuple;
const [, , repr] = tuple;
return repr;
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/rng/lcg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Uint64LCG extends AbstractLCG {
return this.seed.high;
}
randMod(n: number) {
let r = this.rand();
const r = this.rand();
return new Uint64(0, r).mul(new Uint64(0, n)).high;
}
}
12 changes: 6 additions & 6 deletions src/rng/uint64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ export class Uint64 {
}

add(other: Uint64) {
let low = this.low + other.low;
let carry = this.low > 0x100000000 ? 1 : 0;
const low = this.low + other.low;
const carry = this.low > 0x100000000 ? 1 : 0;
this.low = low >>> 0;
this.high = (this.high + other.high + carry) >>> 0;
return this;
}

mul(other: Uint64) {
let a0 = this.low & 0xffff;
let a1 = this.low >>> 16;
let b0 = other.low & 0xffff;
let b1 = other.low >>> 16;
const a0 = this.low & 0xffff;
const a1 = this.low >>> 16;
const b0 = other.low & 0xffff;
const b1 = other.low >>> 16;

this.high = (((a1 * b0 + a0 * b1 + (a0 * b0 >>> 16)) >>> 16) + a1 * b1 + Math.imul(this.low, other.high) + Math.imul(other.low, this.high)) >>> 0;
this.low = Math.imul(this.low, other.low);
Expand Down
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"check-open-brace",
"check-whitespace"
],
"prefer-const": [true, {"destructuring": "all"}],
"quotemark": [true, "single", "jsx-double"],
"radix": true,
"semicolon": [true, "always"],
Expand Down

0 comments on commit de4d283

Please sign in to comment.