2016-02-21 05:13:52 -08:00
|
|
|
// Copyright 2016 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2016-04-25 00:54:57 -07:00
|
|
|
#include <array>
|
2016-04-27 05:53:23 -07:00
|
|
|
#include <memory>
|
2016-09-20 23:52:38 -07:00
|
|
|
#include "audio_core/hle/dsp.h"
|
2016-04-26 23:22:39 -07:00
|
|
|
#include "audio_core/hle/mixers.h"
|
2016-02-21 05:13:52 -08:00
|
|
|
#include "audio_core/hle/pipe.h"
|
2016-04-25 00:54:57 -07:00
|
|
|
#include "audio_core/hle/source.h"
|
2016-04-27 05:53:23 -07:00
|
|
|
#include "audio_core/sink.h"
|
2016-05-14 19:04:03 -07:00
|
|
|
#include "audio_core/time_stretch.h"
|
2016-02-21 05:13:52 -08:00
|
|
|
|
|
|
|
namespace DSP {
|
|
|
|
namespace HLE {
|
|
|
|
|
2016-04-26 23:22:39 -07:00
|
|
|
// Region management
|
|
|
|
|
2016-04-25 02:01:37 -07:00
|
|
|
std::array<SharedMemory, 2> g_regions;
|
|
|
|
|
|
|
|
static size_t CurrentRegionIndex() {
|
|
|
|
// The region with the higher frame counter is chosen unless there is wraparound.
|
|
|
|
// This function only returns a 0 or 1.
|
|
|
|
|
|
|
|
if (g_regions[0].frame_counter == 0xFFFFu && g_regions[1].frame_counter != 0xFFFEu) {
|
2016-10-20 07:26:59 -07:00
|
|
|
// Wraparound has occurred.
|
2016-04-25 02:01:37 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_regions[1].frame_counter == 0xFFFFu && g_regions[0].frame_counter != 0xFFFEu) {
|
2016-10-20 07:26:59 -07:00
|
|
|
// Wraparound has occurred.
|
2016-04-25 02:01:37 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (g_regions[0].frame_counter > g_regions[1].frame_counter) ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SharedMemory& ReadRegion() {
|
|
|
|
return g_regions[CurrentRegionIndex()];
|
|
|
|
}
|
|
|
|
|
|
|
|
static SharedMemory& WriteRegion() {
|
|
|
|
return g_regions[1 - CurrentRegionIndex()];
|
|
|
|
}
|
2016-02-21 05:13:52 -08:00
|
|
|
|
2016-04-26 23:22:39 -07:00
|
|
|
// Audio processing and mixing
|
|
|
|
|
2016-04-25 00:54:57 -07:00
|
|
|
static std::array<Source, num_sources> sources = {
|
2016-09-17 17:38:01 -07:00
|
|
|
Source(0), Source(1), Source(2), Source(3), Source(4), Source(5), Source(6), Source(7),
|
|
|
|
Source(8), Source(9), Source(10), Source(11), Source(12), Source(13), Source(14), Source(15),
|
2016-09-18 18:01:46 -07:00
|
|
|
Source(16), Source(17), Source(18), Source(19), Source(20), Source(21), Source(22), Source(23),
|
|
|
|
};
|
2016-04-26 23:22:39 -07:00
|
|
|
static Mixers mixers;
|
|
|
|
|
|
|
|
static StereoFrame16 GenerateCurrentFrame() {
|
|
|
|
SharedMemory& read = ReadRegion();
|
|
|
|
SharedMemory& write = WriteRegion();
|
|
|
|
|
|
|
|
std::array<QuadFrame32, 3> intermediate_mixes = {};
|
|
|
|
|
|
|
|
// Generate intermediate mixes
|
|
|
|
for (size_t i = 0; i < num_sources; i++) {
|
2016-09-17 17:38:01 -07:00
|
|
|
write.source_statuses.status[i] =
|
|
|
|
sources[i].Tick(read.source_configurations.config[i], read.adpcm_coefficients.coeff[i]);
|
2016-04-26 23:22:39 -07:00
|
|
|
for (size_t mix = 0; mix < 3; mix++) {
|
|
|
|
sources[i].MixInto(intermediate_mixes[mix], mix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate final mix
|
2016-09-17 17:38:01 -07:00
|
|
|
write.dsp_status = mixers.Tick(read.dsp_configuration, read.intermediate_mix_samples,
|
|
|
|
write.intermediate_mix_samples, intermediate_mixes);
|
2016-04-26 23:22:39 -07:00
|
|
|
|
|
|
|
StereoFrame16 output_frame = mixers.GetOutput();
|
|
|
|
|
|
|
|
// Write current output frame to the shared memory region
|
|
|
|
for (size_t samplei = 0; samplei < output_frame.size(); samplei++) {
|
|
|
|
for (size_t channeli = 0; channeli < output_frame[0].size(); channeli++) {
|
|
|
|
write.final_samples.pcm16[samplei][channeli] = s16_le(output_frame[samplei][channeli]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Audio output
|
2016-04-25 00:54:57 -07:00
|
|
|
|
2016-08-31 08:56:30 -07:00
|
|
|
static bool perform_time_stretching = true;
|
2016-04-27 05:53:23 -07:00
|
|
|
static std::unique_ptr<AudioCore::Sink> sink;
|
2016-05-14 19:04:03 -07:00
|
|
|
static AudioCore::TimeStretcher time_stretcher;
|
2016-04-27 05:53:23 -07:00
|
|
|
|
2016-08-31 08:56:30 -07:00
|
|
|
static void FlushResidualStretcherAudio() {
|
|
|
|
time_stretcher.Flush();
|
|
|
|
while (true) {
|
|
|
|
std::vector<s16> residual_audio = time_stretcher.Process(sink->SamplesInQueue());
|
|
|
|
if (residual_audio.empty())
|
|
|
|
break;
|
|
|
|
sink->EnqueueSamples(residual_audio.data(), residual_audio.size() / 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-15 04:51:04 -07:00
|
|
|
static void OutputCurrentFrame(const StereoFrame16& frame) {
|
2016-08-31 08:56:30 -07:00
|
|
|
if (perform_time_stretching) {
|
|
|
|
time_stretcher.AddSamples(&frame[0][0], frame.size());
|
|
|
|
std::vector<s16> stretched_samples = time_stretcher.Process(sink->SamplesInQueue());
|
|
|
|
sink->EnqueueSamples(stretched_samples.data(), stretched_samples.size() / 2);
|
|
|
|
} else {
|
2016-09-07 07:26:38 -07:00
|
|
|
constexpr size_t maximum_sample_latency = 2048; // about 64 miliseconds
|
2016-08-31 08:56:30 -07:00
|
|
|
if (sink->SamplesInQueue() > maximum_sample_latency) {
|
|
|
|
// This can occur if we're running too fast and samples are starting to back up.
|
|
|
|
// Just drop the samples.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sink->EnqueueSamples(&frame[0][0], frame.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnableStretching(bool enable) {
|
|
|
|
if (perform_time_stretching == enable)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!enable) {
|
|
|
|
FlushResidualStretcherAudio();
|
|
|
|
}
|
|
|
|
perform_time_stretching = enable;
|
2016-05-15 04:51:04 -07:00
|
|
|
}
|
|
|
|
|
2016-04-26 23:22:39 -07:00
|
|
|
// Public Interface
|
|
|
|
|
2016-02-21 05:13:52 -08:00
|
|
|
void Init() {
|
|
|
|
DSP::HLE::ResetPipes();
|
2016-05-14 19:04:03 -07:00
|
|
|
|
2016-04-25 00:54:57 -07:00
|
|
|
for (auto& source : sources) {
|
|
|
|
source.Reset();
|
|
|
|
}
|
2016-05-14 19:04:03 -07:00
|
|
|
|
2016-04-26 23:22:39 -07:00
|
|
|
mixers.Reset();
|
|
|
|
|
2016-05-14 19:04:03 -07:00
|
|
|
time_stretcher.Reset();
|
|
|
|
if (sink) {
|
|
|
|
time_stretcher.SetOutputSampleRate(sink->GetNativeSampleRate());
|
|
|
|
}
|
2016-02-21 05:13:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown() {
|
2016-08-31 08:56:30 -07:00
|
|
|
if (perform_time_stretching) {
|
|
|
|
FlushResidualStretcherAudio();
|
2016-05-14 19:04:03 -07:00
|
|
|
}
|
2016-02-21 05:13:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Tick() {
|
2016-04-26 23:22:39 -07:00
|
|
|
StereoFrame16 current_frame = {};
|
2016-04-25 00:54:57 -07:00
|
|
|
|
2016-09-17 17:38:01 -07:00
|
|
|
// TODO: Check dsp::DSP semaphore (which indicates emulated application has finished writing to
|
|
|
|
// shared memory region)
|
2016-04-26 23:22:39 -07:00
|
|
|
current_frame = GenerateCurrentFrame();
|
2016-04-25 00:54:57 -07:00
|
|
|
|
2016-05-15 04:51:04 -07:00
|
|
|
OutputCurrentFrame(current_frame);
|
|
|
|
|
2016-02-21 05:13:52 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-27 05:53:23 -07:00
|
|
|
void SetSink(std::unique_ptr<AudioCore::Sink> sink_) {
|
|
|
|
sink = std::move(sink_);
|
2016-05-14 19:04:03 -07:00
|
|
|
time_stretcher.SetOutputSampleRate(sink->GetNativeSampleRate());
|
2016-04-27 05:53:23 -07:00
|
|
|
}
|
|
|
|
|
2016-02-21 05:13:52 -08:00
|
|
|
} // namespace HLE
|
|
|
|
} // namespace DSP
|