1 // Copyright (c) 2012- PPSSPP Project.
2
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 2.0 for more details.
11
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18 #include <atomic>
19 #include <mutex>
20
21 #include "Common/CommonTypes.h"
22 #include "Common/File/Path.h"
23 #include "Common/Serialize/Serializer.h"
24 #include "Common/Serialize/SerializeFuncs.h"
25 #include "Common/Data/Collections/FixedSizeQueue.h"
26
27 #ifdef _M_SSE
28 #include <emmintrin.h>
29 #endif
30
31 #include "Core/Config.h"
32 #include "Core/CoreTiming.h"
33 #include "Core/Host.h"
34 #include "Core/MemMapHelpers.h"
35 #include "Core/Reporting.h"
36 #include "Core/System.h"
37 #ifndef MOBILE_DEVICE
38 #include "Core/WaveFile.h"
39 #include "Core/ELF/ParamSFO.h"
40 #include "Core/HLE/sceKernelTime.h"
41 #include "StringUtils.h"
42 #endif
43 #include "Core/HLE/__sceAudio.h"
44 #include "Core/HLE/sceAudio.h"
45 #include "Core/HLE/sceKernel.h"
46 #include "Core/HLE/sceKernelThread.h"
47 #include "Core/HW/StereoResampler.h"
48 #include "Core/Util/AudioFormat.h"
49
50 StereoResampler resampler;
51
52 // Should be used to lock anything related to the outAudioQueue.
53 // atomic locks are used on the lock. TODO: make this lock-free
54 std::atomic_flag atomicLock_;
55
56 // We copy samples as they are written into this simple ring buffer.
57 // Might try something more efficient later.
58 FixedSizeQueue<s16, 32768 * 8> chanSampleQueues[PSP_AUDIO_CHANNEL_MAX + 1];
59
60 int eventAudioUpdate = -1;
61 int eventHostAudioUpdate = -1;
62 int mixFrequency = 44100;
63 int srcFrequency = 0;
64
65 const int hwSampleRate = 44100;
66
67 int hwBlockSize = 64;
68 int hostAttemptBlockSize = 512;
69
70 static int audioIntervalCycles;
71 static int audioHostIntervalCycles;
72
73 static s32 *mixBuffer;
74 static s16 *clampedMixBuffer;
75 #ifndef MOBILE_DEVICE
76 WaveFileWriter g_wave_writer;
77 static bool m_logAudio;
78 #endif
79
80 // High and low watermarks, basically. For perfect emulation, the correct values are 0 and 1, respectively.
81 // TODO: Tweak. Hm, there aren't actually even used currently...
82 static int chanQueueMaxSizeFactor;
83 static int chanQueueMinSizeFactor;
84
hleAudioUpdate(u64 userdata,int cyclesLate)85 static void hleAudioUpdate(u64 userdata, int cyclesLate) {
86 // Schedule the next cycle first. __AudioUpdate() may consume cycles.
87 CoreTiming::ScheduleEvent(audioIntervalCycles - cyclesLate, eventAudioUpdate, 0);
88
89 __AudioUpdate();
90 }
91
hleHostAudioUpdate(u64 userdata,int cyclesLate)92 static void hleHostAudioUpdate(u64 userdata, int cyclesLate) {
93 CoreTiming::ScheduleEvent(audioHostIntervalCycles - cyclesLate, eventHostAudioUpdate, 0);
94
95 // Not all hosts need this call to poke their audio system once in a while, but those that don't
96 // can just ignore it.
97 host->UpdateSound();
98 }
99
__AudioCPUMHzChange()100 static void __AudioCPUMHzChange() {
101 audioIntervalCycles = (int)(usToCycles(1000000ULL) * hwBlockSize / hwSampleRate);
102 audioHostIntervalCycles = (int)(usToCycles(1000000ULL) * hostAttemptBlockSize / hwSampleRate);
103 }
104
105
__AudioInit()106 void __AudioInit() {
107 resampler.ResetStatCounters();
108 mixFrequency = 44100;
109 srcFrequency = 0;
110
111 chanQueueMaxSizeFactor = 2;
112 chanQueueMinSizeFactor = 1;
113 hwBlockSize = 64;
114 hostAttemptBlockSize = 512;
115
116 __AudioCPUMHzChange();
117
118 eventAudioUpdate = CoreTiming::RegisterEvent("AudioUpdate", &hleAudioUpdate);
119 eventHostAudioUpdate = CoreTiming::RegisterEvent("AudioUpdateHost", &hleHostAudioUpdate);
120
121 CoreTiming::ScheduleEvent(audioIntervalCycles, eventAudioUpdate, 0);
122 CoreTiming::ScheduleEvent(audioHostIntervalCycles, eventHostAudioUpdate, 0);
123 for (u32 i = 0; i < PSP_AUDIO_CHANNEL_MAX + 1; i++) {
124 chans[i].index = i;
125 chans[i].clear();
126 }
127
128 mixBuffer = new s32[hwBlockSize * 2];
129 clampedMixBuffer = new s16[hwBlockSize * 2];
130 memset(mixBuffer, 0, hwBlockSize * 2 * sizeof(s32));
131
132 resampler.Clear();
133 CoreTiming::RegisterMHzChangeCallback(&__AudioCPUMHzChange);
134 }
135
__AudioDoState(PointerWrap & p)136 void __AudioDoState(PointerWrap &p) {
137 auto s = p.Section("sceAudio", 1, 2);
138 if (!s)
139 return;
140
141 Do(p, eventAudioUpdate);
142 CoreTiming::RestoreRegisterEvent(eventAudioUpdate, "AudioUpdate", &hleAudioUpdate);
143 Do(p, eventHostAudioUpdate);
144 CoreTiming::RestoreRegisterEvent(eventHostAudioUpdate, "AudioUpdateHost", &hleHostAudioUpdate);
145
146 Do(p, mixFrequency);
147 if (s >= 2) {
148 Do(p, srcFrequency);
149 } else {
150 // Assume that it was actually the SRC channel frequency.
151 srcFrequency = mixFrequency;
152 mixFrequency = 44100;
153 }
154
155 // TODO: This never happens because maxVer=1.
156 if (s >= 2) {
157 resampler.DoState(p);
158 } else {
159 // Only to preserve the previous file format. Might cause a slight audio glitch on upgrades?
160 FixedSizeQueue<s16, 512 * 16> outAudioQueue;
161 outAudioQueue.DoState(p);
162
163 resampler.Clear();
164 }
165
166 int chanCount = ARRAY_SIZE(chans);
167 Do(p, chanCount);
168 if (chanCount != ARRAY_SIZE(chans))
169 {
170 ERROR_LOG(SCEAUDIO, "Savestate failure: different number of audio channels.");
171 p.SetError(p.ERROR_FAILURE);
172 return;
173 }
174 for (int i = 0; i < chanCount; ++i) {
175 chans[i].index = i;
176 chans[i].DoState(p);
177 }
178
179 __AudioCPUMHzChange();
180 }
181
__AudioShutdown()182 void __AudioShutdown() {
183 delete [] mixBuffer;
184 delete [] clampedMixBuffer;
185
186 mixBuffer = 0;
187 for (u32 i = 0; i < PSP_AUDIO_CHANNEL_MAX + 1; i++) {
188 chans[i].index = i;
189 chans[i].clear();
190 }
191
192 #ifndef MOBILE_DEVICE
193 if (g_Config.bDumpAudio) {
194 __StopLogAudio();
195 }
196 #endif
197 }
198
__AudioEnqueue(AudioChannel & chan,int chanNum,bool blocking)199 u32 __AudioEnqueue(AudioChannel &chan, int chanNum, bool blocking) {
200 u32 ret = chan.sampleCount;
201
202 if (chan.sampleAddress == 0) {
203 // For some reason, multichannel audio lies and returns the sample count here.
204 if (chanNum == PSP_AUDIO_CHANNEL_SRC || chanNum == PSP_AUDIO_CHANNEL_OUTPUT2) {
205 ret = 0;
206 }
207 }
208
209 // If there's anything on the queue at all, it should be busy, but we try to be a bit lax.
210 //if (chanSampleQueues[chanNum].size() > chan.sampleCount * 2 * chanQueueMaxSizeFactor || chan.sampleAddress == 0) {
211 if (chanSampleQueues[chanNum].size() > 0) {
212 if (blocking) {
213 // TODO: Regular multichannel audio seems to block for 64 samples less? Or enqueue the first 64 sync?
214 int blockSamples = (int)chanSampleQueues[chanNum].size() / 2 / chanQueueMinSizeFactor;
215
216 if (__KernelIsDispatchEnabled()) {
217 AudioChannelWaitInfo waitInfo = {__KernelGetCurThread(), blockSamples};
218 chan.waitingThreads.push_back(waitInfo);
219 // Also remember the value to return in the waitValue.
220 __KernelWaitCurThread(WAITTYPE_AUDIOCHANNEL, (SceUID)chanNum + 1, ret, 0, false, "blocking audio");
221 } else {
222 // TODO: Maybe we shouldn't take this audio after all?
223 ret = SCE_KERNEL_ERROR_CAN_NOT_WAIT;
224 }
225
226 // Fall through to the sample queueing, don't want to lose the samples even though
227 // we're getting full. The PSP would enqueue after blocking.
228 } else {
229 // Non-blocking doesn't even enqueue, but it's not commonly used.
230 return SCE_ERROR_AUDIO_CHANNEL_BUSY;
231 }
232 }
233
234 if (chan.sampleAddress == 0) {
235 return ret;
236 }
237
238 int leftVol = chan.leftVolume;
239 int rightVol = chan.rightVolume;
240
241 if (leftVol == (1 << 15) && rightVol == (1 << 15) && chan.format == PSP_AUDIO_FORMAT_STEREO && IS_LITTLE_ENDIAN) {
242 // TODO: Add mono->stereo conversion to this path.
243
244 // Good news: the volume doesn't affect the values at all.
245 // We can just do a direct memory copy.
246 const u32 totalSamples = chan.sampleCount * (chan.format == PSP_AUDIO_FORMAT_STEREO ? 2 : 1);
247 s16 *buf1 = 0, *buf2 = 0;
248 size_t sz1, sz2;
249 chanSampleQueues[chanNum].pushPointers(totalSamples, &buf1, &sz1, &buf2, &sz2);
250
251 if (Memory::IsValidAddress(chan.sampleAddress + (totalSamples - 1) * sizeof(s16_le))) {
252 Memory::Memcpy(buf1, chan.sampleAddress, (u32)sz1 * sizeof(s16));
253 if (buf2)
254 Memory::Memcpy(buf2, chan.sampleAddress + (u32)sz1 * sizeof(s16), (u32)sz2 * sizeof(s16));
255 }
256 } else {
257 // Remember that maximum volume allowed is 0xFFFFF so left shift is no issue.
258 // This way we can optimally shift by 16.
259 leftVol <<=1;
260 rightVol <<=1;
261
262 if (chan.format == PSP_AUDIO_FORMAT_STEREO) {
263 const u32 totalSamples = chan.sampleCount * 2;
264
265 s16_le *sampleData = (s16_le *) Memory::GetPointer(chan.sampleAddress);
266
267 // Walking a pointer for speed. But let's make sure we wouldn't trip on an invalid ptr.
268 if (Memory::IsValidAddress(chan.sampleAddress + (totalSamples - 1) * sizeof(s16_le))) {
269 s16 *buf1 = 0, *buf2 = 0;
270 size_t sz1, sz2;
271 chanSampleQueues[chanNum].pushPointers(totalSamples, &buf1, &sz1, &buf2, &sz2);
272 AdjustVolumeBlock(buf1, sampleData, sz1, leftVol, rightVol);
273 if (buf2) {
274 AdjustVolumeBlock(buf2, sampleData + sz1, sz2, leftVol, rightVol);
275 }
276 }
277 } else if (chan.format == PSP_AUDIO_FORMAT_MONO) {
278 // Rare, so unoptimized. Expands to stereo.
279 for (u32 i = 0; i < chan.sampleCount; i++) {
280 s16 sample = (s16)Memory::Read_U16(chan.sampleAddress + 2 * i);
281 chanSampleQueues[chanNum].push(ApplySampleVolume(sample, leftVol));
282 chanSampleQueues[chanNum].push(ApplySampleVolume(sample, rightVol));
283 }
284 }
285 }
286 return ret;
287 }
288
__AudioWakeThreads(AudioChannel & chan,int result,int step)289 inline void __AudioWakeThreads(AudioChannel &chan, int result, int step) {
290 u32 error;
291 bool wokeThreads = false;
292 for (size_t w = 0; w < chan.waitingThreads.size(); ++w) {
293 AudioChannelWaitInfo &waitInfo = chan.waitingThreads[w];
294 waitInfo.numSamples -= step;
295
296 // If it's done (there will still be samples on queue) and actually still waiting, wake it up.
297 u32 waitID = __KernelGetWaitID(waitInfo.threadID, WAITTYPE_AUDIOCHANNEL, error);
298 if (waitInfo.numSamples <= 0 && waitID != 0) {
299 // DEBUG_LOG(SCEAUDIO, "Woke thread %i for some buffer filling", waitingThread);
300 u32 ret = result == 0 ? __KernelGetWaitValue(waitInfo.threadID, error) : SCE_ERROR_AUDIO_CHANNEL_NOT_RESERVED;
301 __KernelResumeThreadFromWait(waitInfo.threadID, ret);
302 wokeThreads = true;
303
304 chan.waitingThreads.erase(chan.waitingThreads.begin() + w--);
305 }
306 // This means the thread stopped waiting, so stop trying to wake it.
307 else if (waitID == 0)
308 chan.waitingThreads.erase(chan.waitingThreads.begin() + w--);
309 }
310
311 if (wokeThreads) {
312 __KernelReSchedule("audio drain");
313 }
314 }
315
__AudioWakeThreads(AudioChannel & chan,int result)316 void __AudioWakeThreads(AudioChannel &chan, int result) {
317 __AudioWakeThreads(chan, result, 0x7FFFFFFF);
318 }
319
__AudioSetOutputFrequency(int freq)320 void __AudioSetOutputFrequency(int freq) {
321 if (freq != 44100) {
322 WARN_LOG_REPORT(SCEAUDIO, "Switching audio frequency to %i", freq);
323 } else {
324 DEBUG_LOG(SCEAUDIO, "Switching audio frequency to %i", freq);
325 }
326 mixFrequency = freq;
327 }
328
__AudioSetSRCFrequency(int freq)329 void __AudioSetSRCFrequency(int freq) {
330 srcFrequency = freq;
331 }
332
333 // Mix samples from the various audio channels into a single sample queue.
334 // This single sample queue is where __AudioMix should read from. If the sample queue is full, we should
335 // just sleep the main emulator thread a little.
__AudioUpdate(bool resetRecording)336 void __AudioUpdate(bool resetRecording) {
337 // Audio throttle doesn't really work on the PSP since the mixing intervals are so closely tied
338 // to the CPU. Much better to throttle the frame rate on frame display and just throw away audio
339 // if the buffer somehow gets full.
340 bool firstChannel = true;
341 std::vector<int16_t> srcBuffer;
342
343 for (u32 i = 0; i < PSP_AUDIO_CHANNEL_MAX + 1; i++) {
344 if (!chans[i].reserved)
345 continue;
346
347 __AudioWakeThreads(chans[i], 0, hwBlockSize);
348
349 if (!chanSampleQueues[i].size()) {
350 continue;
351 }
352
353 bool needsResample = i == PSP_AUDIO_CHANNEL_SRC && srcFrequency != 0 && srcFrequency != mixFrequency;
354 size_t sz = needsResample ? (hwBlockSize * 2 * srcFrequency) / mixFrequency : hwBlockSize * 2;
355 if (sz > chanSampleQueues[i].size()) {
356 ERROR_LOG(SCEAUDIO, "Channel %i buffer underrun at %i of %i", i, (int)chanSampleQueues[i].size() / 2, (int)sz / 2);
357 }
358
359 const s16 *buf1 = 0, *buf2 = 0;
360 size_t sz1, sz2;
361
362 chanSampleQueues[i].popPointers(sz, &buf1, &sz1, &buf2, &sz2);
363
364 if (needsResample) {
365 auto read = [&](size_t i) {
366 if (i < sz1)
367 return buf1[i];
368 if (i < sz1 + sz2)
369 return buf2[i - sz1];
370 if (buf2)
371 return buf2[sz2 - 1];
372 return buf1[sz1 - 1];
373 };
374
375 srcBuffer.resize(hwBlockSize * 2);
376
377 // TODO: This is terrible, since it's doing it by small chunk and discarding frac.
378 const uint32_t ratio = (uint32_t)(65536.0 * srcFrequency / (double)mixFrequency);
379 uint32_t frac = 0;
380 size_t readIndex = 0;
381 for (size_t outIndex = 0; readIndex < sz && outIndex < srcBuffer.size(); outIndex += 2) {
382 size_t readIndex2 = readIndex + 2;
383 int16_t l1 = read(readIndex);
384 int16_t r1 = read(readIndex + 1);
385 int16_t l2 = read(readIndex2);
386 int16_t r2 = read(readIndex2 + 1);
387 int sampleL = ((l1 << 16) + (l2 - l1) * (uint16_t)frac) >> 16;
388 int sampleR = ((r1 << 16) + (r2 - r1) * (uint16_t)frac) >> 16;
389 srcBuffer[outIndex] = sampleL;
390 srcBuffer[outIndex + 1] = sampleR;
391 frac += ratio;
392 readIndex += 2 * (uint16_t)(frac >> 16);
393 frac &= 0xffff;
394 }
395
396 buf1 = srcBuffer.data();
397 sz1 = srcBuffer.size();
398 buf2 = nullptr;
399 sz2 = 0;
400 }
401
402 if (firstChannel) {
403 for (size_t s = 0; s < sz1; s++)
404 mixBuffer[s] = buf1[s];
405 if (buf2) {
406 for (size_t s = 0; s < sz2; s++)
407 mixBuffer[s + sz1] = buf2[s];
408 }
409 firstChannel = false;
410 } else {
411 // Surprisingly hard to SIMD efficiently on SSE2 due to lack of 16-to-32-bit sign extension. NEON should be straight-forward though, and SSE4.1 can do it nicely.
412 // Actually, the cmple/pack trick should work fine...
413 for (size_t s = 0; s < sz1; s++)
414 mixBuffer[s] += buf1[s];
415 if (buf2) {
416 for (size_t s = 0; s < sz2; s++)
417 mixBuffer[s + sz1] += buf2[s];
418 }
419 }
420 }
421
422 if (firstChannel) {
423 // Nothing was written above, let's memset.
424 memset(mixBuffer, 0, hwBlockSize * 2 * sizeof(s32));
425 }
426
427 if (g_Config.bEnableSound) {
428 resampler.PushSamples(mixBuffer, hwBlockSize);
429 #ifndef MOBILE_DEVICE
430 if (g_Config.bSaveLoadResetsAVdumping && resetRecording) {
431 __StopLogAudio();
432 std::string discID = g_paramSFO.GetDiscID();
433 Path audio_file_name = GetSysDirectory(DIRECTORY_AUDIO) / StringFromFormat("%s_%s.wav", discID.c_str(), KernelTimeNowFormatted().c_str()).c_str();
434 INFO_LOG(COMMON, "Restarted audio recording to: %s", audio_file_name.c_str());
435 if (!File::Exists(GetSysDirectory(DIRECTORY_AUDIO)))
436 File::CreateDir(GetSysDirectory(DIRECTORY_AUDIO));
437 File::CreateEmptyFile(audio_file_name);
438 __StartLogAudio(audio_file_name);
439 }
440 if (!m_logAudio) {
441 if (g_Config.bDumpAudio) {
442 // Use gameID_EmulatedTimestamp for filename
443 std::string discID = g_paramSFO.GetDiscID();
444 Path audio_file_name = GetSysDirectory(DIRECTORY_AUDIO) / StringFromFormat("%s_%s.wav", discID.c_str(), KernelTimeNowFormatted().c_str());
445 INFO_LOG(COMMON,"Recording audio to: %s", audio_file_name.c_str());
446 // Create the path just in case it doesn't exist
447 if (!File::Exists(GetSysDirectory(DIRECTORY_AUDIO)))
448 File::CreateDir(GetSysDirectory(DIRECTORY_AUDIO));
449 File::CreateEmptyFile(audio_file_name);
450 __StartLogAudio(audio_file_name);
451 }
452 } else {
453 if (g_Config.bDumpAudio) {
454 for (int i = 0; i < hwBlockSize * 2; i++) {
455 clampedMixBuffer[i] = clamp_s16(mixBuffer[i]);
456 }
457 g_wave_writer.AddStereoSamples(clampedMixBuffer, hwBlockSize);
458 } else {
459 __StopLogAudio();
460 }
461 }
462 #endif
463 }
464 }
465
466 // numFrames is number of stereo frames.
467 // This is called from *outside* the emulator thread.
__AudioMix(short * outstereo,int numFrames,int sampleRate)468 int __AudioMix(short *outstereo, int numFrames, int sampleRate) {
469 return resampler.Mix(outstereo, numFrames, false, sampleRate);
470 }
471
__AudioGetDebugStats(char * buf,size_t bufSize)472 void __AudioGetDebugStats(char *buf, size_t bufSize) {
473 resampler.GetAudioDebugStats(buf, bufSize);
474 }
475
__PushExternalAudio(const s32 * audio,int numSamples)476 void __PushExternalAudio(const s32 *audio, int numSamples) {
477 if (audio) {
478 resampler.PushSamples(audio, numSamples);
479 } else {
480 resampler.Clear();
481 }
482 }
483 #ifndef MOBILE_DEVICE
__StartLogAudio(const Path & filename)484 void __StartLogAudio(const Path& filename) {
485 if (!m_logAudio) {
486 m_logAudio = true;
487 g_wave_writer.Start(filename, 44100);
488 g_wave_writer.SetSkipSilence(false);
489 NOTICE_LOG(SCEAUDIO, "Starting Audio logging");
490 } else {
491 WARN_LOG(SCEAUDIO, "Audio logging has already been started");
492 }
493 }
494
__StopLogAudio()495 void __StopLogAudio() {
496 if (m_logAudio) {
497 m_logAudio = false;
498 g_wave_writer.Stop();
499 NOTICE_LOG(SCEAUDIO, "Stopping Audio logging");
500 } else {
501 WARN_LOG(SCEAUDIO, "Audio logging has already been stopped");
502 }
503 }
504 #endif
505
Reset()506 void WAVDump::Reset() {
507 __AudioUpdate(true);
508 }
509