Skip to content

Commit 2afcacf

Browse files
committed
fix: improve note handling and remove TypeScript ignores in SongStatsGenerator
- Added checks to skip undefined notes in the SongStatsGenerator to prevent errors. - Removed unnecessary TypeScript ignore comments for better code clarity. - Updated build order in build.ts to ensure @nbw/sounds is built after @nbw/database.
1 parent 3f37c29 commit 2afcacf

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

bun.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
"name": "@nbw/song",
192192
"dependencies": {
193193
"@encode42/nbs.js": "^5.0.2",
194+
"@nbw/config": "workspace:*",
194195
"@nbw/database": "workspace:*",
195196
"@timohausmann/quadtree-ts": "^2.2.2",
196197
"jszip": "^3.10.1",

packages/song/src/stats.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ export class SongStatsGenerator {
111111
const note = layer.notes[tickStr];
112112
const tick = parseInt(tickStr);
113113

114+
// Skip if note is undefined
115+
if (!note) continue;
116+
114117
if (tick > tickCount) {
115118
tickCount = tick;
116119
}
@@ -120,7 +123,6 @@ export class SongStatsGenerator {
120123
layerCount = layerId;
121124
}
122125

123-
// @ts-ignore //TODO: fix this
124126
const effectivePitch = note.key + note.pitch / 100;
125127

126128
// Differences between Note Block Studio and this implementation:
@@ -148,8 +150,9 @@ export class SongStatsGenerator {
148150
// the default Minecraft sounds are enough to play the song (i.e. you can play it using only
149151
// a custom sounds.json in a resource pack).
150152

151-
// @ts-ignore //TODO: fix this
152-
const instrumentKey = this.song.instruments.loaded[note.instrument].key; // F#4 = 45
153+
const instrument = this.song.instruments.loaded[note.instrument]!;
154+
if (!instrument) continue;
155+
const instrumentKey = instrument.key; // F#4 = 45
153156
const minRange = 45 - (instrumentKey - 45) - 12; // F#3 = 33
154157
const maxRange = 45 - (instrumentKey - 45) + 12; // F#5 = 57
155158

@@ -158,15 +161,13 @@ export class SongStatsGenerator {
158161

159162
// Don't consider tempo changers as detuned notes or custom instruments
160163
const isTempoChanger = tempoChangerInstruments.includes(
161-
// @ts-ignore //TODO: fix this
162164
note.instrument,
163165
);
164166

165-
// @ts-ignore //TODO: fix this
166167
const hasDetune = note.pitch % 100 !== 0;
167168

168169
const usesCustomInstrument =
169-
note.instrument >= this.song.instruments.firstCustomIndex; // @ts-ignore //TODO: fix this
170+
note.instrument >= this.song.instruments.firstCustomIndex;
170171

171172
if (!isTempoChanger) {
172173
if (isOutOfRange) outOfRangeNoteCount++;
@@ -176,7 +177,6 @@ export class SongStatsGenerator {
176177
incompatibleNoteCount++;
177178
}
178179

179-
// @ts-ignore //TODO: fix this
180180
instrumentNoteCounts[note.instrument]++;
181181
noteCount++;
182182
}
@@ -227,15 +227,17 @@ export class SongStatsGenerator {
227227
const note = layer.notes[tickStr];
228228
const tick = parseInt(tickStr);
229229

230-
// @ts-ignore //TODO: fix this // Not a tempo changer
230+
// Skip if note is undefined
231+
if (!note) continue;
232+
233+
// Not a tempo changer
231234
if (!tempoChangerInstruments.includes(note.instrument)) continue;
232235

233236
// The tempo change isn't effective if there's another tempo changer in the same tick,
234237
// so we iterate layers bottom to top and skip the block if a tempo changer has already
235238
// been found in this tick
236239
if (tick in tempoSegments) continue;
237240

238-
// @ts-ignore //TODO: fix this
239241
const tempo = Math.abs(note.pitch) / 15; // note pitch = BPM = (t/s) * 15
240242
tempoSegments[tick] = tempo;
241243
}
@@ -273,10 +275,11 @@ export class SongStatsGenerator {
273275
const currTick = tempoChangeTicks[i];
274276
const nextTick = tempoChangeTicks[i + 1];
275277

276-
// @ts-ignore //TODO: fix this
278+
if (currTick === undefined || nextTick === undefined) continue;
279+
277280
const currTempo = tempoSegments[currTick];
281+
if (currTempo === undefined) continue;
278282

279-
// @ts-ignore //TODO: fix this
280283
const segmentDurationTicks = nextTick - currTick;
281284
const timePerTick = 1 / currTempo;
282285

scripts/build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { $ } from 'bun';
44
// the sub array is for packages that can be built in parallel
55
const packages: (string | string[])[] = [
66
'@nbw/config',
7-
'@nbw/sounds',
8-
'@nbw/database', // Build database first to ensure types are available
7+
'@nbw/database',
98
'@nbw/song',
9+
'@nbw/sounds',
1010
'@nbw/thumbnail',
1111
];
1212

0 commit comments

Comments
 (0)