forked from Picovoice/picovoice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_pv_params_file.py
107 lines (86 loc) · 3.63 KB
/
generate_pv_params_file.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#
# Copyright 2020-2021 Picovoice Inc.
#
# You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
# file accompanying this source.
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
import os
import shutil
import struct
import subprocess
import sys
HEADER = """
/*
Copyright 2020-2021 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
#ifndef PV_PRAMS_H
#define PV_PRAMS_H
#include <stdint.h>
"""
FOOTER = """
#endif // PV_PARAMS
"""
def generate_pv_params(ppn_file, rhn_file, header_file_folders):
script_dir = os.path.dirname(os.path.abspath(__file__))
repo_dir = os.path.join(script_dir, '../..')
ppn_dir = os.path.join(repo_dir, 'resources/porcupine/resources/keyword_files/cortexm')
rhn_dir = os.path.join(repo_dir, 'resources/rhino/resources/contexts/cortexm')
for header_file_path in header_file_folders:
header_file = os.path.join(header_file_path, 'pv_params.h')
with open(header_file, 'w') as f_out:
f_out.write(HEADER)
keyword_file_path = os.path.join(ppn_dir, ppn_file + '_cortexm.ppn')
ppn_c_array = ppn_to_c_array(keyword_file_path)
f_out.write('// wake-word = %s \n' % ppn_file)
f_out.write('static const uint8_t KEYWORD_ARRAY[] = {\n')
f_out.write('\n'.join(ppn_c_array))
f_out.write('};\n\n')
context_file_path = os.path.join(rhn_dir, rhn_file + '_cortexm.rhn')
ppn_c_array = ppn_to_c_array(context_file_path)
f_out.write('// context = %s \n' % rhn_file)
f_out.write('static const uint8_t CONTEXT_ARRAY[] = {\n')
f_out.write('\n'.join(ppn_c_array))
f_out.write('};\n')
f_out.write(FOOTER)
def ppn_to_c_array(binary_file_path):
indent = 8
line_width = 120
with open(binary_file_path, 'rb') as f:
array = f.read()
res = list()
array = ['0x%s' % z.hex() for z in struct.unpack('%dc' % len(array), array)]
row = ' ' * indent
last_x = 0
for x in array:
if len(row) >= line_width:
row = row.rsplit(', ', maxsplit=1)[0] + ','
res.append(row)
row = ' ' * indent + last_x
if row != ' ' * indent:
row += ', '
row += x
last_x = x
if row != ' ' * indent:
res.append(row)
res.append('')
return res
if __name__ == '__main__':
wake_word = 'picovoice'
context = 'smart_lighting'
include_folders = ('stm32h747/stm32h747i-disco/CM7/Inc/',
'stm32f469/stm32f469i-disco/Inc/',
'stm32f411/stm32f411e-disco/Inc/',
'stm32f769/stm32f769i-disco/Inc/',
'stm32f407/stm32f407g-disc1/Inc/',
'stm32h735/stm32h735g-dk/Inc/',
'imxrt1050/imxrt1050-evkb/inc',)
generate_pv_params(wake_word, context, include_folders)