/* 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 . */ package cmd /* #cgo LDFLAGS: -lprojectM-4 #include "projectM-4/projectM.h" #include */ import "C" import ( "container/ring" "errors" "unsafe" "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 context sdl.GLContext handle C.projectm_handle preset *ring.Ring } /* * setupPresets initializes a ring to store the passed presets in. */ func (m *milkDropWindow) setupPresets(presets []string) { m.preset = ring.New(len(presets)) for _, preset := range presets { m.preset.Value = preset m.preset = m.preset.Next() } } /* * prevPreset sets the milkDropWindow's preset to the one after the current * preset. */ func (m *milkDropWindow) nextPreset(smooth bool) { if m.preset.Len() > 0 { m.preset = m.preset.Next() m.loadPreset(smooth) } } /* * prevPreset sets the milkDropWindow's preset to the one before the current * preset. */ func (m *milkDropWindow) prevPreset(smooth bool) { if m.preset.Len() > 0 { m.preset = m.preset.Prev() m.loadPreset(smooth) } } /* * newMilkDropWindow returns a new milkDropWindow with the given width and * height, and sets presets available to the window. */ func newMilkDropWindow(width, height int32, presets []string, softCutDuration float64) (*milkDropWindow, error) { var m milkDropWindow var err error m.setupPresets(presets) 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)) C.projectm_set_soft_cut_duration(m.handle, C.double(softCutDuration)) return &m, nil } /* * destroy handles cleanup of the milkDropWindow. */ func (m *milkDropWindow) destroy() { C.projectm_destroy(m.handle) sdl.GLDeleteContext(m.context) m.window.Destroy() } /* * 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. */ func (m *milkDropWindow) loadPreset(smooth bool) { preset := m.preset.Value.(string) cPreset := C.CString(preset) defer C.free(unsafe.Pointer(cPreset)) C.projectm_load_preset_file(m.handle, cPreset, C.bool(smooth)) } /* * 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(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() } /* * resize informs projectm of the new window size. */ func (m *milkDropWindow) resize(w, h int32) { C.projectm_set_window_size(m.handle, C.ulong(w), C.ulong(h)) }