-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (114 loc) · 3.51 KB
/
main.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
package main
// C code courtesy of https://gist.github.com/ericek111/abe5829f6e52e4b25b3b97a0efd0b22b
/*
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/time.h>
#include <celt/celt.h>
#define BUF_SIZE 1024*1024
int raw2celt(char* in, char* out) {
unsigned char buf[BUF_SIZE];
const unsigned int FRAME_SIZE = 512;
const unsigned int SAMPLE_RATE = 22050;
FILE *f = fopen(in, "r");
if (f == NULL) {
return 1;
}
size_t read = fread(buf, 1, BUF_SIZE, f);
fclose(f);
CELTMode *dm = celt_mode_create(SAMPLE_RATE, FRAME_SIZE, NULL);
CELTDecoder *d = celt_decoder_create_custom(dm, 1, NULL);
size_t outsize = (read / 64) * FRAME_SIZE * sizeof(celt_int16);
celt_int16* pcmout = malloc(outsize);
size_t done = 0;
int frames = 0;
for (; done < read; done += 64, frames++) {
int ret = 0;
if ((ret = celt_decode(d, buf + done, 64, pcmout + frames * FRAME_SIZE, FRAME_SIZE)) < 0) {
fprintf(stderr, "unable to decode... > %d (at %d/%d)\n", ret, done, read);
return 1;
}
}
FILE* file_p = fopen(out, "w");
size_t written = fwrite(pcmout, outsize, 1, file_p);
fclose(file_p);
free(pcmout);
return 0;
}
*/
import "C"
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"google.golang.org/protobuf/proto"
ex "github.com/markus-wa/demoinfocs-golang/v3/examples"
demoinfocs "github.com/markus-wa/demoinfocs-golang/v3/pkg/demoinfocs"
"github.com/markus-wa/demoinfocs-golang/v3/pkg/demoinfocs/msg"
)
// Run like this: go run capture_voice.go -demo /path/to/demo.dem
func main() {
f, err := os.Open(ex.DemoPathFromArgs())
checkError(err)
defer f.Close()
p := demoinfocs.NewParserWithConfig(f, demoinfocs.ParserConfig{
MsgQueueBufferSize: -1,
AdditionalNetMessageCreators: map[int]demoinfocs.NetMessageCreator{
int(msg.SVC_Messages_svc_VoiceInit): func() proto.Message { return &msg.CSVCMsg_VoiceInit{} },
int(msg.SVC_Messages_svc_VoiceData): func() proto.Message { return &msg.CSVCMsg_VoiceData{} },
},
IgnoreErrBombsiteIndexNotFound: false,
NetMessageDecryptionKey: nil,
})
defer p.Close()
outFilesBySectionNr := make(map[uint32]*os.File)
defer func() {
for _, f := range outFilesBySectionNr {
f.Close()
}
}()
p.RegisterNetMessageHandler(func(msg *msg.CSVCMsg_VoiceInit) {
fmt.Println("init:", msg.String())
})
var sections []string
p.RegisterNetMessageHandler(func(msg *msg.CSVCMsg_VoiceData) {
sectionNr := msg.GetSectionNumber()
outFile, ok := outFilesBySectionNr[sectionNr]
if !ok {
sections = append(sections, strconv.Itoa(int(sectionNr)))
outFile, err = os.Create(fmt.Sprintf("out/%d.raw", sectionNr))
checkError(err)
outFilesBySectionNr[sectionNr] = outFile
}
_, err := outFile.Write(msg.GetVoiceData())
checkError(err)
msg.VoiceData = nil
})
err = p.ParseToEnd()
checkError(err)
for _, rawFile := range outFilesBySectionNr {
rawFileName := rawFile.Name()
celtFileName := strings.TrimSuffix(rawFileName, filepath.Ext(rawFileName)) + ".celt"
err = rawFile.Close()
checkError(err)
res := C.raw2celt(C.CString(rawFileName), C.CString(celtFileName))
if res != 0 {
panic("raw2celt failed")
}
}
fmt.Println()
fmt.Printf("order:\n\t%s\n", strings.Join(sections, "\n\t"))
fmt.Println("saved voice chat audio to out/*.celt")
fmt.Println("play via: play -t raw -r 22050 -e signed -b 16 -c 1 out/1.celt")
fmt.Println("or convert to .wav via: sox -t raw -r 22050 -e signed -b 16 -c 1 out/1.celt out/1.wav")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}