@@ -20,6 +20,7 @@ import {
2020} from './errors' ;
2121
2222import {
23+ OrcaInputOptions ,
2324 OrcaOptions ,
2425 OrcaSynthesizeParams ,
2526 OrcaSynthesizeResult ,
@@ -39,6 +40,10 @@ type OrcaStreamResult = {
3940 status : PvStatus ;
4041 pcm : Int16Array ;
4142} ;
43+ type OrcaHardwareDevicesResult = {
44+ hardware_devices : string [ ] ;
45+ status : PvStatus ;
46+ } ;
4247
4348class Stream {
4449 private readonly _stream : any ;
@@ -155,6 +160,12 @@ export class Orca {
155160 * @param {string } accessKey AccessKey obtained from Picovoice Console (https://console.picovoice.ai/).
156161 * @param {OrcaOptions } options Optional configuration arguments.
157162 * @param {string } options.modelPath The path to save and use the model from (.pv extension)
163+ * @param {string } options.device String representation of the device (e.g., CPU or GPU) to use for inference.
164+ * If set to `best`, the most suitable device is selected automatically. If set to `gpu`, the engine uses the
165+ * first available GPU device. To select a specific GPU device, set this argument to `gpu:${GPU_INDEX}`, where
166+ * `${GPU_INDEX}` is the index of the target GPU. If set to `cpu`, the engine will run on the CPU with the
167+ * default number of threads. To specify the number of threads, set this argument to `cpu:${NUM_THREADS}`,
168+ * where `${NUM_THREADS}` is the desired number of threads.
158169 * @param {string } options.libraryPath the path to the Orca dynamic library (.node extension)
159170 */
160171 constructor ( accessKey : string , options : OrcaOptions = { } ) {
@@ -168,6 +179,7 @@ export class Orca {
168179
169180 const {
170181 modelPath = path . resolve ( __dirname , DEFAULT_MODEL_PATH ) ,
182+ device = 'best' ,
171183 libraryPath = getSystemLibraryPath ( ) ,
172184 } = options ;
173185
@@ -190,7 +202,7 @@ export class Orca {
190202 try {
191203 pvOrca . set_sdk ( 'nodejs' ) ;
192204
193- orcaHandleAndStatus = pvOrca . init ( accessKey , modelPath ) ;
205+ orcaHandleAndStatus = pvOrca . init ( accessKey , modelPath , device ) ;
194206 } catch ( err : any ) {
195207 pvStatusToException ( < PvStatus > err . code , err ) ;
196208 }
@@ -465,6 +477,41 @@ export class Orca {
465477 }
466478 }
467479
480+ /**
481+ * Lists all available devices that Orca can use for inference. Each entry in the list can be used
482+ * as the `device` argument when creating an instance of Orca.
483+ * @param {OrcaInputOptions } options Optional configuration arguments.
484+ * @param {string } options.libraryPath the path to the Orca dynamic library (.node extension)
485+ *
486+ * @returns List of all available devices that Orca can use for inference.
487+ */
488+ static listAvailableDevices ( options : OrcaInputOptions = { } ) : string [ ] {
489+ const {
490+ libraryPath = getSystemLibraryPath ( ) ,
491+ } = options ;
492+
493+ const pvOrca = require ( libraryPath ) ; // eslint-disable-line
494+
495+ let orcaHardwareDevicesResult : OrcaHardwareDevicesResult | null = null ;
496+ try {
497+ orcaHardwareDevicesResult = pvOrca . list_hardware_devices ( ) ;
498+ } catch ( err : any ) {
499+ pvStatusToException ( < PvStatus > err . code , err ) ;
500+ }
501+
502+ const status = orcaHardwareDevicesResult ! . status ;
503+ if ( status !== PvStatus . SUCCESS ) {
504+ const errorObject = pvOrca . get_error_stack ( ) ;
505+ if ( errorObject . status === PvStatus . SUCCESS ) {
506+ pvStatusToException ( status , 'Orca failed to get available devices' , errorObject . message_stack ) ;
507+ } else {
508+ pvStatusToException ( status , 'Unable to get Orca error state' ) ;
509+ }
510+ }
511+
512+ return orcaHardwareDevicesResult ! . hardware_devices ;
513+ }
514+
468515 private handlePvStatus ( status : PvStatus , message : string ) : void {
469516 const errorObject = this . _pvOrca . get_error_stack ( ) ;
470517 if ( errorObject . status === PvStatus . SUCCESS ) {
0 commit comments