// An example program that converts an existing test.wav (8-kHz, 16-bit PCM)
// to test.qcp file.
// must define INITGUID before including Qpl.h 
// only define once in a project
#define INITGUID 

#include <windows.h> 
#include <stdio.h>

#include "Qpl.h"

typedef HRESULT (WINAPI *pfnQPLCreateInstance)(REFCLSID, REFIID, void**);

const unsigned char WaveFileName[] = "test.wav";
const unsigned char QcpFileName[] = "test.qcp";

void main( int argc, char *argv[] )
{
	pfnQPLCreateInstance pfn;
	IQPLConvertFile1 *pCf = 0;
	IQPLQcpFile1 *pQf = 0;

	HINSTANCE hDll = LoadLibrary( "Qpl.dll" );

	if ( hDll == NULL )
	{
		printf("Can't find Qpl.dll\n");
		return;
	}
	
	pfn = (pfnQPLCreateInstance) GetProcAddress(hDll, "QPLCreateInstance");

	if ( pfn == 0 )
	{
		printf("Can't get QPLCreateInstance\n");
		return;
	}

	if ( S_OK != pfn( CLSID_QPLPvQcpFile1, IID_IQPLConvertFile1, (void**)&pCf ) )
	{
		printf("Can't find IQPLConvertFile\n");
		return;
	}

	if ( S_OK != pfn( CLSID_QPLPvQcpFile1, IID_IQPLQcpFile1, (void**)&pQf ) )
	{
		printf("Can't find IQPLQcpFile\n");
		return;
	}
	
	printf( "Converting %s to %s\n", WaveFileName, QcpFileName );
	// use encoding level of half fixed rate (gives us 6800 bps)
	if ( pCf->ConvertFile( WaveFileName, QcpFileName, QPL_ENCODE | QPL_QCELP_HALF_FIXED) )
	{
		printf("Conversion failed!\n");
		return;
	}

	if ( pQf->Open( QcpFileName, QPL_READ ) )
	{
		UINT32 len = pQf->GetLength() * pQf->GetBlockSize(); 
		printf("Converted %s is %d ms long\n", QcpFileName, len);
		pQf->Close();	
	}

	pQf->Release();
	pCf->Release();
	FreeLibrary(hDll);
}
