Tonic icon indicating copy to clipboard operation
Tonic copied to clipboard

Out of bounds Int8 when recognizing chords.

Open diquah opened this issue 1 year ago • 0 comments

macOS Version(s) Used to Build

macOS 13 Ventura

Xcode Version(s)

Xcode 14

Description

When recognizing a chord as shown below:

/// INSIDE MY PROJECT

func recognizeChord() -> Chord? {
    var currentPitches: PitchSet = PitchSet()
    for (keyID, pressed) in midiHelper.MIDIKeysDown {
        if pressed {
            currentPitches.add(Pitch(intValue: keyID.clamped(to: 0..<128)))
        }
    }
    let rankings = Chord.getRankedChords(from: currentPitches, allowTheoreticalChords: true)
    return rankings.first
}

The program hard crashes with an out of range exception for the Int8

/// INSIDE TONIC

public var noteNumber: Int8 {
    let octaveBounds = ((octave + 1) * 12) ... ((octave + 2) * 12)
    var note = Int(noteClass.letter.baseNote) + Int(noteClass.accidental.rawValue)
    if noteClass.letter == .B && noteClass.accidental.rawValue > 0 {
        note -= 12
    }
    if noteClass.letter == .C && noteClass.accidental.rawValue < 0 {
        note += 12
    }
    while !octaveBounds.contains(note) {
        note += 12
    }
    return Int8(note) // <--- Error Here
}

The error occurs above D7 (MIDI Key 98).

Crash Logs, Screenshots or Other Attachments (if applicable)

image

diquah avatar Oct 15 '24 02:10 diquah