Jump to content

convert-to-binary


Recommended Posts

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 by AM
bug
Link to post
Share on other sites

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