|
| 1 | +// |
| 2 | +// Copyright 2020 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 | +"use strict"; |
| 12 | +const path = require("path"); |
| 13 | + |
| 14 | +const { PvArgumentError } = require("./errors"); |
| 15 | +const { getPlatform } = require("./platforms"); |
| 16 | + |
| 17 | +const AMERICANO = 0; |
| 18 | +const BLUEBERRY = 1; |
| 19 | +const BUMBLEBEE = 2; |
| 20 | +const GRAPEFRUIT = 3; |
| 21 | +const GRASSHOPPER = 4; |
| 22 | +const PICOVOICE = 5; |
| 23 | +const PORCUPINE = 6; |
| 24 | +const TERMINATOR = 7; |
| 25 | + |
| 26 | +const AMERICANO_STRING = "americano"; |
| 27 | +const BLUEBERRY_STRING = "blueberry"; |
| 28 | +const BUMBLEBEE_STRING = "bumblebee"; |
| 29 | +const GRAPEFRUIT_STRING = "grapefruit"; |
| 30 | +const GRASSHOPPER_STRING = "grasshopper"; |
| 31 | +const PICOVOICE_STRING = "picovoice"; |
| 32 | +const PORCUPINE_STRING = "porcupine"; |
| 33 | +const TERMINATOR_STRING = "terminator"; |
| 34 | + |
| 35 | +const BUILTIN_KEYWORDS_ENUMS = new Set(); |
| 36 | +BUILTIN_KEYWORDS_ENUMS.add(AMERICANO); |
| 37 | +BUILTIN_KEYWORDS_ENUMS.add(BLUEBERRY); |
| 38 | +BUILTIN_KEYWORDS_ENUMS.add(BUMBLEBEE); |
| 39 | +BUILTIN_KEYWORDS_ENUMS.add(GRAPEFRUIT); |
| 40 | +BUILTIN_KEYWORDS_ENUMS.add(GRASSHOPPER); |
| 41 | +BUILTIN_KEYWORDS_ENUMS.add(PICOVOICE); |
| 42 | +BUILTIN_KEYWORDS_ENUMS.add(PORCUPINE); |
| 43 | +BUILTIN_KEYWORDS_ENUMS.add(TERMINATOR); |
| 44 | + |
| 45 | +const BUILTIN_KEYWORDS_STRINGS = new Set(); |
| 46 | +BUILTIN_KEYWORDS_STRINGS.add(AMERICANO_STRING); |
| 47 | +BUILTIN_KEYWORDS_STRINGS.add(BLUEBERRY_STRING); |
| 48 | +BUILTIN_KEYWORDS_STRINGS.add(BUMBLEBEE_STRING); |
| 49 | +BUILTIN_KEYWORDS_STRINGS.add(GRAPEFRUIT_STRING); |
| 50 | +BUILTIN_KEYWORDS_STRINGS.add(GRASSHOPPER_STRING); |
| 51 | +BUILTIN_KEYWORDS_STRINGS.add(PICOVOICE_STRING); |
| 52 | +BUILTIN_KEYWORDS_STRINGS.add(PORCUPINE_STRING); |
| 53 | +BUILTIN_KEYWORDS_STRINGS.add(TERMINATOR_STRING); |
| 54 | + |
| 55 | +const BUILTIN_KEYWORDS_ENUM_TO_STRING = new Map(); |
| 56 | +BUILTIN_KEYWORDS_ENUM_TO_STRING.set(AMERICANO, AMERICANO_STRING); |
| 57 | +BUILTIN_KEYWORDS_ENUM_TO_STRING.set(BLUEBERRY, BLUEBERRY_STRING); |
| 58 | +BUILTIN_KEYWORDS_ENUM_TO_STRING.set(BUMBLEBEE, BUMBLEBEE_STRING); |
| 59 | +BUILTIN_KEYWORDS_ENUM_TO_STRING.set(GRAPEFRUIT, GRAPEFRUIT_STRING); |
| 60 | +BUILTIN_KEYWORDS_ENUM_TO_STRING.set(GRASSHOPPER, GRASSHOPPER_STRING); |
| 61 | +BUILTIN_KEYWORDS_ENUM_TO_STRING.set(PICOVOICE, PICOVOICE_STRING); |
| 62 | +BUILTIN_KEYWORDS_ENUM_TO_STRING.set(PORCUPINE, PORCUPINE_STRING); |
| 63 | +BUILTIN_KEYWORDS_ENUM_TO_STRING.set(TERMINATOR, TERMINATOR_STRING); |
| 64 | + |
| 65 | +const BUILTIN_KEYWORDS_STRING_TO_ENUM = new Map(); |
| 66 | +BUILTIN_KEYWORDS_STRING_TO_ENUM.set(AMERICANO_STRING, AMERICANO); |
| 67 | +BUILTIN_KEYWORDS_STRING_TO_ENUM.set(BLUEBERRY_STRING, BLUEBERRY); |
| 68 | +BUILTIN_KEYWORDS_STRING_TO_ENUM.set(BUMBLEBEE_STRING, BUMBLEBEE); |
| 69 | +BUILTIN_KEYWORDS_STRING_TO_ENUM.set(GRAPEFRUIT_STRING, GRAPEFRUIT); |
| 70 | +BUILTIN_KEYWORDS_STRING_TO_ENUM.set(GRASSHOPPER_STRING, GRASSHOPPER); |
| 71 | +BUILTIN_KEYWORDS_STRING_TO_ENUM.set(PICOVOICE_STRING, PICOVOICE); |
| 72 | +BUILTIN_KEYWORDS_STRING_TO_ENUM.set(PORCUPINE_STRING, PORCUPINE); |
| 73 | +BUILTIN_KEYWORDS_STRING_TO_ENUM.set(TERMINATOR_STRING, TERMINATOR); |
| 74 | + |
| 75 | +function getBuiltinKeywordPath(builtinKeyword) { |
| 76 | + if (!Number.isInteger(builtinKeyword)) { |
| 77 | + throw new PvArgumentError( |
| 78 | + `getBuiltinKeywordPath argument '${builtinKeyword}' is not an integer (enum) value` |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + let platform = getPlatform(); |
| 83 | + let keywordString; |
| 84 | + if (!BUILTIN_KEYWORDS_ENUMS.has(builtinKeyword)) { |
| 85 | + throw new PvArgumentError( |
| 86 | + `Keyword argument ${builtinKeyword} does not map to one of the built-in keywords: [${Array.from( |
| 87 | + BUILTIN_KEYWORDS_STRINGS |
| 88 | + )}]` |
| 89 | + ); |
| 90 | + } else { |
| 91 | + keywordString = BUILTIN_KEYWORDS_ENUM_TO_STRING.get(builtinKeyword); |
| 92 | + } |
| 93 | + |
| 94 | + return path.resolve( |
| 95 | + __dirname, |
| 96 | + `resources/keyword_files/${platform}/${keywordString}_${platform}.ppn` |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +module.exports = { |
| 101 | + AMERICANO: AMERICANO, |
| 102 | + BLUEBERRY: BLUEBERRY, |
| 103 | + BUMBLEBEE: BUMBLEBEE, |
| 104 | + GRAPEFRUIT: GRAPEFRUIT, |
| 105 | + GRASSHOPPER: GRASSHOPPER, |
| 106 | + PICOVOICE: PICOVOICE, |
| 107 | + PORCUPINE: PORCUPINE, |
| 108 | + TERMINATOR: TERMINATOR, |
| 109 | + BUILTIN_KEYWORDS_ENUMS: BUILTIN_KEYWORDS_ENUMS, |
| 110 | + BUILTIN_KEYWORDS_STRINGS: BUILTIN_KEYWORDS_STRINGS, |
| 111 | + BUILTIN_KEYWORDS_ENUM_TO_STRING: BUILTIN_KEYWORDS_ENUM_TO_STRING, |
| 112 | + BUILTIN_KEYWORDS_STRING_TO_ENUM: BUILTIN_KEYWORDS_STRING_TO_ENUM, |
| 113 | + getBuiltinKeywordPath: getBuiltinKeywordPath, |
| 114 | +}; |
0 commit comments