nativeaudio

audio playback for Go
Log | Files | Refs | README | LICENSE

audio_windows.go (874B)


      1 package nativeaudio
      2 
      3 import (
      4 	"fmt"
      5 	"os"
      6 
      7 	"git.sr.ht/~jackmordaunt/nativeaudio/internal/mf"
      8 )
      9 
     10 func start() error {
     11 	return mf.Startup()
     12 }
     13 
     14 func end() error {
     15 	return mf.Shutdown()
     16 }
     17 
     18 // openStream decodes incrementally through Media Foundation.
     19 func openStream(compressed []byte) (*Stream, error) {
     20 	ms, err := mf.Open(compressed)
     21 	if err != nil {
     22 		return nil, err
     23 	}
     24 	f := ms.Format()
     25 	return &Stream{
     26 		r: ms,
     27 		format: Format{
     28 			SampleRate:     f.SampleRate,
     29 			Channels:       f.Channels,
     30 			BytesPerSample: f.BytesPerSample,
     31 		},
     32 	}, nil
     33 }
     34 
     35 // openStreamFile buffers the compressed file, which is small next to
     36 // the PCM the stream avoids holding, then decodes it incrementally.
     37 func openStreamFile(path string) (*Stream, error) {
     38 	by, err := os.ReadFile(path)
     39 	if err != nil {
     40 		return nil, fmt.Errorf("reading input file: %w", err)
     41 	}
     42 	return openStream(by)
     43 }