1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
//
// rhlibrary
//
// The Radio Helsinki Rivendell Library
//
//
// Copyright (C) 2016 Christian Pointner <equinox@helsinki.at>
//
// This file is part of rhlibrary.
//
// rhlibrary 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
// any later version.
//
// rhlibrary 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 rhlibrary. If not, see <http://www.gnu.org/licenses/>.
//
package main
import (
"time"
"code.helsinki.at/rhrd-go/player"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
const (
meterDBFloor float64 = 72 // -72 db is the lowest VU value
meterSegmentsGreen float64 = 21
meterSegmentsOrange float64 = 3
meterSegmentsRed float64 = 1
meterSegmentsTotal float64 = meterSegmentsGreen + meterSegmentsOrange + meterSegmentsRed
)
var (
meterBarCSS *gtk.CssProvider = nil
)
func getMeterBarCSS() (*gtk.CssProvider, error) {
if meterBarCSS != nil {
return meterBarCSS, nil
}
var err error
if meterBarCSS, err = gtk.CssProviderNew(); err != nil {
return nil, err
}
err = meterBarCSS.LoadFromData(`
.level-bar {
-GtkLevelBar-min-block-width: 15;
-GtkLevelBar-min-block-height: 9;
}
.level-bar.trough {
border: 0;
padding: 0;
border-radius: 0;
background-image: unset;
}
.level-bar.fill-block.level-green {
border-color: #006600;
background-image: linear-gradient(to bottom, #36B736, #006600);
}
.level-bar.fill-block.level-orange {
border-color: #AE6600;
background-image: linear-gradient(to bottom, #FFAC36, #AE6600);
}
.level-bar.fill-block.level-red {
border-color: #AE0000;
background-image: linear-gradient(to bottom, #FF3636, #AE0000);
}`)
if err != nil {
return nil, err
}
return meterBarCSS, nil
}
type vuMeterLevel struct {
green float64
orange float64
red float64
}
func (lvl *vuMeterLevel) setLevel(valdb float64) {
val := valdb + meterDBFloor
if val < 0 {
val = 0
} else {
val = (val / meterDBFloor) * meterSegmentsTotal
}
lvl.green = val
lvl.orange = val - meterSegmentsGreen
lvl.red = lvl.orange - meterSegmentsOrange
if lvl.orange < 0 {
lvl.orange = 0
}
if lvl.red < 0 {
lvl.red = 0
}
return
}
type vuMeter struct {
green *gtk.LevelBar
orange *gtk.LevelBar
red *gtk.LevelBar
}
func createMeterBar(segments float64) (bar *gtk.LevelBar, err error) {
if bar, err = gtk.LevelBarNewForInterval(0, segments); err != nil {
return
}
bar.SetMode(gtk.LEVEL_BAR_MODE_DISCRETE)
bar.RemoveOffsetValue(gtk.LEVEL_BAR_OFFSET_LOW)
bar.RemoveOffsetValue(gtk.LEVEL_BAR_OFFSET_HIGH)
var sc *gtk.StyleContext
if sc, err = bar.GetStyleContext(); err != nil {
return
}
var cp *gtk.CssProvider
if cp, err = getMeterBarCSS(); err != nil {
return
}
sc.AddProvider(cp, 600) // TOOD: hardcoded value
return
}
func newVUMeter() (meter *vuMeter, err error) {
meter = &vuMeter{}
if meter.green, err = createMeterBar(meterSegmentsGreen); err != nil {
return
}
if meter.orange, err = createMeterBar(meterSegmentsOrange); err != nil {
return
}
if meter.red, err = createMeterBar(meterSegmentsRed); err != nil {
return
}
meter.green.AddOffsetValue("green", 0)
meter.orange.AddOffsetValue("orange", 0)
meter.red.AddOffsetValue("red", 0)
return
}
func (m *vuMeter) SetValue(lvl *vuMeterLevel) {
m.green.SetValue(lvl.green)
m.orange.SetValue(lvl.orange)
m.red.SetValue(lvl.red)
}
func updateVUMeterLevels(left, right *vuMeter, data player.Meter) {
ch := len(data)
lval := &vuMeterLevel{0, 0, 0}
rval := &vuMeterLevel{0, 0, 0}
if ch >= 2 {
lval.setLevel(data[0].Peak)
rval.setLevel(data[1].Peak)
} else if ch == 1 {
lval.setLevel(data[0].Peak)
rval = lval
}
left.SetValue(lval)
right.SetValue(rval)
}
func addVUMeterGrid(frame *gtk.Frame, p *player.PlayerChan) (err error) {
var grid *gtk.Grid
if grid, err = gtk.GridNew(); err != nil {
return
}
grid.SetRowSpacing(3)
var left, right *vuMeter
if left, err = newVUMeter(); err != nil {
return
}
if right, err = newVUMeter(); err != nil {
return
}
p.AddUpdateHandler(func(_ time.Duration, _ time.Duration, meter player.Meter, _ interface{}) bool {
glib.IdleAdd(func() { updateVUMeterLevels(left, right, meter) })
return true
}, nil)
grid.Attach(left.green, 1, 1, 1, 1)
grid.Attach(left.orange, 2, 1, 1, 1)
grid.Attach(left.red, 3, 1, 1, 1)
grid.Attach(right.green, 1, 2, 1, 1)
grid.Attach(right.orange, 2, 2, 1, 1)
grid.Attach(right.red, 3, 2, 1, 1)
grid.SetRowHomogeneous(true)
frame.Add(grid)
return
}
func getVUMeterWidget(p *player.PlayerChan) (gtk.IWidget, error) {
frame, err := gtk.FrameNew("")
if err != nil {
return nil, err
}
var cp *gtk.CssProvider
if cp, err = gtk.CssProviderNew(); err != nil {
return nil, err
}
if err = cp.LoadFromData(".frame { padding: 3px; }"); err != nil {
return nil, err
}
var sc *gtk.StyleContext
if sc, err = frame.GetStyleContext(); err != nil {
return nil, err
}
sc.AddProvider(cp, 600) // TOOD: hardcoded value
if err = addVUMeterGrid(frame, p); err != nil {
return nil, err
}
var box *gtk.Box
if box, err = gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 7); err != nil {
return nil, err
}
box.PackStart(frame, false, false, 3)
return box, nil
}
|