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

Added DuplicateSoundBuffer support

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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));
Note: See TracChangeset for help on using the changeset viewer.