1218822Sdim// ****************************************************************************
278828Sobrien//
360484Sobrien//		CMidiInQ.h
460484Sobrien//
560484Sobrien// 	This class manages MIDI input.
660484Sobrien//
760484Sobrien//		Use a simple fixed size queue for storing MIDI data.
860484Sobrien//
960484Sobrien//		Fill & drain pointers are maintained automatically whenever
1060484Sobrien//		an Add or Get function succeeds.
1160484Sobrien//
1260484Sobrien//		Set editor tabs to 3 for your viewing pleasure.
1360484Sobrien//
1460484Sobrien// ----------------------------------------------------------------------------
1560484Sobrien//
1660484Sobrien// This file is part of Echo Digital Audio's generic driver library.
1760484Sobrien// Copyright Echo Digital Audio Corporation (c) 1998 - 2005
1860484Sobrien// All rights reserved
1960484Sobrien// www.echoaudio.com
2060484Sobrien//
2160484Sobrien// This library is free software; you can redistribute it and/or
2260484Sobrien// modify it under the terms of the GNU Lesser General Public
2360484Sobrien// License as published by the Free Software Foundation; either
2460484Sobrien// version 2.1 of the License, or (at your option) any later version.
25130561Sobrien//
2660484Sobrien// This library is distributed in the hope that it will be useful,
2760484Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of
2860484Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2960484Sobrien// Lesser General Public License for more details.
3060484Sobrien//
3160484Sobrien// You should have received a copy of the GNU Lesser General Public
3261843Sobrien// License along with this library; if not, write to the Free Software
3360484Sobrien// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
3461843Sobrien//
3589857Sobrien// ****************************************************************************
3689857Sobrien
3760484Sobrien//	Prevent problems with multiple includes
3860484Sobrien#ifndef _MIDIQUEUEOBJECT_
3989857Sobrien#define _MIDIQUEUEOBJECT_
4089857Sobrien
4189857Sobrien#include "CMtcSync.h"
4289857Sobrien
4389857Sobrientypedef struct tMIDI_DATA
4489857Sobrien{
4589857Sobrien	DWORD		dwMidi;
4689857Sobrien	LONGLONG	llTimestamp;
4789857Sobrien}
4889857SobrienMIDI_DATA;
4989857Sobrien
5089857Sobrientypedef MIDI_DATA *PMIDI_DATA;
5189857Sobrien
5289857Sobrien//
5389857Sobrien// Default to one MIDI input client
5489857Sobrien//
5589857Sobrien#ifndef MAX_MIDI_IN_CLIENTS
5689857Sobrien#define MAX_MIDI_IN_CLIENTS		1
5789857Sobrien#endif
5889857Sobrien
5989857Sobrien//
6089857Sobrien// MIDI in queue size
6189857Sobrien//
6289857Sobrien// Total buffer size in bytes will be MIDI_IN_Q_SIZE * sizeof(MIDI_DATA)
6389857Sobrien//
6489857Sobrien// Buffer size must be a power of 2.  This is the default size; to change it,
6589857Sobrien// add #define MIDI_IN_Q_SIZE to your OsSupport??.h file
6689857Sobrien//
6789857Sobrien#ifndef MIDI_IN_Q_SIZE
6889857Sobrien#define MIDI_IN_Q_SIZE	128
6989857Sobrien#endif
70218822Sdim
7189857Sobrien//
7289857Sobrien//	Class used for simple MIDI byte queue
7389857Sobrien//
7489857Sobrienclass CMidiInQ
7589857Sobrien{
7689857Sobrienpublic:
7789857Sobrien	//
7889857Sobrien	//	Construction/destruction
7989857Sobrien	//
8089857Sobrien	CMidiInQ();
8189857Sobrien	~CMidiInQ();
8289857Sobrien
8389857Sobrien	//
8489857Sobrien	// Init
85130561Sobrien	//
86218822Sdim	ECHOSTATUS Init(CEchoGals *pEG);
8789857Sobrien
8889857Sobrien	//
89218822Sdim	//	Get the oldest byte from the circular buffer
9089857Sobrien	//
9189857Sobrien	ECHOSTATUS GetMidi
92218822Sdim	(
93218822Sdim		ECHOGALS_MIDI_IN_CONTEXT	*pContext,
9489857Sobrien		DWORD 							&dwMidiByte,
95218822Sdim		LONGLONG							&llTimestamp
9689857Sobrien	);
9789857Sobrien
9889857Sobrien	//
99130561Sobrien	// Enable and disable MIDI input and MTC sync
100218822Sdim	//
101218822Sdim	ECHOSTATUS Arm(ECHOGALS_MIDI_IN_CONTEXT *pContext);
102218822Sdim	ECHOSTATUS Disarm(ECHOGALS_MIDI_IN_CONTEXT *pContext);
103218822Sdim	ECHOSTATUS ArmMtcSync();
104218822Sdim	ECHOSTATUS DisarmMtcSync();
105218822Sdim
106218822Sdim	//
107218822Sdim	// Get and set MTC base rate
108218822Sdim	//
109218822Sdim	ECHOSTATUS GetMtcBaseRate(DWORD *pdwBaseRate);
110218822Sdim	ECHOSTATUS SetMtcBaseRate(DWORD dwBaseRate);
111218822Sdim
112218822Sdim	//
11389857Sobrien	// If MTC sync is turned on, process received MTC data
11489857Sobrien	// and update the sample rate
115130561Sobrien	//
11689857Sobrien	void ServiceMtcSync();
11789857Sobrien
11889857Sobrien	//
11989857Sobrien	// See if there has been any recent MIDI input activity
12061843Sobrien	//
12189857Sobrien	BOOL IsActive();
12289857Sobrien
12389857Sobrien	//
12489857Sobrien	//	Reset the in/out ptrs
12589857Sobrien	//
12689857Sobrien	void Reset(ECHOGALS_MIDI_IN_CONTEXT *pContext);
127130561Sobrien
12889857Sobrien	//
12989857Sobrien	// Service a MIDI input interrupt
13089857Sobrien	//
13189857Sobrien	ECHOSTATUS ServiceIrq();
13289857Sobrien
13360484Sobrien
13460484Sobrienprotected:
13589857Sobrien
13689857Sobrien	//
13789857Sobrien	// Add a MIDI byte to the circular buffer
13889857Sobrien	//
13989857Sobrien	inline ECHOSTATUS AddMidi
14089857Sobrien	(
14189857Sobrien		DWORD		dwMidiByte,
14289857Sobrien		LONGLONG	llTimestamp
14389857Sobrien	);
14489857Sobrien
14589857Sobrien	//
14689857Sobrien	// Parse MIDI time code data
14789857Sobrien	//
14889857Sobrien	DWORD MtcProcessData( DWORD dwMidiData );
14989857Sobrien
15089857Sobrien	//
151130561Sobrien	// Cleanup
152130561Sobrien	//
153130561Sobrien	void Cleanup();
154218822Sdim
155218822Sdim	//
156218822Sdim	//	Midi buffer management
157218822Sdim	//
158218822Sdim	PMIDI_DATA 	m_pBuffer;
159218822Sdim	DWORD			m_dwFill;
160218822Sdim	DWORD			m_dwBufferSizeMask;
161130561Sobrien	DWORD			m_dwNumClients;
16289857Sobrien
16389857Sobrien	//
16489857Sobrien	// Most recent MIDI input time - used for activity detector
16589857Sobrien	//
16689857Sobrien	ULONGLONG	m_ullLastActivityTime;
16761843Sobrien
16889857Sobrien	//
16989857Sobrien	// MIDI time code
17061843Sobrien	//
17189857Sobrien	WORD			m_wMtcState;
17289857Sobrien	CMtcSync		*m_pMtcSync;
17361843Sobrien
17489857Sobrien	//
17589857Sobrien	// Other objects
17689857Sobrien	//
17789857Sobrien	CEchoGals		*m_pEG;
17889857Sobrien
17989857Sobrien};		// class CMidiInQ
18089857Sobrien
18189857Sobrientypedef CMidiInQ * PCMidiInQ;
18289857Sobrien
18389857Sobrien#endif
18489857Sobrien
18589857Sobrien// *** CMidiInQ.H ***
18689857Sobrien