Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(isf-audio): proper error handling that actually works #3

Open
wants to merge 4 commits into
base: feat-ISFAudio/webpack
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/ISFGLProgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ ISFGLProgram.prototype.createShader = function createShader(src, type) {
const compiled = this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS);
if (!compiled) {
const lastError = this.gl.getShaderInfoLog(shader);
throw new Error({
message: lastError,
type: "shader",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TimPietrusky Why do we not need a type here?

throw ({
message: lastError
});
}
return shader;
Expand All @@ -74,7 +73,7 @@ ISFGLProgram.prototype.createProgram = function createProgram(
const linked = this.gl.getProgramParameter(program, this.gl.LINK_STATUS);
if (!linked) {
const lastError = this.gl.getProgramInfoLog(program);
throw new Error({
throw ({
message: lastError,
type: "program",
});
Expand Down
11 changes: 8 additions & 3 deletions src/ISFLineMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ function getMainLine(src) {
return -1;
}

export default function ISFLineMapper(error, glsl, isf) {
export function lineNumber(error, glsl, isf) {
const glslMainLine = getMainLine(glsl);
const isfMainLine = getMainLine(isf);
const regex = /ERROR: (\d+):(\d+): (.*)/g;
const matches = regex.exec(error.message);
const glslErrorLine = matches[2];
const isfErrorLine = parseInt(glslErrorLine, 10) + isfMainLine - glslMainLine;
let isfErrorLine = -1;

// Sometimes we get errors that don't contain a lineNumber
if (matches !== null) {
const glslErrorLine = matches[2];
isfErrorLine = parseInt(glslErrorLine, 10) + isfMainLine - glslMainLine;
}
return isfErrorLine;
}

Expand Down
8 changes: 8 additions & 0 deletions src/ISFParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ const typeUniformMap = {
const ISFParser = function ISFParser() {};

ISFParser.prototype.parse = function parse(rawFragmentShader, rawVertexShader) {
// Indicates if the shader is using an audio-input (audio or audioFFT)
this.hasAudio = false

try {
this.valid = true;
this.rawFragmentShader = rawFragmentShader;
Expand Down Expand Up @@ -80,6 +83,11 @@ ISFParser.prototype.generateShaders = function generateShaders() {
this.uniformDefs = "";
for (let i = 0; i < this.inputs.length; i += 1) {
this.addUniform(this.inputs[i]);

// Make sure to identify if this shader has audio
if (this.inputs[i].TYPE === "audio" || this.inputs[i].TYPE === "audioFFT") {
this.hasAudio = true;
}
}

for (let i = 0; i < this.passes.length; i += 1) {
Expand Down
29 changes: 17 additions & 12 deletions src/ISFRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import ISFGLProgram from "./ISFGLProgram";
import ISFBuffer from "./ISFBuffer";
import ISFParser from "./ISFParser";
import ISFTexture from "./ISFTexture";
import LineMapper from "./ISFLineMapper";
import { lineNumber, correctedLineErrors } from "./ISFLineMapper";
import { ISFAudio, ISFAudioTexture } from "./ISFAudio";

const { correctedLineErrors } = LineMapper;

const mathJsEval = math.eval;

let globalAudioInstance;
Expand All @@ -24,7 +22,9 @@ function ISFRenderer(gl, options = {}) {
this.frameIndex = 0;
this.audioTextureInstances = [];

const { useWebAudio, fftSize } = options;
const { useWebAudio, fftSize, hasAudio } = options;

this.hasAudio = hasAudio;

if (!globalAudioInstance) {
globalAudioInstance = new ISFAudio({ useWebAudio, fftSize });
Expand Down Expand Up @@ -71,7 +71,7 @@ ISFRenderer.prototype.sourceChanged = function sourceChanged(
} catch (e) {
this.valid = false;
this.error = e;
this.errorLine = LineMapper(
this.errorLine = lineNumber(
e,
this.fragmentShader,
this.model.rawFragmentShader
Expand All @@ -81,6 +81,7 @@ ISFRenderer.prototype.sourceChanged = function sourceChanged(
this.fragmentShader,
this.model.rawFragmentShader
);

}
};

Expand Down Expand Up @@ -416,14 +417,18 @@ ISFRenderer.prototype.setDateUniforms = function setDateUniforms() {
};

ISFRenderer.prototype.draw = function draw(destination) {
this.audio.updateWebAudio();
this.audioTextureInstances.forEach((localInstanceData) => {
const instance = this.audio.audioTextureInstances[localInstanceData.id];

instance.update();

this.setValue(localInstanceData.inputName, instance.context.canvas);
});
// Don't update the audio instances if the shader is not using any audio inputs
if (this.hasAudio) {
this.audio.updateWebAudio();
this.audioTextureInstances.forEach((localInstanceData) => {
const instance = this.audio.audioTextureInstances[localInstanceData.id];

instance.update();

this.setValue(localInstanceData.inputName, instance.context.canvas);
});
}

this.contextState.reset();
this.program.use();
Expand Down