You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
938 B
Go

4 weeks ago
package icsstt
import (
"cloud.google.com/go/speech/apiv1"
"cloud.google.com/go/speech/apiv1/speechpb"
"context"
"log"
"os"
)
func RecognizeSpeech(language, filepath string) error {
ctx := context.Background()
client, err := speech.NewClient(ctx)
if err != nil {
return err
}
defer client.Close()
audioData, err := os.ReadFile(filepath)
if err != nil {
return err
}
req := &speechpb.RecognizeRequest{
Config: &speechpb.RecognitionConfig{
Encoding: speechpb.RecognitionConfig_LINEAR16,
SampleRateHertz: 16000,
LanguageCode: language,
},
Audio: &speechpb.RecognitionAudio{
AudioSource: &speechpb.RecognitionAudio_Content{
Content: audioData,
},
},
}
resp, err := client.Recognize(ctx, req)
if err != nil {
return err
}
for _, result := range resp.Results {
for _, alt := range result.Alternatives {
log.Printf("Transcript: %s\n", alt.Transcript)
}
}
return nil
}