-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech.lua
77 lines (63 loc) · 2.3 KB
/
speech.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
local speech = {}
local saveDir = lovr.filesystem.getSaveDirectory()
function speech:init()
local pbmmExists, scorerExists = lovr.filesystem.isFile('model.pbmm'), lovr.filesystem.isFile('model.scorer')
if not (pbmmExists and scorerExists) then
lovr.filesystem.write('model.pbmm', lovr.filesystem.read('/data/deepspeech-0.9.3-models.pbmm'))
lovr.filesystem.write('model.scorer', lovr.filesystem.read('/data/deepspeech-0.9.3-models.scorer'))
end
self.chunkSize = 1024
self.microphone = lovr.audio.newMicrophone(nil, self.chunkSize * 2, 16000, 16, 1)
self.microphone:startRecording()
self.speechChannel = lovr.thread.getChannel('speech')
self.speechChannel:push(saveDir)
self.speechChannel:push(self.chunkSize)
self.speechChannel:push(self.microphone)
self.speechThread = lovr.thread.newThread([[
local speech = require 'lua-deepspeech'
local lovr = {
thread = require 'lovr.thread',
audio = require 'lovr.audio',
data = require 'lovr.data',
timer = require 'lovr.timer'
}
local channel = lovr.thread.getChannel('speech')
local saveDir = channel:pop()
local chunkSize = channel:pop()
local microphone = channel:pop()
local captions = ''
local prevTime = 0
speech.init({
model = saveDir .. '/model.pbmm',
scorer = saveDir .. '/model.scorer'
})
sampleRate = speech.getSampleRate()
assert(sampleRate == 16000, string.format('Unsupported sample rate %d', sampleRate))
local stream = speech.newStream()
print('~~ mic: '..microphone:getName())
while true do
local time = lovr.timer.getTime()
if time - prevTime > 1.5 then
prevTime = time
captions = stream:decode()
stream:clear()
channel:push(captions)
end
if microphone:getSampleCount() > chunkSize then
local soundData = microphone:getData()
stream:feed(soundData:getBlob():getPointer(), soundData:getSampleCount())
end
end
]])
self.speechThread:start()
end
function speech:update(dt)
self:updateCaptions()
end
function speech:updateCaptions()
local message, present = self.speechChannel:peek()
if present and type(message) == "string" then
addCaption(self.speechChannel:pop())
end
end
return speech