milkbucket/cmd/window.go

157 lines
3.9 KiB
Go
Raw Normal View History

2024-08-25 02:24:16 +00:00
/*
Copyright © 2024 filifa
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2024-08-25 02:05:15 +00:00
package cmd
2024-08-25 00:53:39 +00:00
/*
2024-08-25 23:16:53 +00:00
#cgo LDFLAGS: -lprojectM-4
2024-08-25 00:53:39 +00:00
#include "projectM-4/projectM.h"
#include <stdlib.h>
*/
import "C"
import (
2024-09-03 03:17:11 +00:00
"container/ring"
2024-08-25 00:53:39 +00:00
"encoding/binary"
"errors"
"io"
"os"
"unsafe"
"github.com/veandco/go-sdl2/sdl"
)
2024-09-05 02:10:30 +00:00
// bufSize is the size of the buffer to store data from stdin. NOTE: this might
// need tuning.
2024-08-25 00:53:39 +00:00
const bufSize uint = 2048
2024-09-05 02:10:30 +00:00
// milkDropWindow represents the window projectm will render visualizations in.
2024-08-25 00:53:39 +00:00
type milkDropWindow struct {
window *sdl.Window
context sdl.GLContext
handle C.projectm_handle
2024-09-03 03:17:11 +00:00
preset *ring.Ring
2024-08-25 00:53:39 +00:00
}
2024-09-05 02:10:30 +00:00
/*
* setupPresets initializes a ring to store the passed presets in.
*/
2024-09-03 04:10:46 +00:00
func (m *milkDropWindow) setupPresets(presets []string) {
m.preset = ring.New(len(presets))
for _, preset := range presets {
2024-09-03 03:42:44 +00:00
m.preset.Value = preset
m.preset = m.preset.Next()
}
2024-09-03 03:17:11 +00:00
}
2024-09-05 02:10:30 +00:00
/*
* prevPreset sets the milkDropWindow's preset to the one after the current
* preset.
*/
2024-09-05 01:04:47 +00:00
func (m *milkDropWindow) nextPreset(smooth bool) {
2024-09-03 03:17:11 +00:00
m.preset = m.preset.Next()
2024-09-05 01:04:47 +00:00
m.loadPreset(smooth)
2024-09-03 03:17:11 +00:00
}
2024-09-05 02:10:30 +00:00
/*
* prevPreset sets the milkDropWindow's preset to the one before the current
* preset.
*/
2024-09-05 01:04:47 +00:00
func (m *milkDropWindow) prevPreset(smooth bool) {
2024-09-03 03:31:25 +00:00
m.preset = m.preset.Prev()
2024-09-05 01:04:47 +00:00
m.loadPreset(smooth)
2024-09-03 03:31:25 +00:00
}
2024-09-05 02:10:30 +00:00
/*
* newMilkDropWindow returns a new milkDropWindow with the given width and
* height, and sets presets available to the window.
*/
2024-09-03 04:22:56 +00:00
func newMilkDropWindow(width, height int32, presets []string) (*milkDropWindow, error) {
2024-08-25 00:53:39 +00:00
var m milkDropWindow
2024-09-03 03:49:29 +00:00
var err error
2024-08-25 00:53:39 +00:00
2024-09-03 04:22:56 +00:00
m.setupPresets(presets)
2024-09-03 03:17:11 +00:00
2024-08-25 00:53:39 +00:00
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
}
m.context, err = m.window.GLCreateContext()
if err != nil {
return &m, err
}
m.handle = C.projectm_create()
if m.handle == nil {
return &m, errors.New("error creating projectM instance")
}
C.projectm_set_window_size(m.handle, C.ulong(width), C.ulong(height))
return &m, nil
}
2024-09-05 02:10:30 +00:00
/*
* destroy handles cleanup of the milkDropWindow.
*/
2024-08-25 00:53:39 +00:00
func (m *milkDropWindow) destroy() {
C.projectm_destroy(m.handle)
sdl.GLDeleteContext(m.context)
m.window.Destroy()
}
2024-09-05 02:10:30 +00:00
/*
* loadPreset loads the preset file currently pointed to by m.preset. smooth
* determines whether there is a smooth transition between the current preset
* and the new preset.
*/
2024-09-03 04:10:46 +00:00
func (m *milkDropWindow) loadPreset(smooth bool) {
preset := m.preset.Value.(string)
2024-08-25 02:05:15 +00:00
cPreset := C.CString(preset)
2024-08-25 00:53:39 +00:00
defer C.free(unsafe.Pointer(cPreset))
2024-09-03 04:10:46 +00:00
C.projectm_load_preset_file(m.handle, cPreset, C.bool(smooth))
2024-08-25 00:53:39 +00:00
}
2024-09-05 02:10:30 +00:00
/*
* 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.
*/
2024-08-25 00:53:39 +00:00
func (m *milkDropWindow) render() (bool, error) {
var audioData [bufSize]int16
err := binary.Read(os.Stdin, 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(bufSize), C.PROJECTM_STEREO)
C.projectm_opengl_render_frame(m.handle)
m.window.GLSwap()
return true, nil
}
2024-08-25 03:03:37 +00:00
2024-09-05 02:10:30 +00:00
/*
* resize informs projectm of the new window size.
*/
2024-08-25 03:03:37 +00:00
func (m *milkDropWindow) resize(w, h int32) {
C.projectm_set_window_size(m.handle, C.ulong(w), C.ulong(h))
}