## SPDX-FileCopyrightText: 2021 The SMW Music Python Project Authors
## <https://github.com/com-posers-pit/smw_music/blob/develop/AUTHORS.rst>
##
## SPDX-License-Identifier: AGPL-3.0-only
#amk 2

; beer v${version}
%if datetime:
; Built: ${datetime}
%endif

#spc {
    #author  "${song.composer}"
    #title   "${song.title}"
    #game    "${song.game}"
    #comment "Ported by: ${song.porter}"
}
%if custom_samples:

#path "${sample_path}"

#samples {
    #optimized
%for sample in set(x[0] for x in custom_samples):
    "${sample}"
%endfor
}

#instruments {
%for sample, settings, idx in custom_samples:
    "${sample}"${' '*(max(len(x[0]) for x in custom_samples) - len(sample))}          ${settings} ; @${idx}
%endfor
}
%else:

#samples {
    #optimized
}
%endif

; PORTER: GLOBAL VOLUME HERE
w${song.volume}

%if inst_samples:
; PORTER: INSTRUMENT DEFINITIONS HERE

<%
    do_transpose = False # any(x.transpose for x in inst_samples.values()) TODO: removed due to music21 bug

    voice = lambda x: x + "_voice"
    dyn = lambda x: x + "_dyn"
    artic = lambda x: x + "_artic"
    pan = lambda x: x + "_pan"

    voice_sz = max(len(voice(name)) for name in inst_samples.keys())
    dyn_sz = max(len(dyn(name)) for name in inst_samples.keys())
    artic_sz = max(len(artic(name)) for name in inst_samples.keys())
    pan_sz = max(len(pan(name)) for name in inst_samples.keys())

    def dyn_str(sample):
        rv = []
        for vol, val in sample.dynamics.items():
            rv.append(f'\\"_{vol}={val:02X}\\"')
        return ' '.join(rv)

    def artic_str(sample):
        rv = []
        for artic, val in sample.artics.items():
            rv.append(f'\\"q{artic}=q{val.setting:02X}\\"')
        return ' '.join(rv)

    def pan_str(sample):
        return sample.pan_str if sample.pan_enabled else ''

    def transpose(sample):
        return f' h{sample.transpose}' if do_transpose else ''

    def pitched_defn(name):
        return f'"{name} = {voice(name)} {dyn(name)} {artic(name)} {pan(name)} qDEF"'

    def unpitched_defn(name):
        return f'"{name} = {dyn(name)} {artic(name)} {pan(name)} qDEF @{voice(name)} {name}n"'

    unpitched_sz = max(list(len(unpitched_defn(k)) for k, v in inst_samples.items() if v.percussion), default=0)
%>\
; Instrument voice definitions
; Only change the instrument octave numbers on the right of the equals signs
%for name, sample in inst_samples.items():
"${f'{voice(name):{voice_sz}}'} = ${f'{sample.instrument_idx:3} o{sample.default_octave}'}"
%endfor

; Instrument dynamics definitions
; Yes, this is ugly.  But it lets you tie dynamic levels to instruments and
; crescendos. Only change the hex (00-FF) volumes right after each equals sign.
%for name, sample in inst_samples.items():
"${f'{dyn(name):{dyn_sz}}'} = ${dyn_str(sample)}"
%endfor

; Instrument articulation definitions
; Just like dynamics, only touch numbers after the 'q' after each equals sign.
%for name, sample in inst_samples.items():
"${f'{artic(name):{artic_sz}}'} = ${artic_str(sample)}"
%endfor

; Instrument pan definitions
; Set these to PAN_LEFT, PAN_MID_LEFT, PAN_CENTER, PAN_MID_RIGHT, PAN_RIGHT, or
; pick your own using a y command.  These are blank by default because not
; everyone uses them and it ; saves space to leave them out.
%for name, sample in inst_samples.items():
"${f'{pan(name):{pan_sz}} = {pan_str(sample)}'}"
%endfor

%endif
%if echo_config:
; Echo settings

${f'$EF${echo_config.channel_reg:02X}${echo_config.left_vol_reg:02X}${echo_config.right_vol_reg:02X}'}
${f'$F1${echo_config.delay:02X}${echo_config.fb_reg:02X}${echo_config.fir_filt:02X}'}

%endif
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Macro magic.  Look, but don't touch.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Instrument definitions.  Smoosh together the instrument voice definition
; with its dynamics, articulation, and pan definitions
%for name, sample in inst_samples.items():
%if not sample.percussion:
${pitched_defn(name)}
%endif
%endfor
%for name, sample in inst_samples.items():
%if sample.percussion:
${f'{unpitched_defn(name):{unpitched_sz}}'} "${name}n = o${sample.percussion_octave} ${sample.percussion_note}"
%endif
%endfor

; This is a way to use a hex value in a volume command.  We want to use hex
; values because that's how volume fades work.  Update the corresponding
; dynamics marks in each instrument.
%for dyn in dynamics:
${f'"v{str(dyn): <4} = $E7$_{dyn}"'}
%endfor

; Magic hex values are no fun, we'll give them nicer names
"LEGATO_ON     = $F4$01"
"LEGATO_OFF    = $F4$01"
"GLOBAL_LEGATO = $F4$02"
"CRESC         = $E8"
"DIM           = $E8"
"PAN_LEFT      = y20"
"PAN_MID_LEFT  = y12"
"PAN_CENTER    = y10"
"PAN_MID_RIGHT = y08"
"PAN_RIGHT     = y00"
"VIB_OFF       = $DF"
%for n, channel in enumerate(channels):

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Staff ${n + 1}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#${n}\
${" GLOBAL_LEGATO" if global_legato and (n == 0) else ""}\
${" /" if "/" not in channel else ""}

${channel}
%endfor
