Skip to content

Commit

Permalink
Merge pull request #411 from ryohey/fix-outside-note
Browse files Browse the repository at this point in the history
Fix problem that notes can be placed out of range
  • Loading branch information
ryohey authored Oct 2, 2024
2 parents 6cb0fcc + fd8af27 commit ff78f9f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/src/actions/song.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NotePoint } from "../entities/transform/NotePoint"
import { NoteNumber } from "../entities/unit/NoteNumber"
import { isNotNull } from "../helpers/array"
import { downloadSongAsMidi } from "../midi/midiConversion"
import Song, { emptySong } from "../song"
Expand Down Expand Up @@ -141,7 +141,7 @@ export const transposeNotes =
}
return {
id,
noteNumber: NotePoint.clampNoteNumber(n.noteNumber + deltaPitch),
noteNumber: NoteNumber.clamp(n.noteNumber + deltaPitch),
}
})
.filter(isNotNull),
Expand Down
7 changes: 6 additions & 1 deletion app/src/actions/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AnyChannelEvent, AnyEvent, SetTempoEvent } from "midifile-ts"
import { ValueEventType } from "../entities/event/ValueEventType"
import { Range } from "../entities/geometry/Range"
import { Measure } from "../entities/measure/Measure"
import { NoteNumber } from "../entities/unit/NoteNumber"
import { closedRange, isNotUndefined } from "../helpers/array"
import { isEventInRange } from "../helpers/filterEvents"
import {
Expand Down Expand Up @@ -234,7 +235,11 @@ export const createNote =
song,
}: RootStore) =>
(tick: number, noteNumber: number) => {
if (selectedTrack === undefined || selectedTrack.channel == undefined) {
if (
selectedTrack === undefined ||
selectedTrack.channel == undefined ||
!NoteNumber.isValid(noteNumber)
) {
return
}
pushHistory()
Expand Down
11 changes: 10 additions & 1 deletion app/src/components/PianoRoll/PianoRoll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,18 @@ const PianoRollWrapper: FC = observer(() => {
[s, transform],
)

const onChangeSplitPane = useCallback(() => {
s.setScrollTopInPixels(s.scrollTop)
}, [s])

return (
<Parent ref={ref}>
<StyledSplitPane split="horizontal" minSize={50} defaultSize={"60%"}>
<StyledSplitPane
split="horizontal"
minSize={50}
defaultSize={"60%"}
onChange={onChangeSplitPane}
>
<Alpha onWheel={onWheel} ref={alphaRef}>
<PianoRollStage width={size.width} height={alphaHeight} />
<VerticalScaleScrollBar
Expand Down
7 changes: 2 additions & 5 deletions app/src/entities/transform/NotePoint.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { MaxNoteNumber } from "../../Constants"
import { NoteNumber } from "../unit/NoteNumber"

export interface NotePoint {
tick: number
noteNumber: number
}

export namespace NotePoint {
export const clampNoteNumber = (noteNumber: number) =>
Math.min(MaxNoteNumber, Math.max(0, noteNumber))

export const clamp = (point: NotePoint): NotePoint => ({
tick: Math.max(0, point.tick),
noteNumber: clampNoteNumber(point.noteNumber),
noteNumber: NoteNumber.clamp(point.noteNumber),
})

export function equals(a: NotePoint, b: NotePoint): boolean {
Expand Down
11 changes: 11 additions & 0 deletions app/src/entities/unit/NoteNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MaxNoteNumber } from "../../Constants"

export type NoteNumber = number

export namespace NoteNumber {
export const clamp = (noteNumber: number) =>
Math.min(MaxNoteNumber, Math.max(0, noteNumber))

export const isValid = (noteNumber: number) =>
noteNumber >= 0 && noteNumber <= MaxNoteNumber
}

0 comments on commit ff78f9f

Please sign in to comment.