Compare commits

...

9 Commits

Author SHA1 Message Date
filifa
2d0a10c645 remove ./ 2024-09-07 21:09:04 -05:00
filifa
38016be3d3 add width and height flags 2024-09-07 20:56:02 -05:00
filifa
d13abec803 add soft cut duration flag 2024-09-07 20:50:06 -05:00
filifa
ebb4d7102b use CheckErr 2024-09-07 19:45:50 -05:00
filifa
6a85dcb816 update readme 2024-09-07 19:33:41 -05:00
filifa
92c3b48fa7 refactor preset validation 2024-09-07 19:26:26 -05:00
filifa
96e962b56a adjust usage message 2024-09-07 19:22:10 -05:00
filifa
1e87393d07 don't crash when no presets are passed 2024-09-07 19:20:09 -05:00
filifa
6d045ff304 pass presets with args 2024-09-07 19:16:08 -05:00
3 changed files with 52 additions and 33 deletions

View File

@@ -20,16 +20,16 @@ milkbucket reads a PCM stream from standard input to generate visualizations.
If you have an audio file and a preset in mind, you can use `ffmpeg` to
generate the PCM stream, then pipe to milkbucket, like so:
```
ffmpeg -i $audio -ar 44100 -f s16le - | ./milkbucket -p $preset
ffmpeg -i $audio -ar 44100 -f s16le - | milkbucket $preset
```
Note that you can pass in multiple presets with multiple `-p` flags, then use
the arrow keys to cycle through the presets while running.
Note that you can pass in multiple presets, then use the arrow keys to cycle
through the presets while running.
Note that neither of these commands (ffmpeg or milkbucket) will output any
audio! If you want to hear the audio at the same time (and assuming your
machine uses pipewire), run:
```
ffmpeg -i $audio -ar 44100 -f s16le - | tee >(pw-play --rate=44100 --format=s16 -) | ./milkbucket -p $preset
ffmpeg -i $audio -ar 44100 -f s16le - | tee >(pw-play --rate=44100 --format=s16 -) | milkbucket $preset
```
(If you don't use pipewire try using `aplay` instead of `pw-play`, or some
other command for playing PCM streams.)
@@ -37,5 +37,5 @@ other command for playing PCM streams.)
You can also generate a visualization from your system sound. Assuming pipewire
again, and that you have audio coming from Firefox, run:
```
pw-record --target Firefox - | ./milkbucket -p $preset
pw-record --target Firefox - | milkbucket $preset
```

View File

@@ -20,15 +20,16 @@ import (
"encoding/binary"
"errors"
"io"
"log"
"os"
"github.com/spf13/cobra"
"github.com/veandco/go-sdl2/sdl"
)
var presets []string
var transition bool
var softCutDuration float64
var width int32
var height int32
/*
* checkStdin checks if any data has been passed in through stdin and returns
@@ -46,16 +47,29 @@ func checkStdin() error {
}
/*
* validatePresets performs some basic checks on the presets passed in and
* validatePreset performs some basic checks on the preset passed in and
* returns an error if it finds a problem.
*/
func validatePresets() error {
for _, p := range presets {
info, err := os.Stat(p)
func validatePreset(preset string) error {
info, err := os.Stat(preset)
if err != nil {
return err
} else if info.IsDir() {
return errors.New("preset " + p + " is a directory")
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
}
}
@@ -136,45 +150,43 @@ func update(m *milkDropWindow) (bool, error) {
func milkbucket(cmd *cobra.Command, args []string) {
err := checkStdin()
if err != nil {
log.Fatal(err)
cobra.CheckErr(err)
}
err = sdl.Init(sdl.INIT_VIDEO)
if err != nil {
log.Fatal(err)
cobra.CheckErr(err)
}
defer sdl.Quit()
err = validatePresets()
m, err := newMilkDropWindow(width, height, args, softCutDuration)
if err != nil {
log.Fatal(err)
}
m, err := newMilkDropWindow(800, 600, presets)
if err != nil {
log.Fatal(err)
cobra.CheckErr(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)
cobra.CheckErr(err)
}
}
}
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "milkbucket",
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.
@@ -194,6 +206,7 @@ func init() {
// 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")
rootCmd.Flags().StringArrayVarP(&presets, "presets", "p", []string{}, "preset files to use")
rootCmd.Flags().Float64VarP(&softCutDuration, "soft-cut-duration", "s", 3, "time in seconds for a soft transition between two presets (use with --transition)")
rootCmd.Flags().Int32Var(&width, "width", 800, "window width")
rootCmd.Flags().Int32Var(&height, "height", 600, "window height")
}

View File

@@ -67,8 +67,10 @@ func (m *milkDropWindow) setupPresets(presets []string) {
* preset.
*/
func (m *milkDropWindow) nextPreset(smooth bool) {
if m.preset.Len() > 0 {
m.preset = m.preset.Next()
m.loadPreset(smooth)
}
}
/*
@@ -76,15 +78,17 @@ func (m *milkDropWindow) nextPreset(smooth bool) {
* 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) (*milkDropWindow, error) {
func newMilkDropWindow(width, height int32, presets []string, softCutDuration float64) (*milkDropWindow, error) {
var m milkDropWindow
var err error
@@ -107,6 +111,8 @@ func newMilkDropWindow(width, height int32, presets []string) (*milkDropWindow,
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
}