-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.lua
83 lines (77 loc) · 2.6 KB
/
load.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
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
-- Copyright 2019, Mansour Moufid <[email protected]>
local love = require('love')
love.audio = require('love.audio') -- RecordingDevice
love.thread = require('love.thread')
local array = require('array')
local chirp = require('chirp')
local chirpy = require('chirpy')
local math = require('math')
local nu = require('libnu')
local str = require('str')
nu.array.gc = function (x) return x end
local function getMic(mics, samplerates, formats, samplecounts)
local channels = 1
for _, dev in ipairs(mics) do
for _, samplerate in ipairs(samplerates) do
for _, format in ipairs(formats) do
for _, samplecount in ipairs(samplecounts) do
local ok = dev:start(
samplecount,
samplerate,
format,
channels
)
if ok then
return dev, samplerate, format, samplecount
end
end
end
end
end
return nil
end
local state = chirpy.getstate(...)
state.mics = love.audio.getRecordingDevices()
while state.mic.device == nil do
local device, samplerate, format, samplecount = getMic(
state.mics,
state.mic.samplerates,
state.mic.formats,
state.mic.samplecounts
)
state.mic.device = device
state.mic.samplerate = samplerate
state.mic.format = format
state.mic.samplecount = samplecount
end
love.thread.getChannel('log'):push({
tostring(state.mic.device),
' ' .. state.mic.samplerate .. ' Hz',
' ' .. state.mic.format .. ' bits',
' ' .. state.mic.samplecount .. ' samples',
})
state.chirp.size = math.floor(state.chirp.duration * state.mic.samplerate)
state.mic.nsamples = math.floor(1.0 * state.mic.samplerate)
state.xcor.size = state.mic.nsamples + state.chirp.size
state.xcor.size = math.ceil(state.xcor.size / (2 * 3 * 5)) * (2 * 3 * 5)
for x, t in pairs(state.pointers) do
local n = state.xcor.size
while state[x] == nil do
state[x] = nu.array.new(t, n)
if state[x] ~= nil then
nu.array.zero(state[x], n)
end
end
end
local band = state.chirp.band
local duration = state.chirp.duration
local size = state.chirp.size
chirp.linear(array.reverse(band), duration, state.chirp0, size)
chirp.linear(band, duration, state.chirp1, size)
love.thread.getChannel('log'):push({
'chirp:',
' ' .. str.join('-', band) .. ' Hz ',
' ' .. duration .. ' s',
' ' .. size .. ' samples',
})
love.thread.getChannel('load'):push(chirpy.sendstate(state))