% Arab maqam 36-ET notation.

% 12-ET major and minor second:
#(define-public MINORSECOND 1/2)
#(define-public MAJORSECOND 1)

% Diatonic accidentals:
#(define-public SHARP (- MAJORSECOND MINORSECOND))
#(define-public FLAT (- SHARP))
#(define-public FLATFLAT (* 2 FLAT))
#(define-public SHARPSHARP (* 2 SHARP))

% 1/3 12-ET alterations. Only one intermediate pitch where the combined sizes of
% the intervals half-flat and half-sharp is the same as that of the flats and sharps.
#(define-public HALFFLAT -2/6)
#(define-public HALFSHARP (+ HALFFLAT SHARP))
#(define-public FLATHALFFLAT (+ FLAT HALFFLAT))
#(define-public SHARPHALFSHARP (+ SHARP HALFSHARP))


% Define pitch names

maqamPitchNames = #`(
  (c . ,(ly:make-pitch -1 0 NATURAL))
  (d . ,(ly:make-pitch -1 1 NATURAL))
  (e . ,(ly:make-pitch -1 2 NATURAL))
  (f . ,(ly:make-pitch -1 3 NATURAL))
  (g . ,(ly:make-pitch -1 4 NATURAL))
  (a . ,(ly:make-pitch -1 5 NATURAL))
  (b . ,(ly:make-pitch -1 6 NATURAL))

  ;; b for flat.
  (cb . ,(ly:make-pitch -1 0 FLAT))
  (db . ,(ly:make-pitch -1 1 FLAT))
  (eb . ,(ly:make-pitch -1 2 FLAT))
  (fb . ,(ly:make-pitch -1 3 FLAT))
  (gb . ,(ly:make-pitch -1 4 FLAT))
  (ab . ,(ly:make-pitch -1 5 FLAT))
  (bb . ,(ly:make-pitch -1 6 FLAT))

  ;; s for sharp.
  (cs . ,(ly:make-pitch -1 0 SHARP))
  (ds . ,(ly:make-pitch -1 1 SHARP))
  (es . ,(ly:make-pitch -1 2 SHARP))
  (fs . ,(ly:make-pitch -1 3 SHARP))
  (gs . ,(ly:make-pitch -1 4 SHARP))
  (as . ,(ly:make-pitch -1 5 SHARP))
  (bs . ,(ly:make-pitch -1 6 SHARP))

  ;; hb for half-flat.
  (chb . ,(ly:make-pitch -1 0 HALFFLAT))
  (dhb . ,(ly:make-pitch -1 1 HALFFLAT))
  (ehb . ,(ly:make-pitch -1 2 HALFFLAT))
  (fhb . ,(ly:make-pitch -1 3 HALFFLAT))
  (ghb . ,(ly:make-pitch -1 4 HALFFLAT))
  (ahb . ,(ly:make-pitch -1 5 HALFFLAT))
  (bhb . ,(ly:make-pitch -1 6 HALFFLAT))

  ;; hs for half-sharp.
  (chs . ,(ly:make-pitch -1 0 HALFSHARP))
  (dhs . ,(ly:make-pitch -1 1 HALFSHARP))
  (ehs . ,(ly:make-pitch -1 2 HALFSHARP))
  (fhs . ,(ly:make-pitch -1 3 HALFSHARP))
  (ghs . ,(ly:make-pitch -1 4 HALFSHARP))
  (ahs . ,(ly:make-pitch -1 5 HALFSHARP))
  (bhs . ,(ly:make-pitch -1 6 HALFSHARP))

  ;; bhb for flat half-flat.
  (cbhb . ,(ly:make-pitch -1 0 FLATHALFFLAT))
  (dbhb . ,(ly:make-pitch -1 1 FLATHALFFLAT))
  (ebhb . ,(ly:make-pitch -1 2 FLATHALFFLAT))
  (fbhb . ,(ly:make-pitch -1 3 FLATHALFFLAT))
  (gbhb . ,(ly:make-pitch -1 4 FLATHALFFLAT))
  (abhb . ,(ly:make-pitch -1 5 FLATHALFFLAT))
  (bbhb . ,(ly:make-pitch -1 6 FLATHALFFLAT))

  ;; shs for sharp half-sharp.
  (cshs . ,(ly:make-pitch -1 0 SHARPHALFSHARP))
  (dshs . ,(ly:make-pitch -1 1 SHARPHALFSHARP))
  (eshs . ,(ly:make-pitch -1 2 SHARPHALFSHARP))
  (fshs . ,(ly:make-pitch -1 3 SHARPHALFSHARP))
  (gshs . ,(ly:make-pitch -1 4 SHARPHALFSHARP))
  (ashs . ,(ly:make-pitch -1 5 SHARPHALFSHARP))
  (bshs . ,(ly:make-pitch -1 6 SHARPHALFSHARP))
)

%% set pitch names.
pitchnames = \maqamPitchNames 
#(ly:parser-set-note-names parser maqamPitchNames)


%{ For some reason, putting in variables does not work:
maqamGlyphs = #'((FLATFLAT . "accidentals.flatflat")
       (FLATHALFFLAT . "accidentals.mirroredflat.flat")
       (FLAT . "accidentals.flat")
       (HALFFLAT . "accidentals.mirroredflat")
       (NATURAL . "accidentals.natural")
       (HALFSHARP . "accidentals.sharp.slashslash.stem")
       (SHARP . "accidentals.sharp")
       (SHARPHALFSHARP . "accidentals.sharp.slashslash.stemstemstem")
       (SHARPSHARP . "accidentals.doublesharp")
       )
%}

maqamGlyphs = #'((-1 . "accidentals.flatflat")
       (-5/6 . "accidentals.mirroredflat.flat")
       (-3/6 . "accidentals.flat")
       (-2/6 . "accidentals.mirroredflat")
       (0 . "accidentals.natural")
       (1/6 . "accidentals.sharp.slashslash.stem")
       (3/6 . "accidentals.sharp")
       (4/6 . "accidentals.sharp.slashslash.stemstemstem")
       (1 . "accidentals.doublesharp")
       )

setmagamglyphs = {\override Accidental #'glyph-name-alist = \maqamGlyphs
  \override Staff.KeySignature #'glyph-name-alist = \maqamGlyphs}


bayatiD = {\setmagamglyphs
  \set Staff.keySignature =  #'(
    (2 . -2/6)
    (6 . -1/2))}

%{ Causes error in lilypond/current/scm/lily-library.scm:135:5:
  In procedure ly:book-process in expression (process-procedure book paper ...)
  Wrong type (expecting exact integer): -5/3
bayati = #`(
    (0 . 0)
    (1 . ,HALFFLAT)
    (2 . ,FLAT)
    (3 . 0)
    (4 . 0)
    (5 . ,FLAT)
    (6 . ,FLAT)
  )  
%}

