Skip to content

Commit

Permalink
MCU - v3.0 (#664)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrrostam authored Oct 11, 2023
1 parent f2ea886 commit 7281719
Show file tree
Hide file tree
Showing 81 changed files with 12,180 additions and 11,194 deletions.
137 changes: 69 additions & 68 deletions demo/mcu/generate_pv_params_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

HEADER = """
/*
Copyright 2020-2022 Picovoice Inc.
Copyright 2020-2023 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.
Expand All @@ -38,112 +38,113 @@
"""

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(model_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(os.path.dirname(__file__), header_file_path, 'pv_params.h')
with open(header_file, 'w') as f_out:
header_file = os.path.join(os.path.dirname(__file__), header_file_path, "pv_params.h")
with open(header_file, "w") as f_out:
f_out.write(HEADER)

for language, ppn_rhn_models in model_files.items():

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

f_out.write(f'\n#if defined(__PV_LANGUAGE_{LANGUAGE_CODE_TO_NAME[language].upper()}__)\n\n')
f_out.write(f"\n#if defined(__PV_LANGUAGE_{LANGUAGE_CODE_TO_NAME[language].upper()}__)\n\n")

keyword_file_path = os.path.join(ppn_dir, ppn_rhn_models['wake_word'] + '_cortexm.ppn')
keyword_file_path = os.path.join(ppn_dir, ppn_rhn_models["wake_word"] + "_cortexm.ppn")
ppn_c_array = ppn_to_c_array(keyword_file_path)
f_out.write('// wake-word = %s \n' % ppn_rhn_models['wake_word'])
f_out.write('static const uint8_t KEYWORD_ARRAY[] = {\n')
f_out.write('\n'.join(ppn_c_array))
f_out.write('};\n\n')
f_out.write("// wake-word = %s \n" % ppn_rhn_models["wake_word"])
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, ppn_rhn_models['context'] + '_cortexm.rhn')
context_file_path = os.path.join(rhn_dir, ppn_rhn_models["context"] + "_cortexm.rhn")
ppn_c_array = ppn_to_c_array(context_file_path)
f_out.write('// context = %s \n' % ppn_rhn_models['context'])
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("// context = %s \n" % ppn_rhn_models["context"])
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(f'\n#endif // __PV_LANGUAGE_{LANGUAGE_CODE_TO_NAME[language].upper()}__\n')
f_out.write(f"\n#endif // __PV_LANGUAGE_{LANGUAGE_CODE_TO_NAME[language].upper()}__\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:
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
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__":
models = {
'en': {'wake_word': 'picovoice', 'context': 'smart_lighting'},
'de': {'wake_word': 'hey computer', 'context': 'beleuchtung'},
'es': {'wake_word': 'hola computadora', 'context': 'iluminación_inteligente'},
'fr': {'wake_word': 'bonjour ordinateur', 'context': 'éclairage_intelligent'},
'it': {'wake_word': 'ciao computer', 'context': 'illuminazione'},
'ja': {'wake_word': 'konnichiwa konpyūtā', 'context': 'sumāto_shōmei'},
'ko': {'wake_word': 'annyeong keompyuteo', 'context': 'seumateu_jomyeong'},
'pt': {'wake_word': 'olá computador', 'context': 'luz_inteligente'},
'ru': {'wake_word': 'privet kompyuter', 'context': 'simple_context_ru'},
'nl': {'wake_word': 'hallo computer', 'context': 'simple_context_nl'},
'hi': {'wake_word': 'namaste putra', 'context': 'simple_context_hi'},
'ar': {'wake_word': 'coffee', 'context': 'simple_context_ar'},
'pl': {'wake_word': 'cześć komputer', 'context': 'simple_context_pl'},
'sv': {'wake_word': 'hej dator', 'context': 'simple_context_sv'},
'vn': {'wake_word': 'xin chào máy tính', 'context': 'simple_context_vn'},
'zh': {'wake_word': 'nǐ hǎo diànnǎo', 'context': 'simple_context_zh'},
"ar": {"wake_word": "مرحبا الكمبيوتر", "context": "simple_context_ar"},
"de": {"wake_word": "hey computer", "context": "beleuchtung"},
"en": {"wake_word": "picovoice", "context": "smart_lighting"},
"es": {"wake_word": "hola computadora", "context": "iluminación_inteligente"},
"fa": {"wake_word": "سلام رایانه", "context": "simple_context_fa"},
"fr": {"wake_word": "bonjour ordinateur", "context": "éclairage_intelligent"},
"hi": {"wake_word": "नमस्ते कंप्यूटर", "context": "simple_context_hi"},
"it": {"wake_word": "ciao computer", "context": "illuminazione"},
"ja": {"wake_word": "こんにちは コンピューター", "context": "sumāto_shōmei"},
"ko": {"wake_word": "안녕 컴퓨터", "context": "seumateu_jomyeong"},
"nl": {"wake_word": "hallo computer", "context": "simple_context_nl"},
"pl": {"wake_word": "cześć komputer", "context": "simple_context_pl"},
"pt": {"wake_word": "olá computador", "context": "luz_inteligente"},
"ru": {"wake_word": "привет компьютер", "context": "simple_context_ru"},
"sv": {"wake_word": "hej dator", "context": "simple_context_sv"},
"vn": {"wake_word": "xin chào máy tính", "context": "simple_context_vn"},
"zh": {"wake_word": "你好电脑", "context": "simple_context_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(models, include_folders)
Loading

0 comments on commit 7281719

Please sign in to comment.