A simple way to get a music pitch from a frequency.

Related tags

Audio Pitchy
Overview

Pitchy

CI Status Version Carthage Compatible License Platform

Pitchy provides a simple way to get a music pitch from a frequency. Other than that it has a bunch of useful data structures, calculators and helper functions to work with notes, octaves and acoustic waves.

From Wikipedia:

Pitch is a perceptual property of sounds that allows their ordering on a frequency-related scale, or more commonly, pitch is the quality that makes it possible to judge sounds as "higher" and "lower" in the sense associated with musical melodies.

Table of Contents

Pitchy Icon

Key features

  • Get lower, higher and closest pitch offsets from a specified frequency.
  • Get an acoustic wave with wavelength, period and harmonics.
  • Create a note from a pitch index, frequency or a letter with octave number.
  • Calculate a frequency, note letter and octave from a pitch index
  • Find a pitch index from a specified frequency or a note letter with octave.
  • Convert a frequency to wavelength and vice versa.
  • Convert a wavelength to time period and vice versa.

Usage

Pitch

Create Pitch struct with a specified frequency to get lower, higher and closest pitch offsets:

do {
  // Frequency = 445 Hz
  let pitch = try Pitch(frequency: 445.0)
  let pitchOffsets = pitch.offsets

  print(pitchOffsets.lower.frequency)     // 5 Hz
  print(pitchOffsets.lower.percentage)    // 19.1%
  print(pitchOffsets.lower.note.index)    // 0
  print(pitchOffsets.lower.cents)         // 19.56

  print(pitchOffsets.higher.frequency)    // -21.164 Hz
  print(pitchOffsets.higher.percentage)   // -80.9%
  print(pitchOffsets.higher.note.index)   // 1
  print(pitchOffsets.higher.cents)        // -80.4338

  print(pitchOffsets.closest.note.string) // "A4"

  // You could also use acoustic wave
  print(pitch.wave.wavelength)            // 0.7795 meters
} catch {
  // Handle errors
}

Acoustic wave

Get an acoustic wave with wavelength, period and harmonics.

do {
  // AcousticWave(wavelength: 0.7795)
  // AcousticWave(period: 0.00227259)
  let wave = try AcousticWave(frequency: 440.0)

  print(wave.frequency)       // 440 Hz
  print(wave.wavelength)      // 0.7795 meters
  print(wave.period)          // 0.00227259 s
  print(wave.harmonics[0])    // 440 Hz
  print(wave.harmonics[1])    // 880 Hz
} catch {
  // Handle errors
}

Note

Note could be created with a corresponding frequency, letter + octave number or a pitch index.

do {
  // Note(frequency: 261.626)
  // Note(letter: .C, octave: 4)
  let note = try Note(index: -9)

  print(note.index)                 // -9
  print(note.letter)                // .C
  print(note.octave)                // 4
  print(note.frequency)             // 261.626 Hz
  print(note.string)                // "C4"
  print(try note.lower().string)    // "B3"
  print(try note.higher().string)   // "C#4"
} catch {
  // Handle errors
}

Calculators

Calculators are used in the initialization of Pitch, AcousticWave and Note, but also are included in the public API.

do {
  // PitchCalculator
  let pitchOffsets = try PitchCalculator.offsets(445.0)
  let cents = try PitchCalculator.cents(
    frequency1: 440.0,
    frequency2: 440.0
  ) // 19.56

  // NoteCalculator
  let frequency1 = try NoteCalculator.frequency(forIndex: 0)       // 440.0 Hz
  let letter = try NoteCalculator.letter(forIndex: 0)              // .A
  let octave = try NoteCalculator.octave(forIndex: 0)              // 4
  let index1 = try NoteCalculator.index(forFrequency: 440.0)       // 0
  let index2 = try NoteCalculator.index(forLetter: .A, octave: 4)  // 0

  // WaveCalculator
  let f = try WaveCalculator.frequency(forWavelength: 0.7795)      // 440.0 Hz
  let wl1 = try WaveCalculator.wavelength(forFrequency: 440.0)     // 0.7795 meters
  let wl2 = try WaveCalculator.wavelength(forPeriod: 0.00227259)   // 0.7795 meters
  let period = try WaveCalculator.period(forWavelength: 0.7795)    // 0.00227259 s
} catch {
  // Handle errors
}

FrequencyValidator

With a help of FrequencyValidator it's possible to adjust minimum and maximum frequencies that are used for validations in all calculations:

FrequencyValidator.minimumFrequency = 20.0
FrequencyValidator.maximumFrequency = 4190.0

Error handling

Almost everything is covered with tests, but it's important to pass valid values, such as frequencies and pitch indexes. That's why there a re a list of errors that should be handled properly.

enum Error: ErrorType {
  case InvalidFrequency
  case InvalidWavelength
  case InvalidPeriod
  case InvalidPitchIndex
  case InvalidOctave
}

Installation

Pitchy is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Pitchy'

Pitchy is also available through Carthage. To install just write into your Cartfile:

github "vadymmarkov/Pitchy"

Author

Vadym Markov, [email protected]

Contributing

Check the CONTRIBUTING file for more info.

License

Pitchy is available under the MIT license. See the LICENSE file for more info.

You might also like...
YiVideoEditor is a library for rotating, cropping, adding layers (watermark) and as well as adding audio (music) to the videos.

YiVideoEditor YiVideoEditor is a library for rotating, cropping, adding layers (watermark) and as well as adding audio (music) to the videos. YiVideoE

🎶 Control Apple Music right from your macOS menu bar.
🎶 Control Apple Music right from your macOS menu bar.

