├── .gitignore ├── LICENSE ├── README.md ├── _mpdviz ├── intmath.go ├── mpdviz.go ├── read.go └── read_test.go /.gitignore: -------------------------------------------------------------------------------- 1 | *.test 2 | .gitignode 3 | mpdviz 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | 3 | Copyright (C) 4 | 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 9 | 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MPDViz 2 | ------ 3 | This is a console visualizer for MPD. It has three modes: 4 | 5 | ![spectrum](http://goput.it/ji6.gif "spectrum") 6 | 7 | ![wave](http://goput.it/hnq.gif "wave") 8 | 9 | ![lines](http://goput.it/s9d.gif "lines") 10 | 11 | 12 | Usage of mpdviz: 13 | -c, --color="default" Color to use 14 | -d, --dim=false Turn off bright colors where possible 15 | -f, --file="/tmp/mpd.fifo" Where to read pcm data from 16 | -i, --icolor=false Color bars according to intensity (spectrum/lines) 17 | --imode="dumb" Mode for colorisation (dumb, 256 or grayscale) 18 | --scale=2 Scale divisor (spectrum) 19 | --step=2 Samples for each step (wave/lines) 20 | -v, --viz="wave" Visualisation (spectrum, wave or lines) 21 | -------------------------------------------------------------------------------- /_mpdviz: -------------------------------------------------------------------------------- 1 | #compdef mpdviz 2 | 3 | _arguments -s $_mpdviz_options \ 4 | '(-c --color)'{-c,--color=}'[color to use]:color:(default black red green yellow blue magenta cyan white)' \ 5 | '(-d --dim)'{-d,--dim=}'[turn off bright colors]::boolean:(true fase)' \ 6 | '(-f --file)'{-f,--file=}'[where to read pcm data from]:files:_files' \ 7 | '(-i --icolor)'{-i,--icolor}'[intensity colors]::boolean:(true false)' \ 8 | '(-v --viz)'{-v,--viz}'[visualization]:visualization:((spectrum\:frequency\ spectrum wave\:sound\ wave lines\:lines))' \ 9 | '--imode=[colorization mode]:color gradient:(dumb 256 grayscale)' \ 10 | '--scale=[scale divisor (spectrum)]:divisor' \ 11 | '--step=[samples for each step (wave/lines)]:samples' 12 | 13 | # vim: ft=zsh : 14 | -------------------------------------------------------------------------------- /intmath.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func min(x, y int) int { 4 | if x < y { 5 | return x 6 | } 7 | return y 8 | } 9 | 10 | func max(x, y int) int { 11 | if x > y { 12 | return x 13 | } 14 | return y 15 | } 16 | 17 | func abs(x int) int { 18 | if x < 0 { 19 | return -x 20 | } 21 | return x 22 | } 23 | 24 | func mod(x int, y int) int { 25 | if x < 0 { 26 | return x + y 27 | } 28 | return x % y 29 | } 30 | -------------------------------------------------------------------------------- /mpdviz.go: -------------------------------------------------------------------------------- 1 | // This code is fucking awful because I didn't know anything about audio 2 | // when it was written (and probably still don't by the time anyone reads 3 | // this). 4 | 5 | /* 6 | Copyright (C) 2013-2014 Lucy 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a 9 | copy of this software and associated documentation files (the "Software"), 10 | to deal in the Software without restriction, including without limitation 11 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 | and/or sell copies of the Software, and to permit persons to whom the 13 | Software is furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 24 | DEALINGS IN THE SOFTWARE. 25 | */ 26 | 27 | package main 28 | 29 | import ( 30 | "fmt" 31 | "io" 32 | "math/cmplx" 33 | "os" 34 | 35 | "github.com/lucy/go-fftw" 36 | flag "github.com/lucy/pflag" 37 | "github.com/lucy/termbox-go" 38 | ) 39 | 40 | var ( 41 | color = flag.StringP("color", "c", "default", "Color to use") 42 | dim = flag.BoolP("dim", "d", false, 43 | "Turn off bright colors where possible") 44 | 45 | step = flag.Int("step", 2, "Samples for each step (wave/lines)") 46 | scale = flag.Float64("scale", 2, "Scale divisor (spectrum)") 47 | 48 | icolor = flag.BoolP("icolor", "i", false, 49 | "Color bars according to intensity (spectrum/lines)") 50 | imode = flag.String("imode", "dumb", 51 | "Mode for colorisation (dumb, 256 or grayscale)") 52 | 53 | filename = flag.StringP("file", "f", "/tmp/mpd.fifo", 54 | "Where to read pcm data from") 55 | vis = flag.StringP("viz", "v", "wave", 56 | "Visualisation (spectrum, wave or lines)") 57 | ) 58 | 59 | var colors = map[string]termbox.Attribute{ 60 | "default": termbox.ColorDefault, 61 | "black": termbox.ColorBlack, 62 | "red": termbox.ColorRed, 63 | "green": termbox.ColorGreen, 64 | "yellow": termbox.ColorYellow, 65 | "blue": termbox.ColorBlue, 66 | "magenta": termbox.ColorMagenta, 67 | "cyan": termbox.ColorCyan, 68 | "white": termbox.ColorWhite, 69 | } 70 | 71 | var iColors []termbox.Attribute 72 | 73 | var ( 74 | on = termbox.ColorDefault 75 | off = termbox.ColorDefault 76 | ) 77 | 78 | var maxInt16 float64 = 1<<15 - 1 79 | 80 | func warn(format string, args ...interface{}) { 81 | fmt.Fprintf(os.Stderr, format, args...) 82 | } 83 | 84 | func main() { 85 | flag.Parse() 86 | 87 | if cl, ok := colors[*color]; !ok { 88 | warn("Unknown color \"%s\"\n", *color) 89 | return 90 | } else { 91 | on = cl 92 | } 93 | 94 | if !*dim { 95 | on = on | termbox.AttrBold 96 | } 97 | 98 | switch *imode { 99 | case "dumb": 100 | iColors = []termbox.Attribute{ 101 | termbox.ColorBlue, 102 | termbox.ColorCyan, 103 | termbox.ColorGreen, 104 | termbox.ColorYellow, 105 | termbox.ColorRed, 106 | } 107 | if !*dim { 108 | for i := range iColors { 109 | iColors[i] = iColors[i] + 8 110 | } 111 | } 112 | case "256": 113 | iColors = []termbox.Attribute{ 114 | 21, 27, 39, 45, 51, 86, 85, 84, 82, 115 | 154, 192, 220, 214, 208, 202, 196, 116 | } 117 | case "grayscale": 118 | const num = 19 119 | iColors = make([]termbox.Attribute, num) 120 | for i := termbox.Attribute(0); i < num; i++ { 121 | iColors[i] = i + 255 - num 122 | } 123 | default: 124 | warn("Unsupported mode: \"%s\"\n", *imode) 125 | return 126 | } 127 | 128 | var draw func(*os.File, chan bool) 129 | switch *vis { 130 | case "spectrum": 131 | draw = drawSpectrum 132 | case "wave": 133 | draw = drawWave 134 | case "lines": 135 | draw = drawLines 136 | default: 137 | warn("Unknown visualisation \"%s\"\n"+ 138 | "Supported: spectrum, wave\n", *vis) 139 | return 140 | } 141 | 142 | file, err := os.Open(*filename) 143 | if err != nil { 144 | warn("%s\n", err) 145 | return 146 | } 147 | defer file.Close() 148 | 149 | err = termbox.Init() 150 | if err != nil { 151 | warn("%s\b", err) 152 | return 153 | } 154 | defer termbox.Close() 155 | 156 | end := make(chan bool) 157 | go draw(file, end) 158 | 159 | // input handler 160 | go func() { 161 | for { 162 | ev := termbox.PollEvent() 163 | if ev.Ch == 0 && ev.Key == termbox.KeyCtrlC { 164 | close(end) 165 | return 166 | } 167 | } 168 | }() 169 | 170 | <-end 171 | } 172 | 173 | func size() (int, int) { 174 | w, h := termbox.Size() 175 | return w, h * 2 176 | } 177 | 178 | func drawWave(file *os.File, end chan bool) { 179 | defer close(end) 180 | var ( 181 | inRaw []int16 182 | ibound = len(iColors) - 1 183 | ilen = float64(len(iColors)) 184 | ) 185 | for { 186 | w, h := size() 187 | if s := w * *step; len(inRaw) != s { 188 | inRaw = make([]int16, s) 189 | } 190 | 191 | if readInt16s(file, inRaw) != nil { 192 | return 193 | } 194 | 195 | half_h := float64(h / 2) 196 | div := maxInt16 / half_h 197 | for pos := 0; pos < w; pos++ { 198 | var v float64 199 | for i := 0; i < *step; i++ { 200 | v += float64(inRaw[pos**step+i]) 201 | } 202 | v /= float64(*step) 203 | 204 | if *icolor { 205 | on = iColors[min(ibound, abs(int(v/(maxInt16/ilen))))] 206 | } 207 | 208 | vi := int(v/div + half_h) 209 | if vi%2 == 0 { 210 | termbox.SetCell(pos, vi/2, '▀', on, off) 211 | } else { 212 | termbox.SetCell(pos, vi/2, '▄', on, off) 213 | } 214 | } 215 | 216 | termbox.Flush() 217 | termbox.Clear(off, off) 218 | } 219 | } 220 | 221 | func drawSpectrum(file *os.File, end chan bool) { 222 | defer close(end) 223 | var ( 224 | ilen = len(iColors) - 1 225 | flen = float64(len(iColors)) 226 | resn = -1 227 | in []float64 228 | inRaw []int16 229 | out []complex128 230 | plan *fftw.Plan 231 | ) 232 | 233 | for { 234 | w, h := size() 235 | if resn != w { 236 | w := max(2, w) 237 | if out != nil { 238 | fftw.Free1d(out) 239 | } 240 | resn = w 241 | samples := (w - 1) * 2 242 | in = make([]float64, samples) 243 | inRaw = make([]int16, samples) 244 | out = fftw.Alloc1d(resn) 245 | plan = fftw.PlanDftR2C1d(in, out, fftw.Measure) 246 | } 247 | 248 | if readInt16s(file, inRaw) != nil { 249 | return 250 | } 251 | 252 | for i := range inRaw { 253 | in[i] = float64(inRaw[i]) 254 | } 255 | 256 | plan.Execute() 257 | for i := 0; i < w; i++ { 258 | v := cmplx.Abs(out[i]) / 1e5 / *scale 259 | if *icolor { 260 | on = iColors[min(ilen, int(v*flen))] 261 | } 262 | hd := int(v * float64(h)) 263 | for j := h - 1; j > h-hd; j-- { 264 | termbox.SetCell(i, j/2, '┃', on, off) 265 | } 266 | if hd%2 == 0 { 267 | termbox.SetCell(i, (h-hd)/2, '╻', on, off) 268 | } 269 | } 270 | 271 | termbox.Flush() 272 | termbox.Clear(off, off) 273 | } 274 | } 275 | 276 | type pair struct{ x, y int } 277 | 278 | var dirs = [9]pair{ 279 | {1, 1}, {-1, -1}, 280 | {1, -1}, {-1, 1}, 281 | {1, 0}, {-1, 0}, 282 | {0, 1}, {0, -1}, 283 | {0, 0}, 284 | } 285 | 286 | type coord struct{ x, y, dir int } 287 | 288 | func (c *coord) step(x, y int) { 289 | c.x += dirs[c.dir].x 290 | c.y += dirs[c.dir].y 291 | c.x, c.y = mod(c.x, x), mod(c.y, y) 292 | } 293 | 294 | func drawLines(file *os.File, end chan bool) { 295 | defer close(end) 296 | var ( 297 | c, bc coord 298 | inraw = make([]int16, *step) 299 | hist = make([]pair, 1000) 300 | ilen = len(iColors) - 1 301 | filen = float64(ilen) 302 | ) 303 | 304 | for { 305 | if readInt16s(file, inraw) == io.EOF { 306 | return 307 | } 308 | 309 | var raw float64 310 | for i := range inraw { 311 | raw += float64(inraw[i]) 312 | } 313 | raw /= float64(*step) 314 | c.dir = min(8, abs(int(raw/maxInt16*8))) 315 | bc.dir = 8 - c.dir 316 | if *icolor { 317 | on = iColors[min(ilen, abs(int(raw/maxInt16*filen)))] 318 | } 319 | 320 | w, h := termbox.Size() 321 | bc.step(w, h) 322 | c.step(w, h) 323 | 324 | // TODO: make this more efficient, somehow 325 | hist = append(hist[1:], pair{c.x, c.y}) 326 | 327 | termbox.SetCell(hist[0].x, hist[0].y, ' ', off, off) 328 | termbox.SetCell(c.x, c.y, '#', on, off) 329 | 330 | termbox.Flush() 331 | } 332 | } 333 | -------------------------------------------------------------------------------- /read.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "io" 5 | "reflect" 6 | "unsafe" 7 | ) 8 | 9 | // BenchmarkBinaryReadSlice1000Int16 50000 50957 ns/op 78.50 MB/s 10 | // BenchmarkReadSlice1000Int16 10000000 143 ns/op 27834.15 MB/s 11 | 12 | func readInt16s(r io.Reader, data []int16) error { 13 | size := 2 * len(data) 14 | capa := 2 * cap(data) 15 | h := (*reflect.SliceHeader)(unsafe.Pointer(&data)).Data 16 | buf := *((*[]byte)(unsafe.Pointer(&reflect.SliceHeader{Data: h, Len: size, Cap: capa}))) 17 | _, err := io.ReadFull(r, buf) 18 | return err 19 | } 20 | -------------------------------------------------------------------------------- /read_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/binary" 5 | "testing" 6 | ) 7 | 8 | type byteSliceReader struct { 9 | remain []byte 10 | } 11 | 12 | func (br *byteSliceReader) Read(p []byte) (int, error) { 13 | n := copy(p, br.remain) 14 | br.remain = br.remain[n:] 15 | return n, nil 16 | } 17 | 18 | func BenchmarkBinaryReadSlice1000Int16(b *testing.B) { 19 | bsr := &byteSliceReader{} 20 | slice := make([]int16, 1000) 21 | buf := make([]byte, len(slice)*4) 22 | b.SetBytes(int64(len(buf))) 23 | b.ResetTimer() 24 | for i := 0; i < b.N; i++ { 25 | bsr.remain = buf 26 | binary.Read(bsr, binary.LittleEndian, slice) 27 | } 28 | } 29 | 30 | func BenchmarkReadSlice1000Int16(b *testing.B) { 31 | bsr := &byteSliceReader{} 32 | slice := make([]int16, 1000) 33 | buf := make([]byte, len(slice)*4) 34 | b.SetBytes(int64(len(buf))) 35 | b.ResetTimer() 36 | for i := 0; i < b.N; i++ { 37 | bsr.remain = buf 38 | readInt16s(bsr, slice) 39 | } 40 | } 41 | --------------------------------------------------------------------------------