1// ****************************************************************************
2//
3//		CMidiInQ.h
4//
5// 	This class manages MIDI input.
6//
7//		Use a simple fixed size queue for storing MIDI data.
8//
9//		Fill & drain pointers are maintained automatically whenever
10//		an Add or Get function succeeds.
11//
12//		Set editor tabs to 3 for your viewing pleasure.
13//
14// ----------------------------------------------------------------------------
15//
16// This file is part of Echo Digital Audio's generic driver library.
17// Copyright Echo Digital Audio Corporation (c) 1998 - 2005
18// All rights reserved
19// www.echoaudio.com
20//
21// This library is free software; you can redistribute it and/or
22// modify it under the terms of the GNU Lesser General Public
23// License as published by the Free Software Foundation; either
24// version 2.1 of the License, or (at your option) any later version.
25//
26// This library is distributed in the hope that it will be useful,
27// but WITHOUT ANY WARRANTY; without even the implied warranty of
28// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29// Lesser General Public License for more details.
30//
31// You should have received a copy of the GNU Lesser General Public
32// License along with this library; if not, write to the Free Software
33// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
34//
35// ****************************************************************************
36
37//	Prevent problems with multiple includes
38#ifndef _MIDIQUEUEOBJECT_
39#define _MIDIQUEUEOBJECT_
40
41#include "CMtcSync.h"
42
43typedef struct tMIDI_DATA
44{
45	DWORD		dwMidi;
46	LONGLONG	llTimestamp;
47}
48MIDI_DATA;
49
50typedef MIDI_DATA *PMIDI_DATA;
51
52//
53// Default to one MIDI input client
54//
55#ifndef MAX_MIDI_IN_CLIENTS
56#define MAX_MIDI_IN_CLIENTS		1
57#endif
58
59//
60// MIDI in queue size
61//
62// Total buffer size in bytes will be MIDI_IN_Q_SIZE * sizeof(MIDI_DATA)
63//
64// Buffer size must be a power of 2.  This is the default size; to change it,
65// add #define MIDI_IN_Q_SIZE to your OsSupport??.h file
66//
67#ifndef MIDI_IN_Q_SIZE
68#define MIDI_IN_Q_SIZE	128
69#endif
70
71//
72//	Class used for simple MIDI byte queue
73//
74class CMidiInQ
75{
76public:
77	//
78	//	Construction/destruction
79	//
80	CMidiInQ();
81	~CMidiInQ();
82
83	//
84	// Init
85	//
86	ECHOSTATUS Init(CEchoGals *pEG);
87
88	//
89	//	Get the oldest byte from the circular buffer
90	//
91	ECHOSTATUS GetMidi
92	(
93		ECHOGALS_MIDI_IN_CONTEXT	*pContext,
94		DWORD 							&dwMidiByte,
95		LONGLONG							&llTimestamp
96	);
97
98	//
99	// Enable and disable MIDI input and MTC sync
100	//
101	ECHOSTATUS Arm(ECHOGALS_MIDI_IN_CONTEXT *pContext);
102	ECHOSTATUS Disarm(ECHOGALS_MIDI_IN_CONTEXT *pContext);
103	ECHOSTATUS ArmMtcSync();
104	ECHOSTATUS DisarmMtcSync();
105
106	//
107	// Get and set MTC base rate
108	//
109	ECHOSTATUS GetMtcBaseRate(DWORD *pdwBaseRate);
110	ECHOSTATUS SetMtcBaseRate(DWORD dwBaseRate);
111
112	//
113	// If MTC sync is turned on, process received MTC data
114	// and update the sample rate
115	//
116	void ServiceMtcSync();
117
118	//
119	// See if there has been any recent MIDI input activity
120	//
121	BOOL IsActive();
122
123	//
124	//	Reset the in/out ptrs
125	//
126	void Reset(ECHOGALS_MIDI_IN_CONTEXT *pContext);
127
128	//
129	// Service a MIDI input interrupt
130	//
131	ECHOSTATUS ServiceIrq();
132
133
134protected:
135
136	//
137	// Add a MIDI byte to the circular buffer
138	//
139	inline ECHOSTATUS AddMidi
140	(
141		DWORD		dwMidiByte,
142		LONGLONG	llTimestamp
143	);
144
145	//
146	// Parse MIDI time code data
147	//
148	DWORD MtcProcessData( DWORD dwMidiData );
149
150	//
151	// Cleanup
152	//
153	void Cleanup();
154
155	//
156	//	Midi buffer management
157	//
158	PMIDI_DATA 	m_pBuffer;
159	DWORD			m_dwFill;
160	DWORD			m_dwBufferSizeMask;
161	DWORD			m_dwNumClients;
162
163	//
164	// Most recent MIDI input time - used for activity detector
165	//
166	ULONGLONG	m_ullLastActivityTime;
167
168	//
169	// MIDI time code
170	//
171	WORD			m_wMtcState;
172	CMtcSync		*m_pMtcSync;
173
174	//
175	// Other objects
176	//
177	CEchoGals		*m_pEG;
178
179};		// class CMidiInQ
180
181typedef CMidiInQ * PCMidiInQ;
182
183#endif
184
185// *** CMidiInQ.H ***
186