#! /usr/bin/env python
# encoding: utf-8

import Task, TaskGen, Utils, Logs, os, errno, shutil

# task function for task "dsp"
# use the specified processor to create the output file and copy
# the output file to a corresponding directory in the
# source tree (the directory will be created if it doesn't
# yet exist)
def dsp2cc(task):
    src = task.inputs[0].srcpath(task.env)
    o = task.outputs[0]
    dst = o.bldpath(task.env)
    cpy = o.relpath_gen(o.bld.bldnode)
    cpydir = os.path.dirname(cpy)
    try:
        os.mkdir(cpydir)
    except OSError, e:
        if e.errno != errno.EEXIST:
            Logs.error("runner: cannot create directory -> %s" % e)
            return e.errno
    lst = [task.proc,"-o",dst] + task.proc_args + [src]
    Logs.debug("runner: system command -> %s" % " ".join(lst))
    ret = Utils.exec_command(lst,shell=False)
    if ret != 0:
        return ret
    try:
        shutil.copy2(dst, cpy)
    except (OSError, IOError), e:
        Logs.error("runner: cannot copy file -> %s" % e)
        return e.errno
    return 0

# definition of task "dsp"
Task.task_type_from_func(
    name    = 'dsp',
    func    = dsp2cc,
    color   = 'BLUE',
    ext_in  = '.dsp',
    ext_out = '.cc',
    before  = 'cc cxx')

@TaskGen.extension('.dsp')
def dsp_file(self, node):
    # process all ".dsp" files with task "dsp", put the
    # output file with extension ".cc" in a sibling directory
    # of the source directory with "-cc" appended to its name
    tsk = self.create_task('dsp')
    tsk.proc = self.proc
    tsk.proc_args = getattr(self, "proc_args", [])
    tsk.set_inputs(node)
    parent = node.parent
    o = parent.parent.find_or_declare([parent.name+"-cc",node.name])
    o = o.change_ext('.cc')
    tsk.set_outputs(o)
    bld = tsk.generator.bld
    bld.add_manual_dependency(node,bld.bldnode.find_resource(self.proc))

def build(bld):
    float_arg = ["-s","40000","-float"]
    if bld.env['FAUST_DOUBLE']:
        arg = ["-double"]
    else:
        arg = ["-float"]
    sources = [
        # amp
        'preamp.dsp',
        'inputgain.dsp',
        'noise_shaper.dsp',
        'AntiAlias.dsp',
        'HighShelf.dsp',
        'drive.dsp',
        'osc_tube.dsp',
        'reso_tube.dsp',
        'tube.dsp',
        'tube3.dsp',
        'tubevibrato.dsp',
        #'tone.dsp',
        'multifilter.dsp',
        'eq.dsp',
        'bassbooster.dsp',
        'outputgain.dsp',
        'feed.dsp',
        'balance.dsp',
        'balance1.dsp',
        'tonestack.dsp',
        'amp2.dsp',
        'stage3.dsp',

        # effects
        'overdrive.dsp',
        'compressor.dsp',
        'crybaby.dsp',
        'autowah.dsp',
        'distortion.dsp',
        'distortion1.dsp',
        'freeverb.dsp',
        'flanger.dsp',
        'impulseresponse.dsp',
        'moog.dsp',
        'biquad.dsp',
        'selecteq.dsp',
        ]

    float_sources = [ # big arrays and no sensitive processing
        'jconv_post.dsp',
        'sloop.dsp',
        'echo.dsp',
        'delay.dsp',
        'chorus.dsp',
    ]

    experimental_soures = [
        #"valve.dsp",
        #"valve_rect.dsp",
        "Exp.dsp",
        #"ExpFilter.dsp",
        ]

    task = bld.new_task_gen(
        source = sources,
        proc = "../tools/dsp2cc",
        proc_args = arg,
        )

    bld.new_task_gen(
        source = float_sources,
        proc = "../tools/dsp2cc",
        proc_args = float_arg,
        )

    bld.new_task_gen(
        source=experimental_soures,
        proc = "../tools/dsp2cc_exp",
        proc_args = ["-a","../src/faust-include.cpp","-f","-double"],
        )

def configure(conf):
    pass
