2024-08-25 02:05:15 +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/>.
|
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-09-06 03:40:31 +00:00
|
|
|
"encoding/binary"
|
2024-09-05 02:13:42 +00:00
|
|
|
"errors"
|
2024-09-06 03:40:31 +00:00
|
|
|
"io"
|
2024-09-05 02:38:23 +00:00
|
|
|
"log"
|
2024-08-25 02:05:15 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
|
|
)
|
|
|
|
|
2024-09-05 01:27:01 +00:00
|
|
|
var presets []string
|
2024-09-05 01:04:47 +00:00
|
|
|
var transition bool
|
2024-08-25 02:05:15 +00:00
|
|
|
|
2024-09-05 02:35:38 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
}
|
|
|
|
|
2024-09-05 02:13:42 +00:00
|
|
|
/*
|
|
|
|
* validatePresets performs some basic checks on the presets passed in and
|
|
|
|
* returns an error if it finds a problem.
|
|
|
|
*/
|
|
|
|
func validatePresets() error {
|
|
|
|
for _, p := range presets {
|
|
|
|
info, err := os.Stat(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if info.IsDir() {
|
|
|
|
return errors.New("preset " + p + " is a directory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-05 02:10:30 +00:00
|
|
|
/*
|
|
|
|
* handleWindowEvent handles window events like resizing.
|
|
|
|
*/
|
2024-08-25 02:05:15 +00:00
|
|
|
func handleWindowEvent(event *sdl.WindowEvent, m *milkDropWindow) {
|
|
|
|
switch event.Event {
|
|
|
|
case sdl.WINDOWEVENT_RESIZED:
|
|
|
|
w, h := event.Data1, event.Data2
|
2024-08-25 03:03:37 +00:00
|
|
|
m.resize(w, h)
|
2024-08-25 02:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-05 02:10:30 +00:00
|
|
|
/*
|
|
|
|
* handleKeyboardEvent handles keyboard inputs.
|
|
|
|
*/
|
2024-09-03 03:17:11 +00:00
|
|
|
func handleKeyboardEvent(event *sdl.KeyboardEvent, m *milkDropWindow) {
|
|
|
|
scancode := event.Keysym.Scancode
|
2024-09-03 03:44:24 +00:00
|
|
|
if event.Type == sdl.KEYDOWN && scancode == sdl.SCANCODE_RIGHT && event.Repeat == 0 {
|
2024-09-05 01:04:47 +00:00
|
|
|
m.nextPreset(transition)
|
2024-09-03 03:44:24 +00:00
|
|
|
} else if event.Type == sdl.KEYDOWN && scancode == sdl.SCANCODE_LEFT && event.Repeat == 0 {
|
2024-09-05 01:04:47 +00:00
|
|
|
m.prevPreset(transition)
|
2024-09-03 03:17:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-05 02:10:30 +00:00
|
|
|
/*
|
|
|
|
* handleEvent passes the event to a more specific function based on its type.
|
|
|
|
* It returns false if the program should quit, true otherwise.
|
|
|
|
*/
|
2024-08-25 02:05:15 +00:00
|
|
|
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)
|
2024-09-03 03:17:11 +00:00
|
|
|
case *sdl.KeyboardEvent:
|
|
|
|
event := event.(*sdl.KeyboardEvent)
|
|
|
|
handleKeyboardEvent(event, m)
|
2024-08-25 02:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-09-05 02:31:52 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-09-06 04:16:14 +00:00
|
|
|
func update(m *milkDropWindow) (bool, error) {
|
2024-09-05 02:31:52 +00:00
|
|
|
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
|
|
|
keepRunning := handleEvent(event, m)
|
|
|
|
if !keepRunning {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 04:16:14 +00:00
|
|
|
audioData := make([]int16, 2048)
|
2024-09-06 03:40:31 +00:00
|
|
|
|
2024-09-06 04:16:14 +00:00
|
|
|
err := binary.Read(os.Stdin, binary.LittleEndian, audioData)
|
2024-09-06 03:40:31 +00:00
|
|
|
if err == io.ErrUnexpectedEOF {
|
|
|
|
return false, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.render(audioData)
|
|
|
|
return true, nil
|
2024-09-05 02:31:52 +00:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:10:30 +00:00
|
|
|
/*
|
|
|
|
* milkbucket sets up the program and starts a rendering loop.
|
|
|
|
*/
|
2024-08-25 02:05:15 +00:00
|
|
|
func milkbucket(cmd *cobra.Command, args []string) {
|
2024-09-05 02:35:38 +00:00
|
|
|
err := checkStdin()
|
2024-08-26 02:03:57 +00:00
|
|
|
if err != nil {
|
2024-09-05 02:38:23 +00:00
|
|
|
log.Fatal(err)
|
2024-08-26 02:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = sdl.Init(sdl.INIT_VIDEO)
|
2024-08-25 02:05:15 +00:00
|
|
|
if err != nil {
|
2024-09-05 02:38:23 +00:00
|
|
|
log.Fatal(err)
|
2024-08-25 02:05:15 +00:00
|
|
|
}
|
|
|
|
defer sdl.Quit()
|
|
|
|
|
2024-09-05 02:13:42 +00:00
|
|
|
err = validatePresets()
|
|
|
|
if err != nil {
|
2024-09-05 02:38:23 +00:00
|
|
|
log.Fatal(err)
|
2024-09-04 03:38:13 +00:00
|
|
|
}
|
2024-09-03 04:33:35 +00:00
|
|
|
|
2024-09-03 04:22:56 +00:00
|
|
|
m, err := newMilkDropWindow(800, 600, presets)
|
2024-08-25 02:05:15 +00:00
|
|
|
if err != nil {
|
2024-09-05 02:38:23 +00:00
|
|
|
log.Fatal(err)
|
2024-08-25 02:05:15 +00:00
|
|
|
}
|
|
|
|
defer m.destroy()
|
|
|
|
|
2024-09-03 04:10:46 +00:00
|
|
|
m.loadPreset(false)
|
2024-08-25 02:05:15 +00:00
|
|
|
|
|
|
|
running := true
|
|
|
|
for running {
|
2024-09-06 04:16:14 +00:00
|
|
|
running, err = update(m)
|
2024-08-25 02:05:15 +00:00
|
|
|
if err != nil {
|
2024-09-05 02:38:23 +00:00
|
|
|
log.Fatal(err)
|
2024-08-25 02:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// rootCmd represents the base command when called without any subcommands
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "milkbucket",
|
2024-09-03 02:03:17 +00:00
|
|
|
Short: "Audio visualizer",
|
|
|
|
Long: `milkbucket is an audio visualizer.
|
2024-08-25 02:05:15 +00:00
|
|
|
|
2024-09-03 02:03:17 +00:00
|
|
|
It uses Milkdrop preset files to generate visualizations from standard input.`,
|
2024-08-25 02:05:15 +00:00
|
|
|
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.
|
2024-09-05 01:04:47 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&transition, "transition", "t", false, "smoothly transition between presets")
|
|
|
|
|
2024-09-05 01:27:01 +00:00
|
|
|
rootCmd.Flags().StringArrayVarP(&presets, "presets", "p", []string{}, "preset files to use")
|
2024-08-25 02:05:15 +00:00
|
|
|
}
|