-/*
-HOW IT WORKS
-
-the main thread adds the amount of cpu time elapsed to samplebase. togglespeaker uses
-samplebase + current cpu time to find appropriate spot in buffer. it then fills the
-buffer up to the current time with the old toggle value before flipping it. the sound
-irq takes what it needs from the sound buffer and then adjusts both the buffer and
-samplebase back the appropriate amount.
-
-
-A better way might be as follows:
-
-Keep timestamp array of speaker toggle times. In the sound routine, unpack as many as will
-fit into the given buffer and keep going. Have the toggle function check to see if the
-buffer is full, and if it is, way for a signal from the interrupt that there's room for
-more. Can keep a circular buffer. Also, would need a timestamp buffer on the order of 2096
-samples *in theory* could toggle each sample
-
-Instead of a timestamp, just keep a delta. That way, don't need to deal with wrapping and
-all that (though the timestamp could wrap--need to check into that)
-
-Need to consider corner cases where a sound IRQ happens but no speaker toggle happened.
-
-If (delta > SAMPLES_PER_FRAME) then
-
-Here's the relevant cases:
-
-delta < SAMPLES_PER_FRAME -> Change happened within this time frame, so change buffer
-frame came and went, no change -> fill buffer with last value
-How to detect: Have bool bufferWasTouched = true when ToggleSpeaker() is called.
-Clear bufferWasTouched each frame.
-
-Two major cases here:
-
- o Buffer is touched on current frame
- o Buffer is untouched on current frame
-
-In the first case, it doesn't matter too much if the previous frame was touched or not,
-we don't really care except in finding the correct spot in the buffer to put our change
-in. In the second case, we need to tell the IRQ that nothing happened and to continue
-to output the same value.
-
-SO: How to synchronize the regular frame buffer with the IRQ buffer?
-
-What happens:
- Sound IRQ --> Every 1024 sample period (@ 44.1 KHz = 0.0232s)
- Emulation --> Render a frame --> 1/60 sec --> 735 samples
- --> sound buffer is filled
-
-Since the emulation is faster than the SIRQ the sound buffer should fill up
-prior to dumping it to the sound card.
-
-Problem is this: If silence happens for a long time then ToggleSpeaker is never
-called and the sound buffer has stale data; at least until soundBufferPos goes to
-zero and stays there...
-
-BUT this should be handled correctly by toggling the speaker value *after* filling
-the sound buffer...
-
-Still getting random clicks when running...
-(This may be due to the lock/unlock sound happening in ToggleSpeaker()...)
-*/
-