Jump to content

Featured Replies

Posted

Here is a function that converts hertz to midi:

(defun hertz-to-midi (frequency) 
    "Gets the corresponding MIDI value from a Hertz frequency"
    (round        
         ;; https://en.wikipedia.org/wiki/MIDI_Tuning_Standard#Frequency_values
         ;; d = 69 + 12*log2(f/440Hz)
         (+ 69 (* 12 (log (/ frequency 440) 2)))))

 

This allows two more useful functions to be made:

(defun hertz-to-integer (frequency)
    "Gets the corresponding pitch integer value from a Hertz frequency" 
    (let ((*standard-output* (make-broadcast-stream))) ; Override function prints 
         (midi-to-integer 
             (hertz-to-midi frequency))))

(defun hertz-to-pitch (frequency)
    "Gets the corresponding pitch from a Hertz frequency"
    (midi-to-pitch 
         (hertz-to-midi frequency)))

 

This allows us to play with the harmonic series ( https://en.wikipedia.org/wiki/Harmonic_series_(music) )

;;; Nth Harmonic

(defun n-harmonic-hertz (note n) 
    "Gets the n harmonic of a note as a hertz (Hz) value"
    ;; n * 440 * 2^( (m - 69) * (1/12) )
    (let ((*standard-output* (make-broadcast-stream))) ; Override function prints 
         (* n 440 (expt 2 (* (/ 1 12) (- (pitch-to-midi note) 69))))))

(defun n-harmonic-midi (note n) 
    "Gets the n harmonic of a note as a midi value"
    (hertz-to-midi (n-harmonic-hertz note n)))

(defun n-harmonic-pitch (note n) 
    "Gets the n harmonic of a note as a pitch (tempered) value"
    (let ((*standard-output* (make-broadcast-stream))) ; Override function prints 
         (midi-to-pitch (n-harmonic-midi note n))))

(defun n-harmonic-integer (note n) 
    "Gets the n harmonic of a note as a pitch (tempered) value"
    (hertz-to-integer (n-harmonic-hertz note n)))


;;; Harmonic series 

(defun harmonic-series-pitch (fundamental number) 
    "Gets 'number' amount of superior harmonics for a note as a list as pitches"
    (loop for i from 1 to number append (list (n-harmonic-pitch fundamental i))))

(defun harmonic-series-midi (fundamental number) 
    "Gets 'number' amount of superior harmonics for a note as a list as MIDI values"
    (loop for i from 1 to number append (list (n-harmonic-midi fundamental i))))

(defun harmonic-series-hertz (fundamental number) 
    "Gets 'number' amount of superior harmonics for a note as a list as Hertz frequencies"
    (loop for i from 1 to number append (list (n-harmonic-hertz fundamental i))))

(defun harmonic-series-integer (fundamental number) 
    "Gets 'number' amount of superior harmonics for a note as a list as integer values"
    (loop for i from 1 to number append (list (n-harmonic-integer fundamental i))))

 

Examples: 

;;; Examples

(harmonic-series-hertz 'a4 8)
=> (440 880 1320 1760 2200 2640 3080 3520)

(harmonic-series-midi 'a4 8)
=> (69 81 88 93 97 100 103 105)

(harmonic-series-integer 'a4 8)
=> (9 21 28 33 37 40 43 45)

(harmonic-series-pitch 'a4 8)
=> (a4 a5 e6 a6 cs7 e7 g7 a7)

Thank you Alain for the harmonic series function, I will add them to the forthcoming update with some extended functionality.
At the moment I am playing with the spectral composition tools and the missing Hertz conversion tools have been added to Opusmodus already.

  • 2 weeks later...
  • Author

I saw the update. This is great! I'm very glad I could contribute.

Create an account or sign in to comment


Copyright © 2014-2025 Opusmodus™ Ltd. All rights reserved.
Product features, specifications, system requirements and availability are subject to change without notice.
Opusmodus, the Opusmodus logo, and other Opusmodus trademarks are either registered trademarks or trademarks of Opusmodus Ltd.
All other trademarks contained herein are the property of their respective owners.

Powered by Invision Community

Important Information

Terms of Use Privacy Policy