/***************************************************************************
 * timiditylauncher.cpp: implementation of TimidityLauncher class
 *
 * This file is part of KTabEdit, a KDE tabulature editor
 *
 * copyright (C) 2003-2006 the KTabEdit development team <mrgwen@gmail.com>
 ***************************************************************************/

/***************************************************************************
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * See the file COPYING for more information.
 ***************************************************************************/

#include "timiditylauncher.h"

#include <KApplication>

#include "settings.h"

TimidityLauncher::TimidityLauncher()
: QProcess()
{
	connect(this, SIGNAL(readyReadStandardOutput()), this, SLOT(readFromStdout()));
	connect(this, SIGNAL(readyReadStandardError()), this, SLOT(readFromStderr()));
}

TimidityLauncher::TimidityLauncher(const QString& program, const QStringList& arguments)
: QProcess(),
  mProgram(program),
  mArguments(arguments)
{
}

TimidityLauncher::~TimidityLauncher()
{
}

void TimidityLauncher::setProgram(const QString& program)
{
	mProgram = program;
}

QString TimidityLauncher::program() const
{
	return mProgram;
}

void TimidityLauncher::setArguments(const QStringList& arguments)
{
	mArguments = arguments;
}

QStringList TimidityLauncher::arguments() const
{
	return mArguments;
}

bool TimidityLauncher::launchTimidity()
{
	if (!Settings::getInstance()->midiTimidity())
		return false;
	
	setupTimidity();
	
	return launchTimidityWithCmdLine(mProgram, mArguments);
}

bool TimidityLauncher::launchTimidityByHand()
{
	return launchTimidityWithCmdLine(mProgram, mArguments);
}

bool TimidityLauncher::launchTimidityWithCmdLine(const QString& program, const QStringList& arguments)
{
	clearTimidityOutput();
	
	start(program, arguments);
	
	while (mResult.isEmpty() && mError.isEmpty())
		kapp->processEvents();
	
	return mError.isEmpty();
}

void TimidityLauncher::setupTimidity()
{
	mArguments.clear();
	
	mProgram = Settings::getInstance()->timidityPath();
	
	mArguments << "-iA";
	
	QString buffer;
	buffer.setNum(Settings::getInstance()->timidityBuffer());
	QString fragment;
	fragment.setNum(Settings::getInstance()->timidityFragment());
	mArguments << "-B" + buffer + ',' + fragment;
	
	mArguments << "-O" + Settings::getInstance()->timidityOutput();
	mArguments << "-EFchorus=" + Settings::getInstance()->timidityChorus();
	mArguments << "-EFdelay=" + Settings::getInstance()->timidityDelay();
	
	QString noise;
	noise.setNum(Settings::getInstance()->timidityNoise());
	mArguments << "-EFns=" + noise;
	
	mArguments << "-EFresamp=" + Settings::getInstance()->timidityResamp();
	mArguments << "-EFreverb=" + Settings::getInstance()->timidityReverb();
	mArguments << "-EFvlpf=" + Settings::getInstance()->timidityVlpf();
	
	QString realtime;
	
	if (Settings::getInstance()->timidityRealTimeEnable()) {
		realtime.setNum(Settings::getInstance()->timidityRealTime());
		mArguments << "--realtime-priority=" + realtime;
	} else realtime = "no realtime enabled";
	
	QString frequency;
	frequency.setNum(Settings::getInstance()->timidityFrequency());
	mArguments << "-s " + frequency;
}

QString TimidityLauncher::errorMsg()
{
	return arguments().join(", ") + " output : " + mResult + ' ' + mError;
}

void TimidityLauncher::clearTimidityOutput()
{
	mResult = "";
	mError = "";
}

void TimidityLauncher::readFromStdout()
{
	mResult.append(readAllStandardOutput());
}

void TimidityLauncher::readFromStderr()
{
	mError.append(readAllStandardError());
}

#include "timiditylauncher.moc"
