}
void systemWriteDataToSoundBuffer(){  int len = soundBufferLen;  LPVOID  lpvPtr1;   DWORD   dwBytes1;   LPVOID  lpvPtr2;   DWORD   dwBytes2;   if(!pDirectSound)    return;  if(soundRecording) {    if(dsbSecondary) {      if(soundRecorder == NULL) {        soundRecorder = new CWaveSoundWrite;        WAVEFORMATEX format;        dsbSecondary->GetFormat(&format, sizeof(format), NULL);        soundRecorder->Open(soundRecordName, &format);      }    }          if(soundRecorder) {      UINT wrote = 0;      soundRecorder->Write(len, (u8 *)soundFinalWave, &wrote);    }  }  if(aviRecording) {    if(dsbSecondary) {      if(!aviRecorder->IsSoundAdded()) {        WAVEFORMATEX format;        dsbSecondary->GetFormat(&format, sizeof(format), NULL);        aviRecorder->SetSoundFormat(&format);      }    }    aviRecorder->AddSound((const char *)soundFinalWave, len);  }    HRESULT hr;  if(!speedup && synchronize) {    DWORD status=0;    hr = dsbSecondary->GetStatus(&status);    if(status && DSBSTATUS_PLAYING) {      if(!soundPaused) {              DWORD play;                while(true) {          dsbSecondary->GetCurrentPosition(&play, NULL);                    if(play < soundNextPosition ||             play > soundNextPosition+soundBufferLen) {            break;          }          if(dsbEvent)            WaitForSingleObject(dsbEvent, 50);        }      }    } else {      soundPaused = 1;    }  }  // Obtain memory address of write block. This will be in two parts  // if the block wraps around.  hr = dsbSecondary->Lock(soundNextPosition, soundBufferLen, &lpvPtr1,                           &dwBytes1, &lpvPtr2, &dwBytes2,                          0);    // If DSERR_BUFFERLOST is returned, restore and retry lock.   if (DSERR_BUFFERLOST == hr) {     dsbSecondary->Restore();     hr = dsbSecondary->Lock(soundNextPosition, soundBufferLen,&lpvPtr1,                            &dwBytes1, &lpvPtr2, &dwBytes2,                            0);  }   soundNextPosition += soundBufferLen;  soundNextPosition = soundNextPosition % soundBufferTotalLen;    if SUCCEEDED(hr) {     // Write to pointers.     CopyMemory(lpvPtr1, soundFinalWave, dwBytes1);     if (NULL != lpvPtr2) {       CopyMemory(lpvPtr2, soundFinalWave+dwBytes1, dwBytes2);     }     // Release the data back to DirectSound.     hr = dsbSecondary->Unlock(lpvPtr1, dwBytes1, lpvPtr2,                               dwBytes2);  }}