Skip to content

Commit 6993c66

Browse files
committed
Release 1.3-5
* Fix wavguess crash
1 parent 48a8c1a commit 6993c66

3 files changed

Lines changed: 113 additions & 12 deletions

File tree

.vscode/launch.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
"environment": [],
2525
"console": "integratedTerminal",
2626
"preLaunchTask": "CMake: build"
27+
},
28+
{
29+
"name": "(Windows) Launch LOVE",
30+
"type": "cppvsdbg",
31+
"request": "launch",
32+
"program": "lovec.exe",
33+
"args": ["."],
34+
"stopAtEntry": false,
35+
"cwd": "${workspaceFolder}/test/",
36+
"environment": [],
37+
"console": "integratedTerminal",
38+
"preLaunchTask": "CMake: build"
2739
}
2840
]
2941
}

rockspecs/lua-vosk-1.3-5.rockspec

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package = 'lua-vosk'
2+
version = '1.3-5'
3+
4+
source = {
5+
url = 'git+https://github.com/igor725/lua-vosk',
6+
tag = 'v1.3-5'
7+
}
8+
9+
description = {
10+
summary = 'Vosk binding for Lua.',
11+
detailed = [[
12+
Vosk is an offline open source speech recognition toolkit.
13+
It enables speech recognition for 20+ languages and dialects -
14+
English, Indian English, German, French, Spanish, Portuguese,
15+
Chinese, Russian, Turkish, Vietnamese, Italian, Dutch, Catalan,
16+
Arabic, Greek, Farsi, Filipino, Ukrainian, Kazakh, Swedish,
17+
Japanese, Esperanto, Hindi, Czech, Polish.
18+
19+
This library implements easy to use interface for Vosk API.
20+
]],
21+
homepage = 'https://github.com/igor725/lua-vosk',
22+
license = 'MIT'
23+
}
24+
25+
dependencies = {
26+
'lua >= 5.1'
27+
}
28+
29+
build = {
30+
type = 'builtin',
31+
modules = {
32+
luavosk = {
33+
sources = {
34+
'src/luamodel.c', 'src/luarecognizer.c',
35+
'src/luaspkmodel.c', 'src/main.c',
36+
'src/voskbridge.c'
37+
},
38+
defines = {
39+
'VOSK_ENABLE_JSON'
40+
}
41+
}
42+
}
43+
}

src/main.c

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,44 +30,90 @@ static int loglevel(lua_State *L) {
3030
return 0;
3131
}
3232

