-
Notifications
You must be signed in to change notification settings - Fork 2
/
mixer.go
185 lines (158 loc) · 4.04 KB
/
mixer.go
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
package main
import (
"image/color"
"image/draw"
"image"
"github.com/skelterjohn/go.wde"
"github.com/sqweek/sqribe/midi"
"github.com/sqweek/sqribe/score"
)
type MixVolume struct {
Gain float64
Muted bool
}
type MixConfig struct {
Master, Midi, Wave MixVolume
MuteMetronome bool
Staff map[*score.Staff]*StaffMix
preSolo map[*score.Staff]bool // records Muted status of staves before entering solo
}
type StaffMix struct {
Voice int
Velocity int
Muted bool
}
var Mixer MixConfig
func init() {
Mixer.Staff = make(map[*score.Staff]*StaffMix)
Mixer.Master.Gain = 1.0
Mixer.Midi.Gain = 1.0
Mixer.Wave.Gain = 1.0
}
func (m *MixConfig) LoadStaff(staff *score.Staff, saved SavedStaff) {
stm := m.For(staff)
stm.Voice = saved.Voice
stm.Velocity = saved.Velocity + 100
stm.Muted = saved.Muted
}
func (m *MixConfig) For(staff *score.Staff) *StaffMix {
if sm, ok := m.Staff[staff]; ok {
return sm
}
m.Staff[staff] = &StaffMix{midi.InstPiano, 100, false}
return m.Staff[staff]
}
func (m *MixConfig) IsSolo(s *score.Staff) bool {
if m.For(s).Muted {
return false
}
for staff, mix := range m.Staff {
if staff != s && !mix.Muted {
return false
}
}
return true
}
func (m *MixConfig) ToggleSolo(s *score.Staff) {
if m.IsSolo(s) {
for staff, mix := range m.Staff {
old := false
if m.preSolo != nil {
old, _ = m.preSolo[staff] // old remains false if staff not present in map
}
mix.Muted = old
}
m.preSolo = nil
} else {
m.preSolo = make(map[*score.Staff]bool)
for staff, mix := range m.Staff {
m.preSolo[staff] = mix.Muted
mix.Muted = (staff != s)
}
}
}
type VolLayout struct {
r, icon, slide image.Rectangle
}
func (v *VolLayout) layout(r image.Rectangle) {
v.r = r
v.icon = centerV(leftH(box(16, 16), r), r)
v.slide = image.Rectangle{image.Pt(v.icon.Max.X + 1, r.Min.Y), r.Max}
}
type MixWidget struct {
ImageWidget
mLevel, wLevel float64
layout struct {
master, midi, wave VolLayout
}
}
func NewMixWidget(refresh chan Widget) *MixWidget {
var mw MixWidget
mw.refresh = refresh
return &mw
}
func (m *MixWidget) Levels(mid, wav float64) {
m.mLevel = mid
m.wLevel = wav
m.refresh <- m
}
func (m *MixWidget) Toggle(mute *bool) {
toggle(mute)
m.refresh <- m
}
func (m *MixWidget) AdjustGain(gain *float64, δ float64) {
(*gain) += δ
m.refresh <- m
}
func (m *MixWidget) LeftClick(mouse image.Point) {
m.click(mouse, -0.1)
}
func (m *MixWidget) RightClick(mouse image.Point) {
m.click(mouse, 0.1)
}
func (m *MixWidget) click(mouse image.Point, δ float64) {
if mouse.In(m.layout.master.r) {
m.AdjustGain(&Mixer.Master.Gain, δ)
} else if mouse.In(m.layout.midi.r) {
m.AdjustGain(&Mixer.Midi.Gain, δ)
} else if mouse.In(m.layout.wave.r) {
m.AdjustGain(&Mixer.Wave.Gain, δ)
}
}
func (m *MixWidget) Draw(screen wde.Image, r image.Rectangle) {
dst, changed := m.Img(r)
if changed {
hbox := leftH(box(r.Dx(), (r.Dy() - 2) / 3), r)
m.layout.master.layout(topV(hbox, r))
m.layout.midi.layout(centerV(hbox, r))
m.layout.wave.layout(botV(hbox, r))
}
drawvol(dst, m.layout.master, Mixer.Master, IconVol, 0)
drawvol(dst, m.layout.midi, Mixer.Midi, IconMidi, m.mLevel)
drawvol(dst, m.layout.wave, Mixer.Wave, IconWave, m.wLevel)
screen.CopyRGBA(dst, r)
}
func drawvol(dst draw.Image, layout VolLayout, vol MixVolume, icon *image.Alpha, level float64) {
bg := color.RGBA{0xcc, 0xcc, 0xcc, 0xff}
fg := color.RGBA{0x00, 0x00, 0x00, 0xff}
if vol.Muted {
fg = color.RGBA{0x88, 0x88, 0x88, 0xff}
}
p := vol.Gain / 4.0
if p > 0.97 {
p = 0.97
}
draw.Draw(dst, layout.r, &image.Uniform{bg}, image.ZP, draw.Src)
if level != 0 {
lev := layout.r.Inset(1)
lev.Max.X = lev.Min.X + int(float64(lev.Dx())*level)
draw.Draw(dst, lev, &image.Uniform{levelCB.At(level)}, image.ZP, draw.Src)
}
draw.DrawMask(dst, layout.icon, &image.Uniform{fg}, image.ZP, icon, image.ZP, draw.Over)
drawHorzSlider(dst, layout.slide, fg, p)
}
var levelCB ColourBar = ColourBar{[]ColourPoint{
{0.50, color.NRGBA{0x00, 0xff, 0x00, 0xff}},
{0.75, color.NRGBA{0xff, 0xff, 0x00, 0xff}},
{1.00, color.NRGBA{0xff, 0x00, 0x00, 0xff}},
}}