-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamer-client.go
168 lines (141 loc) · 4.35 KB
/
streamer-client.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
package main
import (
"bufio"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
)
const (
recordingDir = "./recordings/"
filePrefix = "Jump-"
fileExtension = ".mp3"
)
// StartRecording starts the recording process based on ControlRecording signal
func StartRecording() {
for <-ControlRecording {
currentTime := time.Now().Format("2006-01-02_15:04:05")
filename := filePrefix + currentTime + fileExtension
CurrentRecordingFilename = filename
filename = recordingDir + filename
radioMP3URL, err := getRadioURLFromM3U(StreamURL)
if err != nil {
log.Fatalf("Error getting radio URL: %v", err)
}
log.Printf("Parsed radio url %v", radioMP3URL)
log.Printf("Started recording at %v", TimeStartedRecording)
err = streamDownloadAndCompare(radioMP3URL, filename)
if err != nil {
log.Fatalf("Error during stream download: %v", err)
}
}
}
// getRadioURLFromM3U retrieves the actual radio URL from the M3U file
func getRadioURLFromM3U(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("HTTP request failed with status code: %d", resp.StatusCode)
}
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
if len(line) > 0 && (line[0] == 'h' || line[0] == 'H') {
return line, nil
}
}
if err := scanner.Err(); err != nil {
return "", err
}
return "", fmt.Errorf("no URL found in the M3U file")
}
func streamDownloadAndCompare(url, filePath string) error {
// Observeration: during mix restarts there is about a 3 second time without any sound
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
log.Printf("Followed Redirect to %v", req.URL)
return nil // Follow all redirects
},
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
initialSegment := make([]byte, 0, 20*1024*1024) // 20MB buffer for the first 10 seconds
currentChunk := make([]byte, 1024)
rollingSeconds := make([]byte, 0, 20*1024*1024) // 20MB buffer for rolling seconds
finishedSnippetRecording := false
rollingSecondsFull := false
var initialFingerprint []int32
for IsRecording {
n, err := resp.Body.Read(currentChunk)
if err != nil && err != io.EOF {
return err
}
if n == 0 {
IsRecording = false
break
}
log.Printf("Working on chunk ending at byte %v", n)
fmt.Println("Time:", uint(int(ComparisonSnippetTime.Seconds())))
if !finishedSnippetRecording {
initialSegment = append(initialSegment, currentChunk[:n]...)
if time.Since(TimeStartedRecording).Seconds() >= ComparisonSnippetTime.Seconds() { // Since we're adding currentChunk[:n]s every 100ms
finishedSnippetRecording = true
log.Println("Finished recording comparison segment!")
// calculate fingerprint only once to save performance
initialFingerprint = calculateFingerprint(initialSegment, targetSampleRate, int(ComparisonSnippetTime.Seconds()))
}
} else {
rollingSeconds = append(rollingSeconds, currentChunk[:n]...)
if len(rollingSeconds) > len(initialSegment) {
rollingSeconds = rollingSeconds[len(rollingSeconds)-len(initialSegment):]
log.Printf("\nRolling seconds full.\n")
rollingSecondsFull = true
} else {
rollingSecondsFull = false
}
if rollingSecondsFull {
rollingSecondsFP := calculateFingerprint(rollingSeconds, targetSampleRate, int(ComparisonSnippetTime.Seconds()))
if compareSongs(initialFingerprint, rollingSecondsFP, SimilarityThreshold) { // Seems to be slow // maybe somehow make it multi threaded
log.Printf("\nSimilar Segment found, stopping recording!")
IsRecording = false
TimeStoppedRecording = time.Now()
break
}
}
log.Printf("\nToMuchLength: %v", len(rollingSeconds)-len(initialSegment)) //Debug logging
}
log.Printf("\n")
if _, err := file.Write(currentChunk[:n]); err != nil {
return err
}
}
return nil
}
func startRecording() {
TimeStartedRecording = time.Now()
TimeStoppedRecording = time.Time{} // reset it to be empty
IsRecording = true
ControlRecording <- true
}
func stopRecording() {
TimeStoppedRecording = time.Now()
IsRecording = false
ControlRecording <- false
}