-#warning "This is VERY similar to ToggleSpeaker(); merge into common function. !!! FIX !!!"
- if (!soundInitialized)
- return;
-
-#ifdef DEBUG
-printf("SOUND: AdjustLastToggleCycles() start...\n");
-#endif
- // Step 1: Calculate delta time
- uint64 deltaCycles = elapsedCycles - lastToggleCycles;
-
- // Step 2: Calculate new buffer position
- uint32 currentPos = (uint32)((double)deltaCycles / CYCLES_PER_SAMPLE);
-
- // Step 3: Make sure there's room for it
- // We need to lock since we touch both soundBuffer and soundBufferPos
- SDL_mutexP(mutex2);
- while ((soundBufferPos + currentPos) > (SOUND_BUFFER_SIZE - 1))
- {
- // Hm.
- // This might not empty the buffer enough, causing hash and trash. !!! FIX !!! [DONE]
- SDL_mutexV(mutex2);//Release it so sound thread can get it,
- SDL_CondWait(conditional, mutex);//Sleep/wait for the sound thread
- SDL_mutexP(mutex2);//Re-lock it until we're done with it...
-
-//HMM, this doesn't need to lock or recalculate this value
-// currentPos = (uint32)((double)deltaCycles / CYCLES_PER_SAMPLE);
- }
-
- // Step 4: Backfill and adjust lastToggleCycles
- // currentPos is position from "zero" or soundBufferPos...
- currentPos += soundBufferPos;
-
- // Backfill with current toggle state
- while (soundBufferPos < currentPos)
- soundBuffer[soundBufferPos++] = (uint8)sample;
-
- SDL_mutexV(mutex2);
- lastToggleCycles = elapsedCycles;
-#ifdef DEBUG
-printf("SOUND: AdjustLastToggleCycles() end...\n");
-#endif
-#endif