Skip to content

Commit

Permalink
Fix sample length calculation
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
cemolcay committed Mar 13, 2024
1 parent 49fdd12 commit ef42d8e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/MusicTheory/NoteValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Sources/MusicTheory/Tempo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion Tests/MusicTheoryTests/MusicTheoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit ef42d8e

Please sign in to comment.