//////////////////////////////////////////////////////////////////////
// SMPTETimecodeConsole.cpp : Defines the entry point for the console application.
//////////////////////////////////////////////////////////////////////
// Demonstration of SMPTE Timecode Conversions
//
// Brooks Harris
// 2015-04-04
// http://www.edlmax.com/services.htm
//
// If this code works, I wrote it. If not, I've never heard of it.
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SMPTETimecode.h"

void testSMPTETimecode(
long lFrames,
SMPTETCBASERATE SMPTETCBASERATEx,
SMPTETCTYPE SMPTETCTYPEx,
long lCntMax)
{

long lFramesResult = 0;
char sTimecodeResult[16];

long lCnt = 0;
while(1)
{
if(lCnt > lCntMax)
break;

SMPTE_Frames_To_TextTC(lFrames,
SMPTETCBASERATEx,
SMPTETCTYPEx,
sTimecodeResult);

SMPTE_TextTC_To_Frames(sTimecodeResult,
SMPTETCBASERATEx,
&lFramesResult);

printf("%08ld %s %08ld %ld\n" ,lFrames, sTimecodeResult, lFramesResult, lFrames - lFramesResult);

lFrames++;
lCnt++;
}

}

void test30fpsDropFrame()
{

SMPTETCBASERATE SMPTETCBASERATEx = SMPTETCBASERATE_30FPS;
SMPTETCTYPE SMPTETCTYPEx = SMPTETCTYPE_FIELD2_DROP_FRAME;

printf("\nTest SMPTE Timecode - 30fps drop-frame\n");

printf("zero start, 60 frames\n");
long lFrames = 0; // zero start
long lCntMax = 60; // 60 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("first minute, 10 frames\n");
lFrames = 1800 - 5; // first minute
lCntMax = 10; // 10 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("first 10th minute, 10 frames\n");
lFrames = ((1798 * 10) + 2) - 5; // first 10th minute
lCntMax = 10; // 10 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("first hour, 10 frames\n");
lFrames = ((1798 * 60) + (6 * 2)) - 5; // first hour
lCntMax = 10; // 10 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("end of drop-frame day, 10 frames\n");
lFrames = 2589408 - 5; // end of drop-frame day
lCntMax = 10; // 10 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("\n");

}

void test30fpsNonDropFrame()
{

SMPTETCBASERATE SMPTETCBASERATEx = SMPTETCBASERATE_30FPS;
SMPTETCTYPE SMPTETCTYPEx = SMPTETCTYPE_FIELD2_NON_DROP_FRAME;

printf("\nTest SMPTE Timecode - 30fps non-drop frame\n");

printf("zero start, 60 frames\n");
long lFrames = 0; // zero start
long lCntMax = 60; // 60 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("first minute, 10 frames\n");
lFrames = 1800 - 5; // first minute
lCntMax = 10; // 10 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("first 10th minute, 10 frames\n");
lFrames = (1800 * 10) - 5; // first 10th minute
lCntMax = 10; // 10 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("first hour, 10 frames\n");
lFrames = (1800 * 60) - 5; // first hour
lCntMax = 10; // 0 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("end of non-drop-frame day, 10 frames\n");
lFrames = 2592000 - 5; // end of non-drop-frame day
lCntMax = 10; // 10 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("\n");

}

void test25fpsNonDropFrame()
{

SMPTETCBASERATE SMPTETCBASERATEx = SMPTETCBASERATE_25FPS;
SMPTETCTYPE SMPTETCTYPEx = SMPTETCTYPE_FIELD2_NON_DROP_FRAME;

printf("\nTest SMPTE Timecode - 25fps non-drop frame\n");

printf("zero start, 60 frames\n");
long lFrames = 0; // zero start
long lCntMax = 60; // 60 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("first minute, 10 frames\n");
lFrames = 1500 - 5; // first minute
lCntMax = 10; // 10 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("first 10th minute, 10 frames\n");
lFrames = (1500 * 10) - 5; // first 10th minute
lCntMax = 10; // 10 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("first hour, 10 frames\n");
lFrames = 90000 - 5; // first hour
lCntMax = 10; // 0 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("end of non-drop-frame day, 10 frames\n");
lFrames = 2160000 - 5; // end of non-drop-frame day
lCntMax = 10; // 10 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("\n");

}

void test24fpsNonDropFrame()
{

SMPTETCBASERATE SMPTETCBASERATEx = SMPTETCBASERATE_24FPS;
SMPTETCTYPE SMPTETCTYPEx = SMPTETCTYPE_FIELD2_NON_DROP_FRAME;

printf("\nTest SMPTE Timecode - 24fps non-drop frame\n");

printf("zero start, 60 frames\n");
long lFrames = 0; // zero start
long lCntMax = 60; // 60 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("first minute, 10 frames\n");
lFrames = 1440 - 5; // first minute
lCntMax = 10; // 10 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("first 10th minute, 10 frames\n");
lFrames = (1440 * 10) - 5; // first 10th minute
lCntMax = 10; // 10 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("first hour, 10 frames\n");
lFrames = 86400 - 5; // first hour
lCntMax = 10; // 0 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("end of non-drop-frame day, 10 frames\n");
lFrames = 2073600 - 5; // end of non-drop-frame day
lCntMax = 10; // 10 frames
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);
printf("\n");

}

void test30fpsDropFrame24Hours()
{

#include <time.h> // for time_t etc - only used by this function

// makes a pretty big file....

SMPTETCBASERATE SMPTETCBASERATEx = SMPTETCBASERATE_30FPS;
SMPTETCTYPE SMPTETCTYPEx = SMPTETCTYPE_FIELD2_DROP_FRAME;

time_t time_tA;
time_tA = time(&time_tA);

printf("\nTest SMPTE Timecode - 30fps drop-frame 24 hours \n");
printf("zero start, 60 frames\n");

long lFrames = 0; // zero start
long lCntMax = 2592000; // 24 hours non-drop
testSMPTETimecode(lFrames, SMPTETCBASERATEx, SMPTETCTYPEx, lCntMax);

printf("\n");
time_t time_tB;
time_tB = time(&time_tB);
time_t time_tElapsed = time_tB - time_tA;
printf("\n Elapsed %d\n", time_tElapsed);

}

int main(int argc, char* argv[])
{
// command line parameters not used

test24fpsNonDropFrame();

test25fpsNonDropFrame();

test30fpsNonDropFrame();

test30fpsDropFrame();

//test30fpsDropFrame24Hours(); // makes a pretty big file....

return 0;
}