/*
  ==============================================================================

    This file was generated by user!

  ==============================================================================
*/

#include <math.h>

#include "JaySynthSound.h"
#include "JaySynthVoice.h"

/** A JaySynth voice that just plays incredible sounds.. */
JaySynthVoice::JaySynthVoice(voice_t *pVoice, int id, voice_common_t *pCom)
{
	m_pVoice = pVoice;
	VoiceInit(pVoice, pCom, id, 44100);
}

JaySynthVoice::~JaySynthVoice()
{
	VoiceFree(m_pVoice);
}

void JaySynthVoice::setSampleRate (synth_float_t sampleRate)
{
	VoiceSetFS(m_pVoice, sampleRate);
}

void JaySynthVoice::setBufsize (int bufsize)
{
	VoiceSetBufsize(m_pVoice, bufsize);
}

bool JaySynthVoice::canPlaySound (SynthesiserSound* sound)
{
    return dynamic_cast <JaySynthSound*> (sound) != 0;
}

void JaySynthVoice::startNote (const int midiNoteNumber, const float velocity,
                SynthesiserSound* sound, const int /*currentPitchWheelPosition*/)
{

	VoiceEvent(m_pVoice, voice_event_trigger);
		return;

/*
	if (m_pVoice->state == note_state_idle)
	{
		VoiceEvent(m_pVoice, voice_event_trigger);
	}
	else
	{
		VoiceEvent(m_pVoice, voice_event_retrigger);
	}
*/
}

void JaySynthVoice::stopNote (const bool allowTailOff)
{
	if (allowTailOff)
    {
		VoiceEvent(m_pVoice, voice_event_release);
    }
    else
    {
        // we're being told to stop playing immediately, so reset everything..
		VoiceEvent(m_pVoice, voice_event_release);
        clearCurrentNote();
    }
}

void JaySynthVoice::pitchWheelMoved (const int /*newValue*/)
{
}

void JaySynthVoice::controllerMoved (const int /*controllerNumber*/, const int /*newValue*/)
{
}

void JaySynthVoice::renderNextBlock (AudioSampleBuffer& outputBuffer, int startSample, int numSamples)
{
/* Done in JaySynth::renderNextBlock() 

	float *buf1 = outputBuffer.getSampleData (0, startSample);
	float *buf2 = outputBuffer.getSampleData (1, startSample);

	VoiceProcessDataV_Common(m_pVoice, buf1, buf2, numSamples);

	if (m_pVoice->state == note_state_idle)
		return;

	VoiceProcessDataV(m_pVoice, buf1, buf2, numSamples);

	if (m_pVoice->state == note_state_idle)
		clearCurrentNote();
*/
}

void JaySynthVoice::setParameter(int index, synth_float_t value)
{
	VoiceParam2Set(m_pVoice, index, value);
}
