1// ****************************************************************************
2//
3//		CMtcSync.h
4//
5//		Include file for interfacing with the CMtcSync class.
6//
7//		This class manages syncing to MIDI time code.
8//
9//		Set editor tabs to 3 for your viewing pleasure.
10//
11// ----------------------------------------------------------------------------
12//
13// This file is part of Echo Digital Audio's generic driver library.
14// Copyright Echo Digital Audio Corporation (c) 1998 - 2005
15// All rights reserved
16// www.echoaudio.com
17//
18// This library is free software; you can redistribute it and/or
19// modify it under the terms of the GNU Lesser General Public
20// License as published by the Free Software Foundation; either
21// version 2.1 of the License, or (at your option) any later version.
22//
23// This library is distributed in the hope that it will be useful,
24// but WITHOUT ANY WARRANTY; without even the implied warranty of
25// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26// Lesser General Public License for more details.
27//
28// You should have received a copy of the GNU Lesser General Public
29// License along with this library; if not, write to the Free Software
30// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31//
32// ****************************************************************************
33
34//	Prevent problems with multiple includes
35#ifndef _MTCQUEUEOBJECT_
36#define _MTCQUEUEOBJECT_
37
38typedef struct tMTC_DATA
39{
40	DWORD	dwData;			// DWORD here wastes memory, but is easier to debug...
41	DWORD dwTimestamp;	// Timestamp in terms of the DSP sample count
42} MTC_DATA;
43
44
45//
46//	CMtcSync
47//
48class CMtcSync
49{
50public:
51	//
52	//	Construction/destruction
53	//
54	CMtcSync( CEchoGals *pEG );
55	~CMtcSync();
56
57	//
58	// Methods used to store MTC data & timestamps
59	//
60	void StoreTimestampHigh(DWORD dwData);
61	void StoreTimestampLow(DWORD dwData);
62	void StoreMtcData(DWORD dwData);
63
64	//
65	// Call this to process the MTC data and adjust the sample rate
66	//
67	void Sync();
68
69	//
70	//	Reset the state of things
71	//
72	void Reset();
73
74	//
75	//  Overload new & delete so memory for this object is allocated from non-paged memory.
76	//
77	PVOID operator new( size_t Size );
78	VOID  operator delete( PVOID pVoid );
79
80protected:
81
82	enum	// Quarter-frame message types
83	{
84		MTC_QF_FRAME_LSN = 0,
85		MTC_QF_FRAME_MSN,
86		MTC_QF_SECOND_LSN,
87		MTC_QF_SECOND_MSN,
88		MTC_QF_MINUTE_LSN,
89		MTC_QF_MINUTE_MSN,
90		MTC_QF_HOUR_LSN,
91		MTC_QF_HOUR_MSN,
92
93		MTC_TOLERANCE		= 64,
94		MTC_DAMPING			= 98 * 0x1000 / 100,	// 95% in 12 bit fixed point
95
96		MAX_DFRAME_SYNC_COUNT	= 4096,
97
98		DAMPING_RATIO_LIMIT_LOW	 = 4096 - 16,
99		DAMPING_RATIO_LIMIT_HIGH = 4096 + 16
100	};
101
102	//
103	//	Midi buffer management
104	//
105	MTC_DATA 	m_Buffer[ ECHO_MTC_QUEUE_SZ ];
106	DWORD			m_dwFill;
107	DWORD			m_dwDrain;
108
109	//
110	// State information
111	//
112	DWORD			m_dwBaseSampleRate;
113
114	DWORD			m_dwLastDframeTimestamp;	// Timestamp of last double frame
115														// in units of samples
116
117	DWORD			m_iSamplesPerDframe;
118	DWORD			m_dwFramesPerSec;
119
120	DWORD			m_dwLastDframe;				// Last doubleframe number
121	DWORD			m_dwCurrentDframe;			// Doubleframe number currently being assembled
122														// from MTC data bytes
123
124	DWORD			m_iNumDframesSynced;			// How many frames have elapsed since the
125														// last sample rate adjustment
126
127	INT32			m_iNumSamplesSynced;			// Sum of all actual timestamp deltas since
128														// last sample rate adjustment
129	DWORD			m_dwNextQfType;
130
131	DWORD			m_dwTemp;
132
133	//
134	// Other stuff
135	//
136	CEchoGals 	*m_pEG;
137
138	friend class CMidiInQ;
139
140};		// class CMtcSync
141
142typedef CMtcSync * PCMtcSync;
143
144#endif
145
146// *** CMtcSync.H ***
147