-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcs2-voice-data.go
170 lines (134 loc) · 3.54 KB
/
cs2-voice-data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"CS2VoiceData/decoder"
"fmt"
"log"
"os"
"strconv"
"github.com/go-audio/audio"
"github.com/go-audio/wav"
dem "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs"
"github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs/msgs2"
)
func main() {
// Create a map of a users to voice data.
// Each chunk of voice data is a slice of bytes, store all those slices in a grouped slice.
var voiceDataPerPlayer = map[string][][]byte{}
// The file path to an unzipped demo file.
file, err := os.Open("1-34428882-6181-4c75-a24b-4982764122e2.dem")
if err != nil {
log.Fatal("Failed to open demo file")
}
defer file.Close()
parser := dem.NewParser(file)
var format string
// Add a parser register for the VoiceData net message.
parser.RegisterNetMessageHandler(func(m *msgs2.CSVCMsg_VoiceData) {
// Get the users Steam ID 64.
steamId := strconv.Itoa(int(m.GetXuid()))
// Append voice data to map
format = m.Audio.Format.String()
voiceDataPerPlayer[steamId] = append(voiceDataPerPlayer[steamId], m.Audio.VoiceData)
})
// Parse the full demo file.
err = parser.ParseToEnd()
// For each users data, create a wav file containing their voice comms.
for playerId, voiceData := range voiceDataPerPlayer {
wavFilePath := fmt.Sprintf("%s.wav", playerId)
if format == "VOICEDATA_FORMAT_OPUS" {
err = opusToWav(voiceData, wavFilePath)
if err != nil {
fmt.Println(err)
continue
}
} else if format == "VOICEDATA_FORMAT_STEAM" {
convertAudioDataToWavFiles(voiceData, wavFilePath)
}
}
defer parser.Close()
}
func convertAudioDataToWavFiles(payloads [][]byte, fileName string) {
// This sample rate can be set using data from the VoiceData net message.
// But every demo processed has used 24000 and is single channel.
voiceDecoder, err := decoder.NewOpusDecoder(24000, 1)
if err != nil {
fmt.Println(err)
}
o := make([]int, 0, 1024)
for _, payload := range payloads {
c, err := decoder.DecodeChunk(payload)
if err != nil {
fmt.Println(err)
}
// Not silent frame
if c != nil && len(c.Data) > 0 {
pcm, err := voiceDecoder.Decode(c.Data)
if err != nil {
fmt.Println(err)
}
converted := make([]int, len(pcm))
for i, v := range pcm {
// Float32 buffer implementation is wrong in go-audio, so we have to convert to int before encoding
converted[i] = int(v * 2147483647)
}
o = append(o, converted...)
}
}
outFile, err := os.Create(fileName)
if err != nil {
fmt.Println(err)
}
defer outFile.Close()
// Encode new wav file, from decoded opus data.
enc := wav.NewEncoder(outFile, 24000, 32, 1, 1)
buf := &audio.IntBuffer{
Data: o,
Format: &audio.Format{
SampleRate: 24000,
NumChannels: 1,
},
}
// Write voice data to the file.
if err := enc.Write(buf); err != nil {
fmt.Println(err)
}
enc.Close()
}
func opusToWav(data [][]byte, wavName string) (err error) {
opusDecoder, err := decoder.NewDecoder(48000, 1)
if err != nil {
return
}
var pcmBuffer []int
for _, d := range data {
pcm, err := decoder.Decode(opusDecoder, d)
if err != nil {
log.Println(err)
continue
}
pp := make([]int, len(pcm))
for i, p := range pcm {
pp[i] = int(p * 2147483647)
}
pcmBuffer = append(pcmBuffer, pp...)
}
file, err := os.Create(wavName)
if err != nil {
return
}
defer file.Close()
enc := wav.NewEncoder(file, 48000, 32, 1, 1)
defer enc.Close()
buffer := &audio.IntBuffer{
Data: pcmBuffer,
Format: &audio.Format{
SampleRate: 48000,
NumChannels: 1,
},
}
err = enc.Write(buffer)
if err != nil {
return
}
return
}