import numpy as np
import soundbox
import sys

if len(sys.argv) != 2:
    print(f"""Utilisation: {sys.argv[0]} [output]

Génère un morceau au synthétiseur dans le fichier [output].""")
    sys.exit(1)

output_file = sys.argv[1]


def sharp_sine(dur, freq, value=1):
    return soundbox.envelope(
        attack=.005, decay=.1, release=.1,
        signal=soundbox.sine(dur + .2, freq, value))


def smooth_sine(dur, freq, value=1):
    return soundbox.envelope(
        attack=.1, decay=.2, release=.1,
        signal=soundbox.sine(dur + .2, freq, value))


length = 9
signal = soundbox.silence(length)

chords_l = (
    (('do', 2),),
    (('sol', 2),),
    (('la', 2),),
    (('sol', 2),),
    (('do', 2),),
    (('sol', 2),),
    (('la', 2),),
    (('sol', 2),),
)

chords_r = (
    (('do', 3), ('mi', 3), ('sol', 3)),
    (('sol', 3), ('si', 3), ('re', 4)),
    (('la', 3), ('do', 4), ('mi', 4)),
    (('fa', 3), ('la', 3), ('do', 4)),
)

for shift in np.arange(.5, length - 4, 4):
    for i in range(len(chords_l)):
        soundbox.add_signal(signal, start=i / 2 + shift,
            source=soundbox.chord(
                instr=smooth_sine, dur=.6 if shift == 4.5 and i == 7 else .4,
                freqs=soundbox.note_freqs(chords_l[i]),
                value=.4
            ))

    for i in range(len(chords_r)):
        soundbox.add_signal(signal, start=i + shift,
            source=soundbox.chord(
                instr=sharp_sine, dur=0.9,
                freqs=soundbox.note_freqs(chords_r[i]),
                value=.4
            ))

soundbox.save_signal(output_file, signal)