About Music Bar is macOS application that places music controls right in your menu bar. Installation Download and unzip the latest release. Drag the a

macOS app that allows the control of Spotify and AppleMusic/iTunes music playback from the menu bar.
macOS app that allows the control of Spotify and AppleMusic/iTunes music playback from the menu bar.

PlayStatus is a simple macOS app that allows the control of Spotify, Apple Music(macOS 10.15+) and iTunes including iTunes Radio/Beats1 playback from

:musical_note: A Mac app wrapper for music.youtube.com
:musical_note: A Mac app wrapper for music.youtube.com

A simple Mac app wrapper using WKWebView for YouTube Music that allows YouTube Music to run as a standalone process. Features Media Keys Keyboard shor

Unofficial Yandex Music API
Unofficial Yandex Music API

YM-API - Unofficial Swift Yandex Music Library Fully ported Yandex Music API Swift implementation. Russian Readme (Readme на русском) Thanks to Marsha

A charmful decade with many colors patterns, disco music, and other cultural expressions that we refer to as vintage
A charmful decade with many colors patterns, disco music, and other cultural expressions that we refer to as vintage

MontyHallProblem Welcome to the 70s! 🕺 That is a charmful decade with many colors patterns, disco music, and other cultural expressions that we refer

App for searching music and films in iTunes
App for searching music and films in iTunes

inMovies App description: This is an app for searching contnent such as films and music in iTunes. Running the app brings you to page with following c

Custom iOS music player view
Custom iOS music player view

InteractivePlayerView Custom iOS music player view Screen About InteractivePlayerView is an IBDesignableView (Custom View) which has its own progress,

Cool Animated music indicator view written in Swift
Cool Animated music indicator view written in Swift

Cool Animated music indicator view written in Swift. ESTMusicIndicator is an implementation of NAKPlaybackIndicatorView in Swift for iOS 8. 本人著作的书籍《La

Comments
  • Pitch with different base frequency

    Pitch with different base frequency

    Hi! At first thank you for useful library.

    There is a way in your library to change base frequency of Pitch? In other words can I change pitch from A440 (when a4 = 440hz) to scientific C256 (when c1=256hz) ?

    opened by kreshikhin 1
  • Allow user to change frequency

    Allow user to change frequency

    Not each instrument is created equal and some musicians like to tune to 442, which is why this change is necessary to make the NoteCalculator.Standard.frequency customizable.

    opened by timroesner 2
Releases(3.0.0)
  • 3.0.0(Oct 9, 2017)

  • 2.0.0(Sep 15, 2016)

  • 1.0.0(Dec 30, 2015)

    Initial release:

    • Get lower, higher and closest pitch offsets from a specified frequency.
    • Get an acoustic wave with wavelength, period and harmonics.
    • Create a note from a pitch index, frequency or a letter with octave number.
    • Calculate a frequency, note letter and octave from a pitch index
    • Find a pitch index from a specified frequency or a note letter with octave.
    • Convert a frequency to wavelength and vice versa.
    • Convert a wavelength to time period and vice versa.
    Source code(tar.gz)
    Source code(zip)
Owner
Vadym Markov
iOS Software Engineer
Vadym Markov
MusicScore - A music score library with MusicPart, Measure, Note, Pitch and Tempo representations in swift structs

MusicScore A music score library with MusicPart, Measure, Note, Pitch and Tempo

null 7 Sep 19, 2022
Swift-music - swift-music is a swift package that provides an easy-to-use API for music related developments.

?? swift-music Introduction swift-music is a swift package that provides an easy-to-use API for music related developments. Currently available module

Jin Zhang 4 Feb 8, 2022
Classical music front-end for Apple Music: iOS app

concertino_ios Concertino is a classical music front-end for Apple Music. It's splitted in several projects. This one provides only the iOS app. (Ther

Open Opus 107 Dec 22, 2022
iOS music player app that downloads music from the internet, even YouTube

About YouTag is an iOS music player app that downloads music from the internet, even YouTube, and manages it in a local library. Music videos can also

null 263 Jan 8, 2023
A pure Swift Spotify Music App in Apple Music style

HBMusic A pure Swift Spotify Music App in Apple Music style. How to run pod inst

haoboxuxu 6 Dec 29, 2021
MusicKit is a framework and DSL for creating, analyzing, and transforming music in Swift.

MusicKit MusicKit is a framework and DSL for creating, analyzing, and transforming music in Swift. Examples Functional harmony let C5 = Pitch(midi: 72

Ben Guo 591 Oct 18, 2022
Beautiful Music Player app built using SwiftUI to demonstrate Neumorphic design pattern and MVVM architecture.

Beautiful Music Player app built using SwiftUI to demonstrate Neumorphic design pattern ?? and MVVM architecture ?? . Made with love ❤️ by Sameer Nawaz

Sameer Nawaz 120 Jan 4, 2023
Music Player for iOS which looks & feels like classic player

Prodigal Music Player APP looks and feels like a classic device. Bring back the good old player to life. Screenshots Home Page Album Gallery Home Page

bob.sun 40 Nov 11, 2022
A real-time, votable, democratized music queue on iPad and iPhone using Spotify

Queue'd Music Queue'd is the best way to enjoy music with your friends. Add your favorite songs to a shared music queue at your favorite bars, restaur

Ryan Daulton 88 Dec 2, 2022
MuVis is a macOS, iOS, iPadOS app for real-time music visualization.

MuVis MuVis is an open-source multiplatform app (using SwiftUI, Swift, and Xcode) for music visualization. It renders informative (and musically usefu

Keith Bromley 7 Oct 10, 2022