|
| 1 | +/** |
| 2 | + * @file rac_stt_whisperkit_coreml.h |
| 3 | + * @brief RunAnywhere Commons - WhisperKit CoreML STT Backend (Apple Neural Engine) |
| 4 | + * |
| 5 | + * C API for the WhisperKit CoreML STT backend. The actual inference runs in |
| 6 | + * Swift via WhisperKit + CoreML; C++ provides the callback infrastructure, |
| 7 | + * vtable dispatch, and automatic telemetry through the standard stt_component |
| 8 | + * pipeline. |
| 9 | + * |
| 10 | + * This backend is Apple-only. On non-Apple platforms it is never registered. |
| 11 | + */ |
| 12 | + |
| 13 | +#ifndef RAC_STT_WHISPERKIT_COREML_H |
| 14 | +#define RAC_STT_WHISPERKIT_COREML_H |
| 15 | + |
| 16 | +#include "rac/core/rac_types.h" |
| 17 | +#include "rac/features/stt/rac_stt_types.h" |
| 18 | + |
| 19 | +#ifdef __cplusplus |
| 20 | +extern "C" { |
| 21 | +#endif |
| 22 | + |
| 23 | +// ============================================================================= |
| 24 | +// SWIFT CALLBACK TYPES |
| 25 | +// ============================================================================= |
| 26 | + |
| 27 | +/** |
| 28 | + * Callback to check if WhisperKit CoreML can handle a model ID. |
| 29 | + * |
| 30 | + * @param model_id Model identifier to check (can be NULL) |
| 31 | + * @param user_data User-provided context |
| 32 | + * @return RAC_TRUE if WhisperKit CoreML can handle this model |
| 33 | + */ |
| 34 | +typedef rac_bool_t (*rac_whisperkit_coreml_stt_can_handle_fn)(const char* model_id, |
| 35 | + void* user_data); |
| 36 | + |
| 37 | +/** |
| 38 | + * Callback to load a WhisperKit CoreML model. |
| 39 | + * |
| 40 | + * @param model_path Path to model directory containing .mlmodelc files |
| 41 | + * @param model_id Model identifier |
| 42 | + * @param user_data User-provided context |
| 43 | + * @return Opaque handle to loaded service, or NULL on failure |
| 44 | + */ |
| 45 | +typedef rac_handle_t (*rac_whisperkit_coreml_stt_create_fn)(const char* model_path, |
| 46 | + const char* model_id, |
| 47 | + void* user_data); |
| 48 | + |
| 49 | +/** |
| 50 | + * Callback to transcribe audio via WhisperKit CoreML. |
| 51 | + * |
| 52 | + * @param handle Service handle from create |
| 53 | + * @param audio_data PCM audio data (Int16, 16kHz mono) |
| 54 | + * @param audio_size Size of audio data in bytes |
| 55 | + * @param options Transcription options |
| 56 | + * @param out_result Output: transcription result (text must be strdup'd) |
| 57 | + * @param user_data User-provided context |
| 58 | + * @return RAC_SUCCESS or error code |
| 59 | + */ |
| 60 | +typedef rac_result_t (*rac_whisperkit_coreml_stt_transcribe_fn)(rac_handle_t handle, |
| 61 | + const void* audio_data, |
| 62 | + size_t audio_size, |
| 63 | + const rac_stt_options_t* options, |
| 64 | + rac_stt_result_t* out_result, |
| 65 | + void* user_data); |
| 66 | + |
| 67 | +/** |
| 68 | + * Callback to destroy/unload a WhisperKit CoreML service. |
| 69 | + * |
| 70 | + * @param handle Service handle to destroy |
| 71 | + * @param user_data User-provided context |
| 72 | + */ |
| 73 | +typedef void (*rac_whisperkit_coreml_stt_destroy_fn)(rac_handle_t handle, void* user_data); |
| 74 | + |
| 75 | +/** |
| 76 | + * Swift callbacks for WhisperKit CoreML STT operations. |
| 77 | + */ |
| 78 | +typedef struct rac_whisperkit_coreml_stt_callbacks { |
| 79 | + rac_whisperkit_coreml_stt_can_handle_fn can_handle; |
| 80 | + rac_whisperkit_coreml_stt_create_fn create; |
| 81 | + rac_whisperkit_coreml_stt_transcribe_fn transcribe; |
| 82 | + rac_whisperkit_coreml_stt_destroy_fn destroy; |
| 83 | + void* user_data; |
| 84 | +} rac_whisperkit_coreml_stt_callbacks_t; |
| 85 | + |
| 86 | +// ============================================================================= |
| 87 | +// CALLBACK REGISTRATION |
| 88 | +// ============================================================================= |
| 89 | + |
| 90 | +/** |
| 91 | + * Sets the Swift callbacks for WhisperKit CoreML STT operations. |
| 92 | + * Must be called before rac_backend_whisperkit_coreml_register(). |
| 93 | + * |
| 94 | + * @param callbacks Callback functions (copied internally) |
| 95 | + * @return RAC_SUCCESS on success |
| 96 | + */ |
| 97 | +RAC_API rac_result_t |
| 98 | +rac_whisperkit_coreml_stt_set_callbacks(const rac_whisperkit_coreml_stt_callbacks_t* callbacks); |
| 99 | + |
| 100 | +/** |
| 101 | + * Gets the current Swift callbacks. |
| 102 | + * |
| 103 | + * @return Pointer to callbacks, or NULL if not set |
| 104 | + */ |
| 105 | +RAC_API const rac_whisperkit_coreml_stt_callbacks_t* |
| 106 | +rac_whisperkit_coreml_stt_get_callbacks(void); |
| 107 | + |
| 108 | +/** |
| 109 | + * Checks if Swift callbacks are registered. |
| 110 | + * |
| 111 | + * @return RAC_TRUE if callbacks are available |
| 112 | + */ |
| 113 | +RAC_API rac_bool_t rac_whisperkit_coreml_stt_is_available(void); |
| 114 | + |
| 115 | +// ============================================================================= |
| 116 | +// BACKEND REGISTRATION |
| 117 | +// ============================================================================= |
| 118 | + |
| 119 | +/** |
| 120 | + * Register the WhisperKit CoreML backend with the module and service registries. |
| 121 | + * Swift callbacks must be set via rac_whisperkit_coreml_stt_set_callbacks() first. |
| 122 | + * |
| 123 | + * @return RAC_SUCCESS on success |
| 124 | + */ |
| 125 | +RAC_API rac_result_t rac_backend_whisperkit_coreml_register(void); |
| 126 | + |
| 127 | +/** |
| 128 | + * Unregister the WhisperKit CoreML backend. |
| 129 | + * |
| 130 | + * @return RAC_SUCCESS on success |
| 131 | + */ |
| 132 | +RAC_API rac_result_t rac_backend_whisperkit_coreml_unregister(void); |
| 133 | + |
| 134 | +#ifdef __cplusplus |
| 135 | +} |
| 136 | +#endif |
| 137 | + |
| 138 | +#endif /* RAC_STT_WHISPERKIT_COREML_H */ |
0 commit comments