Rewrite audio line output. Now deterministic (from the game's POV) and smoother.

This commit is contained in:
Henrik Rydgard committed 2012-11-17 14:20:59 +01:00
1 parent ba171c8360
commit 5a48578a12
11 files changed
+475 -229

No files matched your search

+135 -50
View File
@@ -24,6 +24,9 @@
#include "../CoreTiming.h"
#include "../MemMap.h"
#include "../Host.h"
#include "../Config.h"
#include "FixedSizeQueue.h"
#include "Common/Thread.h"
// While buffers == MAX_BUFFERS, block on blocking write
// non-blocking writes will return busy, I guess
@@ -34,46 +37,52 @@
std::recursive_mutex section;
int eventAudioUpdate = -1;
int eventHostAudioUpdate = -1;
int mixFrequency = 44100;
const int hwSampleRate = 44100;
const int hwBlockSize = 480;
const int hostAttemptBlockSize = 64;
const int audioIntervalUs = (int)(1000000ULL * hwBlockSize / hwSampleRate);
const int audioHostIntervalUs = (int)(1000000ULL * hostAttemptBlockSize / hwSampleRate);
// High and low watermarks, basically.
const int chanQueueMaxSizeFactor = 4;
const int chanQueueMinSizeFactor = 2;
// A whole second, should be enough for anything.
FixedSizeQueue<s16, hwBlockSize * 8> outAudioQueue;
const int audioIntervalMs = 20;
void hleAudioUpdate(u64 userdata, int cyclesLate)
{
host->UpdateSound();
__AudioUpdate();
CoreTiming::ScheduleEvent(msToCycles(audioIntervalMs), eventAudioUpdate, 0);
CoreTiming::ScheduleEvent(usToCycles(audioIntervalUs), eventAudioUpdate, 0);
}
void hleHostAudioUpdate(u64 userdata, int cyclesLate)
{
host->UpdateSound();
CoreTiming::ScheduleEvent(usToCycles(audioHostIntervalUs), eventHostAudioUpdate, 0);
}
void __AudioInit()
{
eventAudioUpdate = CoreTiming::RegisterEvent("AudioUpdate", &hleAudioUpdate);
mixFrequency = 44100;
CoreTiming::ScheduleEvent(msToCycles(1), eventAudioUpdate, 0);
eventAudioUpdate = CoreTiming::RegisterEvent("AudioUpdate", &hleAudioUpdate);
eventHostAudioUpdate = CoreTiming::RegisterEvent("AudioUpdateHost", &hleHostAudioUpdate);
CoreTiming::ScheduleEvent(usToCycles(audioIntervalUs), eventAudioUpdate, 0);
CoreTiming::ScheduleEvent(usToCycles(audioHostIntervalUs), eventHostAudioUpdate, 0);
for (int i = 0; i < 8; i++)
chans[i].clear();
}
void __AudioShutdown()
{
}
void __AudioUpdate()
{
// DEBUG_LOG(HLE, "Updating audio");
section.lock();
for (int i = 0; i < MAX_CHANNEL; i++)
{
if (chans[i].triggered)
{
chans[i].triggered = false;
// Instead of looping through all threads, which this does, we should keep track of which threads are waiting and just
// resume them.
__KernelTriggerWait(WAITTYPE_AUDIOCHANNEL, (SceUID)i, true);
}
}
section.unlock();
for (int i = 0; i < 8; i++)
chans[i].clear();
}
u32 __AudioEnqueue(AudioChannel &chan, int chanNum, bool blocking)
@@ -81,15 +90,18 @@ u32 __AudioEnqueue(AudioChannel &chan, int chanNum, bool blocking)
section.lock();
if (chan.sampleAddress == 0)
return SCE_ERROR_AUDIO_NOT_OUTPUT;
if (chan.sampleQueue.size() > chan.sampleCount*2) {
if (chan.sampleQueue.size() > chan.sampleCount*2*chanQueueMaxSizeFactor) {
// Block!
if (blocking) {
chan.waitingThread = __KernelGetCurThread();
// WARNING: This changes currentThread so must grab waitingThread before (line above).
__KernelWaitCurThread(WAITTYPE_AUDIOCHANNEL, (SceUID)chanNum, 0, 0, false);
section.unlock();
return 0;
}
else
{
chan.waitingThread = 0;
return SCE_ERROR_AUDIO_CHANNEL_BUSY;
}
}
@@ -114,49 +126,122 @@ u32 __AudioEnqueue(AudioChannel &chan, int chanNum, bool blocking)
return 0;
}
int __AudioMix(short *outstereo, int numSamples)
// Mix samples from the various audio channels into a single sample queue.
// This single sample queue is where __AudioMix should read from. If the sample queue is full, we should
// just sleep the main emulator thread a little.
void __AudioUpdate()
{
memset(outstereo, 0, numSamples*sizeof(short)*2);
NOTICE_LOG(HLE, "__AudioUpdate");
#ifdef _WIN32
// HACK - TODO: Remove
bool noThrottle = GetAsyncKeyState(VK_TAB) != 0;
#else
bool noThrottle = false;
#endif
// Sleep here until the host audio hardware is ready to receive samples.
// This will effectively throttle the frame rate.
// Disable Audio for now.
// return numSamples;
if (!noThrottle) {
while (true)
{
if (outAudioQueue.size() < hwBlockSize * 4)
break; // room can only increase without us pushing, so there's no race condition between here and section.lock()
Common::SleepCurrentThread(0);
}
}
section.lock();
int numActiveChans = 0;
for (int j = 0; j < MAX_CHANNEL; j++)
{
numActiveChans += chans[j].running ? 1 : 0;
}
if (!numActiveChans)
{
section.unlock();
return numSamples;
}
s32 mixBuffer[hwBlockSize * 2];
memset(mixBuffer, 0, sizeof(mixBuffer));
for (int i = 0; i < MAX_CHANNEL; i++)
{
for (int s = 0; s < numSamples; s++)
if (!chans[i].reserved)
continue;
if (!chans[i].sampleQueue.size()) {
DEBUG_LOG(HLE, "No queued samples, skipping channel %i", i);
continue;
}
for (int s = 0; s < hwBlockSize; s++)
{
if (chans[i].sampleQueue.size() >= 2)
{
s16 sample1 = chans[i].sampleQueue.front();
s16 sampleL = chans[i].sampleQueue.front();
s16 sampleR = chans[i].sampleQueue.front();
chans[i].sampleQueue.pop();
s16 sample2 = chans[i].sampleQueue.front();
chans[i].sampleQueue.pop();
outstereo[s*2] += sample1 / 4;//(sample * chans[i].vol1) >> 8;
outstereo[s*2+1] += sample2 / 4;//.(sample * chans[i].vol2) >> 8;
mixBuffer[s * 2] += sampleL;
mixBuffer[s * 2 + 1] += sampleR;
}
else
{
ERROR_LOG(HLE, "channel %i buffer underrun at %i of %i", i, s, hwBlockSize);
break;
}
}
if (chans[i].sampleQueue.size() < chans[i].sampleCount)
if (chans[i].sampleQueue.size() < chans[i].sampleCount * 2 * chanQueueMinSizeFactor)
{
chans[i].triggered = true;
// Ask the thread to send more samples until next time, queue is being drained.
if (chans[i].waitingThread) {
SceUID waitingThread = chans[i].waitingThread;
chans[i].waitingThread = 0;
// DEBUG_LOG(HLE, "Woke thread %i for some buffer filling", waitingThread);
__KernelResumeThreadFromWait(waitingThread);
}
}
}
if (!noThrottle && g_Config.bEnableSound) {
// Push the mixed samples onto the output audio queue.
for (int i = 0; i < hwBlockSize; i++) {
s32 sampleL = mixBuffer[i * 2] >> 2; // TODO - what factor?
s32 sampleR = mixBuffer[i * 2 + 1] >> 2;
outAudioQueue.push((s16)sampleL);
outAudioQueue.push((s16)sampleR);
}
}
section.unlock();
return numSamples;
}
void __AudioSetOutputFrequency(int freq)
{
mixFrequency = freq;
}
// numFrames is number of stereo frames.
int __AudioMix(short *outstereo, int numFrames)
{
// TODO: if mixFrequency != the actual output frequency, resample!
section.lock();
int underrun = -1;
s16 sampleL = 0;
s16 sampleR = 0;
for (size_t i = 0; i < numFrames; i++) {
if (outAudioQueue.size() >= 2)
{
sampleL = outAudioQueue.front();
outAudioQueue.pop();
sampleR = outAudioQueue.front();
outAudioQueue.pop();
outstereo[i * 2] = sampleL;
outstereo[i * 2 + 1] = sampleR;
} else {
underrun = i;
outstereo[i * 2] = sampleL; // repeat last sample, can reduce clicking
outstereo[i * 2 + 1] = sampleR; // repeat last sample, can reduce clicking
}
}
if (underrun >= 0) {
ERROR_LOG(HLE, "audio out buffer UNDERRUN at %i of %i", underrun, numFrames);
} else {
// DEBUG_LOG(HLE, "No underrun, mixed %i samples fine", numFrames);
}
section.unlock();
return numFrames;
}