[142] | 1 | /* $Id: midistrm.cpp 205 2007-06-14 01:33:51Z stevenhl $ */
|
---|
| 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.
|
---|
| 38 | USHORT 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 | */
|
---|
| 69 | void 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 | */
|
---|
| 94 | void 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.
|
---|
| 145 | ULONG 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.
|
---|
| 151 | void MIDISTREAM::SetCurrentTime(ULONG time)
|
---|
| 152 | {
|
---|
| 153 | ((MIDIAUDIO*) pahw)->getTimer()->vSetTime( time );
|
---|
| 154 | ulCurrentTime = time;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | ULONG MIDISTREAM::Read(PSTREAMBUF, unsigned)
|
---|
| 158 | {
|
---|
| 159 | return(ERROR_INVALID_FUNCTION);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | ULONG 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.
|
---|
[147] | 169 | lWait = 0; //SvL, reset this too
|
---|
[142] | 170 | ulStreamState = STREAM_STREAMING;
|
---|
| 171 | return NO_ERROR;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 |
|
---|
| 175 | /**@internal MIDISTREAM::_allNotesOff
|
---|
| 176 | * Shut off all notes that are currently playing.
|
---|
| 177 | * @param None.
|
---|
| 178 | * @return void
|
---|
| 179 | * @notes This function walks the _notesOn array and shuts off any note
|
---|
| 180 | * that is flagged as being actively played.
|
---|
| 181 | */
|
---|
| 182 | void MIDISTREAM::_allNotesOff( void )
|
---|
| 183 | {
|
---|
[205] | 184 | for ( BYTE noteNum=0; noteNum < NUM_MidiNotes; ++noteNum)
|
---|
[142] | 185 | if (_notesOn[noteNum])
|
---|
| 186 | // This note number is playing on one or more channels.
|
---|
| 187 | // Shut the note off on all channels on which it is playing.
|
---|
[205] | 188 | for ( BYTE mchan=0; mchan < NUM_MidiChannels; ++mchan)
|
---|
[142] | 189 | if (_notesOn[noteNum] & _usBitNumber[mchan]) {
|
---|
| 190 | ((MIDIAUDIO*) pahw)->noteOff( mchan, noteNum, 0 );
|
---|
| 191 | _notesOn[noteNum] &= ~(_usBitNumber[mchan]);
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 |
|
---|
| 196 | ULONG MIDISTREAM::StopStream(PCONTROL_PARM pControl)
|
---|
| 197 | {
|
---|
| 198 |
|
---|
| 199 | ulStreamState = STREAM_STOPPED;
|
---|
| 200 | pahw->Stop(this);
|
---|
| 201 | _allNotesOff();
|
---|
| 202 | ReturnBuffers();
|
---|
| 203 | pControl->ulTime = GetCurrentTime();
|
---|
| 204 | return NO_ERROR;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 |
|
---|
| 208 | ULONG MIDISTREAM::PauseStream(PCONTROL_PARM pControl)
|
---|
| 209 | {
|
---|
| 210 |
|
---|
| 211 | if (ulStreamState == STREAM_PAUSED) // is the stream paused?
|
---|
| 212 | return ERROR_INVALID_SEQUENCE;
|
---|
| 213 | pahw->Pause(this);
|
---|
| 214 | _allNotesOff();
|
---|
| 215 | pControl->ulTime = GetCurrentTime();
|
---|
| 216 | ulStreamState = STREAM_PAUSED;
|
---|
| 217 | return NO_ERROR;
|
---|
| 218 |
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | ULONG MIDISTREAM::ResumeStream(void)
|
---|
| 222 | {
|
---|
| 223 |
|
---|
| 224 | if (ulStreamState != STREAM_PAUSED) // is the stream paused?
|
---|
| 225 | return ERROR_INVALID_SEQUENCE;
|
---|
| 226 | state = S_Init; // Reset parser state.
|
---|
| 227 | message.clear(); // Clear current message.
|
---|
| 228 | pahw->Resume(this);
|
---|
| 229 | ulStreamState = STREAM_STREAMING;
|
---|
| 230 | return NO_ERROR;
|
---|
| 231 |
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | MIDISTREAM::MIDISTREAM(ULONG streamtype, USHORT filesysnum):
|
---|
| 235 | STREAM(streamtype, filesysnum)
|
---|
| 236 | {
|
---|
| 237 | // Initialize tempo & scheduling information.
|
---|
| 238 | ulTempo = 1200;
|
---|
| 239 | usCPQN = 0;
|
---|
| 240 | CalcDelay();
|
---|
| 241 | lWait = 0;
|
---|
| 242 | _ulLastProcess = 0;
|
---|
| 243 |
|
---|
| 244 | // Reset the parser.
|
---|
| 245 | state = S_Init;
|
---|
| 246 | message.clear();
|
---|
| 247 |
|
---|
| 248 | // Reset our tracking of which notes are currently on.
|
---|
| 249 | for (int i=0; i<NUM_MidiNotes; ++i)
|
---|
| 250 | _notesOn[ i ] = 0;
|
---|
| 251 | }
|
---|
| 252 |
|
---|