Skip to content

Commit

Permalink
MCU - v3.0 (#1099)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrrostam authored and ksyeo1010 committed Oct 12, 2023
1 parent 5d72a6b commit bfd4af0
Show file tree
Hide file tree
Showing 85 changed files with 10,599 additions and 9,691 deletions.
134 changes: 71 additions & 63 deletions demo/mcu/generate_pv_params_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,109 +37,117 @@
"""

LANGUAGE_CODE_TO_NAME = {
'ar': 'arabic',
'nl': 'dutch',
'en': 'english',
'fr': 'french',
'de': 'german',
'hi': 'hindi',
'it': 'italian',
'ja': 'japanese',
'ko': 'korean',
'zh': 'mandarin',
'pl': 'polish',
'pt': 'portuguese',
'ru': 'russian',
'es': 'spanish',
'sv': 'swedish',
'vn': 'vietnamese',
"ar": "arabic",
"de": "german",
"en": "english",
"es": "spanish",
"fa": "farsi",
"fr": "french",
"hi": "hindi",
"it": "italian",
"ja": "japanese",
"ko": "korean",
"nl": "dutch",
"pl": "polish",
"pt": "portuguese",
"ru": "russian",
"sv": "swedish",
"vn": "vietnamese",
"zh": "mandarin",
}


def generate_pv_params(ppn_files, header_file_folders):
script_dir = os.path.dirname(os.path.abspath(__file__))
repo_dir = os.path.join(script_dir, '../..')
repo_dir = os.path.join(script_dir, "../..")

for header_file_path in header_file_folders:
header_file = os.path.join(header_file_path, 'pv_params.h')
with open(os.path.join(script_dir, header_file), 'w') as f_out:
header_file = os.path.join(header_file_path, "pv_params.h")
with open(os.path.join(script_dir, header_file), "w") as f_out:
f_out.write(HEADER)

for language, keywords in ppn_files.items():

if language == 'en':
ppn_dir = os.path.join(repo_dir, 'resources/keyword_files/cortexm')
if language == "en":
ppn_dir = os.path.join(repo_dir, "resources/keyword_files/cortexm")
else:
ppn_dir = os.path.join(repo_dir, f'resources/keyword_files_{language}/cortexm')
ppn_dir = os.path.join(repo_dir, f"resources/keyword_files_{language}/cortexm")

f_out.write(f'#if defined(__PV_LANGUAGE_{LANGUAGE_CODE_TO_NAME[language].upper()}__)\n\n')
f_out.write(f"#if defined(__PV_LANGUAGE_{LANGUAGE_CODE_TO_NAME[language].upper()}__)\n\n")
for index, keyword in enumerate(keywords):
keyword_file_path = os.path.join(ppn_dir, keyword + '_cortexm.ppn')
keyword_file_path = os.path.join(ppn_dir, keyword + "_cortexm.ppn")
ppn_c_array = ppn_to_c_array(keyword_file_path)
f_out.write(f'// Wake-word = {keyword}\n')
f_out.write(f"// Wake-word = {keyword}\n")
if index == 0:
f_out.write('static const uint8_t DEFAULT_KEYWORD_ARRAY[] '
'__attribute__ ((aligned (16))) = {\n')
f_out.write(
"static const uint8_t DEFAULT_KEYWORD_ARRAY[] " "__attribute__ ((aligned (16))) = {\n"
)
else:
f_out.write(
f'static const uint8_t {keyword.upper()}_KEYWORD_ARRAY[] '
'__attribute__ ((aligned (16))) = {\n')
f_out.write('\n'.join(ppn_c_array))
f_out.write('};\n\n')
f"static const uint8_t {keyword.upper()}_KEYWORD_ARRAY[] "
"__attribute__ ((aligned (16))) = {\n"
)
f_out.write("\n".join(ppn_c_array))
f_out.write("};\n\n")

f_out.write('#endif\n\n')
f_out.write("#endif\n\n")

f_out.write(FOOTER)


def ppn_to_c_array(ppn_file_path):
indent = 8
line_width = 120
with open(ppn_file_path, 'rb') as f:
with open(ppn_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
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] + ','
row = row.rsplit(", ", maxsplit=1)[0] + ","
res.append(row)
row = ' ' * indent + last_x
if row != ' ' * indent:
row += ', '
row = " " * indent + last_x
if row != " " * indent:
row += ", "
row += x
last_x = x
if row != ' ' * indent:
if row != " " * indent:
res.append(row)
res.append('')
res.append("")
return res


if __name__ == '__main__':
if __name__ == "__main__":
wake_words = {
'en': ('porcupine', 'picovoice', 'bumblebee', 'alexa',),
'de': ('hey computer',),
'es': ('hola computadora',),
'fr': ('bonjour ordinateur',),
'it': ('ciao computer',),
'ja': ('konnichiwa konpyūtā',),
'ko': ('annyeong keompyuteo',),
'pt': ('olá computador',),
'ru': ('privet kompyuter',),
'nl': ('hallo computer',),
'hi': ('namaste putra',),
'ar': ('coffee',),
'pl': ('cześć komputer',),
'sv': ('hej dator',),
'vn': ('xin chào máy tính',),
'zh': ('nǐ hǎo diànnǎo',),
"ar": ("مرحبا الكمبيوتر",),
"de": ("hey computer",),
"en": (
"porcupine",
"picovoice",
"bumblebee",
"alexa",
),
"es": ("hola computadora",),
"fa": ("سلام رایانه",),
"fr": ("bonjour ordinateur",),
"hi": ("नमस्ते कंप्यूटर",),
"it": ("ciao computer",),
"ja": ("こんにちは コンピューター",),
"ko": ("안녕 컴퓨터",),
"nl": ("hallo computer",),
"pl": ("cześć komputer",),
"pt": ("olá computador",),
"ru": ("привет компьютер",),
"sv": ("hej dator",),
"vn": ("xin chào máy tính",),
"zh": ("你好电脑",),
}
include_folders = [
'imxrt1050/imxrt1050-evkb/inc',
'stm32f407/stm32f407g-disc1/Inc/',
'stm32f411/stm32f411e-disco/Inc/',
'stm32f769/stm32f769i-disco/Inc/',
"imxrt1050/imxrt1050-evkb/inc",
"stm32f407/stm32f407g-disc1/Inc/",
"stm32f411/stm32f411e-disco/Inc/",
"stm32f769/stm32f769i-disco/Inc/",
]

generate_pv_params(wake_words, include_folders)
Loading

0 comments on commit bfd4af0

Please sign in to comment.