Jump to content

hertz-to-midi and harmonic series (sharing)


Recommended Posts

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)

 

Link to comment
Share on other sites

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.

Edited by opmo
Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

Terms of Use Privacy Policy