Skip to content

Commit

Permalink
chore: Update audio buffer initialization in realtime analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
tphakala committed May 20, 2024
1 parent 10a5925 commit 87ad3c4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/analysis/realtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func RealtimeAnalysis(settings *conf.Settings) error {
myaudio.InitRingBuffer(bufferSize)

// Audio buffer for extended audio clip capture
audioBuffer := myaudio.NewAudioBuffer(60, conf.SampleRate, bytesPerSample)
audioBuffer := myaudio.NewAudioBuffer(60, conf.SampleRate, conf.BitDepth/8)

// init detection queue
queue.Init(5, 5)
Expand Down
15 changes: 7 additions & 8 deletions internal/myaudio/audiobuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package myaudio

import (
"errors"
"log"
"sync"
"time"

"github.com/tphakala/birdnet-go/internal/conf"
)

// AudioBuffer represents a circular buffer for storing PCM audio data, with timestamp tracking.
Expand All @@ -19,7 +20,7 @@ type AudioBuffer struct {
startTime time.Time
initialized bool
lock sync.Mutex
cond *sync.Cond // Condition variable for signaling when the buffer is full
settings *conf.Settings

Check failure on line 23 in internal/myaudio/audiobuffer.go

View workflow job for this annotation

GitHub Actions / lint

field `settings` is unused (unused)
}

// NewAudioBuffer initializes a new AudioBuffer with timestamp tracking
Expand All @@ -34,7 +35,7 @@ func NewAudioBuffer(durationSeconds int, sampleRate, bytesPerSample int) *AudioB
bufferDuration: time.Second * time.Duration(durationSeconds),
initialized: false,
}
ab.cond = sync.NewCond(&ab.lock) // Initialize the condition variable with the buffer's lock

return ab
}

Expand Down Expand Up @@ -65,9 +66,6 @@ func (ab *AudioBuffer) Write(data []byte) {
ab.startTime = time.Now().Add(-ab.bufferDuration)
//log.Printf("Buffer has wrapped around, adjusting start time to %v", ab.startTime)
}

// Signal any waiting readers that the buffer has been updated.
ab.cond.Broadcast()
}

// ReadSegment extracts a segment of audio data based on precise start and end times, handling wraparounds.
Expand Down Expand Up @@ -104,12 +102,13 @@ func (ab *AudioBuffer) ReadSegment(requestedStartTime time.Time, duration int) (
if time.Now().After(requestedEndTime) {
var segment []byte
if startIndex < endIndex {
log.Printf("Reading segment from %d to %d", startIndex, endIndex)

//log.Printf("Reading segment from %d to %d", startIndex, endIndex)
segmentSize := endIndex - startIndex
segment = make([]byte, segmentSize)
copy(segment, ab.data[startIndex:endIndex])
} else {
log.Printf("Buffer has wrapped, reading segment from %d to %d", startIndex, endIndex)
//log.Printf("Buffer has wrapped, reading segment from %d to %d", startIndex, endIndex)
segmentSize := (ab.bufferSize - startIndex) + endIndex
segment = make([]byte, segmentSize)
firstPartSize := ab.bufferSize - startIndex
Expand Down

0 comments on commit 87ad3c4

Please sign in to comment.