source: sbliveos2/trunk/drv16/midistrm.cpp@ 143

Last change on this file since 143 was 142, checked in by ktk, 25 years ago

Import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1/* $Id: midistrm.cpp 142 2000-04-23 14:55:46Z ktk $ */
2
3/* SCCSID = %W% %E% */
4/****************************************************************************
5 * *
6 * Copyright (c) IBM Corporation 1994 - 1997. *
7 * *
8 * The following IBM OS/2 source code is provided to you solely for the *
9 * the purpose of assisting you in your development of OS/2 device drivers. *
10 * You may use this code in accordance with the IBM License Agreement *
11 * provided in the IBM Device Driver Source Kit for OS/2. *
12 * *
13 ****************************************************************************/
14/**@internal %W%
15 * @notes
16 * MIDISTREAM class implementation. The Midi Stream class is derived
17 * from the Stream class.
18 * @version %I%
19 * @context Unless otherwise noted, all interfaces are Ring-0, 16-bit,
20 * kernel stack.
21 * @history
22 *
23 */
24#define INCL_NOPMAPI
25#define INCL_DOSERRORS // for ERROR_INVALID_FUNCTION
26#include <os2.h>
27#include <os2me.h>
28#include <audio.h> // for #define MIDI
29
30#include <include.h>
31#include <devhelp.h>
32
33#include "midistrm.hpp"
34#include "event.hpp"
35#include "maudio.hpp"
36
37// An array to map integer 0..15 to the corresponding bit number in a USHORT.
38USHORT MIDISTREAM::_usBitNumber[ NUM_MidiChannels ] =
39 { 0x0001, 0x0002, 0x0004, 0x0008,
40 0x0010, 0x0020, 0x0040, 0x0080,
41 0x0100, 0x0200, 0x0400, 0x0800,
42 0x1000, 0x2000, 0x4000, 0x8000 };
43
44
45/**@internal CalcDelay
46 * @param None
47 * @return None
48 * @notes
49 * 600,000,000 microseconds/10 minutes
50 *---------------------------------------------------------- == X microseconds/clock
51 * usCPQNnum
52 * (ulTempo beats/10 min) * ( 24 * ------------- clocks/beat )
53 * usCPQNden
54 *
55 *
56 * 25,000,000 * usCPQNden
57 *== --------------------------
58 * ulTempo * usCPQNnum
59 *
60 * where
61 * usCPQNden = ((usCPQN & 0x3F) + 1) * 3
62 * usCPQNnum = 1 if bit 6 of usCPQN is set
63 *
64 * or
65 *
66 * usCPQNden = 1
67 * usCPQNnum = usCPQN + 1 if bit 6 is not set
68 */
69void MIDISTREAM::CalcDelay(void)
70{
71 ULONG ul;
72
73 if (usCPQN & 0x40) { // bit 6 is set if it's a denominator
74 ul = 25000000 * ((usCPQN & 0x3F) + 1);
75 ulPerClock = ul / ulTempo;
76 ulPerClock *= 3;
77 } else {
78 ul = ulTempo * (usCPQN+1);
79 ulPerClock = 25000000 / ul;
80 }
81}
82
83
84/**@external MIDISTRM::Process
85 * Consume MIDI bytes from the MMPM/2 stream buffers and send
86 * them off to the MIDI parser to be interpreted.
87 * @param void
88 * @return void
89 * @notes Runs at Task time on a global context hook; does not run
90 * on an interrupt level. Interacts with the Timer object defined
91 * for this stream to obtain current time and to request next Stream
92 * time to be scheduled.
93 */
94void MIDISTREAM::Process( void )
95{
96 ULONG ulNewTime; // Time, in mSec, on entry.
97 ULONG ulElapsedTime; // Elapsed time, last tick to this one.
98
99 // Update time variables.
100 ulNewTime = ((MIDIAUDIO*) pahw)->getTimer()->ulGetTime();
101 if ( ulNewTime > _ulLastProcess )
102 ulElapsedTime = ulNewTime - _ulLastProcess;
103 else
104 ulElapsedTime = 0;
105
106 _ulLastProcess = ulNewTime;
107 ulCurrentTime = ulNewTime;
108
109 if (qhInProcess.IsElements() == 0) // no buffers to process?
110 return;
111
112 if (ulStreamState == STREAM_PAUSED) // is the stream paused?
113 return;
114
115 ProcessEvents();
116
117 lWait -= ulElapsedTime * 1000;
118
119 PSTREAMBUFFER pstreambuff = (PSTREAMBUFFER) qhInProcess.Head();
120 PSTREAMBUF pbuff = pstreambuff->pBuffptr;
121 ULONG buffsz = pstreambuff->ulBuffsz;
122
123 while (lWait <= 0 && qhInProcess.IsElements()) {
124 parse(*(pbuff + (pstreambuff->ulBuffpos)++));
125
126 if (pstreambuff->ulBuffpos >= buffsz) {
127 qhDone.PushOnTail(qhInProcess.PopHead());
128 pstreambuff = (PSTREAMBUFFER) qhInProcess.Head();
129 pbuff = pstreambuff->pBuffptr;
130 buffsz = pstreambuff->ulBuffsz;
131 }
132 }
133 while (qhDone.IsElements())
134 ReturnBuffer();
135
136 // Determine next time to run. If we submit a time that has already
137 // passed, we'll get scheduled for the next tick.
138
139 ULONG ulTimeNextRun = ulCurrentTime + (lWait / 1000);
140 ((MIDIAUDIO*) pahw)->getTimer()->vSchedule( ulTimeNextRun );
141}
142
143
144// ### By getting Timer time, we're assuming that this stream owns the HW.
145ULONG MIDISTREAM::GetCurrentTime(void)
146{
147 return ((MIDIAUDIO*) pahw)->getTimer()->ulGetTime() ;
148}
149
150// ### By setting Timer time, we're assuming that this stream owns the HW.
151void MIDISTREAM::SetCurrentTime(ULONG time)
152{
153 ((MIDIAUDIO*) pahw)->getTimer()->vSetTime( time );
154 ulCurrentTime = time;
155}
156
157ULONG MIDISTREAM::Read(PSTREAMBUF, unsigned)
158{
159 return(ERROR_INVALID_FUNCTION);
160}
161
162ULONG MIDISTREAM::StartStream(void)
163{
164
165 if (!pahw->Start(this))
166 return ERROR_START_STREAM;
167 state = S_Init; // Reset parser state.
168 message.clear(); // Clear current message.
169 ulStreamState = STREAM_STREAMING;
170 return NO_ERROR;
171}
172
173
174/**@internal MIDISTREAM::_allNotesOff
175 * Shut off all notes that are currently playing.
176 * @param None.
177 * @return void
178 * @notes This function walks the _notesOn array and shuts off any note
179 * that is flagged as being actively played.
180 */
181void MIDISTREAM::_allNotesOff( void )
182{
183 for ( USHORT noteNum=0; noteNum < NUM_MidiNotes; ++noteNum)
184 if (_notesOn[noteNum])
185 // This note number is playing on one or more channels.
186 // Shut the note off on all channels on which it is playing.
187 for ( USHORT mchan=0; mchan < NUM_MidiChannels; ++mchan)
188 if (_notesOn[noteNum] & _usBitNumber[mchan]) {
189 ((MIDIAUDIO*) pahw)->noteOff( mchan, noteNum, 0 );
190 _notesOn[noteNum] &= ~(_usBitNumber[mchan]);
191 }
192}
193
194
195ULONG MIDISTREAM::StopStream(PCONTROL_PARM pControl)
196{
197
198 ulStreamState = STREAM_STOPPED;
199 pahw->Stop(this);
200 _allNotesOff();
201 ReturnBuffers();
202 pControl->ulTime = GetCurrentTime();
203 return NO_ERROR;
204}
205
206
207ULONG MIDISTREAM::PauseStream(PCONTROL_PARM pControl)
208{
209
210 if (ulStreamState == STREAM_PAUSED) // is the stream paused?
211 return ERROR_INVALID_SEQUENCE;
212 pahw->Pause(this);
213 _allNotesOff();
214 pControl->ulTime = GetCurrentTime();
215 ulStreamState = STREAM_PAUSED;
216 return NO_ERROR;
217
218}
219
220ULONG MIDISTREAM::ResumeStream(void)
221{
222
223 if (ulStreamState != STREAM_PAUSED) // is the stream paused?
224 return ERROR_INVALID_SEQUENCE;
225 state = S_Init; // Reset parser state.
226 message.clear(); // Clear current message.
227 pahw->Resume(this);
228 ulStreamState = STREAM_STREAMING;
229 return NO_ERROR;
230
231}
232
233MIDISTREAM::MIDISTREAM(ULONG streamtype, USHORT filesysnum):
234 STREAM(streamtype, filesysnum)
235{
236 // Initialize tempo & scheduling information.
237 ulTempo = 1200;
238 usCPQN = 0;
239 CalcDelay();
240 lWait = 0;
241 _ulLastProcess = 0;
242
243 // Reset the parser.
244 state = S_Init;
245 message.clear();
246
247 // Reset our tracking of which notes are currently on.
248 for (int i=0; i<NUM_MidiNotes; ++i)
249 _notesOn[ i ] = 0;
250}
251
Note: See TracBrowser for help on using the repository browser.