/* 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 import ( "encoding/json" "log" "os" "github.com/spf13/cobra" "github.com/veandco/go-sdl2/sdl" ) var preset string var scriptPath string type script struct { Presets []string Times []float64 } func (s *script) load(path string) error { f, err := os.Open(path) if err != nil { return err } defer f.Close() err = json.NewDecoder(f).Decode(s) return err } func getPresets() []string { var presets []string if preset != "" { presets = []string{preset} } if scriptPath != "" { var s script s.load(scriptPath) presets = s.Presets } return presets } func handleWindowEvent(event *sdl.WindowEvent, m *milkDropWindow) { switch event.Event { case sdl.WINDOWEVENT_RESIZED: w, h := event.Data1, event.Data2 m.resize(w, h) } } 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() } else if event.Type == sdl.KEYDOWN && scancode == sdl.SCANCODE_LEFT && event.Repeat == 0 { m.prevPreset() } } 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 } func milkbucket(cmd *cobra.Command, args []string) { stat, err := os.Stdin.Stat() if err != nil { panic(err) } else if (stat.Mode() & os.ModeCharDevice) != 0 { log.Fatal("nothing to read from stdin") } err = sdl.Init(sdl.INIT_VIDEO) if err != nil { panic(err) } defer sdl.Quit() presets := getPresets() m, err := newMilkDropWindow(800, 600, presets) if err != nil { panic(err) } defer m.destroy() m.loadPreset(false) running := true for running { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { running = handleEvent(event, m) if !running { break } } if !running { break } running, err = m.render() if err != nil { panic(err) } } } // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "milkbucket", Short: "Audio visualizer", Long: `milkbucket is an audio visualizer. It uses Milkdrop preset files to generate visualizations from standard input.`, Run: milkbucket, } // 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().StringVarP(&preset, "preset", "p", "", "preset file to use") rootCmd.Flags().StringVarP(&scriptPath, "script", "s", "", "script file to use") rootCmd.MarkFlagsMutuallyExclusive("preset", "script") }