208 lines
4.9 KiB
Go
208 lines
4.9 KiB
Go
/*
|
|
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/>.
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"errors"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
)
|
|
|
|
var transition bool
|
|
|
|
/*
|
|
* checkStdin checks if any data has been passed in through stdin and returns
|
|
* an error if we don't believe so.
|
|
*/
|
|
func checkStdin() error {
|
|
stat, err := os.Stdin.Stat()
|
|
if err != nil {
|
|
return err
|
|
} else if (stat.Mode() & os.ModeCharDevice) != 0 {
|
|
return errors.New("nothing to read from stdin")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
/*
|
|
* validatePreset performs some basic checks on the preset passed in and
|
|
* returns an error if it finds a problem.
|
|
*/
|
|
func validatePreset(preset string) error {
|
|
info, err := os.Stat(preset)
|
|
if err != nil {
|
|
return err
|
|
} else if info.IsDir() {
|
|
return errors.New("preset " + preset + " is a directory")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
/*
|
|
* validatePresets validates each preset passed in and returns an error if it
|
|
* finds a problem.
|
|
*/
|
|
func validatePresets(cmd *cobra.Command, args []string) error {
|
|
for _, p := range args {
|
|
err := validatePreset(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
/*
|
|
* handleWindowEvent handles window events like resizing.
|
|
*/
|
|
func handleWindowEvent(event *sdl.WindowEvent, m *milkDropWindow) {
|
|
switch event.Event {
|
|
case sdl.WINDOWEVENT_RESIZED:
|
|
w, h := event.Data1, event.Data2
|
|
m.resize(w, h)
|
|
}
|
|
}
|
|
|
|
/*
|
|
* handleKeyboardEvent handles keyboard inputs.
|
|
*/
|
|
func handleKeyboardEvent(event *sdl.KeyboardEvent, m *milkDropWindow) {
|
|
scancode := event.Keysym.Scancode
|
|
if event.Type == sdl.KEYDOWN && scancode == sdl.SCANCODE_RIGHT && event.Repeat == 0 {
|
|
m.nextPreset(transition)
|
|
} else if event.Type == sdl.KEYDOWN && scancode == sdl.SCANCODE_LEFT && event.Repeat == 0 {
|
|
m.prevPreset(transition)
|
|
}
|
|
}
|
|
|
|
/*
|
|
* handleEvent passes the event to a more specific function based on its type.
|
|
* It returns false if the program should quit, true otherwise.
|
|
*/
|
|
func handleEvent(event sdl.Event, m *milkDropWindow) bool {
|
|
switch event.(type) {
|
|
case *sdl.QuitEvent:
|
|
return false
|
|
case *sdl.WindowEvent:
|
|
event := event.(*sdl.WindowEvent)
|
|
handleWindowEvent(event, m)
|
|
case *sdl.KeyboardEvent:
|
|
event := event.(*sdl.KeyboardEvent)
|
|
handleKeyboardEvent(event, m)
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
/*
|
|
* update handles events and renders new frames of the visualization. It
|
|
* returns a bool indicating whether the program should keep running and an
|
|
* error, if any.
|
|
*/
|
|
func update(m *milkDropWindow) (bool, error) {
|
|
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
|
keepRunning := handleEvent(event, m)
|
|
if !keepRunning {
|
|
return false, nil
|
|
}
|
|
}
|
|
|
|
audioData := make([]int16, maxSamples(2, 2))
|
|
|
|
err := binary.Read(os.Stdin, binary.LittleEndian, audioData)
|
|
if err == io.ErrUnexpectedEOF {
|
|
return false, nil
|
|
} else if err != nil {
|
|
return true, err
|
|
}
|
|
|
|
m.render(audioData)
|
|
return true, nil
|
|
}
|
|
|
|
/*
|
|
* milkbucket sets up the program and starts a rendering loop.
|
|
*/
|
|
func milkbucket(cmd *cobra.Command, args []string) {
|
|
err := checkStdin()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
err = sdl.Init(sdl.INIT_VIDEO)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer sdl.Quit()
|
|
|
|
m, err := newMilkDropWindow(800, 600, args)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer m.destroy()
|
|
|
|
if len(args) > 0 {
|
|
m.loadPreset(false)
|
|
}
|
|
|
|
running := true
|
|
for running {
|
|
running, err = update(m)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// rootCmd represents the base command when called without any subcommands
|
|
var rootCmd = &cobra.Command{
|
|
Use: "milkbucket [presets]",
|
|
Short: "Audio visualizer",
|
|
Long: `milkbucket is an audio visualizer.
|
|
|
|
It uses Milkdrop preset files to generate visualizations from standard input.`,
|
|
Run: milkbucket,
|
|
Args: validatePresets,
|
|
}
|
|
|
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
|
func Execute() {
|
|
err := rootCmd.Execute()
|
|
if err != nil {
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
// Here you will define your flags and configuration settings.
|
|
// Cobra supports persistent flags, which, if defined here,
|
|
// will be global for your application.
|
|
|
|
// Cobra also supports local flags, which will only run
|
|
// when this action is called directly.
|
|
rootCmd.Flags().BoolVarP(&transition, "transition", "t", false, "smoothly transition between presets")
|
|
}
|