Repository navigation
Expand file tree
/
Copy pathdsp.cpp
More file actions
126 lines (121 loc) · 4.85 KB
/
Copy pathdsp.cpp
File metadata and controls
126 lines (121 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "dsp_abi.h"
#include <XDSP.h>
#include <array>
#include <atomic>
#include <cstring>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <unordered_map>
namespace rage::dsp {
constexpr size_t frames = 1024;
constexpr size_t log_frames = 10;
using Vectors = std::array<DirectX::XMVECTOR,frames/4>;
struct Client { UINT limit{}, active{}; };
struct Stream {
Client* owner{};
Status status{};
std::mutex commands;
Vectors real{}, imaginary{}, sorted_real{}, sorted_imaginary{};
};
namespace {
std::shared_mutex lifetime;
std::unordered_map<Client*,std::unique_ptr<Client>> clients;
std::unordered_map<Stream*,std::unique_ptr<Stream>> streams;
const auto& roots() {
static const auto table = [] {
std::array<DirectX::XMVECTOR,frames> values{};
XDSP::FFTInitializeUnityTable(values.data(),frames);
return values;
}();
return table;
}
void forward_fft(Stream& stream, const Command& cmd) {
using namespace DirectX;
// Stage the complete input before writing. Some callers reuse their input memory.
const float step = (cmd.end_gain - cmd.begin_gain) / frames;
for (size_t group=0; group<stream.real.size(); ++group) {
const size_t i = group*4;
const float gain = cmd.begin_gain + step*static_cast<float>(i);
const XMVECTOR samples = XMVectorSet(cmd.input[i],cmd.input[i+1],cmd.input[i+2],cmd.input[i+3]);
stream.real[group] = XMVectorMultiply(samples,XMVectorSet(gain,gain+step,gain+2*step,gain+3*step));
stream.imaginary[group] = XMVectorZero();
}
XDSP::FFT(stream.real.data(),stream.imaginary.data(),roots().data(),frames);
XDSP::FFTUnswizzle(stream.sorted_real.data(),stream.real.data(),log_frames);
XDSP::FFTUnswizzle(stream.sorted_imaginary.data(),stream.imaginary.data(),log_frames);
// The title copies exactly 4096 bytes per block. Emit 512 Cartesian bins.
for (size_t group=0; group<frames/8; ++group) {
XMFLOAT4A re, im;
XMStoreFloat4A(&re,stream.sorted_real[group]);
XMStoreFloat4A(&im,stream.sorted_imaginary[group]);
const float pairs[]{re.x,im.x,re.y,im.y,re.z,im.z,re.w,im.w};
std::memcpy(cmd.output+group*8,pairs,sizeof(pairs));
}
}
}
}
using namespace rage::dsp;
HRESULT WINAPI XDspConnectWithMaximumStreamLimit(void* base, UINT bytes, UINT seconds, UINT limit,
UINT channels, Client** out) {
if (!out) return E_POINTER;
*out = nullptr;
if (!base || bytes < 65536 || bytes > 0x1f400000 || seconds > 128 ||
!limit || limit > 256 || !channels || channels > 2) return E_INVALIDARG;
try {
auto client = std::make_unique<Client>();
client->limit = limit;
auto* key = client.get();
std::unique_lock lock(lifetime);
clients.emplace(key,std::move(client));
*out = key;
return S_OK;
} catch (const std::bad_alloc&) { return E_OUTOFMEMORY; }
}
HRESULT WINAPI XDspDisconnect(Client* client) {
std::unique_lock lock(lifetime);
if (!clients.contains(client)) return E_INVALIDARG;
if (client->active) return streams_active;
clients.erase(client);
return S_OK;
}
HRESULT WINAPI XDspActivate(Client* client, const Description* desc, const Status** status, Stream** out) {
if (status) *status = nullptr;
if (out) *out = nullptr;
if (!out || !status) return E_POINTER;
std::unique_lock lock(lifetime);
if (!clients.contains(client) || !desc) return E_INVALIDARG;
if (desc->kind != 2 || desc->options || desc->channels != 1 || desc->frames != frames) return E_NOTIMPL;
if (desc->ir_floats || desc->impulse) return E_INVALIDARG;
if (client->active >= client->limit) return no_resources;
try {
auto stream = std::make_unique<Stream>();
stream->owner = client;
Stream* key = stream.get();
roots();
streams.emplace(key,std::move(stream));
++client->active;
*status = &key->status;
*out = key;
return S_OK;
} catch (const std::bad_alloc&) { return E_OUTOFMEMORY; }
}
HRESULT WINAPI XDspDeactivate(Stream* stream) {
std::unique_lock lock(lifetime);
if (!streams.contains(stream)) return E_INVALIDARG;
--stream->owner->active;
streams.erase(stream);
return S_OK;
}
HRESULT WINAPI XDspSubmitCommand(Stream* stream, const Command* cmd, UINT* ticket) {
std::shared_lock alive(lifetime);
if (!streams.contains(stream) || !cmd || !cmd->input || !cmd->output) return E_INVALIDARG;
if (cmd->reserved) return E_NOTIMPL;
std::lock_guard lock(stream->commands);
const UINT seq = std::atomic_ref(stream->status.submitted).fetch_add(1)+1;
forward_fft(*stream,*cmd);
std::atomic_ref(stream->status.result).store(S_OK,std::memory_order_relaxed);
std::atomic_ref(stream->status.completed).store(seq,std::memory_order_release);
if (ticket) *ticket = seq;
return S_OK;
}