Skip to content

Commit

Permalink
beta40 (#136)
Browse files Browse the repository at this point in the history
* beta40

* beta40

* add record support
  • Loading branch information
Hoshinonyaruko authored Nov 18, 2023
1 parent 72f1d7b commit bb259cb
Show file tree
Hide file tree
Showing 14 changed files with 356 additions and 123 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ channel_temp/
log/

# ignore dist
webui/dist/
webui/dist/

#ignore data
data/
308 changes: 221 additions & 87 deletions go-silk/silk.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,116 +182,250 @@ func EncodePcmBuffToSilk(src []byte, sampleRate, bitRate int, tencent bool) (dst
_ = binary.Write(&out, binary.LittleEndian, nBytes)
_, _ = out.Write(payload[:nBytes])
}
if !tencent {
_ = binary.Write(&out, binary.LittleEndian, int16(-1))
}
// if !tencent {
// _ = binary.Write(&out, binary.LittleEndian, int16(-1))
// }

dst = out.Bytes()
fmt.Printf("dst 长度: %d\n", len(dst))

return
}

// func EncodePcmBuffToSilkv2(src []byte, sampleRate, bitRate int, tencent bool) (dst []byte, err error) {
// var tls = libc.NewTLS()
// var reader = bytes.NewBuffer(src)
// var encControl sdk.SDK_EncControlStruct
// var encStatus sdk.SDK_EncControlStruct
// var packetSizeMs = int32(20)
// { // default setting
// encControl.FAPI_sampleRate = int32(sampleRate)
// encControl.FmaxInternalSampleRate = 24000
// encControl.FpacketSize = (packetSizeMs * int32(sampleRate)) / 1000
// encControl.FpacketLossPercentage = int32(0)
// encControl.FuseInBandFEC = 0
// encControl.FuseDTX = 0
// encControl.Fcomplexity = 2
// encControl.FbitRate = int32(bitRate)
// }
// var encSizeBytes int32
// ret := sdk.SDK_Get_Encoder_Size(tls, uintptr(unsafe.Pointer(&encSizeBytes)))
// if ret != 0 {
// return nil, fmt.Errorf("SKP_Silk_create_encoder returned %d", ret)
// }
// psEnc := libc.Xmalloc(tls, types.Size_t(encSizeBytes))
// defer libc.Xfree(tls, psEnc)
// ret = sdk.SDK_InitEncoder(tls, psEnc, uintptr(unsafe.Pointer(&encStatus)))
// if ret != 0 {
// return nil, fmt.Errorf("SKP_Silk_reset_encoder returned %d", ret)
// }
// var frameSize = sampleRate / 1000 * 40
// fmt.Printf("包长:%v", frameSize)
// var (
// nBytes = int16(250 * 5)
// in = make([]byte, frameSize)
// payload = make([]byte, nBytes)
// out = bytes.Buffer{}
// totPackets, sumBytes, sumActBytes, totActPackets = 0, 0, 0, 0
// smplsSinceLastPacket = 0
// )
// if tencent {
// _, _ = out.Write([]byte("\x02#!SILK_V3"))
// } else {
// _, _ = out.Write([]byte("#!SILK_V3"))
// }
// var counter int
// for {
// counter, err = reader.Read(in)
// if err != nil {
// if err == io.EOF {
// err = nil
// break
// }
// return
// }
// if counter < frameSize {
// break
// }
// nBytes = int16(1250)
// ret = sdk.SDK_Encode(
// tls,
// psEnc,
// uintptr(unsafe.Pointer(&encControl)),
// uintptr(unsafe.Pointer(&in[0])),
// int32(counter)/2,
// uintptr(unsafe.Pointer(&payload[0])),
// uintptr(unsafe.Pointer(&nBytes)),
// )

// if ret != 0 {
// return nil, fmt.Errorf("SKP_Silk_Encode returned %d", ret)
// }

// packetSizeMs = (1000 * int32(encControl.FpacketSize)) / encControl.FAPI_sampleRate
// smplsSinceLastPacket += counter

// if (1000 * smplsSinceLastPacket / sampleRate) == int(packetSizeMs) {
// totPackets++
// sumBytes += int(nBytes)
// nrg := 0.0
// for k := 0; k < counter; k++ {
// nrg += float64(in[k]) * float64(in[k])
// }
// if (nrg / float64(counter)) > 1e3 {
// sumActBytes += int(nBytes)
// totActPackets++
// }

