Some checks failed
CodeQL / Analyze (go) (push) Successful in 6m28s
Docker Image / build-docker (push) Failing after 13m26s
Lint and Testing / lint (push) Successful in 11m17s
Lint and Testing / test (push) Successful in 11m17s
Lint and Testing / golangci (push) Successful in 2m40s
86 lines
2.0 KiB
Go
86 lines
2.0 KiB
Go
package transcode
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/go-audio/wav"
|
|
)
|
|
|
|
func samplePath(t *testing.T, name string) string {
|
|
t.Helper()
|
|
p := filepath.Join("..", "third_party", "whisper.cpp", "samples", name)
|
|
if _, err := os.Stat(p); err != nil {
|
|
t.Skip("sample not found:", p)
|
|
}
|
|
return p
|
|
}
|
|
|
|
func TestToWhisperWAV_mp3(t *testing.T) {
|
|
src := samplePath(t, "jfk.mp3")
|
|
dst := filepath.Join(t.TempDir(), "out.wav")
|
|
if err := ToWhisperWAV(context.Background(), src, dst); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertWhisperWAV(t, dst)
|
|
}
|
|
|
|
func TestToWhisperWAV_wav(t *testing.T) {
|
|
src := samplePath(t, "jfk.wav")
|
|
dst := filepath.Join(t.TempDir(), "out.wav")
|
|
if err := ToWhisperWAV(context.Background(), src, dst); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertWhisperWAV(t, dst)
|
|
}
|
|
|
|
func TestResolveFormat_noExtension_mp3(t *testing.T) {
|
|
src := samplePath(t, "jfk.mp3")
|
|
data, err := os.ReadFile(src)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(t.TempDir(), "upload")
|
|
if err := os.WriteFile(path, data, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ext, err := resolveFormat(path)
|
|
if err != nil || ext != ".mp3" {
|
|
t.Fatalf("ext=%q err=%v", ext, err)
|
|
}
|
|
}
|
|
|
|
func TestEngine_Available(t *testing.T) {
|
|
if err := NewEngine("").Available(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func assertWhisperWAV(t *testing.T, path string) {
|
|
t.Helper()
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer f.Close()
|
|
dec := wav.NewDecoder(f)
|
|
if !dec.IsValidFile() {
|
|
t.Fatal("invalid wav")
|
|
}
|
|
buf, err := dec.FullPCMBuffer()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if dec.SampleRate != 16000 {
|
|
t.Fatalf("sample rate %d", dec.SampleRate)
|
|
}
|
|
if dec.NumChans != 1 {
|
|
t.Fatalf("channels %d", dec.NumChans)
|
|
}
|
|
if len(buf.Data) == 0 {
|
|
t.Fatal("empty audio")
|
|
}
|
|
}
|