;(in-package :common-lisp-user)
(load
 (merge-pathnames
  (make-pathname
   :directory '(:relative "File conversion")
   :name "csv-files"
   :type "lisp")
  *MCStylistic-MonthYear-functions-path*))
(load
 (merge-pathnames
  (make-pathname
   :directory '(:relative "File conversion")
   :name "kern-by-col"
   :type "lisp")
  *MCStylistic-MonthYear-functions-path*))
(load
 (merge-pathnames
  (make-pathname
   :directory '(:relative "File conversion")
   :name "midi-export"
   :type "lisp")
  *MCStylistic-MonthYear-functions-path*))

(setq
 *music-data-root*
 (make-pathname
  :directory
  '(:absolute
    "Users" "tomthecollins" "Shizz" "Data" "Music"
    "bachChorales")))
(setq
 *path&name*
 (merge-pathnames
  (make-pathname
   :directory
   '(:relative
     "bachChoraleBWV244p37" "polyphonic"))
  *music-data-root*))
(setq *piece-name* "bachChoraleBWV244p37")

(setq
 kern-source
 (merge-pathnames
  (make-pathname
   :directory
   '(:relative "kern") :name *piece-name* :type "krn")
  *path&name*))
(setq
 csv-destination
 (merge-pathnames
  (make-pathname
   :directory
   '(:relative "csv") :name *piece-name* :type "csv")
  *path&name*))
(setq
 MIDI-destination
 (merge-pathnames
  (make-pathname
   :directory
   '(:relative "midi") :name *piece-name* :type "mid")
  *path&name*))
(setq
 dataset-destination
 (merge-pathnames
  (make-pathname
   :directory
   '(:relative "lisp") :name *piece-name* :type "txt")
  *path&name*))

(progn
  (setq *scale* 1000)
  (setq *anacrusis* -1)
  (setq
   dataset (kern-file2dataset-by-col kern-source))
  ; Remove the roots of the chord labels from the set.
  (setq
   dataset-restrict
   (sort-dataset-asc
    (restrict-dataset-in-nth-to-tests
     dataset 4 (list #'<=) (list 3))))
  (saveit
   MIDI-destination
   (modify-to-check-dataset dataset-restrict *scale*))
  (write-to-file
   (mapcar
    #'(lambda (x)
        (append
         (list (+ (first x) *anacrusis*))
         (rest x)))
    dataset-restrict)
   dataset-destination)
  (dataset2csv
   dataset-destination csv-destination))

; Check for spurious durations.
(restrict-dataset-in-nth-to-tests
 dataset-restrict 3 (list #'>=) (list 2))
(length
 (restrict-dataset-in-nth-to-tests
  dataset-restrict 3 (list #'>=) (list 2)))
