1// ****************************************************************************
2//
3//		CEchoGalsMTC.cpp
4//
5//		CEchoGalsMTC is used to add MIDI time code sync to the base
6//		CEchoGals class.  CEchoGalsMTC derives from CEchoGals; CLayla and
7//		CLayla24 derive in turn from CEchoGalsMTC.
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#include "CEchoGalsMTC.h"
35
36
37//****************************************************************************
38//
39// Constructor and destructor
40//
41//****************************************************************************
42
43CEchoGalsMTC::CEchoGalsMTC( PCOsSupport pOsSupport )
44		  : CEchoGals( pOsSupport )
45{
46	ECHO_DEBUGPRINTF( ( "CEchoGalsMTC::CEchoGalsMTC() is born!\n" ) );
47
48	m_wInputClock = ECHO_CLOCK_INTERNAL;
49
50}	// CEchoGalsMTC::CEchoGalsMTC()
51
52
53CEchoGalsMTC::~CEchoGalsMTC()
54{
55	ECHO_DEBUGPRINTF( ( "CEchoGalsMTC::~CEchoGalsMTC() is toast!\n" ) );
56}	// CEchoGalsMTC::~CEchoGalsMTC()
57
58
59
60
61//****************************************************************************
62//
63// Input clock
64//
65//****************************************************************************
66
67//============================================================================
68//
69// Set the input clock
70//
71// This needs to intercept the input clock value here since MTC sync is
72// actually implemented in software
73//
74//============================================================================
75
76ECHOSTATUS CEchoGalsMTC::SetInputClock(WORD wClock)
77{
78	ECHOSTATUS Status;
79
80	Status = ECHOSTATUS_OK;
81
82	//
83	// Check for MTC clock
84	//
85	if (ECHO_CLOCK_MTC == wClock)
86	{
87		if (ECHO_CLOCK_MTC != m_wInputClock)
88		{
89			//
90			// Tell the MIDI input object to enable MIDI time code sync
91			//
92			Status = m_MidiIn.ArmMtcSync();
93
94			if (ECHOSTATUS_OK == Status)
95			{
96				//
97				// Store the current clock as MTC...
98				//
99				m_wInputClock = ECHO_CLOCK_MTC;
100
101				//
102				// but set the real clock to internal.
103				//
104				Status = CEchoGals::SetInputClock( ECHO_CLOCK_INTERNAL );
105			}
106
107		}
108	}
109	else
110	{
111		//
112		// Pass the clock setting to the base class
113		//
114		Status = CEchoGals::SetInputClock( wClock );
115		if (ECHOSTATUS_OK == Status)
116		{
117			WORD wOldClock;
118			DWORD dwRate;
119
120			//
121			// Get the base rate for MTC sync
122			//
123			m_MidiIn.GetMtcBaseRate( &dwRate );
124
125			//
126			// Make sure MTC sync is off
127			//
128			m_MidiIn.DisarmMtcSync();
129
130			//
131			// Store the new clock
132			//
133			wOldClock = m_wInputClock;
134			m_wInputClock = wClock;
135
136			//
137			// If the previous clock was MTC, re-set the sample rate
138			//
139			if (ECHO_CLOCK_MTC == wOldClock)
140				SetAudioSampleRate( dwRate );
141		}
142	}
143
144	return Status;
145
146}	// SetInputClock
147
148
149//============================================================================
150//
151// Get the input clock
152//
153//============================================================================
154
155ECHOSTATUS CEchoGalsMTC::GetInputClock(WORD &wClock)
156{
157	wClock = m_wInputClock;
158
159	return ECHOSTATUS_OK;
160}
161
162
163//****************************************************************************
164//
165// Sample rate
166//
167//****************************************************************************
168
169//============================================================================
170//
171// Set the sample rate
172//
173// Again, the rate needs to be intercepted here.
174//
175//============================================================================
176
177ECHOSTATUS CEchoGalsMTC::SetAudioSampleRate( DWORD dwSampleRate )
178{
179	ECHOSTATUS Status;
180
181	//
182	// Syncing to MTC?
183	//
184	if (ECHO_CLOCK_MTC == m_wInputClock)
185	{
186		//
187		// Test the rate
188		//
189		Status = QueryAudioSampleRate( dwSampleRate );
190
191		//
192		// Set the base rate if it's OK
193		//
194		if (ECHOSTATUS_OK == Status)
195		{
196			m_MidiIn.SetMtcBaseRate( dwSampleRate );
197		}
198	}
199	else
200	{
201		//
202		// Call the base class
203		//
204		Status = CEchoGals::SetAudioSampleRate( dwSampleRate );
205	}
206
207	return Status;
208
209}	// SetAudioSampleRate
210
211
212//============================================================================
213//
214// Get the sample rate
215//
216//============================================================================
217
218ECHOSTATUS CEchoGalsMTC::GetAudioSampleRate( PDWORD pdwSampleRate )
219{
220	ECHOSTATUS Status;
221
222	if (NULL == pdwSampleRate)
223		return ECHOSTATUS_INVALID_PARAM;
224
225	//
226	// Syncing to MTC?
227	//
228	if (ECHO_CLOCK_MTC == m_wInputClock)
229	{
230		//
231		// Get the MTC base rate
232		//
233		Status = m_MidiIn.GetMtcBaseRate( pdwSampleRate );
234	}
235	else
236	{
237		//
238		// Call the base class
239		//
240		Status = CEchoGals::GetAudioSampleRate( pdwSampleRate );
241	}
242
243	return Status;
244
245}	// GetAudioSampleRate
246
247
248
249
250//****************************************************************************
251//
252// Call this periodically to change the sample rate based on received MTC
253// data
254//
255//****************************************************************************
256
257void CEchoGalsMTC::ServiceMtcSync()
258{
259	m_MidiIn.ServiceMtcSync();
260}
261
262
263// *** CEchoGalsMTC.cpp ***
264