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
38 changes: 25 additions & 13 deletions Sources/Tonic/Note.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,31 @@ public struct Note: Sendable, Equatable, Hashable, Codable {

/// MIDI Note 0-127 starting at C
public var noteNumber: Int8 {
let octaveBounds = ((octave + 2) * 12) ... ((octave + 3) * 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)
}
if octave < -2 {
return 0
}
if octave > 8 {
return 127
}
let octaveBounds = ((octave + 2) * 12) ... ((octave + 3) * 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
}
if note < 0 {
return 0
}
if note > 127 {
return 127
}
return Int8(note)
}

/// The pitch for the note
public var pitch: Pitch {
Expand Down
17 changes: 17 additions & 0 deletions Tests/TonicTests/NoteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,21 @@ final class NoteTests: XCTestCase {
let empty = NoteSet()
XCTAssertNil(empty.first)
}

func testClampNoteBounds() {
let bMinus3 = Note(.B, octave: -3)
XCTAssertEqual(bMinus3.noteNumber, 0)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm following the spacing convention established in other tests in this file, where assertion statements
are grouped with their supporting setup statements using blank lines. This convention
helps clarify the logical grouping and makes the test structure more readable.

let cFlatMinus3 = Note(.C, accidental: .flat, octave: -3)
XCTAssertEqual(cFlatMinus3.noteNumber, 0)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm following the spacing convention established in other tests in this file, where assertion statements
are grouped with their supporting setup statements using blank lines. This convention
helps clarify the logical grouping and makes the test structure more readable.

let a8 = Note(.A, octave: 8)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm following what appears to be a well established convention in the tests for identifiers referring to notes

XCTAssertEqual(a8.noteNumber, 127)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm following the spacing convention established in other tests in this file, where assertion statements
are grouped with their supporting setup statements using blank lines. This convention
helps clarify the logical grouping and makes the test structure more readable.

let gSharp8 = Note(.G, accidental: .sharp, octave: 8)
XCTAssertEqual(gSharp8.noteNumber, 127)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm following the spacing convention established in other tests in this file, where assertion statements
are grouped with their supporting setup statements using blank lines. This convention
helps clarify the logical grouping and makes the test structure more readable.

let c9 = Note(.C, octave: 9)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm following what appears to be a well established convention in the tests for identifiers referring to notes

XCTAssertEqual(c9.noteNumber, 127)
}
}
Loading