-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tools/audiostream): add AudioStream interface and implementation
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |