Skip to content

Commit

Permalink
feat(tools/audiostream): add AudioStream interface and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nullswan committed Nov 9, 2024
1 parent bcb8fca commit 2187556
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions internal/tools/audiostream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package tools

import (
"fmt"
"log/slog"

"github.com/gordonklaus/portaudio"
"github.com/nullswan/nomi/internal/audio"
)

type AudioStream interface {
Start() error
Close() error
}

type audioStream struct {
stream *audio.StreamHandler
}

func (a *audioStream) Close() error {
err := a.stream.Stop()
if err != nil {
return fmt.Errorf("failed to stop audio stream: %w", err)
}
return nil
}

func (a *audioStream) Start() error {
err := a.stream.Start()
if err != nil {
return fmt.Errorf("failed to start audio stream: %w", err)
}
return nil
}

func NewAudioStream(
logger *slog.Logger,
device *portaudio.DeviceInfo,
callback func([]float32), // TODO(nullswan): Make registrable instead of passed
) (AudioStream, error) {
stream, err := audio.NewInputStream(
logger,
device,
&audio.StreamParameters{},
callback,
)
if err != nil {
return nil, fmt.Errorf("failed to create audio stream: %w", err)
}

return &audioStream{
stream: stream,
}, nil
}

0 comments on commit 2187556

Please sign in to comment.