Ignore:
Timestamp:
Nov 14, 1999, 11:12:15 PM (26 years ago)
Author:
hugh
Message:

Implemented fake soundcard
The Bufferpos will be advanced based on the elapsed time
since start playing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/dsound/OS2SNDBUFFER.CPP

    r1421 r1744  
    1 /* $Id: OS2SNDBUFFER.CPP,v 1.4 1999-10-23 23:00:49 sandervl Exp $ */
     1/* $Id: OS2SNDBUFFER.CPP,v 1.5 1999-11-14 22:12:15 hugh Exp $ */
    22
    33/*
     
    1414*   Header Files                                                               *
    1515*******************************************************************************/
     16#define INCL_DOSMISC
    1617#include <os2win.h>
    1718#include <stdlib.h>
     
    2324#include "os2dsound.h"
    2425#include "os2sndbuffer.h"
    25 #include "misc.h"
    26 
     26#include <misc.h>
     27
     28
     29extern DWORD GetTickCountOS2();
     30
     31WAVEFORMATEX wfxDefaultPrimaryBuf = { WAVE_FORMAT_PCM,
     32                                      2,
     33                                      22050,
     34                                      44100,
     35                                      2,
     36                                      8,
     37                                      0};
    2738
    2839//******************************************************************************
     
    6172  lpfxFormat= NULL;
    6273  lpBuffer  = NULL;
    63 
     74  fPlaying  = FALSE;
     75  fLoop     = FALSE;
     76  newWritePos = 0;
     77  LastTickCnt = 0;
    6478  memcpy(&bufferdesc, lpDSBufferDesc, sizeof(DSBUFFERDESC));
    65   if(!(bufferdesc.dwFlags & DSBCAPS_PRIMARYBUFFER)) {
    66     if(bufferdesc.lpwfxFormat == NULL || bufferdesc.dwBufferBytes == 0) {
    67         dprintf(("bufferdesc not valid!"));
    68         lastError = DSERR_INVALIDPARAM;
    69         return;
    70     }
    71     lpfxFormat = (WAVEFORMATEX *)malloc(bufferdesc.lpwfxFormat->cbSize + sizeof(WAVEFORMATEX));
    72     memcpy(lpfxFormat, bufferdesc.lpwfxFormat, bufferdesc.lpwfxFormat->cbSize + sizeof(WAVEFORMATEX));
    73 
    74     lpBuffer = (LPSTR)VirtualAlloc(0, bufferdesc.dwBufferBytes, MEM_COMMIT, PAGE_READWRITE);
    75     if(lpBuffer == NULL) {
    76         dprintf(("VirtualAlloc failed"));
    77         lpBuffer  = NULL;
    78         lastError = DSERR_OUTOFMEMORY;
    79         return;
    80     }
    81   }
    82   else {//todo: primary buffer
     79
     80  if(bufferdesc.dwFlags & DSBCAPS_PRIMARYBUFFER)
     81  {
     82    // Primary Buffer
     83    // Fake this for now and setup the default values for it
     84    //todo: Do the primary buffer right
     85    bufferdesc.dwBufferBytes = 4096;
     86    bufferdesc.lpwfxFormat = &wfxDefaultPrimaryBuf;
     87
     88  }
     89
     90  if(bufferdesc.lpwfxFormat == NULL || bufferdesc.dwBufferBytes == 0)
     91  {
     92      dprintf(("bufferdesc not valid!"));
     93      lastError = DSERR_INVALIDPARAM;
     94      return;
     95  }
     96
     97  lpfxFormat = (WAVEFORMATEX *)malloc(bufferdesc.lpwfxFormat->cbSize + sizeof(WAVEFORMATEX));
     98  memcpy( lpfxFormat,
     99          bufferdesc.lpwfxFormat,
     100          bufferdesc.lpwfxFormat->cbSize + sizeof(WAVEFORMATEX));
     101
     102  lpBuffer = (LPSTR)VirtualAlloc( 0, bufferdesc.dwBufferBytes, MEM_COMMIT, PAGE_READWRITE);
     103  if(lpBuffer == NULL)
     104  {
     105      dprintf(("VirtualAlloc failed"));
     106      lpBuffer  = NULL;
     107      lastError = DSERR_OUTOFMEMORY;
     108      return;
    83109  }
    84110}
     
    98124{
    99125  dprintf(("OS2IDirectSoundBuffer::QueryInterface\n"));
    100   if(This == NULL) {
     126  if(This == NULL)
     127  {
    101128    return DSERR_INVALIDPARAM;
    102129  }
     
    118145
    119146  dprintf(("OS2IDirectSoundBuffer::AddRef %d\n", me->Referenced+1));
    120   if(me == NULL) {
     147  if(me == NULL)
     148  {
    121149    return 0;
    122150  }
     
    129157 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    130158
    131   dprintf((" OS2IDirectSoundBuffer::Release %d\n", me->Referenced-1));
    132   if(me == NULL) {
     159  dprintf(("OS2IDirectSoundBuffer::Release %d\n", me->Referenced-1));
     160  if(me == NULL)
     161  {
    133162    return 0;
    134163  }
    135   if(me->Referenced) {
     164  if(me->Referenced)
     165  {
    136166    me->Referenced--;
    137     if(me->Referenced == 0) {
    138         delete me;
    139         return(0);
    140     }
    141     else    return me->Referenced;
    142   }
    143   else  return(0);
     167    if(me->Referenced == 0)
     168    {
     169      delete me;
     170      return(0);
     171    }
     172    else
     173      return me->Referenced;
     174  }
     175  else
     176    return(0);
    144177}
    145178//******************************************************************************
     
    149182 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    150183
    151   dprintf(("SoundBufGetCaps"));
    152   if(me == NULL || lpDSCaps == NULL) {
     184  dprintf(("OS2IDirectSoundBuffer::SoundBufGetCaps"));
     185  if(me == NULL || lpDSCaps == NULL)
     186  {
    153187    return DSERR_INVALIDPARAM;
    154188  }
     
    162196 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    163197
    164   dprintf(("SoundBufGetCurrentPosition"));
    165   if(me == NULL || lpdwCurrentPlayCursor == NULL || lpdwCurrentWriteCursor == NULL) {
    166     return DSERR_INVALIDPARAM;
    167   }
     198  dprintf(("OS2IDirectSoundBuffer::SoundBufGetCurrentPosition"));
     199  if( me == NULL || lpdwCurrentPlayCursor == NULL || lpdwCurrentWriteCursor == NULL)
     200  {
     201    dprintf(("  Invalid parameters %d %d %d",me,lpdwCurrentPlayCursor,lpdwCurrentWriteCursor));
     202    return DSERR_INVALIDPARAM;
     203  }
     204
     205  // Simply advance the bufferpointer according to time;
     206  if(me->fPlaying)
     207  {
     208    ULONG CurrentTicks, Ticks, NewPos;
     209    CurrentTicks = GetTickCountOS2();
     210    Ticks = CurrentTicks-me->LastTickCnt;
     211    dprintf(("  Is Playing, Ticks: Last %d Current %d Difference %d",me->LastTickCnt, CurrentTicks,Ticks));
     212    me->LastTickCnt = CurrentTicks;
     213
     214    NewPos = ((me->frequency*Ticks)/1000)*(me->bufferdesc.lpwfxFormat->wBitsPerSample/8);
     215    dprintf(("  advance pos by %d bytes",NewPos));
     216
     217    if(me->playpos+NewPos >= me->bufferdesc.dwBufferBytes)
     218    {
     219      if(me->fLoop)
     220      {
     221        me->playpos = (me->playpos+NewPos) % me->bufferdesc.dwBufferBytes;
     222      }
     223      else
     224      {
     225        me->playpos  = me->bufferdesc.dwBufferBytes;
     226        me->fPlaying = FALSE;
     227      }
     228
     229    }
     230    else
     231    {
     232      me->playpos += NewPos;
     233    }
     234    me->writepos = me->playpos + 2048;
     235    if(me->writepos >= me->bufferdesc.dwBufferBytes)
     236      me->writepos = me->writepos % me->bufferdesc.dwBufferBytes;
     237
     238  }
     239  else
     240  {
     241    dprintf(("  Buffer Not playing"));
     242  }
     243  dprintf(("  Buffer %d: PlayPos %d, WritePos %d\n",me, me->playpos,me->writepos));
    168244  *lpdwCurrentPlayCursor  = me->playpos;
    169245  *lpdwCurrentWriteCursor = me->writepos;
     
    178254 int copysize;
    179255
    180   dprintf(("SoundBufGetFormat"));
    181   if(me == NULL || lpwfxFormat == NULL || ddwSizeAllocated == 0) {
     256  dprintf(("OS2IDirectSoundBuffer::SoundBufGetFormat"));
     257  if( me == NULL || lpwfxFormat == NULL || ddwSizeAllocated == 0)
     258  {
    182259    return DSERR_INVALIDPARAM;
    183260  }
     
    185262  memcpy(lpwfxFormat, me->lpfxFormat, copysize);
    186263
    187   if(lpdwSizeWritten) {
     264  if(lpdwSizeWritten)
     265  {
    188266    *lpdwSizeWritten = copysize;
    189267  }
     
    196274 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    197275
    198   dprintf(("SoundBufGetVolume"));
    199   if(me == NULL || lplVolume == NULL) {
     276  dprintf(("OS2IDirectSoundBuffer::SoundBufGetVolume"));
     277  if(me == NULL || lplVolume == NULL)
     278  {
    200279    return DSERR_INVALIDPARAM;
    201280  }
     
    209288 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    210289
    211   dprintf(("SoundBufGetPan"));
    212   if(me == NULL || lplPan == NULL) {
     290  dprintf(("OS2IDirectSoundBuffer::SoundBufGetPan"));
     291  if(me == NULL || lplPan == NULL)
     292  {
    213293    return DSERR_INVALIDPARAM;
    214294  }
     
    222302 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    223303
    224   dprintf(("SoundBufGetFrequency"));
    225   if(me == NULL || lpdwFrequency == NULL) {
     304  dprintf(("OS2IDirectSoundBuffer::SoundBufGetFrequency"));
     305  if(me == NULL || lpdwFrequency == NULL)
     306  {
    226307    return DSERR_INVALIDPARAM;
    227308  }
     
    235316 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    236317
    237   dprintf(("SoundBufGetStatus"));
    238   if(me == NULL || lpdwStatus == NULL) {
     318  dprintf(("OS2IDirectSoundBuffer::SoundBufGetStatus"));
     319  if(me == NULL || lpdwStatus == NULL)
     320  {
    239321    return DSERR_INVALIDPARAM;
    240322  }
     
    249331
    250332  dprintf(("SoundBufInitialize"));
    251   if(me == NULL) {
     333  if(me == NULL)
     334  {
    252335    return DSERR_INVALIDPARAM;
    253336  }
     
    263346 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    264347
    265   dprintf(("SoundBufLock"));
    266   if(me == NULL || !lplpvAudioPtr1 || !lpdwAudioBytes1) {
     348  dprintf(("OS2IDirectSoundBuffer::SoundBufLock"));
     349  if(me == NULL || !lplpvAudioPtr1 || !lpdwAudioBytes1)
     350  {
    267351    return(DSERR_INVALIDPARAM);
    268352  }
    269353  //not sure if this is an error, but it's certainly a smart thing to do (cond. == true)
    270   if(dwWriteBytes > me->bufferdesc.dwBufferBytes) {
     354  if(dwWriteBytes > me->bufferdesc.dwBufferBytes)
     355  {
    271356    dprintf(("SoundBufLock: dwWriteBytes > me->bufferdesc.dwBufferBytes"));
    272357    return(DSERR_INVALIDPARAM);
    273358  }
    274   if(dwFlags & DSBLOCK_FROMWRITECURSOR) {
     359  if(dwFlags & DSBLOCK_FROMWRITECURSOR)
     360  {
    275361    dwWriteCursor = me->writepos;
    276362  }
    277   if(dwWriteCursor + dwWriteBytes > me->bufferdesc.dwBufferBytes ) {
     363  if(dwWriteCursor + dwWriteBytes > me->bufferdesc.dwBufferBytes )
     364  {
    278365    *(DWORD *)lplpvAudioPtr1  = (DWORD)(me->lpBuffer + dwWriteCursor);
    279366    *lpdwAudioBytes1          = me->bufferdesc.dwBufferBytes - dwWriteCursor;
    280     if(lplpvAudioPtr2 && lpdwAudioBytes2) {
     367    if(lplpvAudioPtr2 && lpdwAudioBytes2)
     368    {
    281369        *(DWORD *)lplpvAudioPtr2   = (DWORD)me->lpBuffer;
    282370        *lpdwAudioBytes2           = dwWriteBytes - *lpdwAudioBytes1;
    283371    }
    284372  }
    285   else {
     373  else
     374  {
    286375    *(DWORD *)lplpvAudioPtr1  = (DWORD)(me->lpBuffer + dwWriteCursor);
    287376    *lpdwAudioBytes1          = dwWriteBytes;
    288     if(lplpvAudioPtr2 && lpdwAudioBytes2) {
     377    if(lplpvAudioPtr2 && lpdwAudioBytes2)
     378    {
    289379        *(DWORD *)lplpvAudioPtr2   = 0;
    290380        *lpdwAudioBytes2           = 0;
     
    296386//******************************************************************************
    297387//******************************************************************************
    298 HRESULT __stdcall SoundBufPlay(THIS_ DWORD,DWORD,DWORD )
    299 {
    300  OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    301 
    302   dprintf(("SoundBufPlay"));
    303   if(me == NULL) {
    304     return DSERR_INVALIDPARAM;
    305   }
     388HRESULT __stdcall SoundBufPlay(THIS_ DWORD dwRes1,DWORD dwRes2,DWORD dwFlags)
     389{
     390 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
     391
     392  dprintf(("OS2IDirectSoundBuffer::SoundBufPlay"));
     393  if(me == NULL)
     394  {
     395    dprintf(("  Invalid parms me is NULL\n"));
     396    return DSERR_INVALIDPARAM;
     397  }
     398  me->fPlaying = TRUE;
     399  me->fLoop = dwFlags == DSBPLAY_LOOPING;
     400  me->LastTickCnt = GetTickCountOS2();
     401  dprintf(("  Buffer %d: Start at Tick %d Loop %s",me,me->LastTickCnt,me->fLoop?"YES":"NO"));
    306402  return DS_OK;
    307403}
     
    312408 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    313409
    314   dprintf(("SoundBufSetCurrentPosition"));
    315   if(me == NULL) {
     410  dprintf(("OS2IDirectSoundBuffer::SoundBufSetCurrentPosition"));
     411  if(me == NULL)
     412  {
    316413    return DSERR_INVALIDPARAM;
    317414  }
     
    327424 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    328425
    329   dprintf(("SoundBufSetFormat"));
    330   if(me == NULL) {
     426  dprintf(("OS2IDirectSoundBuffer::SoundBufSetFormat"));
     427  if(me == NULL)
     428  {
    331429    return DSERR_INVALIDPARAM;
    332430  }
     
    339437 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    340438
    341   dprintf(("SoundBufSetVolume"));
    342   if(me == NULL) {
     439  dprintf(("OS2IDirectSoundBuffer::SoundBufSetVolume"));
     440  if(me == NULL)
     441  {
    343442    return DSERR_INVALIDPARAM;
    344443  }
     
    352451 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    353452
    354   dprintf(("SoundBufSetPan"));
    355   if(me == NULL) {
     453  dprintf(("OS2IDirectSoundBuffer::SoundBufSetPan"));
     454  if(me == NULL)
     455  {
    356456    return DSERR_INVALIDPARAM;
    357457  }
     
    365465 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    366466
    367   dprintf(("SoundBufSetFrequency"));
    368   if(me == NULL) {
     467  dprintf(("OS2IDirectSoundBuffer::SoundBufSetFrequency"));
     468  if(me == NULL)
     469  {
    369470    return DSERR_INVALIDPARAM;
    370471  }
     
    378479 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    379480
    380   dprintf(("SoundBufStop"));
    381   if(me == NULL) {
    382     return DSERR_INVALIDPARAM;
     481  dprintf(("OS2IDirectSoundBuffer::SoundBufStop"));
     482  if(me == NULL)
     483  {
     484    return DSERR_INVALIDPARAM;
     485  }
     486  // Simply advance the bufferpointer according to time;
     487  if(me->fPlaying)
     488  {
     489    ULONG CurrentTicks, Ticks, NewPos;
     490    CurrentTicks = GetTickCountOS2();
     491
     492    Ticks = CurrentTicks - me->LastTickCnt;
     493    me->LastTickCnt = CurrentTicks;
     494
     495    NewPos = ((me->frequency*Ticks)/1000)*(me->bufferdesc.lpwfxFormat->wBitsPerSample>>8);
     496    if(me->playpos+NewPos >= me->bufferdesc.dwBufferBytes)
     497    {
     498      if(me->fLoop)
     499      {
     500        me->playpos = (me->playpos+NewPos % me->bufferdesc.dwBufferBytes);
     501      }
     502      else
     503      {
     504        me->playpos  = me->bufferdesc.dwBufferBytes;
     505        me->fPlaying = FALSE;
     506      }
     507
     508    }
     509    else
     510    {
     511      me->playpos += NewPos;
     512    }
     513    me->writepos = me->playpos + 2048;
     514    if(me->writepos >= me->bufferdesc.dwBufferBytes)
     515      me->writepos = me->writepos % me->bufferdesc.dwBufferBytes;
     516
     517    me->fPlaying = FALSE;
     518
    383519  }
    384520  return DS_OK;
     
    390526 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    391527
    392   dprintf(("SoundBufUnlock"));
    393   if(me == NULL) {
     528  dprintf(("OS2IDirectSoundBuffer::SoundBufUnlock"));
     529  if(me == NULL)
     530  {
    394531    return DSERR_INVALIDPARAM;
    395532  }
     
    402539 OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
    403540
    404   dprintf(("SoundBufRestore"));
    405   if(me == NULL) {
    406     return DSERR_INVALIDPARAM;
    407   }
    408   return DS_OK;
    409 }
    410 //******************************************************************************
    411 //******************************************************************************
     541  dprintf(("OS2IDirectSoundBuffer::SoundBufRestore"));
     542  if(me == NULL)
     543  {
     544    return DSERR_INVALIDPARAM;
     545  }
     546  return DS_OK;
     547}
     548//******************************************************************************
     549//******************************************************************************
Note: See TracChangeset for help on using the changeset viewer.