1
0
mirror of https://github.com/fumiama/ahsai.git synced 2026-06-05 07:40:23 +08:00

优化cut

This commit is contained in:
fumiama
2022-08-07 16:40:09 +08:00
parent 11ac7b0d6e
commit 7891a6ed4c
3 changed files with 37 additions and 23 deletions

View File

@@ -17,7 +17,7 @@ func TestAPI(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = SaveOggToWav(u, "test.wav") err = SaveOggToFile(u, "test.wav")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

58
ogg.go
View File

@@ -1,6 +1,7 @@
package ahsai package ahsai
import ( import (
"io"
"net/http" "net/http"
"os" "os"
"time" "time"
@@ -11,7 +12,25 @@ import (
"github.com/faiface/beep/wav" "github.com/faiface/beep/wav"
) )
func SaveOggToWav(u, path string) error { func cutstream(s beep.StreamSeekCloser) {
tmp := make([][2]float64, 1024)
c := 0
for c < 6 {
_, _ = s.Stream(tmp)
sum := (tmp[0][0] + tmp[0][1]) / 2
for j := 1; j < 1024; j++ {
sum += (tmp[j][0] + tmp[j][1]) / 2
sum /= 2
}
if sum < 1e-32 && sum > -1e-32 {
c++
} else {
c = 0
}
}
}
func SaveOggToFile(u, path string) error {
resp, err := http.Get(u) resp, err := http.Get(u)
if err != nil { if err != nil {
return err return err
@@ -27,17 +46,22 @@ func SaveOggToWav(u, path string) error {
return err return err
} }
defer f.Close() defer f.Close()
tmp := make([][2]float64, 1024) cutstream(s)
_, _ = s.Stream(tmp) return wav.Encode(f, s, format)
sum := (tmp[0][0] + tmp[0][1]) / 2 }
for sum > 1e-32 || sum < -1e-32 {
_, _ = s.Stream(tmp) func SaveOggToWriteSeeker(u string, f io.WriteSeeker) error {
sum = (tmp[0][0] + tmp[0][1]) / 2 resp, err := http.Get(u)
for j := 1; j < 1024; j++ { if err != nil {
sum += (tmp[j][0] + tmp[j][1]) / 2 return err
sum /= 2
}
} }
s, format, err := vorbis.Decode(resp.Body)
if err != nil {
resp.Body.Close()
return err
}
defer s.Close()
cutstream(s)
return wav.Encode(f, s, format) return wav.Encode(f, s, format)
} }
@@ -52,17 +76,7 @@ func PlayOgg(u string) error {
return err return err
} }
defer s.Close() defer s.Close()
tmp := make([][2]float64, 1024) cutstream(s)
_, _ = s.Stream(tmp)
sum := (tmp[0][0] + tmp[0][1]) / 2
for sum > 1e-32 || sum < -1e-32 {
_, _ = s.Stream(tmp)
sum = (tmp[0][0] + tmp[0][1]) / 2
for j := 1; j < 1024; j++ {
sum += (tmp[j][0] + tmp[j][1]) / 2
sum /= 2
}
}
err = speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/32)) err = speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/32))
if err != nil { if err != nil {
return err return err

BIN
test.wav

Binary file not shown.