move buffering stuff to root

This commit is contained in:
filifa 2024-09-05 22:40:31 -05:00
parent 3882ffe4f7
commit 6f5f8b1818
2 changed files with 22 additions and 26 deletions

View File

@ -17,7 +17,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd package cmd
import ( import (
"bufio"
"encoding/binary"
"errors" "errors"
"io"
"log" "log"
"os" "os"
@ -107,7 +110,7 @@ func handleEvent(event sdl.Event, m *milkDropWindow) bool {
* returns a bool indicating whether the program should keep running and an * returns a bool indicating whether the program should keep running and an
* error, if any. * error, if any.
*/ */
func update(m *milkDropWindow) (bool, error) { func update(r *bufio.Reader, m *milkDropWindow) (bool, error) {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
keepRunning := handleEvent(event, m) keepRunning := handleEvent(event, m)
if !keepRunning { if !keepRunning {
@ -115,8 +118,17 @@ func update(m *milkDropWindow) (bool, error) {
} }
} }
keepRunning, err := m.render() audioData := make([]int16, r.Size()/2)
return keepRunning, err
err := binary.Read(r, binary.LittleEndian, audioData)
if err == io.ErrUnexpectedEOF {
return false, nil
} else if err != nil {
return true, err
}
m.render(audioData)
return true, nil
} }
/* /*
@ -147,9 +159,11 @@ func milkbucket(cmd *cobra.Command, args []string) {
m.loadPreset(false) m.loadPreset(false)
r := bufio.NewReader(os.Stdin)
running := true running := true
for running { for running {
running, err = update(m) running, err = update(r, m)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -24,12 +24,8 @@ package cmd
import "C" import "C"
import ( import (
"bufio"
"container/ring" "container/ring"
"encoding/binary"
"errors" "errors"
"io"
"os"
"unsafe" "unsafe"
"github.com/veandco/go-sdl2/sdl" "github.com/veandco/go-sdl2/sdl"
@ -41,7 +37,6 @@ type milkDropWindow struct {
context sdl.GLContext context sdl.GLContext
handle C.projectm_handle handle C.projectm_handle
preset *ring.Ring preset *ring.Ring
buffer *bufio.Reader
} }
/* /*
@ -83,8 +78,6 @@ func newMilkDropWindow(width, height int32, presets []string) (*milkDropWindow,
m.setupPresets(presets) m.setupPresets(presets)
m.buffer = bufio.NewReader(os.Stdin)
m.window, err = sdl.CreateWindow("milkbucket", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, width, height, sdl.WINDOW_OPENGL|sdl.WINDOW_RESIZABLE) m.window, err = sdl.CreateWindow("milkbucket", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, width, height, sdl.WINDOW_OPENGL|sdl.WINDOW_RESIZABLE)
if err != nil { if err != nil {
return &m, err return &m, err
@ -127,25 +120,14 @@ func (m *milkDropWindow) loadPreset(smooth bool) {
} }
/* /*
* render reads data from stdin and passes the data to projectm for rendering. * render passes data to projectm for rendering. It returns a bool for if the
* It returns a bool for if the program should keep running and an error. * program should keep running and an error.
*/ */
func (m *milkDropWindow) render() (bool, error) { func (m *milkDropWindow) render(data []int16) {
audioData := make([]int16, m.buffer.Size()/2) C.projectm_pcm_add_int16(m.handle, (*C.int16_t)(&data[0]), C.uint(len(data)), C.PROJECTM_STEREO)
err := binary.Read(m.buffer, binary.LittleEndian, audioData)
if err == io.ErrUnexpectedEOF {
return false, nil
} else if err != nil {
return true, err
}
C.projectm_pcm_add_int16(m.handle, (*C.int16_t)(&audioData[0]), C.uint(len(audioData)), C.PROJECTM_STEREO)
C.projectm_opengl_render_frame(m.handle) C.projectm_opengl_render_frame(m.handle)
m.window.GLSwap() m.window.GLSwap()
return true, nil
} }
/* /*