diff options
Diffstat (limited to 'Alc/backends')
-rw-r--r-- | Alc/backends/null.c | 39 | ||||
-rw-r--r-- | Alc/backends/wave.c | 62 |
2 files changed, 57 insertions, 44 deletions
diff --git a/Alc/backends/null.c b/Alc/backends/null.c index 26e63b2e..a96ab701 100644 --- a/Alc/backends/null.c +++ b/Alc/backends/null.c @@ -72,38 +72,45 @@ static int ALCnullBackend_mixerProc(void *ptr) { ALCnullBackend *self = (ALCnullBackend*)ptr; ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice; - ALuint now, start; + struct timespec now, start; ALuint64 avail, done; + const long restTime = (long)((ALuint64)device->UpdateSize * 1000000000 / + device->Frequency / 2); SetRTPriority(); SetThreadName(MIXER_THREAD_NAME); done = 0; - start = timeGetTime(); + if(altimespec_get(&start, AL_TIME_UTC) != AL_TIME_UTC) + { + ERR("Failed to get starting time\n"); + return 1; + } while(!self->killNow && device->Connected) { - now = timeGetTime(); + if(altimespec_get(&now, AL_TIME_UTC) != AL_TIME_UTC) + { + ERR("Failed to get current time\n"); + return 1; + } - avail = (ALuint64)(now-start) * device->Frequency / 1000; + avail = (now.tv_sec - start.tv_sec) * device->Frequency; + avail += (ALint64)(now.tv_nsec - start.tv_nsec) * device->Frequency / 1000000000; if(avail < done) { - /* Timer wrapped (50 days???). Add the remainder of the cycle to - * the available count and reset the number of samples done */ - avail += (U64(1)<<32)*device->Frequency/1000 - done; - done = 0; + /* Oops, time skipped backwards. Reset the number of samples done + * with one update available since we (likely) just came back from + * sleeping. */ + done = avail - device->UpdateSize; } + if(avail-done < device->UpdateSize) - { - long restTime = (long)((device->UpdateSize - (avail-done)) * 1000000000 / - device->Frequency); al_nssleep(0, restTime); - continue; - } - - do { + else while(avail-done >= device->UpdateSize) + { aluMixData(device, NULL, device->UpdateSize); done += device->UpdateSize; - } while(avail-done >= device->UpdateSize); + } } return 0; diff --git a/Alc/backends/wave.c b/Alc/backends/wave.c index 5f5c92c1..343a3cb5 100644 --- a/Alc/backends/wave.c +++ b/Alc/backends/wave.c @@ -87,47 +87,53 @@ static void fwrite32le(ALuint val, FILE *f) static int WaveProc(void *ptr) { - ALCdevice *Device = (ALCdevice*)ptr; - wave_data *data = (wave_data*)Device->ExtraData; + ALCdevice *device = (ALCdevice*)ptr; + wave_data *data = (wave_data*)device->ExtraData; + struct timespec now, start; + ALint64 avail, done; ALuint frameSize; - ALuint now, start; - ALuint64 avail, done; size_t fs; - const long restTime = (long)((ALuint64)Device->UpdateSize * 1000000000 / - Device->Frequency / 2); + const long restTime = (long)((ALuint64)device->UpdateSize * 1000000000 / + device->Frequency / 2); SetThreadName(MIXER_THREAD_NAME); - frameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType); + frameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType); done = 0; - start = timeGetTime(); - while(!data->killNow && Device->Connected) + if(altimespec_get(&start, AL_TIME_UTC) != AL_TIME_UTC) { - now = timeGetTime(); - - avail = (ALuint64)(now-start) * Device->Frequency / 1000; - if(avail < done) + ERR("Failed to get starting time\n"); + return 1; + } + while(!data->killNow && device->Connected) + { + if(altimespec_get(&now, AL_TIME_UTC) != AL_TIME_UTC) { - /* Timer wrapped (50 days???). Add the remainder of the cycle to - * the available count and reset the number of samples done */ - avail += ((ALuint64)1<<32)*Device->Frequency/1000 - done; - done = 0; + ERR("Failed to get current time\n"); + return 1; } - if(avail-done < Device->UpdateSize) + + avail = (now.tv_sec - start.tv_sec) * device->Frequency; + avail += (ALint64)(now.tv_nsec - start.tv_nsec) * device->Frequency / 1000000000; + if(avail < done) { - al_nssleep(0, restTime); - continue; + /* Oops, time skipped backwards. Reset the number of samples done + * with one update available since we (likely) just came back from + * sleeping. */ + done = avail - device->UpdateSize; } - while(avail-done >= Device->UpdateSize) + if(avail-done < device->UpdateSize) + al_nssleep(0, restTime); + else while(avail-done >= device->UpdateSize) { - aluMixData(Device, data->buffer, Device->UpdateSize); - done += Device->UpdateSize; + aluMixData(device, data->buffer, device->UpdateSize); + done += device->UpdateSize; if(!IS_LITTLE_ENDIAN) { - ALuint bytesize = BytesFromDevFmt(Device->FmtType); + ALuint bytesize = BytesFromDevFmt(device->FmtType); ALubyte *bytes = data->buffer; ALuint i; @@ -149,16 +155,16 @@ static int WaveProc(void *ptr) } else { - fs = fwrite(data->buffer, frameSize, Device->UpdateSize, + fs = fwrite(data->buffer, frameSize, device->UpdateSize, data->f); (void)fs; } if(ferror(data->f)) { ERR("Error writing to file\n"); - ALCdevice_Lock(Device); - aluHandleDisconnect(Device); - ALCdevice_Unlock(Device); + ALCdevice_Lock(device); + aluHandleDisconnect(device); + ALCdevice_Unlock(device); break; } } |