generate_pv_params_file.py
Go to the documentation of this file.
1 #
2 # Copyright 2020-2021 Picovoice Inc.
3 #
4 # You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
5 # file accompanying this source.
6 #
7 # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8 # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9 # specific language governing permissions and limitations under the License.
10 #
11 
12 import os
13 import struct
14 
15 HEADER = """
16 /*
17  Copyright 2020-2022 Picovoice Inc.
18 
19  You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
20  file accompanying this source.
21 
22  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
23  an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
24  specific language governing permissions and limitations under the License.
25 */
26 
27 #ifndef PV_PARAMS_H
28 #define PV_PARAMS_H
29 
30 #include <stdint.h>
31 
32 """
33 
34 FOOTER = """
35 #endif // PV_PARAMS
36 
37 """
38 
39 LANGUAGE_CODE_TO_NAME = {
40  'en': 'english',
41  'de': 'german',
42  'es': 'spanish',
43  'fr': 'french',
44 }
45 
46 
47 def generate_pv_params(ppn_files, header_file_folders):
48  script_dir = os.path.dirname(os.path.abspath(__file__))
49  repo_dir = os.path.join(script_dir, '../..')
50 
51  for header_file_path in header_file_folders:
52  header_file = os.path.join(header_file_path, 'pv_params.h')
53  with open(os.path.join(script_dir, header_file), 'w') as f_out:
54  f_out.write(HEADER)
55 
56  for language, keywords in ppn_files.items():
57  if language == 'en':
58  ppn_dir = os.path.join(repo_dir, 'resources/keyword_files/cortexm')
59  else:
60  ppn_dir = os.path.join(repo_dir, f'resources/keyword_files_{language}/cortexm')
61 
62  f_out.write(f'#if defined(__PV_LANGUAGE_{LANGUAGE_CODE_TO_NAME[language].upper()}__)\n\n')
63  for index, keyword in enumerate(keywords):
64  keyword_file_path = os.path.join(ppn_dir, keyword + '_cortexm.ppn')
65  ppn_c_array = ppn_to_c_array(keyword_file_path)
66  f_out.write(f'// Wake-word = {keyword}\n')
67  if index == 0:
68  f_out.write('static const uint8_t DEFAULT_KEYWORD_ARRAY[] '
69  '__attribute__ ((aligned (16))) = {\n')
70  else:
71  f_out.write(
72  f'static const uint8_t {keyword.upper()}_KEYWORD_ARRAY[] '
73  '__attribute__ ((aligned (16))) = {\n')
74  f_out.write('\n'.join(ppn_c_array))
75  f_out.write('};\n\n')
76 
77  f_out.write(f'#endif\n\n')
78 
79  f_out.write(FOOTER)
80 
81 
82 def ppn_to_c_array(ppn_file_path):
83  indent = 8
84  line_width = 120
85  with open(ppn_file_path, 'rb') as f:
86  array = f.read()
87  res = list()
88  array = ['0x%s' % z.hex() for z in struct.unpack('%dc' % len(array), array)]
89  row = ' ' * indent
90  last_x = 0
91  for x in array:
92  if len(row) >= line_width:
93  row = row.rsplit(', ', maxsplit=1)[0] + ','
94  res.append(row)
95  row = ' ' * indent + last_x
96  if row != ' ' * indent:
97  row += ', '
98  row += x
99  last_x = x
100  if row != ' ' * indent:
101  res.append(row)
102  res.append('')
103  return res
104 
105 
106 if __name__ == '__main__':
107  wake_words = {
108  'en': ('porcupine', 'picovoice', 'bumblebee', 'alexa',),
109  'de': ('hey computer',),
110  'es': ('hola computadora',),
111  'fr': ('salut ordinateur',)
112  }
113  include_folders = (
114  'stm32h747/stm32h747i-disco/CM7/Inc/',
115  'stm32f469/stm32f469i-disco/Inc/',
116  'stm32f769/stm32f769i-disco/Inc/',
117  'stm32f411/stm32f411e-disco/Inc/',
118  'stm32f407/stm32f407g-disc1/Inc/',
119  'stm32h735/stm32h735g-dk/Inc/',
120  'imxrt1050/imxrt1050-evkb/inc',
121  'psoc062s2/include'
122  )
123 
124  generate_pv_params(wake_words, include_folders)
generate_pv_params_file.generate_pv_params
def generate_pv_params(ppn_files, header_file_folders)
Definition: generate_pv_params_file.py:47
generate_pv_params_file.ppn_to_c_array
def ppn_to_c_array(ppn_file_path)
Definition: generate_pv_params_file.py:82


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:13:56