This commit is contained in:
5684185+vsariola@users.noreply.github.com
2025-12-30 22:43:35 +02:00
parent f765d75fde
commit 2303e89bbd
10 changed files with 330 additions and 110 deletions

View File

@ -12,7 +12,7 @@ type (
broker *Broker
loudnessDetector loudnessDetector
peakDetector peakDetector
chunkHistory sointu.AudioBuffer
chunker chunker
}
WeightingType int
@ -136,26 +136,7 @@ func (s *Detector) handleMsg(msg MsgToDetector) {
switch data := msg.Data.(type) {
case *sointu.AudioBuffer:
buf := *data
for len(buf) > 0 {
var chunk sointu.AudioBuffer
if len(s.chunkHistory) > 0 && len(s.chunkHistory) < 4410 {
l := min(len(buf), 4410-len(s.chunkHistory))
s.chunkHistory = append(s.chunkHistory, buf[:l]...)
if len(s.chunkHistory) < 4410 {
break
}
chunk = s.chunkHistory
buf = buf[l:]
} else {
if len(buf) >= 4410 {
chunk = buf[:4410]
buf = buf[4410:]
} else {
s.chunkHistory = s.chunkHistory[:0]
s.chunkHistory = append(s.chunkHistory, buf...)
break
}
}
s.chunker.Process(buf, 4410, 0, func(chunk sointu.AudioBuffer) {
TrySend(s.broker.ToModel, MsgToModel{
HasDetectorResult: true,
DetectorResult: DetectorResult{
@ -163,7 +144,7 @@ func (s *Detector) handleMsg(msg MsgToDetector) {
Peaks: s.peakDetector.update(chunk),
},
})
}
})
s.broker.PutAudioBuffer(data)
}
}
@ -438,20 +419,13 @@ func (d *peakDetector) reset() {
}
}
func (c *chunker) Process(input sointu.AudioBuffer, windowLength, overlap int, cb func(sointu.AudioBuffer)) sointu.AudioBuffer {
func (c *chunker) Process(input sointu.AudioBuffer, windowLen, overlap int, cb func(sointu.AudioBuffer)) {
c.buffer = append(c.buffer, input...)
b := c.buffer
for len(b) >= windowLength {
cb(b[:windowLength])
b = b[windowLength-overlap:]
for len(b) >= windowLen {
cb(b[:windowLen])
b = b[windowLen-overlap:]
}
copy(c.buffer, b)
c.buffer = c.buffer[:len(b)]
for {
if len(c.buffer) > 0 {
l := min(len(input), windowLength-len(c.buffer))
c.buffer = append(c.buffer, input[:l]...)
input = input[l:]
}
}
}