// if !tencent {
// _ = binary.Write(&out, binary.BigEndian, nBytes)
// } else {
// _ = binary.Write(&out, binary.LittleEndian, nBytes)
// }
// _, _ = out.Write(payload[:nBytes])
// smplsSinceLastPacket = 0
// }
// }
// if !tencent {
// _ = binary.Write(&out, binary.LittleEndian, int16(-1))
// }
// dst = out.Bytes()
// return
// }

// Assuming necessary imports and definitions

func EncodePcmBuffToSilkv2(src []byte, sampleRate, bitRate int, tencent, bigEndian bool, complexityMode int) (dst []byte, err error) {
var tls = libc.NewTLS()
var reader = bytes.NewBuffer(src)
var encControl sdk.SDK_EncControlStruct
var encStatus sdk.SDK_EncControlStruct
var packetSizeMs = int32(20)
var frameSizeReadFromFileMs = int32(20)
// func EncodePcmBuffToSilkv2(src []byte, sampleRate, bitRate int, tencent, bigEndian bool, complexityMode int) (dst []byte, err error) {
// var tls = libc.NewTLS()
// var reader = bytes.NewBuffer(src)
// var encControl sdk.SDK_EncControlStruct
// var encStatus sdk.SDK_EncControlStruct
// var packetSizeMs = int32(20)
// var frameSizeReadFromFileMs = int32(20)

// Setting based on the complexity mode (could be passed as a parameter)
encControl.Fcomplexity = int32(complexityMode)
// // Setting based on the complexity mode (could be passed as a parameter)
// encControl.Fcomplexity = int32(complexityMode)

// Default settings
encControl.FAPI_sampleRate = int32(sampleRate)
encControl.FmaxInternalSampleRate = 24000
encControl.FpacketSize = (packetSizeMs * int32(sampleRate)) / 1000
encControl.FpacketLossPercentage = int32(0)
encControl.FuseInBandFEC = 0
encControl.FuseDTX = 0
encControl.FbitRate = int32(bitRate)
// // Default settings
// encControl.FAPI_sampleRate = int32(sampleRate)
// encControl.FpacketSize = (packetSizeMs * int32(sampleRate)) / 1000
// encControl.FpacketLossPercentage = int32(0)
// encControl.FuseInBandFEC = 0
// encControl.FuseDTX = 0
// encControl.FbitRate = int32(bitRate)

// Create Encoder
var encSizeBytes int32
ret := sdk.SDK_Get_Encoder_Size(tls, uintptr(unsafe.Pointer(&encSizeBytes)))
if ret != 0 {
return nil, fmt.Errorf("SKP_Silk_create_encoder returned %d", ret)
}
// // Set max internal sample rate
// maxInternalFsHz := int32(24000)
// if encControl.FAPI_sampleRate < maxInternalFsHz {
// maxInternalFsHz = encControl.FAPI_sampleRate
// }
// encControl.FmaxInternalSampleRate = maxInternalFsHz

// Memory management
psEnc := libc.Xmalloc(tls, types.Size_t(encSizeBytes))
defer libc.Xfree(tls, psEnc)
// // Create Encoder
// var encSizeBytes int32
// ret := sdk.SDK_Get_Encoder_Size(tls, uintptr(unsafe.Pointer(&encSizeBytes)))
// if ret != 0 {
// return nil, fmt.Errorf("SKP_Silk_create_encoder returned %d", ret)
// }

// Reset Encoder
ret = sdk.SDK_InitEncoder(tls, psEnc, uintptr(unsafe.Pointer(&encStatus)))
if ret != 0 {
return nil, fmt.Errorf("SKP_Silk_reset_encoder returned %d", ret)
}
// // Memory management
// psEnc := libc.Xmalloc(tls, types.Size_t(encSizeBytes))
// defer libc.Xfree(tls, psEnc)

var (
nBytes = int16(250 * 5)
in = make([]byte, frameSizeReadFromFileMs*int32(sampleRate)/1000)
payload = make([]byte, nBytes)
out = bytes.Buffer{}
)
// // Reset Encoder
// ret = sdk.SDK_InitEncoder(tls, psEnc, uintptr(unsafe.Pointer(&encStatus)))
// if ret != 0 {
// return nil, fmt.Errorf("SKP_Silk_reset_encoder returned %d", ret)
// }

// Add Silk header to stream
if tencent {
_, _ = out.Write([]byte("\x02#!SILK_V3"))
} else {
_, _ = out.Write([]byte("#!SILK_V3"))
}
// var (
// nBytes = int16(250 * 5)
// in = make([]byte, frameSizeReadFromFileMs*int32(sampleRate)/1000)
// payload = make([]byte, nBytes)
// out = bytes.Buffer{}
// )

// Encoding loop
var counter int
for {
counter, err = reader.Read(in)
if err != nil {
if err == io.EOF {
err = nil
break
}
return
}
// // Add Silk header to stream
// if tencent {
// _, _ = out.Write([]byte("\x02#!SILK_V3"))
// } else {
// _, _ = out.Write([]byte("#!SILK_V3"))
// }

if bigEndian {
SwapEndian(in)
}
// // Encoding loop
// var counter int
// var smplsSinceLastPacket int
// for {
// counter, err = reader.Read(in)
// if err != nil {
// if err == io.EOF {
// err = nil
// break
// }
// return
// }

nBytes = int16(1250)
ret = sdk.SDK_Encode(
tls,
psEnc,
uintptr(unsafe.Pointer(&encControl)),
uintptr(unsafe.Pointer(&in[0])),
int32(counter)/2,
uintptr(unsafe.Pointer(&payload[0])),
uintptr(unsafe.Pointer(&nBytes)),
)
// // Fill the rest of the buffer with zeros if necessary
// if counter < len(in) {
// for i := counter; i < len(in); i++ {
// in[i] = 0
// }
// }

if ret != 0 {
return nil, fmt.Errorf("SKP_Silk_Encode returned %d", ret)
}
// // Swap bytes if the system is big-endian
// if bigEndian {
// SwapEndian(in, counter)
// }

_ = binary.Write(&out, binary.LittleEndian, nBytes)
_, _ = out.Write(payload[:nBytes])
}
// //log.Printf("Before encoding: nBytes = %d, counter = %d", nBytes, counter)
// //log.Printf("encControl: %+v", encControl)
// nBytes = 1250
// ret = sdk.SDK_Encode(
// tls,
// psEnc,
// uintptr(unsafe.Pointer(&encControl)),
// uintptr(unsafe.Pointer(&in[0])),
// int32(counter)/2,
// uintptr(unsafe.Pointer(&payload[0])),
// uintptr(unsafe.Pointer(&nBytes)),
// )
// if ret != 0 {
// return nil, fmt.Errorf("SKP_Silk_Encode returned %d", ret)
// }
// // Packet handling logic
// packetSizeMs := (1000 * int(encControl.FpacketSize)) / int(encControl.FAPI_sampleRate)
// smplsSinceLastPacket += counter
// if ((1000 * smplsSinceLastPacket) / int(sampleRate)) == packetSizeMs {
// // Swap bytes if the system is big-endian
// if bigEndian {
// var buf [2]byte
// binary.BigEndian.PutUint16(buf[:], uint16(nBytes))
// nBytes = int16(binary.LittleEndian.Uint16(buf[:]))
// }

if !tencent {
_ = binary.Write(&out, binary.LittleEndian, int16(-1))
}
// // Write the payload
// _ = binary.Write(&out, binary.LittleEndian, nBytes)
// _, _ = out.Write(payload[:nBytes])
// smplsSinceLastPacket = 0
// }
// }

dst = out.Bytes()
return
}
// if !tencent {
// _ = binary.Write(&out, binary.LittleEndian, int16(-1))
// }

