MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / parseAudio

Function parseAudio

service/audio.go:9–32  ·  view source on GitHub ↗
(audioBase64 string, format string)

Source from the content-addressed store, hash-verified

7)
8
9func parseAudio(audioBase64 string, format string) (duration float64, err error) {
10 audioData, err := base64.StdEncoding.DecodeString(audioBase64)
11 if err != nil {
12 return 0, fmt.Errorf("base64 decode error: %v", err)
13 }
14
15 var samplesCount int
16 var sampleRate int
17
18 switch format {
19 case "pcm16":
20 samplesCount = len(audioData) / 2 // 16位 = 2字节每样本
21 sampleRate = 24000 // 24kHz
22 case "g711_ulaw", "g711_alaw":
23 samplesCount = len(audioData) // 8位 = 1字节每样本
24 sampleRate = 8000 // 8kHz
25 default:
26 samplesCount = len(audioData) // 8位 = 1字节每样本
27 sampleRate = 8000 // 8kHz
28 }
29
30 duration = float64(samplesCount) / float64(sampleRate)
31 return duration, nil
32}
33
34func DecodeBase64AudioData(audioBase64 string) (string, error) {
35 // 检查并移除 data:audio/xxx;base64, 前缀

Callers 2

CountAudioTokenInputFunction · 0.85
CountAudioTokenOutputFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected