|
| 1 | +package pool |
| 2 | + |
| 3 | +import ( |
| 4 | + |
| 5 | + // Packages |
| 6 | + |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + model "github.com/mutablelogic/go-whisper/pkg/whisper/model" |
| 10 | + "github.com/mutablelogic/go-whisper/sys/whisper" |
| 11 | + |
| 12 | + // Namespace imports |
| 13 | + . "github.com/djthorpe/go-errors" |
| 14 | +) |
| 15 | + |
| 16 | +////////////////////////////////////////////////////////////////////////////// |
| 17 | +// TYPES |
| 18 | + |
| 19 | +// Context is used for running the transcription or translation |
| 20 | +type Context struct { |
| 21 | + Model *model.Model |
| 22 | + |
| 23 | + whisper *whisper.Context |
| 24 | + params whisper.FullParams |
| 25 | +} |
| 26 | + |
| 27 | +////////////////////////////////////////////////////////////////////////////// |
| 28 | +// LIFECYCLE |
| 29 | + |
| 30 | +// Create a new context object |
| 31 | +func New(path string, model *model.Model) (*Context, error) { |
| 32 | + ctx := new(Context) |
| 33 | + |
| 34 | + // Init the context |
| 35 | + if err := ctx.Init(path, model); err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + // Init the transcription with default parameters |
| 40 | + ctx.params = whisper.DefaultFullParams(whisper.SAMPLING_GREEDY) |
| 41 | + |
| 42 | + // Return success |
| 43 | + return ctx, nil |
| 44 | +} |
| 45 | + |
| 46 | +// Init the context |
| 47 | +func (m *Context) Init(path string, model *model.Model) error { |
| 48 | + // Check parameters |
| 49 | + if model == nil { |
| 50 | + return ErrBadParameter |
| 51 | + } |
| 52 | + |
| 53 | + // Get a context |
| 54 | + ctx := whisper.Whisper_init_from_file_with_params(filepath.Join(path, model.Path), whisper.DefaultContextParams()) |
| 55 | + if ctx == nil { |
| 56 | + return ErrInternalAppError.With("whisper_init_from_file_with_params") |
| 57 | + } |
| 58 | + |
| 59 | + // Set resources |
| 60 | + m.whisper = ctx |
| 61 | + m.Model = model |
| 62 | + |
| 63 | + // Return success |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +// Close the context and release all resources |
| 68 | +func (m *Context) Close() error { |
| 69 | + var result error |
| 70 | + |
| 71 | + // Do nothing if nil |
| 72 | + if m == nil { |
| 73 | + return nil |
| 74 | + } |
| 75 | + |
| 76 | + // Release resources |
| 77 | + if m.whisper != nil { |
| 78 | + whisper.Whisper_free(m.whisper) |
| 79 | + } |
| 80 | + m.whisper = nil |
| 81 | + m.Model = nil |
| 82 | + |
| 83 | + // Return any errors |
| 84 | + return result |
| 85 | +} |
| 86 | + |
| 87 | +////////////////////////////////////////////////////////////////////////////// |
| 88 | +// PUBLIC METHODS |
| 89 | + |
| 90 | +// Transcribe samples. The samples should be 16KHz float32 samples in |
| 91 | +// a single channel. |
| 92 | +// TODO: We need a low-latency streaming version of this function. |
| 93 | +// TODO: We need a callback for segment progress. |
| 94 | +func (ctx *Context) Transcribe(samples []float32) error { |
| 95 | + // Perform the transcription |
| 96 | + return whisper.Whisper_full(ctx.whisper, ctx.params, samples) |
| 97 | +} |
0 commit comments