// dst = out.Bytes()
// return

func SwapEndian(data []byte) {
if len(data)%2 != 0 {
panic("SwapEndian requires an even length byte slice")
// }

func SwapEndian(data []byte, length int) {
if length%2 != 0 {
// The length should be even, as we are swapping 16-bit values.
panic("length of data for SwapEndian should be even")
}

for i := 0; i < len(data); i += 2 {
for i := 0; i < length; i += 2 {
data[i], data[i+1] = data[i+1], data[i]
}
}

2 changes: 1 addition & 1 deletion handlers/send_group_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func handleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap
for _, url := range urls {
var singleItem = make(map[string][]string)
singleItem[key] = []string{url} // 创建一个只包含一个 URL 的 singleItem
mylog.Println("singleItem:", singleItem)
//mylog.Println("singleItem:", singleItem)
msgseq := echo.GetMappingSeq(messageID)
echo.AddMappingSeq(messageID, msgseq+1)
groupReply := generateGroupMessage(messageID, singleItem, "", msgseq+1)
Expand Down
Binary file added silk/exec/silk_codec-android-arm64
Binary file not shown.
Binary file added silk/exec/silk_codec-android-x86
Binary file not shown.
Binary file added silk/exec/silk_codec-android-x86_64
Binary file not shown.
Binary file added silk/exec/silk_codec-linux-arm64
Binary file not shown.
Binary file added silk/exec/silk_codec-linux-x64
Binary file not shown.
Binary file added silk/exec/silk_codec-macos
Binary file not shown.
Loading

0 comments on commit bb259cb

Please sign in to comment.