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.

181 lines
3.4 KiB
Go

package icsstt
import (
"math"
"sync"
"batchmodule/icslog"
)
const RR_200MSEC_SIZE = 3200
type RRData struct {
wPos int
wTPos int
wtSize int
rPos int
size int // max size
audio []byte
rtAudio []byte //for GetAudio()
rtAudioLen int
m *sync.Mutex
}
func NewRRData(size int) *RRData {
if size <= 0 {
return nil
}
rr := RRData{wPos: 0, rPos: 0, size: size, rtAudioLen: 0}
rr.m = &sync.Mutex{}
rr.audio = make([]byte, size)
rr.rtAudio = make([]byte, size)
return &rr
}
func (rr *RRData) AddAudio(data []byte) int {
l := icslog.InitializeLogger()
size := len(data)
if size <= 0 || rr.audio == nil {
return -1
}
rr.m.Lock()
if rr.wPos == 0 && rr.wTPos != 0 {
rr.wPos = rr.wtSize
}
if rr.wPos+size > rr.size {
l.Printf("AddAudio> RRData write size exceeded!!! size: %d-%d", rr.size, rr.wPos+size)
rr.wPos = rr.wtSize
//l.Printf(icslog.LOG_LEVEL_DEBUG2, -1, "AddAudio> RRData write size exceeded. wPos reset. %d-%d-%d", rr.wPos, rr.size, rr.wPos+size)
}
//l.Printf(icslog.LOG_LEVEL_DEBUG2, -1, "AddAudio> wPos: %d, size: %d, total size: %d", rr.wPos, size, rr.size)
copy(rr.audio[rr.wPos:], data[:])
rr.wPos += size
if rr.size <= rr.rtAudioLen {
rr.rtAudio = append(rr.rtAudio, data...)
} else {
copy(rr.rtAudio[rr.rtAudioLen:], data)
}
rr.rtAudioLen += size
rr.m.Unlock()
return rr.wPos
}
func (rr *RRData) AddTempAudio(data []byte) int {
//1sec temp buffer
if rr.size < 40000 || rr.audio == nil {
return -1
}
size := len(data)
if size <= 0 {
return -1
}
rr.m.Lock()
//l := icslog.GetIcsLog()
//l.Printf(icslog.LOG_LEVEL_DEBUG2, -1, "AddTempAudio> wtPos: %d, size: %d", rr.wTPos, size)
if rr.wTPos+size > 40000 {
//rr.wTPos = 0
rr.wtSize = 40000
rr.audio = rr.audio[size:]
copy(rr.audio[rr.wTPos-size:], data[:])
} else {
rr.wtSize += size
copy(rr.audio[rr.wTPos:], data[:])
rr.wTPos += size
}
rr.m.Unlock()
return rr.wTPos
}
func (rr *RRData) ReadAudio(size int) (int, []byte) {
l := icslog.InitializeLogger()
if rr.audio == nil {
return -2, nil
}
rr.m.Lock()
if int(math.Abs(float64(rr.wPos-rr.rPos))) < 1600 || int(math.Abs(float64(rr.wPos-rr.rPos))) < size {
rr.m.Unlock()
return 0, nil
}
rData := make([]byte, size)
if rr.size < rr.rPos+size {
rr.m.Unlock()
l.Printf("ReadAudio> RRData read size exceeded!!! size: %d-%d", rr.size, rr.rPos+size)
return -1, nil
//rr.rPos = 0
}
//l.Printf(icslog.LOG_LEVEL_DEBUG2, -1, "ReadAudio> rPos: %d, size: %d, total size: %d", rr.rPos, size, rr.size)
copy(rData[:], rr.audio[rr.rPos:rr.rPos+size])
//fmt.Println("read audio data:", rr.rPos)
rr.rPos += size
rr.m.Unlock()
return rr.rPos, rData
}
func (rr *RRData) Clear() {
rr.m.Lock()
rr.wPos = 0
rr.rPos = 0
rr.audio = nil
rr.rtAudio = nil
rr.m.Unlock()
l := icslog.InitializeLogger()
l.Print("Cleared RRData")
}
func (rr *RRData) GetAudio() []byte {
if rr.audio == nil {
return nil
}
rr.m.Lock()
if rr.rtAudioLen <= 0 {
rr.m.Unlock()
return nil
}
//l.Printf(icslog.LOG_LEVEL_DEBUG2, -1, "GetAudio> audio length: %d", rr.rtAudioLen)
var rData []byte
if rr.wtSize > 0 {
rData = make([]byte, rr.wtSize+rr.rtAudioLen)
copy(rData, rr.audio[:rr.wtSize])
copy(rData[rr.wtSize:], rr.rtAudio)
} else {
rData = make([]byte, rr.rtAudioLen)
copy(rData, rr.rtAudio)
}
rr.m.Unlock()
return rData
}
func (rr *RRData) Close() {
rr.audio = nil
rr.rtAudio = nil
//rr = new(RRData)
}