Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ module.exports = {
extends: ['scratch', 'scratch/node'],
env: {
es6: true
},
rules: {
'require-jsdoc': 'off'
}
};
3 changes: 3 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ jobs:
- uses: actions/checkout@v4
with:
persist-credentials: false
fetch-depth: 0
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run lint
- run: node scripts/check-conventions.mjs --base=origin/${{ github.base_ref }}
if: github.event_name == 'pull_request'
- run: npm run build
- run: npm run test
24 changes: 24 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# AGENTS.md

scratch-audio plays MistWarp project sounds: decoding (including ADPCM), the sound players, effects such as pitch, pan and volume, and microphone loudness. It is a fork of TurboWarp's audio engine, written in CommonJS JavaScript, and scratch-gui pins it to a commit.

## Where code goes

- `src/AudioEngine.js` is the entry point the VM uses. `SoundPlayer.js` plays one sound, `SoundBank.js` holds a target's sounds, and `effects/` holds the effect chain. `Loudness.js` reads the microphone.

## Things that look safe to change but are not

- Sound timing and effects must match Scratch. The scratch-parity project (git.rotur.dev/Mist/scratch-parity) includes a sound timing regression, and its results must not change.
- Browsers suspend an `AudioContext` until the page gets a user gesture. Resume it, don't assume it is running.
- Keep the `AudioEngine`, `SoundPlayer` and effect method names stable. scratch-vm calls them directly.

## Checks

`npm run lint`, `npm run tap` (this tap version has no `terse` reporter), `npm run build`, and `npm run check:conventions`, which rejects code comments and em dashes in the lines you add.

## Working here

- The default branch is `develop`. This repo is a fork, so open PRs with `gh pr create -R MistWarp/scratch-audio --base develop`.
- No code comments: name things so the code explains itself. No em dashes anywhere.
- Never add `Co-Authored-By` or any AI or tool attribution to commits or PRs.
- READMEs are overviews. Documentation goes in MistWarp/docs (https://mistwarp.org/docs/).
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"build": "webpack --bail",
"watch": "webpack --watch",
"lint": "eslint .",
"tap": "tap test/effects/*.js test/*.js"
"tap": "tap test/effects/*.js test/*.js",
"check:conventions": "node scripts/check-conventions.mjs"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -40,6 +41,7 @@
"eslint-config-scratch": "9.0.3",
"json": "9.0.6",
"tap": "^16.3.10",
"typescript": "~6.0.3",
"web-audio-test-api": "0.5.2",
"webpack": "4.47.0",
"webpack-cli": "3.3.12"
Expand Down
129 changes: 129 additions & 0 deletions scripts/check-conventions.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import {execFileSync} from 'node:child_process';
import fs from 'node:fs';
import ts from 'typescript';

const EM_DASH = '\u2014';
const SOURCE_FILE = /\.(js|jsx|mjs|cjs|ts|tsx)$/;
const TEXT_FILE = /\.(js|jsx|mjs|cjs|ts|tsx|json|md|css|html|yml|yaml|txt)$/;
const SKIPPED_PATHS = [
/^dist\//,
/(^|\/)package-lock\.json$/
];
const DIRECTIVE = /^(\/\/|\/\*)\s*(eslint[\s-]|global\s|@ts-|istanbul\s|prettier-ignore|\/\s*<reference)/;

const options = process.argv.slice(2);
const staged = options.includes('--staged');
const base = (options.find(option => option.startsWith('--base=')) || '--base=origin/develop').slice('--base='.length);

const git = (...args) => execFileSync('git', args, {encoding: 'utf8', maxBuffer: 1 << 30});

const diffArgs = staged ?
['diff', '--cached'] :
['diff', git('merge-base', base, 'HEAD').trim()];

const parseDiff = text => {
const files = new Map();
const removed = new Set();
let current = null;
let line = 0;
for (const row of text.split('\n')) {
if (row.startsWith('+++ ')) {
const path = row.slice(4).replace(/^b\//, '');
current = path === '/dev/null' ? null : path;
if (current && !files.has(current)) files.set(current, new Map());
} else if (row.startsWith('@@')) {
const match = /\+(\d+)/.exec(row);
line = match ? Number(match[1]) : 0;
} else if (row.startsWith('+') && current) {
files.get(current).set(line, row.slice(1));
line += 1;
} else if (row.startsWith('-') && !row.startsWith('---')) {
removed.add(row.slice(1).trim());
}
}
return {files, removed};
};

const lineStarts = text => {
const starts = [0];
for (let index = 0; index < text.length; index++) {
if (text[index] === '\n') starts.push(index + 1);
}
return starts;
};

const lineOf = (starts, position) => {
let low = 0;
let high = starts.length - 1;
while (low < high) {
const middle = Math.ceil((low + high) / 2);
if (starts[middle] <= position) low = middle;
else high = middle - 1;
}
return low + 1;
};

const commentRanges = (path, text) => {
const kind = /\.tsx$/.test(path) ? ts.ScriptKind.TSX :
/\.ts$/.test(path) ? ts.ScriptKind.TS :
/\.jsx$/.test(path) ? ts.ScriptKind.JSX : ts.ScriptKind.JS;
const source = ts.createSourceFile(path, text, ts.ScriptTarget.Latest, true, kind);
const seen = new Map();
const collect = ranges => {
for (const range of ranges || []) seen.set(range.pos, range);
};
const visit = node => {
if (node.kind === ts.SyntaxKind.JsxText || node.kind === ts.SyntaxKind.JsxTextAllWhiteSpaces) return;
if (node.kind >= ts.SyntaxKind.FirstToken && node.kind <= ts.SyntaxKind.LastToken) {
collect(ts.getLeadingCommentRanges(text, node.pos));
collect(ts.getTrailingCommentRanges(text, node.end));
return;
}
collect(ts.getLeadingCommentRanges(text, node.pos));
node.getChildren(source).forEach(visit);
};
visit(source);
collect(ts.getLeadingCommentRanges(text, source.endOfFileToken.pos));
return [...seen.values()];
};

const problems = [];
const {files, removed: removedLines} = parseDiff(git(...diffArgs, '--unified=0', '--no-color', '--diff-filter=ACMRD'));

const removedText = [...removedLines].join('\n');
const wasRemoved = text => text.length > 0 && removedText.includes(text);

for (const [path, added] of files) {
if (SKIPPED_PATHS.some(pattern => pattern.test(path)) || !TEXT_FILE.test(path)) continue;
for (const [line, content] of added) {
if (content.includes(EM_DASH) && !removedLines.has(content.trim())) {
problems.push(`${path}:${line} em dash: use a comma, parentheses or "to"`);
}
}
if (!SOURCE_FILE.test(path)) continue;
const text = staged ? git('show', `:${path}`) : fs.readFileSync(path, 'utf8');
const starts = lineStarts(text);
for (const range of commentRanges(path, text)) {
const comment = text.slice(range.pos, range.end);
if (DIRECTIVE.test(comment)) continue;
const first = lineOf(starts, range.pos);
const last = lineOf(starts, range.end - 1);
for (let line = first; line <= last; line++) {
if (typeof added.get(line) !== 'string') continue;
const lineStart = starts[line - 1];
const lineEnd = line < starts.length ? starts[line] - 1 : text.length;
const portion = text.slice(Math.max(range.pos, lineStart), Math.min(range.end, lineEnd)).trim();
if (!/[A-Za-z0-9]/.test(portion) || wasRemoved(portion)) continue;
problems.push(`${path}:${line} code comment: name things so the code explains itself`);
break;
}
}
}

if (problems.length) {
console.error(problems.join('\n'));
console.error(`\n${problems.length} convention problem${problems.length === 1 ? '' : 's'} in the changed lines.`);
process.exitCode = 1;
} else {
console.log('conventions ok');
}
5 changes: 4 additions & 1 deletion src/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module.exports = {
root: true,
extends: ['scratch', 'scratch/es6'],
env: {browser: true}
env: {browser: true},
rules: {
'require-jsdoc': 'off'
}
};
Loading