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

Last change on this file since 152 was 152, checked in by sandervl, 25 years ago

misc updates

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