|
| 1 | +import { assertDefined } from '@repo/common/assert' |
1 | 2 | import { Notes, Pitch, type PitchNotation, Range, Time } from '@repo/synth' |
2 | 3 |
|
3 | | -const HIGHEST_PITCH: PitchNotation = 'B9' |
4 | | - |
5 | | -const LOWEST_PITCH: PitchNotation = 'C0' |
6 | | - |
7 | | -export const NOTE_HALF_STEPS = Pitch.midi(HIGHEST_PITCH) - Pitch.midi(LOWEST_PITCH) + 1 |
| 4 | +const HIGHEST_PITCH: PitchNotation = 'b9' |
| 5 | +const LOWEST_PITCH: PitchNotation = 'c0' |
| 6 | +const NOTE_HALF_STEPS = Pitch[HIGHEST_PITCH].midi - Pitch[LOWEST_PITCH].midi + 1 |
8 | 7 |
|
9 | 8 | export type MusicSheetDimensions = ReturnType<typeof musicSheetDimensions> |
10 | 9 |
|
@@ -37,10 +36,14 @@ export const musicSheetDimensions = (params: Params) => { |
37 | 36 | height: halfStepHeightPx, |
38 | 37 | width: (duration: Time) => duration.notes * wholeNoteWidthPx, |
39 | 38 | left: (time: Time) => time.notes * wholeNoteWidthPx, |
40 | | - top: (pitch: PitchNotation) => (Pitch.midi(HIGHEST_PITCH) - Pitch.midi(pitch)) * halfStepHeightPx, |
| 39 | + top: (pitch: PitchNotation) => (Pitch[HIGHEST_PITCH].midi - Pitch[pitch].midi) * halfStepHeightPx, |
41 | 40 | time: (left: number) => Time.atNote(Notes.orClamp(Range.positive.clamp(left / wholeNoteWidthPx))), |
42 | | - pitch: (top: number) => |
43 | | - Pitch.parseMidi(Pitch.midiRange.clamp(-top / halfStepHeightPx + Pitch.midi(HIGHEST_PITCH), 'ceil')).notation, |
| 41 | + pitch: (top: number) => { |
| 42 | + const nearestMidi = Math.ceil(-top / halfStepHeightPx + Pitch[HIGHEST_PITCH].midi) |
| 43 | + const midiPitch = Pitch[nearestMidi] |
| 44 | + assertDefined(midiPitch) |
| 45 | + return midiPitch.notation |
| 46 | + }, |
44 | 47 | }, |
45 | 48 | } as const |
46 | 49 | } |
0 commit comments