AM Posted December 27, 2020 Share Posted December 27, 2020 (edited) for my current project i have to CONVERT/MAP pitches/lengths/velocity or MIDI into a binary sequence. so i coded this simple FUNCTION... feel free to use/adapt.... greetings andré ;; SUB (defun fill-to-x-bit (listseq &key (bitlength 7)) (loop for i in listseq when (< (length i) bitlength) collect (append (gen-repeat (- bitlength (length i)) 0) i) else collect i)) ;; MAIN (defun convert-to-binary (alist &key (parameter 'pitch) (length-resolution 127) (velocity-resolution 12) (pitch-resolution nil)) (let ((alist (progn (setf alist (cond ((stringp alist) (flatten (midi-to-omn alist :instrument 1))) (t alist))) (if (omn-formp alist) (cond ((equal parameter 'pitch) (setf alist (omn :pitch alist))) ((equal parameter 'length) (setf alist (omn :length alist))) ((equal parameter 'velocity) (setf alist (omn :velocity alist)))) alist)))) (cond ((pitchp (car alist)) (progn (setf alist (pitch-to-midi (pitch-melodize alist))) (fill-to-x-bit (decimal-to-binary (vector-round 0 (if (null pitch-resolution) (- (find-max alist) (find-min alist)) pitch-resolution) alist))))) ((lengthp (car alist)) (fill-to-x-bit (decimal-to-binary (vector-round 0 length-resolution (mapcar 'float (omn :length alist)))))) ((velocityp (car alist)) (fill-to-x-bit (decimal-to-binary (vector-round 0 velocity-resolution (get-velocity alist))))) ))) ;; EXAMPLES => the function recognizes the input-format: omn, pitch, length... or MIDI (convert-to-binary '(c4 d4 eb3 e5 f3)) => ((0 0 0 1 0 0 1) (0 0 0 1 0 1 1) (0 0 0 0 0 0 0) (0 0 1 1 0 0 1) (0 0 0 0 0 1 0)) (convert-to-binary '(t c4 d4 eb3 e5) :parameter 'pitch) => ((0 0 0 1 0 0 1) (0 0 0 1 0 1 1) (0 0 0 0 0 0 0) (0 0 1 1 0 0 1)) (convert-to-binary '(w h q -1/4 w -s -3t t t. s 3s 5q q. w)) => ((1 1 1 1 1 1 1) (1 0 0 1 1 0 0) (0 1 1 0 0 1 1) (0 0 0 0 0 0 0) (1 1 1 1 1 1 1) (0 0 1 0 0 1 1) (0 0 1 1 0 0 0) (0 0 1 1 1 0 1) (0 0 1 1 1 1 0) (0 1 0 0 0 0 0) (0 0 1 1 1 0 0) (0 0 1 1 1 1 0) (1 0 0 0 0 0 0) (1 1 1 1 1 1 1)) (convert-to-binary '(ppppp pppp ppp pp p mp mf f ff fff ffff fffff)) => ((0 0 0 0 0 0 0) (0 0 0 0 0 0 1) (0 0 0 0 0 1 0) (0 0 0 0 0 1 1) (0 0 0 0 1 0 0) (0 0 0 0 1 0 1) (0 0 0 0 1 1 0) (0 0 0 0 1 1 1) (0 0 0 1 0 0 1) (0 0 0 1 0 1 0) (0 0 0 1 0 1 1) (0 0 0 1 1 0 0)) ;; for midi => insert midipath (convert-to-binary <midipath> :parameter 'pitch) => Edited December 28, 2020 by AM bug Stephane Boussuge, JulioHerrlein and opmo 2 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.