Changeset 5292 for trunk/src


Ignore:
Timestamp:
Mar 10, 2001, 7:21:06 AM (24 years ago)
Author:
mike
Message:

Added DuplicateSoundBuffer support

Location:
trunk/src/dsound
Files:
3 edited

Legend:

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

    r3555 r5292  
    1 /* $Id: OS2DSOUND.CPP,v 1.7 2000-05-18 20:37:08 mike Exp $ */
     1/* $Id: OS2DSOUND.CPP,v 1.8 2001-03-10 06:21:06 mike Exp $ */
    22
    33/*
     
    55 *
    66 * Copyright 1998 Sander van Leeuwen
    7  * Copyright 2000 Michal Necasek
     7 * Copyright 2000 Kevin Langman
     8 * Copyright 2001 Michal Necasek
    89 *
    910 * Project Odin Software License can be found in LICENSE.TXT
     
    1819#include <string.h>
    1920
    20 
    21 
    2221#include <stdio.h> /****** DEBUGGING ********/
    23 
    24 
    2522
    2623#define INITGUID
     
    3431#include <misc.h>
    3532
    36 // TODO: primary buffer should probably created right away when creating the DSound
    37 // object
    3833// TODO: handle cooperative levels properly!!
    3934
     
    6055   Vtbl.Initialize           = SoundInitialize;
    6156
    62    dprintf(("DSOUND-OS2IDirectSound::OS2IDirectSound\n"));
     57   dprintf(("DSOUND-OS2IDirectSound::OS2IDirectSound"));
    6358
    6459   speakerConfig = DSSPEAKER_STEREO;
     
    7570   if ( primary->GetLastError() != DS_OK ){
    7671      ULONG lastErr = primary->GetLastError();
    77       dprintf(("LastErr = %d\n", lastErr));
     72      dprintf(("LastErr = %d", lastErr));
    7873      delete primary;
    7974      lastError = lastErr;
     
    8277   primary->Vtbl.AddRef(primary);
    8378
    84    dprintf(("Sound init OK\n"));
    85 }
     79   dprintf(("Sound init OK"));
     80}
     81
    8682//******************************************************************************
    8783//******************************************************************************
     
    9288   OS2IDirectSound::fDSExists = FALSE;
    9389}
     90
    9491//******************************************************************************
    9592//******************************************************************************
    9693HRESULT WIN32API SoundQueryInterface(THIS, REFIID riid, LPVOID * ppvObj)
    9794{
    98    dprintf(("DSOUND-OS2IDirectSound::QueryInterface\n"));
     95   dprintf(("DSOUND-OS2IDirectSound::QueryInterface"));
    9996   if (This == NULL) {
    10097      return DSERR_INVALIDPARAM;
     
    118115   OS2IDirectSound *me = (OS2IDirectSound *)This;
    119116
    120    dprintf(("DSOUND-OS2IDirectSound::AddRef %d\n", me->Referenced+1));
     117   dprintf(("DSOUND-OS2IDirectSound::AddRef %d", me->Referenced+1));
    121118   return ++me->Referenced;
    122119}
     
    128125   OS2IDirectSound *me = (OS2IDirectSound *)This;
    129126
    130    dprintf(("DSOUND-OS2IDirectSound::Release %d\n", me->Referenced-1));
     127   dprintf(("DSOUND-OS2IDirectSound::Release %d", me->Referenced-1));
    131128   if (me->Referenced) {
    132129      me->Referenced--;
     
    146143HRESULT WIN32API SoundCompact(THIS)
    147144{
    148   dprintf(("DSOUND-OS2IDirectSound::Compact\n"));
     145  dprintf(("DSOUND-OS2IDirectSound::Compact - NOP"));
    149146  return DS_OK;
    150147}
     
    160157   OS2IDirectSoundBuffer *sndbuf;
    161158
    162    dprintf(("DSOUND-OS2IDirectSound::CreateSoundBuffer\n"));
     159   dprintf(("DSOUND-OS2IDirectSound::CreateSoundBuffer"));
    163160   if (me == NULL || lpDSBufferDesc == NULL || lplpDirectSoundBuffer == NULL) {
    164161      return DSERR_INVALIDPARAM;
     
    171168
    172169   if (lpDSBufferDesc->dwFlags & DSBCAPS_PRIMARYBUFFER) {
    173 
    174 /*
    175 
    176       // The Primary buffer is now created by the Constructor.. This only
    177       // makes sence becuse you can really only have one primary buffer and
    178       // you need it before you can to do anything.
    179 
    180       OS2PrimBuff *primbuff;
    181       primbuff = new OS2PrimBuff(me, lpDSBufferDesc);
    182 
    183       if (primbuff == NULL)
    184          return DSERR_OUTOFMEMORY;
    185 
    186       if ( primbuff->GetLastError() != DS_OK ){
    187          ULONG lastErr = primbuff->GetLastError();
    188 
    189          dprintf(("LastErr = %d\n", lastErr));
    190          delete primbuff;
    191          return lastErr;
    192       }
    193       *lplpDirectSoundBuffer = (LPDIRECTSOUNDBUFFER)primbuff;
    194       primbuff->Vtbl.AddRef(primbuff);
    195       dprintf(("Created PrimBuff... Exiting Create Buffer function\n"));
    196 */
    197 
    198170      *lplpDirectSoundBuffer = (LPDIRECTSOUNDBUFFER)me->primary;
    199171      return DS_OK;
     
    204176      return DSERR_OUTOFMEMORY;
    205177   }
    206    if(sndbuf->GetLastError() != DS_OK) {
     178   if (sndbuf->GetLastError() != DS_OK) {
    207179      ULONG lastErr = sndbuf->GetLastError();
    208180      delete sndbuf;
     
    220192HRESULT WIN32API SoundGetCaps(THIS_ LPDSCAPS lpCaps)
    221193{
    222    dprintf(("DSOUND-OS2IDirectSound::GetCaps\n"));
     194   dprintf(("DSOUND-OS2IDirectSound::GetCaps"));
    223195
    224196   if (lpCaps == NULL)
     
    258230//******************************************************************************
    259231//******************************************************************************
    260 HRESULT WIN32API SoundDuplicateSoundBuffer(THIS_ LPDIRECTSOUNDBUFFER, LPLPDIRECTSOUNDBUFFER )
    261 {
    262    dprintf(("DSOUND-OS2IDirectSound::DuplicateSoundBuffer\n"));
    263    return DSERR_OUTOFMEMORY;
     232HRESULT WIN32API SoundDuplicateSoundBuffer(THIS_ LPDIRECTSOUNDBUFFER lpBuf, LPLPDIRECTSOUNDBUFFER lplpDirectSoundBuffer)
     233{
     234   OS2IDirectSound *me = (OS2IDirectSound *)This;
     235   OS2IDirectSoundBuffer *srcBuf = (OS2IDirectSoundBuffer*)lpBuf;
     236   OS2IDirectSoundBuffer *tgtBuf;
     237
     238   dprintf(("DSOUND-OS2IDirectSound::DuplicateSoundBuffer (%X to %X)", lpBuf, lplpDirectSoundBuffer));
     239   if (me == NULL || lpBuf == NULL || lplpDirectSoundBuffer == NULL) {
     240      return DSERR_INVALIDPARAM;
     241   }
     242
     243   tgtBuf = new OS2IDirectSoundBuffer(me, srcBuf);
     244   if (tgtBuf == NULL) {
     245      return DSERR_OUTOFMEMORY;
     246   }
     247
     248   if (tgtBuf->GetLastError() != DS_OK) {
     249      ULONG lastErr = tgtBuf->GetLastError();
     250      delete tgtBuf;
     251      return lastErr;
     252   }
     253   tgtBuf->Vtbl.AddRef(tgtBuf);
     254
     255   *lplpDirectSoundBuffer = (LPDIRECTSOUNDBUFFER)tgtBuf;
     256
     257   return DS_OK;
    264258}
    265259
     
    270264   OS2IDirectSound *me = (OS2IDirectSound *)This;
    271265
    272    dprintf(("DSOUND-OS2IDirectSound::SetCooperativeLevel (to %d)\n", level));
     266   dprintf(("DSOUND-OS2IDirectSound::SetCooperativeLevel (to %d)", level));
    273267   if (me == NULL) {
    274268     return DSERR_INVALIDPARAM;
     
    278272   return DS_OK;
    279273}
     274
    280275//******************************************************************************
    281276//******************************************************************************
     
    284279   OS2IDirectSound *me = (OS2IDirectSound *)This;
    285280
    286    dprintf(("DSOUND-OS2IDirectSound::GetSpeakerConfig\n"));
     281   dprintf(("DSOUND-OS2IDirectSound::GetSpeakerConfig"));
    287282   if (me == NULL) {
    288283      return(DSERR_INVALIDPARAM);
     
    299294   OS2IDirectSound *me = (OS2IDirectSound *)This;
    300295
    301    dprintf(("DSOUND-OS2IDirectSound::SetSpeakerConfig %X\n", speakerCfg));
     296   dprintf(("DSOUND-OS2IDirectSound::SetSpeakerConfig %X", speakerCfg));
    302297   if (me == NULL) {
    303298     return DSERR_INVALIDPARAM;
     
    311306HRESULT WIN32API SoundInitialize(THIS_ LPGUID)
    312307{
    313    dprintf(("DSOUND-OS2IDirectSound::Initialize NOP\n"));
    314    return DS_OK;
    315 }
    316 //******************************************************************************
    317 //******************************************************************************
     308   dprintf(("DSOUND-OS2IDirectSound::Initialize NOP"));
     309   return DS_OK;
     310}
     311//******************************************************************************
     312//******************************************************************************
  • trunk/src/dsound/OS2SNDBUFFER.CPP

    r5285 r5292  
    1 /* $Id: OS2SNDBUFFER.CPP,v 1.9 2001-03-06 20:11:16 mike Exp $ */
     1/* $Id: OS2SNDBUFFER.CPP,v 1.10 2001-03-10 06:21:06 mike Exp $ */
    22
    33/*
     
    55 *
    66 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
    7  * Copyright 2000 Michal Necasek
     7 * Copyright 2001 Michal Necasek
    88 *
    99 * Project Odin Software License can be found in LICENSE.TXT
     
    5050        : Referenced(0), lastError(DS_OK)
    5151{
     52   //This is the "normal" constructor
    5253   lpVtbl = &Vtbl;
    5354   Vtbl.AddRef               = SoundBufAddRef;
     
    110111   DSpan     = 0;
    111112   lpfxFormat= NULL;
    112    lpBuffer  = NULL;
    113113   fPlaying  = FALSE;
    114114   fLoop     = FALSE;
     
    135135   dprintf((" Buffer format: %dHz, %dbit, %d channels", lpfxFormat->nSamplesPerSec, lpfxFormat->wBitsPerSample, lpfxFormat->nChannels));
    136136
    137    lpBuffer = (LPSTR)VirtualAlloc(0, bufferdesc.dwBufferBytes, MEM_COMMIT, PAGE_READWRITE);
    138    if (lpBuffer == NULL) {
     137   lpSndBuffer = (SOUNDBUF*)VirtualAlloc(0, bufferdesc.dwBufferBytes + sizeof(DWORD), MEM_COMMIT, PAGE_READWRITE);
     138   if (lpSndBuffer == NULL) {
    139139      dprintf(("VirtualAlloc failed"));
    140       lpBuffer  = NULL;
     140      lpSndBuffer  = NULL;
    141141      lastError = DSERR_OUTOFMEMORY;
    142142      return;
    143143   }
     144   lpBuffer = &(lpSndBuffer->pData[0]);
     145   lpSndBuffer->dwReferences = 1;
     146
     147   next = dsbroot;
     148   dsbroot = this;
     149}
     150
     151//******************************************************************************
     152//******************************************************************************
     153OS2IDirectSoundBuffer::OS2IDirectSoundBuffer(OS2IDirectSound *DSound, OS2IDirectSoundBuffer *srcBuf)
     154        : Referenced(0), lastError(DS_OK)
     155{
     156   //This is the constructor used for duplicating sound buffers
     157   lpVtbl = &Vtbl;
     158   Vtbl.AddRef               = SoundBufAddRef;
     159   Vtbl.Release              = SoundBufRelease;
     160   Vtbl.QueryInterface       = SoundBufQueryInterface;
     161   Vtbl.GetCaps              = SoundBufGetCaps;
     162   Vtbl.GetFormat            = SoundBufGetFormat;
     163   Vtbl.GetVolume            = SoundBufGetVolume;
     164   Vtbl.GetStatus            = SoundBufGetStatus;
     165   Vtbl.GetCurrentPosition   = SoundBufGetCurrentPosition;
     166   Vtbl.GetPan               = SoundBufGetPan;
     167   Vtbl.GetFrequency         = SoundBufGetFrequency;
     168   Vtbl.Initialize           = SoundBufInitialize;
     169   Vtbl.Restore              = SoundBufRestore;
     170   Vtbl.SetFormat            = SoundBufSetFormat;
     171   Vtbl.SetVolume            = SoundBufSetVolume;
     172   Vtbl.SetCurrentPosition   = SoundBufSetCurrentPosition;
     173   Vtbl.SetPan               = SoundBufSetPan;
     174   Vtbl.SetFrequency         = SoundBufSetFrequency;
     175   Vtbl.Lock                 = SoundBufLock;
     176   Vtbl.Unlock               = SoundBufUnlock;
     177   Vtbl.Stop                 = SoundBufStop;
     178   Vtbl.Play                 = SoundBufPlay;
     179
     180   dprintf(("DSOUND-OS2IDirectSoundBuffer::OS2IDirectSoundBuffer/Duplicate (buf=%X)", this));
     181
     182   if (srcBuf->fPrimary) {
     183      dprintf(("Error: Cannot duplicate primary sound buffer!"));
     184      lpBuffer  = NULL;
     185      lastError = DSERR_INVALIDPARAM;
     186      return;
     187   }
     188
     189   // get the primary buffer from the DSound instance.
     190   primary = DSound->primary;
     191
     192   // No need to check if primary exists - it has to (this can't be the first
     193   // secondary buffer being created)
     194
     195   // Add a reference to the primary buffer for _every_ secondary buffer; this makes
     196   // sure that as long as a secondary buffer exists the primary buffer won't get
     197   // destroyed; moreover if the user didn't create  a primary buffer himself/herself,
     198   // it will automatically get created and destroyed with the last secondary buffer
     199   primary->Vtbl.AddRef(primary);
     200
     201   parentDS  = DSound;
     202   writepos  = 0;
     203   playpos   = 0;
     204   status    = 0;
     205   fLocked   = FALSE;
     206   fPrimary  = FALSE;
     207   volume    = srcBuf->volume;
     208   DSvolume  = srcBuf->DSvolume;
     209   pan       = srcBuf->pan;
     210   DSpan     = srcBuf->DSpan;
     211   lpBuffer  = srcBuf->lpBuffer;
     212   fPlaying  = FALSE;
     213   fLoop     = srcBuf->fLoop;
     214   notify    = NULL;
     215   memcpy(&bufferdesc, &(srcBuf->bufferdesc), sizeof(DSBUFFERDESC));
     216
     217   // Note: this cannot be a primary buffer - those take a different route
     218
     219   // frequency has to be set according to the bufer format
     220   frequency = srcBuf->frequency;
     221
     222   lpfxFormat = (WAVEFORMATEX *)malloc(sizeof(WAVEFORMATEX));
     223   memcpy(lpfxFormat,srcBuf->lpfxFormat,
     224          sizeof(WAVEFORMATEX));
     225
     226   dprintf((" Buffer format: %dHz, %dbit, %d channels", lpfxFormat->nSamplesPerSec, lpfxFormat->wBitsPerSample, lpfxFormat->nChannels));
     227
     228   // Increment reference count for the buffer memory
     229   lpSndBuffer  = srcBuf->lpSndBuffer;
     230   lpSndBuffer->dwReferences++;
    144231
    145232   next = dsbroot;
     
    157244   dprintf(("DSOUND-OS2IDirectSoundBuffer::~OS2IDirectSoundBuffer (buf=%X)", this));
    158245
    159    // free allocted memory
    160    if (lpBuffer)
    161       VirtualFree(lpBuffer, 0, MEM_RELEASE);
     246   // free allocted memory - unless it's shared by a duplicated buffer
     247   if (lpSndBuffer) {
     248      lpSndBuffer->dwReferences--;
     249      if (lpSndBuffer->dwReferences == 0)
     250         VirtualFree(lpSndBuffer, 0, MEM_RELEASE);
     251   }
    162252
    163253   if (lpfxFormat)
     
    328418      return DSERR_INVALIDPARAM;
    329419   }
    330    copysize = MIN(ddwSizeAllocated, (me->lpfxFormat->cbSize + sizeof(WAVEFORMATEX)));
     420   copysize = min(ddwSizeAllocated, (me->lpfxFormat->cbSize + sizeof(WAVEFORMATEX)));
    331421   memcpy(lpwfxFormat, me->lpfxFormat, copysize);
    332422
     
    457547{
    458548   OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
     549
     550   ULONG ulBytesToPlay;
    459551
    460552   dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufPlay (buf=%X)", me));
  • trunk/src/dsound/OS2SNDBUFFER.H

    r3555 r5292  
    1 /* $Id: OS2SNDBUFFER.H,v 1.7 2000-05-18 20:37:09 mike Exp $ */
     1/* $Id: OS2SNDBUFFER.H,v 1.8 2001-03-10 06:21:06 mike Exp $ */
    22
    33/*
     
    1818#define THIS_ VOID*This,
    1919
     20// simple structure to handle duplicated buffers - the first DWORD of
     21// a sound buffer contains the buffer's usage count (to prevent freeing
     22// of buffers still in use)
     23typedef struct _SOUNDBUF {
     24    DWORD   dwReferences;
     25    char    pData[1];
     26} SOUNDBUF;
     27
    2028// a forward declaration of some classes is needed here!
    2129class OS2IDirectSoundNotify;
     
    3139
    3240    OS2IDirectSoundBuffer(OS2IDirectSound *DSound, const DSBUFFERDESC *lpDSBufferDesc);
     41    OS2IDirectSoundBuffer(OS2IDirectSound *DSound, OS2IDirectSoundBuffer *srcBuf);
    3342    OS2IDirectSoundBuffer() {};
    3443    ~OS2IDirectSoundBuffer();
     
    3948    inline  HRESULT       GetLastError()    { return lastError;    };
    4049    inline  void          SetNotify(OS2IDirectSoundNotify *_notify) {
    41                                     notify = _notify; };
     50                          notify = _notify; };
    4251    inline  void          Set3DBuffer(OS2IDirectSound3DBuffer *_buffer3D) {
    43                                       buffer3D = _buffer3D; };
     52                          buffer3D = _buffer3D; };
    4453 private:
    4554
     
    6170    LONG  frac;                // saved mixer position overflow
    6271
    63     char *lpBuffer;            // sound data
     72    SOUNDBUF *lpSndBuffer;     // sound data + reference count
     73    char     *lpBuffer;        // actual sound data
    6474
    6575    OS2IDirectSoundNotify*   notify;    // pointer to the associated Notify object (if exists)
Note: See TracChangeset for help on using the changeset viewer.