nativeaudio

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

commit 878fc355ded97993497419d131540f000344d499
parent f5f38413db88956b1dd02b48e023119e25b9e40e
Author: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Date:   Wed, 16 Sep 2026 21:44:53 -0400

audio: rename Format.BitDepth to BytesPerSample

The field held bytes per sample (2 for s16le) while its name implied
bits (16). Renaming rather than changing the value forces callers to
notice: a silent 2 to 16 change would compile and then mis-size every
buffer. The macOS decoder is updated alongside the Windows and ffmpeg
paths.

Diffstat:
Maudio.go | 6+++---
Maudio_ffmpeg.go | 2+-
Maudio_macos.go | 2+-
Maudio_windows.go | 6+++---
Minternal/mf/mf.go | 12++++++------
Minternal/test/audio_test.go | 8++++----
Mplayback.go | 4++--
7 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/audio.go b/audio.go @@ -74,7 +74,7 @@ func Decode(compressed []byte) (uncompressed []byte, format Format, err error) { // Format describes the features of the associated PCM data necessary // for correct playback. type Format struct { - SampleRate int // samples per second. - Channels int // number channels. - BitDepth int // bytes per sample. + SampleRate int // samples per second. + Channels int // number channels. + BytesPerSample int // bytes per sample; 2 for the s16le output this package produces. } diff --git a/audio_ffmpeg.go b/audio_ffmpeg.go @@ -160,6 +160,6 @@ func (s stream) Format() (Format, error) { Channels: s.Channels, // We are going to tell ffmpeg to output s16le, though there // might be a better place to make this assumption. - BitDepth: 2, + BytesPerSample: 2, }, nil } diff --git a/audio_macos.go b/audio_macos.go @@ -244,7 +244,7 @@ func decode(buf []byte) (_ []byte, f Format, _ error) { } f.Channels = int(outputDescription.mChannelsPerFrame) - f.BitDepth = int(outputDescription.mBitsPerChannel / 8) + f.BytesPerSample = int(outputDescription.mBitsPerChannel / 8) f.SampleRate = int(outputDescription.mSampleRate) return out, f, nil diff --git a/audio_windows.go b/audio_windows.go @@ -47,8 +47,8 @@ func decode(compressed []byte) (uncompressed []byte, format Format, err error) { return nil, format, err } return pcm, Format{ - SampleRate: f.SampleRate, - Channels: f.Channels, - BitDepth: f.BitDepth, + SampleRate: f.SampleRate, + Channels: f.Channels, + BytesPerSample: f.BytesPerSample, }, nil } diff --git a/internal/mf/mf.go b/internal/mf/mf.go @@ -22,9 +22,9 @@ import ( // Format describes the PCM produced by Decode. type Format struct { - SampleRate int // samples per second. - Channels int // number channels. - BitDepth int // bytes per sample. + SampleRate int // samples per second. + Channels int // number channels. + BytesPerSample int // bytes per sample. } // Startup initialises Media Foundation and resolves the entry points @@ -155,9 +155,9 @@ func getFormat(mt *IMFMediaType) (f Format, _ error) { } f = Format{ - SampleRate: int(sampleRate), - Channels: int(numChannels), - BitDepth: int(bitsPerSample / 8), + SampleRate: int(sampleRate), + Channels: int(numChannels), + BytesPerSample: int(bitsPerSample / 8), } return f, nil diff --git a/internal/test/audio_test.go b/internal/test/audio_test.go @@ -44,8 +44,8 @@ func TestLoad(t *testing.T) { } t.Logf("format: %+v", f) // Check for known meta data values (ffprobe -i compressed.m4a). - if f.BitDepth != 2 { - t.Fatalf("unexpected bit depth: want 2, got %d", f.BitDepth) + if f.BytesPerSample != 2 { + t.Fatalf("unexpected bit depth: want 2, got %d", f.BytesPerSample) } if f.SampleRate != 44100 { t.Fatalf("unexpected sample rate: want 44100, got %d", f.SampleRate) @@ -71,8 +71,8 @@ func TestDecode(t *testing.T) { } t.Logf("format: %+v", f) // Check for known meta data values (ffprobe -i compressed.m4a). - if f.BitDepth != 2 { - t.Fatalf("unexpected bit depth: want 2, got %d", f.BitDepth) + if f.BytesPerSample != 2 { + t.Fatalf("unexpected bit depth: want 2, got %d", f.BytesPerSample) } if f.SampleRate != 44100 { t.Fatalf("unexpected sample rate: want 44100, got %d", f.SampleRate) diff --git a/playback.go b/playback.go @@ -47,8 +47,8 @@ func playbackContext(f Format) (*oto.Context, error) { // playPCM plays s16le PCM synchronously through the shared oto context, // returning once the audio has finished. func playPCM(data []byte, format Format) error { - if format.BitDepth != 2 { - return fmt.Errorf("playback: unsupported sample size %d bytes, want 2", format.BitDepth) + if format.BytesPerSample != 2 { + return fmt.Errorf("playback: unsupported sample size %d bytes, want 2", format.BytesPerSample) } ctx, err := playbackContext(format) if err != nil {