Skip to content

Commit de6b38c

Browse files
bindings.go : add DetectedLanguage to go bindings (#2947)
Adding in DetectedLanguage(), a function to retrieve the detected language that's populated by processing audio. Also adding in a unit test to test the success. Co-authored-by: Amanda Der Bedrosian <[email protected]>
1 parent 46d6e0a commit de6b38c

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

bindings/go/pkg/whisper/context.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func (context *context) Language() string {
7171
return whisper.Whisper_lang_str(context.params.Language())
7272
}
7373

74+
func (context *context) DetectedLanguage() string {
75+
return whisper.Whisper_lang_str(context.model.ctx.Whisper_full_lang_id())
76+
}
77+
7478
// Set translate flag
7579
func (context *context) SetTranslate(v bool) {
7680
context.params.SetTranslate(v)

bindings/go/pkg/whisper/context_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,34 @@ func TestProcess(t *testing.T) {
9191
err = context.Process(data, nil, nil, nil)
9292
assert.NoError(err)
9393
}
94+
95+
func TestDetectedLanguage(t *testing.T) {
96+
assert := assert.New(t)
97+
98+
fh, err := os.Open(SamplePath)
99+
assert.NoError(err)
100+
defer fh.Close()
101+
102+
// Decode the WAV file - load the full buffer
103+
dec := wav.NewDecoder(fh)
104+
buf, err := dec.FullPCMBuffer()
105+
assert.NoError(err)
106+
assert.Equal(uint16(1), dec.NumChans)
107+
108+
data := buf.AsFloat32Buffer().Data
109+
110+
model, err := whisper.New(ModelPath)
111+
assert.NoError(err)
112+
assert.NotNil(model)
113+
defer model.Close()
114+
115+
context, err := model.NewContext()
116+
assert.NoError(err)
117+
118+
err = context.Process(data, nil, nil, nil)
119+
assert.NoError(err)
120+
121+
expectedLanguage := "en"
122+
actualLanguage := context.DetectedLanguage()
123+
assert.Equal(expectedLanguage, actualLanguage)
124+
}

bindings/go/pkg/whisper/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Context interface {
4141
SetTranslate(bool) // Set translate flag
4242
IsMultilingual() bool // Return true if the model is multilingual.
4343
Language() string // Get language
44+
DetectedLanguage() string // Get detected language
4445

4546
SetOffset(time.Duration) // Set offset
4647
SetDuration(time.Duration) // Set duration

0 commit comments

Comments
 (0)