music - visualise.go
1 package music
2
3 import (
4 "image/draw"
5
6 "vimagination.zapto.org/limage/lcolor"
7 )
8
9 var (
10 topLine = lcolor.RGB{R: 255}
11 middleLine = lcolor.RGB{G: 255}
12 bottomLine = lcolor.RGB{B: 255}
13 point = lcolor.RGB{}
14 between = lcolor.RGB{R: 127, G: 127, B: 127}
15 )
16
17 func (p *Player) Visualise(im draw.Image, channels int) {
18 if channels < 1 {
19 return
20 }
21 b := im.Bounds()
22 n := b.Dx()
23 sounds := make([][]float32, channels)
24 for i := range sounds {
25 sounds[i] = make([]float32, n)
26 }
27 p.mu.Lock()
28 p.processMusic(sounds)
29 p.mu.Unlock()
30 channelHeight := b.Dy() / channels
31 top := 0
32 middle := channelHeight >> 1
33 bottom := channelHeight
34 for _, channel := range sounds {
35 x := b.Min.X
36 last := 0
37 for _, sample := range channel {
38 p := int(sample*float32(channelHeight>>1)) + middle
39 im.Set(x, top, topLine)
40 im.Set(x, middle, middleLine)
41 im.Set(x, bottom, bottomLine)
42 if last < p {
43 for i := last; i < p; i++ {
44 im.Set(x, i, between)
45 }
46 } else if last > p {
47 for i := last; i > p; i-- {
48 im.Set(x, i, between)
49 }
50 }
51 last = p
52 im.Set(x, p, point)
53 x++
54 }
55 top += channelHeight
56 middle += channelHeight
57 bottom += channelHeight
58 }
59 }