-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.lua
50 lines (46 loc) · 1.65 KB
/
read.lua
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
-- Copyright 2019, Mansour Moufid <[email protected]>
local love = require('love')
love.audio = require('love.audio') -- RecordingDevice
love.sound = require('love.sound') -- SoundData
love.thread = require('love.thread')
love.timer = require('love.timer')
local chirpy = require('chirpy')
local nu = require('libnu')
local state = chirpy.getstate(...)
love.thread.getChannel('read.tock'):push(true)
while true do
local tick = love.thread.getChannel('read.tick'):demand()
if tick == false then
break
end
local n = 0
while n < state.mic.nsamples do
local j = love.thread.getChannel('j'):pop()
if j ~= nil then
n = n - j
end
local m = state.mic.device:getSampleCount()
if m > math.min(state.mic.nsamples - n, 2 ^ 10) then
local data = state.mic.device:getData()
if data ~= nil then
m = data:getSampleCount()
for k = 0, m - 1 do
local sample = data:getSample(k)
-- assert(sample >= -1 and sample <= 1, sample)
local i = n + k
if i >= 0 and i < state.mic.nsamples then
state.samples[i] = sample
end
end
local mean, _ = nu.sum.meanvar(state.samples + n, m)
for i = n, n + m - 1 do
state.samples[i] = state.samples[i] - mean
end
love.thread.getChannel('read.stat'):push({n, n + m})
n = n + m
end
end
love.timer.sleep(1e-3)
end
love.thread.getChannel('read.tock'):push(true)
end