use projectm_pcm_get_max_samples to set buffer size

This commit is contained in:
filifa 2024-09-07 15:54:40 -05:00
parent afb198052e
commit d59a2859b6
2 changed files with 13 additions and 1 deletions

View File

@ -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) err := binary.Read(os.Stdin, binary.LittleEndian, audioData)
if err == io.ErrUnexpectedEOF { if err == io.ErrUnexpectedEOF {

View File

@ -31,6 +31,18 @@ import (
"github.com/veandco/go-sdl2/sdl" "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. // milkDropWindow represents the window projectm will render visualizations in.
type milkDropWindow struct { type milkDropWindow struct {
window *sdl.Window window *sdl.Window