Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/bench/tests/pcmmix.lua
2725 views
1
local function prequire(name) local success, result = pcall(require, name); return success and result end
2
local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../bench_support")
3
4
local samples = 100_000
5
6
-- create two 16-bit stereo pcm audio buffers
7
local ch1 = buffer.create(samples * 2 * 2)
8
local ch2 = buffer.create(samples * 2 * 2)
9
10
-- just init with random data
11
for i = 0, samples * 2 - 1 do
12
buffer.writei16(ch1, i * 2, math.random(-32768, 32767))
13
buffer.writei16(ch2, i * 2, math.random(-32768, 32767))
14
end
15
16
function test()
17
local mix = buffer.create(samples * 2 * 2)
18
19
for i = 0, samples - 1 do
20
local s1l = buffer.readi16(ch1, i * 4)
21
local s1r = buffer.readi16(ch1, i * 4 + 2)
22
23
local s2l = buffer.readi16(ch2, i * 4)
24
local s2r = buffer.readi16(ch2, i * 4 + 2)
25
26
local combinedl = s1l + s2l - s1l * s2l / 32768
27
local combinedr = s1r + s2r - s1r * s2r / 32768
28
29
buffer.writei16(mix, i * 4, combinedl)
30
buffer.writei16(mix, i * 4 + 2, combinedr)
31
end
32
end
33
34
bench.runCode(test, "pcmmix")
35
36