diff --git a/Sources/MusicTheory/NoteValue.swift b/Sources/MusicTheory/NoteValue.swift index 8cdf19c..06b994d 100644 --- a/Sources/MusicTheory/NoteValue.swift +++ b/Sources/MusicTheory/NoteValue.swift @@ -105,7 +105,7 @@ public enum NoteModifier: Double, Codable, CaseIterable, CustomStringConvertible /// - noteValueType: The note value type to measure the length of the note value. /// - Returns: Returns how many notes of a single `NoteValueType` is equivalent to a given `NoteValue`. public func / (noteValue: NoteValue, noteValueType: NoteValueType) -> Double { - return noteValue.modifier.rawValue * noteValueType.rate / noteValue.type.rate + return (noteValue.type.rate * noteValue.modifier.rawValue) / noteValueType.rate } /// Defines the duration of a note beatwise. diff --git a/Sources/MusicTheory/Tempo.swift b/Sources/MusicTheory/Tempo.swift index 81ad921..dd80cbd 100644 --- a/Sources/MusicTheory/Tempo.swift +++ b/Sources/MusicTheory/Tempo.swift @@ -41,7 +41,7 @@ public struct Tempo: Codable, Hashable, CustomStringConvertible { /// - Returns: Returns the sample length of a note value. public func sampleLength(of noteValue: NoteValue, sampleRate: Double = 44100.0) -> Double { let secondsPerBeat = 60.0 / bpm - return secondsPerBeat * sampleRate * ((4 / noteValue.type.rate) * noteValue.modifier.rawValue) + return secondsPerBeat * sampleRate * ((Double(timeSignature.beats) * noteValue.type.rate) * noteValue.modifier.rawValue) } /// Calculates the LFO speed of a note vaule in hertz. diff --git a/Tests/MusicTheoryTests/MusicTheoryTests.swift b/Tests/MusicTheoryTests/MusicTheoryTests.swift index 3381b34..0edf8d3 100644 --- a/Tests/MusicTheoryTests/MusicTheoryTests.swift +++ b/Tests/MusicTheoryTests/MusicTheoryTests.swift @@ -119,9 +119,14 @@ extension MusicTheoryTests { extension MusicTheoryTests { func testNoteValueConversions() { - let noteValue = NoteValue(type: .half, modifier: .dotted) + var noteValue = NoteValue(type: .half, modifier: .dotted) XCTAssertEqual(noteValue / NoteValueType.sixteenth, 12) XCTAssertEqual(noteValue / NoteValueType.whole, 0.75) + noteValue = NoteValue(type: .half, modifier: .default) + XCTAssertEqual(noteValue / NoteValueType.sixteenth, 8) + XCTAssertEqual(noteValue / NoteValueType.whole, 0.5) + XCTAssertEqual(noteValue / NoteValueType.quarter, 2) + XCTAssertEqual(noteValue / NoteValueType.thirtysecond, 16) } func testDurations() {