go: allow set a use_gpu option in Go bindings

This commit is contained in:
AnyCPU 2025-09-16 17:55:23 +03:00
parent edea8a9c3c
commit b1f1a14198
2 changed files with 31 additions and 2 deletions

View File

@ -0,0 +1,24 @@
package whisper
type (
contextParamsOption interface{ apply(*ContextParams) }
contextParamsOptionFunc func(*ContextParams)
)
func (fn contextParamsOptionFunc) apply(to *ContextParams) {
fn(to)
}
func WithUseGPU(v bool) contextParamsOption {
return contextParamsOptionFunc(func(p *ContextParams) {
p.SetUseGPU(v)
})
}
func (p *ContextParams) UseGPU() bool {
return bool(p.use_gpu)
}
func (p *ContextParams) SetUseGPU(v bool) {
p.use_gpu = toBool(v)
}

View File

@ -71,6 +71,7 @@ type (
TokenData C.struct_whisper_token_data
SamplingStrategy C.enum_whisper_sampling_strategy
Params C.struct_whisper_full_params
ContextParams C.struct_whisper_context_params
)
///////////////////////////////////////////////////////////////////////////////
@ -101,10 +102,14 @@ var (
// Allocates all memory needed for the model and loads the model from the given file.
// Returns NULL on failure.
func Whisper_init(path string) *Context {
func Whisper_init(path string, options ...contextParamsOption) *Context {
cPath := C.CString(path)
defer C.free(unsafe.Pointer(cPath))
if ctx := C.whisper_init_from_file_with_params(cPath, C.whisper_context_default_params()); ctx != nil {
params := ContextParams(C.whisper_context_default_params())
for _, o := range options {
o.apply(&params)
}
if ctx := C.whisper_init_from_file_with_params(cPath, (C.struct_whisper_context_params)(params)); ctx != nil {
return (*Context)(ctx)
} else {
return nil