33+
static lua_Integer _readfile(lua_State *L, void *buff, lua_Integer size, int idx) {
34+
lua_getmetatable(L, idx);
35+
lua_getfield(L, -1, "read");
36+
lua_pushvalue(L, idx);
37+
lua_pushinteger(L, size);
38+
lua_call(L, 2, 2);
39+
if (lua_isnil(L, -2)) {
40+
lua_pop(L, 3);
41+
return 0;
42+
}
43+
44+
size_t eleng = 0;
45+
const char *read = luaL_checklstring(L, -2, &eleng);
46+
char *cbuff = (char *)buff;
47+
while(eleng--) *cbuff++ = *read++;
48+
lua_pop(L, 3);
49+
return (lua_Integer)(cbuff - (char *)buff);
50+
}
51+
52+
static lua_Integer _seekfile(lua_State *L, lua_Integer offset, int origin, int idx) {
53+
lua_getmetatable(L, idx);
54+
lua_getfield(L, -1, "seek");
55+
lua_pushvalue(L, idx);
56+
switch (origin) {
57+
case SEEK_CUR:
58+
lua_pushliteral(L, "cur");
59+
break;
60+
61+
case SEEK_END:
62+
lua_pushliteral(L, "end");
63+
break;
64+
65+
case SEEK_SET:
66+
lua_pushliteral(L, "set");
67+
break;
68+
}
69+
lua_pushinteger(L, offset);
70+
lua_call(L, 3, 2);
71+
if (lua_isnil(L, -2)) {
72+
lua_pop(L, 3);
73+
return 1;
74+
}
75+
76+
lua_pop(L, 3);
77+
return 0;
78+
}
79+
3380
static int wavguess(lua_State *L) {
34-
FILE *fw = *(FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE);
3581
int temp, nfsz, nsamp;
3682
short nfmt, nchs;
3783

38-
if (fread(&temp, 4, 1, fw) != 1 || temp != 0x46464952/*RIFF*/)
84+
if (_readfile(L, &temp, 4, 1) != 4 || temp != 0x46464952/*RIFF*/)
3985
return luaL_error(L, "Not a wave file");
40-
if (fseek(fw, 4, SEEK_CUR) != 0 || fread(&temp, 4, 1, fw) != 1 || temp != 0x45564157/*WAVE*/)
86+
if (_seekfile(L, 4, SEEK_CUR, 1) != 0 || _readfile(L, &temp, 4, 1) != 4 || temp != 0x45564157/*WAVE*/)
4187
return luaL_error(L, "WAVE id was not found");
42-
if (fread(&temp, 4, 1, fw) != 1 || temp != 0x20746D66/*fmt */)
88+
if (_readfile(L, &temp, 4, 1) != 4 || temp != 0x20746D66/*fmt */)
4389
return luaL_error(L, "No format sub-chunk found");
44-
if (fread(&nfsz, 4, 1, fw) != 1 || nfsz <= 0)
90+
if (_readfile(L, &nfsz, 4, 1) != 4 || nfsz <= 0)
4591
return luaL_error(L, "Invalid format sub-chunk size");
46-
if (fread(&nfmt, 2, 1, fw) != 1 || (nfmt != 0x0001 && nfmt != 0x0003))
92+
if (_readfile(L, &nfmt, 2, 1) != 2 || (nfmt != 0x0001 && nfmt != 0x0003))
4793
return luaL_error(L, "Unsupported data format");
48-
if (fread(&nchs, 2, 1, fw) != 1 || nchs > 1)
94+
if (_readfile(L, &nchs, 2, 1) != 2 || nchs > 1)
4995
return luaL_error(L, "Not a mono sound file");
50-
if (fread(&nsamp, 4, 1, fw) != 1 || nsamp == 0)
96+
if (_readfile(L, &nsamp, 4, 1) != 4 || nsamp == 0)
5197
return luaL_error(L, "Failed to determine sample rate");
52-
if (fseek(fw, 8, SEEK_CUR) != 0)
98+
if (_seekfile(L, 8, SEEK_CUR, 1) != 0)
5399
return luaL_error(L, "Unexpected EOF");
54100

55101
// Читаем расширенные данные для чанка формата, если такие имеются
56102
if (nfsz > 16) {
57103
temp = 0;
58104

59-
if (fread(&temp, 2, 1, fw) != 1 || fseek(fw, temp, SEEK_CUR) != 0)
105+
if (_readfile(L, &temp, 2, 1) != 2 || _seekfile(L, temp, SEEK_CUR, 1) != 0)
60106
return luaL_error(L, "Something wrong with the wave extension block");
61107
}
62108

63109
while (1) {
64-
if(fread(&temp, 4, 1, fw) != 1 || fread(&nfsz, 4, 1, fw) != 1) // Реюзаем nfsz, т.к. дальше он не нужен
110+
if(_readfile(L, &temp, 4, 1) != 4 || _readfile(L, &nfsz, 4, 1) != 4) // Реюзаем nfsz, т.к. дальше он не нужен
65111
return luaL_error(L, "Failed to read chunk info");
66112

67113
if (temp == 0x61746164) // При появлении data чанка завершаем цикл
68114
break;
69115

70-
fseek(fw, nfsz, SEEK_CUR);
116+
_seekfile(L, nfsz, SEEK_CUR, 1);
71117
}
72118

73119
lua_pushinteger(L, nsamp);

0 commit comments

Comments
 (0)