This repository has been archived by the owner on Nov 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclap.go
66 lines (55 loc) · 1.65 KB
/
clap.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
// Copyright 2023 Schibsted. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package concluder
import (
"bytes"
"log"
"math"
"time"
)
func (a *AudioRecorder) ListenForClapSoundToStopRecording(nClaps int, onRecordingStop func()) {
audioLength := 1 * time.Second
loopSleep := 50 * time.Millisecond
// Clap detection parameters
energyThreshold := 5000.0 // Adjust this value based on the sensitivity you want
windowSize := 1024 // Size of the sliding window used for energy calculation
clapCooldown := 400 * time.Millisecond // Time to wait before detecting another clap
clapsDetected := 0
lastClapDetected := time.Now().Add(-clapCooldown)
for a.Recording {
// Create new audio.IntBuffer.
tailData, err := a.GetRecordedDataTail(audioLength)
if err != nil {
log.Println(err)
time.Sleep(loopSleep)
continue
}
audioBuf, err := newAudioIntBuffer(bytes.NewReader(tailData))
if err != nil {
log.Println(err)
time.Sleep(loopSleep)
continue
}
// Analyze audioBuf to see if it contains a clap sound.
numSamples := len(audioBuf.Data)
for i := 0; i < numSamples-windowSize; i++ {
energy := 0.0
for j := 0; j < windowSize; j++ {
energy += math.Abs(float64(audioBuf.Data[i+j]))
}
energy /= float64(windowSize)
if energy > energyThreshold && time.Since(lastClapDetected) > clapCooldown {
log.Println("GOT A CLAP SOUND")
clapsDetected++
lastClapDetected = time.Now()
if clapsDetected >= nClaps {
a.StopRecording()
if onRecordingStop != nil {
onRecordingStop()
}
return
}
}
}
time.Sleep(loopSleep)
}
}