source: sbliveos2/trunk/drv16/wavestrm.cpp@ 144

Last change on this file since 144 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: 16.9 KB
Line 
1/* $Id: wavestrm.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 * @version %I%
17 * @context Unless otherwise noted, all interfaces are Ring-0, 16-bit,
18 * <stack context>.
19 * @history
20 *
21 */
22#define INCL_NOPMAPI
23#define INCL_DOSERRORS // for ERROR_INVALID_FUNCTION
24#include <os2.h>
25#include <os2me.h>
26#include <audio.h> // for #define MIDI
27
28#include "wavestrm.hpp"
29#include "audiohw.hpp"
30#include "waudio.hpp"
31#include "memutil.h"
32#include <ossidc.h>
33#include <dbgos2.h>
34#include "ioctl.h"
35
36//
37// _vRealignBuffer
38// called just after a wave stream pause on a playback.
39// Gets the end position of the stream when paused and a pointer to a
40// STREAMBUFFER. Basicly this function looks at the streambuffer and if
41// there is any unplayed data in it it adjusts the bufpos counter.
42// the donepos counter is ALWAYS set to zero. It will return 0 if all
43// the data has been played and 1 if there is still some data left.
44//
45USHORT WAVESTREAM::_vRealignBuffer(ULONG FAR *bytesinc, PSTREAMBUFFER pbuffer)
46{
47 // if none of the data in this stream buffer has been consumed
48 if (!*bytesinc) {
49 pbuffer->ulDonepos = 0;
50 pbuffer->ulBuffpos = 0;
51 return 1;
52 }
53
54 pbuffer->ulDonepos += *bytesinc;
55 pbuffer->ulBuffpos = pbuffer->ulDonepos;
56 *bytesinc = 0;
57 if(pbuffer->ulDonepos >= pbuffer->ulBuffsz) {
58 //calc position in next buffer
59 *bytesinc = pbuffer->ulDonepos - pbuffer->ulBuffsz;
60 return 0; //all of the buffer has been consumed
61 }
62 return 1;
63}
64//
65// _vRealignPausedBuffers(void)
66// when a stream is paused we need to "realign" the data in the audio buffer
67// with reality. On playback, not all the data in the audio buffer has been
68// consumed. Likewise on a capture, not all the good data in the audio buffer
69// has been copied out. After receiving the DDCMDCONTROL Pause we will call
70// this function to line the MMPM buffers back up.
71// there are 2 cases here: first one is the case of a capture stream.
72// for a capture stream we simply read any data that is still in the audio
73// buffer into a MMPM buffer.
74// for a playback stream things are not so straight forward.
75// first check the STREAMBUFFER on pHead to see if any of it's data is in the
76// audio buffer and not consumed, if yes back up the ulBuffpos in the
77// STREAMBUFFER. Next check any STREAMBUFFERS on pdone starting with the last
78// one. (the one on the tail of the queue) If necessary back up the ulBuffpos
79// and put the STREAMBUFFER on the Head queue.
80//
81void WAVESTREAM::_vRealignPausedBuffers(ULONG endpos)
82{
83 PSTREAMBUFFER ptempbuff;
84
85 switch (ulStreamType & STREAM_WRITE) {
86 case STREAM_READ:
87 //SvL: Don't get the lastest recording data as a read command
88 // would now restart recording (as it's stopped)
89 // Just return what we've got or push the buffer on the inprocess queue
90 ptempbuff = (PSTREAMBUFFER)qhDone.Head();
91 if(ptempbuff) {
92 if(ptempbuff->ulBuffpos) {//if we recorded anything into this buffer, then return it now
93 ReturnBuffer();
94 return;
95 }
96 ptempbuff->ulBuffpos = 0;
97 ptempbuff->ulDonepos = 0;
98 qhInProcess.PushOnHead(qhDone.PopHead());
99 }
100 break;
101
102 case STREAM_WRITE:
103 {
104 PQUEUEHEAD pTempHead = new QUEUEHEAD;
105 ULONG bytesinc;
106 USHORT usRC;
107
108 bytesinc = endpos - _ulBytesProcessed;
109 bytesinc &= 0xFFFFFFFC; //keep it on a dword boundary
110
111 // if there are bufferes on the done queue, pop them off the head and
112 // push them on the head of qhTempHead. This will reorder them so
113 // that the more recently used ones will be in the front of the queue.
114 // Pass them all to _vRealignBuffer. If the rc from _vRealignBuffer is
115 // 0 then there is no unprocessed data in the buffer (it is ready to
116 // be returned) so put it on the Tail of the done queue.
117 // If the rc is 1 then put it on the head of the InProcess queue.
118
119 while (qhDone.IsElements()) {
120 pTempHead->PushOnTail(qhDone.PopHead());
121 } /* endwhile */
122
123 while (qhInProcess.IsElements()) {
124 pTempHead->PushOnTail(qhInProcess.PopHead());
125 } /* endwhile */
126
127 while(pTempHead->IsElements()) {
128 usRC = _vRealignBuffer(&bytesinc, (PSTREAMBUFFER)pTempHead->Head());
129 if (usRC) {
130 qhInProcess.PushOnTail(pTempHead->PopHead());
131 }
132 else {
133 qhDone.PushOnTail(pTempHead->PopHead());
134 }
135 } /* endwhile */
136 if(qhDone.IsElements())
137 ReturnBuffer();
138
139 delete pTempHead; // free the memory this ain't no Java here !!
140 break;
141 }
142 default:
143 break;
144 } /* endswitch */
145}
146//
147// get ready to start streaming
148// this requires the following:
149// call Initbuffer in the audiobuffer object
150// if this is a write stream call _vFillAudioBuf
151//
152void WAVESTREAM::AddBuffers(void)
153{
154 if (ulStreamType & STREAM_WRITE) {
155 if(!qhInProcess.Head() && !qhDone.Head()) {
156 //underrun: stop playback
157 dprintf(("underrun: stop playback"));
158 pahw->Stop(this);
159 fUnderrun = TRUE;
160 return;
161 }
162 AddBuffer();
163 AddBuffer();
164 }
165}
166
167//
168// write one buffer to the audio buffer
169// the caller of this function MUST make sure it ok to write the audio buffer..
170// _AudioBufWrite will not check if there is room in the audio buffer of if
171// there are buffers on pHead... BEWARE
172//
173void WAVESTREAM::AddBuffer()
174{
175 PSTREAMBUFFER pTemp = (PSTREAMBUFFER)qhDone.Head();
176 ULONG pdataBuf;
177 ULONG Buff_left, byteswritten;
178
179 if(!pTemp || pTemp->ulBuffpos >= (pTemp->ulBuffsz & 0xFFFFFFFC)) {
180 pTemp = (PSTREAMBUFFER)qhInProcess.Head();
181 }
182 if(!pTemp) return;
183
184 // get the buffer pointer and amount of data remaining
185 pdataBuf = (ULONG)pTemp->pBuffptr + pTemp->ulBuffpos;
186 Buff_left = pTemp->ulBuffsz - pTemp->ulBuffpos;
187
188 // write the audio buffer
189 byteswritten = OSS16_StreamAddBuffer(this, pdataBuf, Buff_left);
190 if(byteswritten == 0) {
191 return; //no more room
192 }
193
194// dprintf(("AddBuffer %lx size %d, bytes written %d", pdataBuf, (USHORT)Buff_left, (USHORT)byteswritten));
195
196 // update the buffer pos counter
197 pTemp->ulBuffpos += byteswritten;
198
199 if (!qhDone.Head() || pTemp != qhDone.Head()) {
200 qhDone.PushOnTail(qhInProcess.PopHead());
201 }
202}
203
204// Read data from the audio Buffer.
205// Called at interrupt time to get the good data from the audiobuffer object.
206//
207BOOL WAVESTREAM::_vReadAudioBuf(void)
208{
209 PSTREAMBUFFER pTemp = (PSTREAMBUFFER)qhDone.Head();
210 ULONG pdataBuf;
211 ULONG Buff_left, bytesread;
212
213 if(!pTemp || pTemp->ulBuffpos >= (pTemp->ulBuffsz & 0xFFFFFFFC)) {
214 pTemp = (PSTREAMBUFFER)qhInProcess.Head();
215 }
216 if(!pTemp) return FALSE;
217
218 // get the buffer pointer and amount of data remaining
219 pdataBuf = (ULONG)pTemp->pBuffptr + pTemp->ulBuffpos;
220 Buff_left = pTemp->ulBuffsz - pTemp->ulBuffpos;
221
222 // write the audio buffer
223 bytesread = OSS16_StreamAddBuffer(this, pdataBuf, Buff_left);
224 if(bytesread == 0) {
225 return FALSE; //no more data
226 }
227
228 dprintf(("_vReadAudioBuf %lx size %d, bytes read %d", pdataBuf, Buff_left, bytesread));
229
230 // update the buffer pos counter
231 pTemp->ulBuffpos += bytesread;
232 _ulBytesProcessed += bytesread;
233
234 if (!qhDone.Head() || pTemp != qhDone.Head()) {
235 qhDone.PushOnTail(qhInProcess.PopHead());
236 }
237 if(pTemp->ulBuffpos == pTemp->ulBuffsz) {
238 ReturnBuffer();
239 }
240
241 return TRUE;
242}
243// called by the irq function in the hardware object when we get an interrupt
244// first call _vUpdateProcessed() to update the dma amd audio buffer related
245// stuff. Next if we have buffers on the primary queue try to read/write them
246// to the audiobuffer. Look at the buffers on the done queue and see if they
247// can be returned and finally process any events pending.
248void WAVESTREAM::Process(void)
249{
250 PSTREAMBUFFER ptemp;
251 ULONG ulCurBytesProcessed = 0;
252 ULONG bytesinc;
253
254 switch (ulStreamType & STREAM_WRITE) {
255 case STREAM_WRITE:
256 {
257 OSS16_StreamGetPos(this, &ulCurBytesProcessed);
258 if(ulCurBytesProcessed == 0) {
259 //shouldn't happen (TODO recording)
260 DebugInt3();
261 return;
262 }
263 bytesinc = ulCurBytesProcessed - _ulBytesProcessed;
264 if(ulCurBytesProcessed < _ulBytesProcessed) {
265 dprintf(("WARNING: Process: Current pos %ld incr %d", ulCurBytesProcessed, (USHORT)bytesinc));
266 }
267 _ulBytesProcessed = ulCurBytesProcessed;
268
269 while(bytesinc) {
270 if(qhDone.IsElements()) { // if there are buffers that have been
271 // completly written to the audio buffer
272 // check the first one on the done queue
273 // if it's data has been consumed by
274 // the hardware return it
275 ptemp = (PSTREAMBUFFER)qhDone.Head();
276 ptemp->ulDonepos += bytesinc;
277 bytesinc = 0;
278 if(ptemp->ulDonepos >= ptemp->ulBuffsz) {
279 //calc position in next buffer
280 bytesinc = ptemp->ulDonepos - ptemp->ulBuffsz;
281//// dprintf(("Process: Return buffer %lx size %d", ptemp->pBuffptr, ptemp->ulBuffsz));
282 ReturnBuffer();
283 }
284 }
285 else break; //shouldn't happen
286 }
287
288 //Get rid of empty buffers
289 ptemp = (PSTREAMBUFFER)qhInProcess.Head();
290 if(ptemp && (ptemp->ulBuffsz == 0 || ptemp->pBuffptr == NULL)) {
291 dprintf(("Returning 0 size buffer immediately"));
292 ReturnBuffer();
293 }
294 AddBuffers();
295 break;
296 }
297 case STREAM_READ:
298 while(_vReadAudioBuf());
299 break;
300 default:
301 break;
302 } /* endswitch */
303
304 ProcessEvents();
305}
306
307ULONG WAVESTREAM::Write(PSTREAMBUF pbuf, unsigned uLength)
308{
309 qhInProcess.PushOnTail((PQUEUEELEMENT)new STREAMBUFFER(uLength, pbuf));
310//// dprintf(("WAVESTREAM::Write: Push on tail %lx %d", ((PSTREAMBUFFER)qhInProcess.Head())->pBuffptr, ((PSTREAMBUFFER)qhInProcess.Head())->ulBuffsz));
311 if(fUnderrun) {
312 fUnderrun = FALSE;
313 OSS16_StreamReset(this);
314 AddBuffers();
315 if(ulStreamType == STREAM_WAVE_PLAY)
316 OSS16_SetWaveOutVol(this, volume);
317 }
318 return 0;
319}
320
321ULONG WAVESTREAM::Read(PSTREAMBUF pbuf, unsigned uLength)
322{
323 qhInProcess.PushOnTail((PQUEUEELEMENT)new STREAMBUFFER(uLength, pbuf));
324 return 0;
325}
326
327// WAVESTREAM::GetCurrentTime(void)
328// get current time will calculate the stream time in milliseconds based on
329// NOW.... the open mpeg folks this is the greatest thing since my last
330// pay raise!!
331// but then again you know what those ring 3 programmers are like....
332// the algorythum goes like this....
333// bytes consumed / consume rate = seconds
334// Note before calling BufferUpdate check to see if the stream is running.
335// if it is not then call with flags of 0. This will prevent the audio buffer
336// trying to get the latest consumption info from the hardware object. (dma or
337// or pci as the case may be) Asking a hardware object that is not running for
338// status information is just not a good idea.....
339//
340
341ULONG WAVESTREAM::GetCurrentTime()
342{
343 ULONG Seconds, MilliSeconds, Overflow, Processed;
344
345 if (ulStreamState == STREAM_STREAMING) // if the stream is active
346 {
347 if (ulStreamType & STREAM_WRITE) {
348 OSS16_StreamGetPos(this, &Processed);
349 }
350 else Processed = _ulBytesProcessed;
351 }
352 else Processed = 0;
353
354 // if we haven't processed anything then just return
355 // _ulTimeBase
356 if(Processed == 0)
357 return(_ulTimeBase);
358
359 Seconds = Processed / _configinfo.ulPCMConsumeRate;
360 Overflow = Processed - (Seconds * _configinfo.ulPCMConsumeRate);
361 MilliSeconds = (Overflow * 1000) / _configinfo.ulPCMConsumeRate;
362 MilliSeconds += (Seconds * 1000);
363 return(MilliSeconds + _ulTimeBase);
364
365}
366//
367// SetCurrentTime
368// MMPM will send in the "starting stream time" as they see it.
369// "our stream time" will always start at 0, so we save "their" time and
370// add it to the elapsed time we calculate when we need to return time.
371//
372void WAVESTREAM::SetCurrentTime(ULONG time)
373{
374 _ulTimeBase = time;
375}
376
377//
378//
379ULONG WAVESTREAM::StartStream(void)
380{
381 PSTREAMBUFFER pTemp = (PSTREAMBUFFER)qhInProcess.Head();
382 ULONG fragsize;
383
384 // configure the wave device
385 ((PWAVEAUDIO)pahw)->ConfigDev(this, &_configinfo);
386
387 if(ulStreamType == STREAM_WAVE_PLAY) {
388 fragsize = _configinfo.ulPCMConsumeRate/64; //start with 64 irqs/sec
389 }
390 else fragsize = _configinfo.ulPCMConsumeRate/32; //start with 32 irqs/sec (no need for more)
391
392 //if the buffer is smaller than our predefined fragmentsize (*2), then correct it
393 //I assume here that buffers sizes don't radically change (except the last one)
394 //while playing a stream. If they do get a lot smaller, then we'll run into problems.
395 //There's nothing we can do about it as the fragment size can't be changed
396 //while the stream is playing.
397 if(pTemp->ulBuffsz/2 < fragsize) {
398 fragsize = pTemp->ulBuffsz/2;
399 if(fragsize < _configinfo.ulPCMConsumeRate/256)
400 {//lower limit; don't accept extremely small buffers
401 fragsize = _configinfo.ulPCMConsumeRate/256;
402 }
403 }
404 OSS16_StreamSetFragment(this, fragsize);
405 dprintf(("WAVESTREAM::StartStream: Fragment size %d", (USHORT)fragsize));
406 _ulBytesProcessed = 0;
407 fUnderrun = FALSE;
408
409 ulStreamState = STREAM_STREAMING;
410 //Adding the first buffer also starts playback
411 if(ulStreamType == STREAM_WAVE_PLAY) {
412 AddBuffers();
413 }
414 else {
415 if(!fRecSrcIOCTL90)
416 OSS16_SetVolume(this, MIX_SETINPUTSRC, inputsrc);
417 if(!fRecGainIOCTL90)
418 OSS16_SetVolume(this, MIX_SETINPUTGAIN, inputgain);
419 OSS16_StartStream(this);
420 }
421
422 //Must set volume after adding buffers (voices inside sblive driver might not
423 //be allocated otherwise (first start) )
424 if(ulStreamType == STREAM_WAVE_PLAY)
425 OSS16_SetWaveOutVol(this, volume);
426
427 dprintf(("WAVESTREAM::StartStream %lx", ulStreamId));
428 return NO_ERROR;
429
430}
431ULONG WAVESTREAM::StopStream(PCONTROL_PARM pControl)
432{
433 if(ulStreamState == STREAM_STOPPED) {
434 dprintf(("WAVESTREAM::StopStream %lx (already stopped)", ulStreamId));
435 fUnderrun = FALSE;
436 pControl->ulTime = GetCurrentTime();
437 return NO_ERROR;
438 }
439 pahw->Stop(this);
440 //Reset cleans up waveout instance
441 OSS16_StreamReset(this);
442
443 ulStreamState = STREAM_STOPPED;
444 fUnderrun = FALSE;
445 dprintf(("WAVESTREAM::StopStream %lx", ulStreamId));
446 ReturnBuffers();
447 pControl->ulTime = GetCurrentTime();
448 _ulTimeBase = GetCurrentTime();
449 return NO_ERROR;
450
451}
452
453ULONG WAVESTREAM::PauseStream(PCONTROL_PARM pControl)
454{
455 ULONG endpos;
456
457 OSS16_StreamGetPos(this, &endpos);
458
459 pahw->Stop(this);
460 //Reset cleans up waveout instance
461 OSS16_StreamReset(this);
462
463 ulStreamState = STREAM_PAUSED;
464 fUnderrun = FALSE;
465
466 dprintf(("WAVESTREAM::PauseStream %lx", ulStreamId));
467 _vRealignPausedBuffers(endpos);
468 pControl->ulTime = GetCurrentTime();
469 _ulTimeBase = GetCurrentTime();
470 return NO_ERROR;
471
472}
473ULONG WAVESTREAM::ResumeStream(void)
474{
475 // configure the wave device
476 ((PWAVEAUDIO)pahw)->ConfigDev(this, &_configinfo);
477 if(ulStreamType == STREAM_WAVE_PLAY)
478 OSS16_SetWaveOutVol(this, volume);
479
480 dprintf(("WAVESTREAM::ResumeStream %lx", ulStreamId));
481 _ulBytesProcessed = 0;
482 fUnderrun = FALSE;
483
484 ulStreamState = STREAM_STREAMING;
485 //Adding the first buffer also starts playback
486 AddBuffers();
487
488 return NO_ERROR;
489
490}
491
492void WAVESTREAM::SetInputSrc(int src)
493{
494 inputsrc = src;
495}
496
497void WAVESTREAM::SetInputGain(ULONG gain)
498{
499 inputgain = gain;
500}
501
502void WAVESTREAM::SetVolume(ULONG volume)
503{
504 this->volume = volume;
505 if(ulStreamState == STREAM_STREAMING && ulStreamType == STREAM_WAVE_PLAY) {
506 OSS16_SetWaveOutVol(this, volume);
507 }
508}
509
510WAVESTREAM::WAVESTREAM(ULONG streamtype, LPMCI_AUDIO_INIT pinit, USHORT filesysnum):
511 STREAM(streamtype, filesysnum)
512{
513 _configinfo.ulSampleRate = pinit->lSRate;
514 _configinfo.ulBitsPerSample = pinit->lBitsPerSRate;
515 _configinfo.ulNumChannels = pinit->sChannels;
516 _configinfo.ulDataType = pinit->sMode;
517 _ulBytesProcessed = 0;
518 _ulTimeBase = 0;
519 fUnderrun = FALSE;
520
521 pinit->ulFlags |= FIXED; // Fixed length data
522 pinit->ulFlags |= LEFT_ALIGNED; // Left align bits on byte bndry
523 if (pinit->lBitsPerSRate == 8)
524 pinit->ulFlags|= TWOS_COMPLEMENT; // 2's complement data
525
526 ulStreamId = OSS16_OpenStream(this);
527 dprintf(("WAVESTREAM ctor %lx: rate %d bps %d numchan %d type %x", ulStreamId, (USHORT)_configinfo.ulSampleRate, (USHORT)_configinfo.ulBitsPerSample, (USHORT)_configinfo.ulNumChannels, (USHORT)_configinfo.ulNumChannels, (USHORT)_configinfo.ulDataType));
528}
529
530WAVESTREAM::~WAVESTREAM()
531{
532 dprintf(("WAVESTREAM dtor %lx", ulStreamId));
533 if(ulStreamId) {
534 OSS16_CloseStream(this);
535 }
536}
537
Note: See TracBrowser for help on using the repository browser.