move buffering stuff to root
This commit is contained in:
parent
3882ffe4f7
commit
6f5f8b1818
22
cmd/root.go
22
cmd/root.go
|
@ -17,7 +17,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"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
|
||||
* 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() {
|
||||
keepRunning := handleEvent(event, m)
|
||||
if !keepRunning {
|
||||
|
@ -115,8 +118,17 @@ func update(m *milkDropWindow) (bool, error) {
|
|||
}
|
||||
}
|
||||
|
||||
keepRunning, err := m.render()
|
||||
return keepRunning, err
|
||||
audioData := make([]int16, r.Size()/2)
|
||||
|
||||
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)
|
||||
|
||||
r := bufio.NewReader(os.Stdin)
|
||||
|
||||
running := true
|
||||
for running {
|
||||
running, err = update(m)
|
||||
running, err = update(r, m)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -24,12 +24,8 @@ package cmd
|
|||
import "C"
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"container/ring"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"unsafe"
|
||||
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
|
@ -41,7 +37,6 @@ type milkDropWindow struct {
|
|||
context sdl.GLContext
|
||||
handle C.projectm_handle
|
||||
preset *ring.Ring
|
||||
buffer *bufio.Reader
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -83,8 +78,6 @@ func newMilkDropWindow(width, height int32, presets []string) (*milkDropWindow,
|
|||
|
||||
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)
|
||||
if err != nil {
|
||||
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.
|
||||
* It returns a bool for if the program should keep running and an error.
|
||||
* render passes data to projectm for rendering. It returns a bool for if the
|
||||
* program should keep running and an error.
|
||||
*/
|
||||
func (m *milkDropWindow) render() (bool, error) {
|
||||
audioData := make([]int16, m.buffer.Size()/2)
|
||||
|
||||
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)
|
||||
func (m *milkDropWindow) render(data []int16) {
|
||||
C.projectm_pcm_add_int16(m.handle, (*C.int16_t)(&data[0]), C.uint(len(data)), C.PROJECTM_STEREO)
|
||||
|
||||
C.projectm_opengl_render_frame(m.handle)
|
||||
m.window.GLSwap()
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in New Issue