diff --git a/cmd/root.go b/cmd/root.go index 51a43e3..9984e10 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -117,7 +117,7 @@ func update(m *milkDropWindow) (bool, error) { } } - audioData := make([]int16, 2048) + audioData := make([]int16, maxSamples(2, 2)) err := binary.Read(os.Stdin, binary.LittleEndian, audioData) if err == io.ErrUnexpectedEOF { diff --git a/cmd/window.go b/cmd/window.go index dc37acf..8f16aa5 100644 --- a/cmd/window.go +++ b/cmd/window.go @@ -31,6 +31,18 @@ import ( "github.com/veandco/go-sdl2/sdl" ) +/* + * maxSamples returns the maximum number of audio samples stored by projectm + * when passing it audio data, given the number of bytes in one sample and the + * number of channels. + */ +func maxSamples(bytesPerSample uint, numChannels uint) uint { + // NOTE: not 100% sure about this, but I think + // projectm_pcm_get_max_samples assumes 4 bytes per sample, so we + // multiply by 4/bytesPerSample to convert + return 4 / bytesPerSample * numChannels * uint(C.projectm_pcm_get_max_samples()) +} + // milkDropWindow represents the window projectm will render visualizations in. type milkDropWindow struct { window *sdl.Window