This repository has been archived by the owner on May 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
options.go
290 lines (242 loc) · 8 KB
/
options.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package waveform
import "fmt"
var (
// errBGColorFunctionNil is returned when a nil ColorFunc is used in
// a call to BGColorFunction.
errBGColorFunctionNil = &OptionsError{
Option: "bgColorFunction",
Reason: "function cannot be nil",
}
// errFGColorFunctionNil is returned when a nil ColorFunc is used in
// a call to FGColorFunction.
errFGColorFunctionNil = &OptionsError{
Option: "fgColorFunction",
Reason: "function cannot be nil",
}
// errSampleFunctionNil is returned when a nil SampleReduceFunc is used in
// a call to SampleFunc.
errSampleFunctionNil = &OptionsError{
Option: "sampleFunction",
Reason: "function cannot be nil",
}
// errResolutionZero is returned when integer 0 is used in a call
// to Resolution.
errResolutionZero = &OptionsError{
Option: "resolution",
Reason: "resolution cannot be 0",
}
// errScaleXZero is returned when integer 0 is used as the X value
// in a call to Scale.
errScaleXZero = &OptionsError{
Option: "scale",
Reason: "X scale cannot be 0",
}
// errScaleYZero is returned when integer 0 is used as the Y value
// in a call to Scale.
errScaleYZero = &OptionsError{
Option: "scale",
Reason: "Y scale cannot be 0",
}
)
// OptionsError is an error which is returned when invalid input
// options are set on a Waveform struct.
type OptionsError struct {
Option string
Reason string
}
// Error returns the string representation of an OptionsError.
func (e *OptionsError) Error() string {
return fmt.Sprintf("%s: %s", e.Option, e.Reason)
}
// OptionsFunc is a function which is applied to an input Waveform
// struct, and can manipulate its properties.
type OptionsFunc func(*Waveform) error
// SetOptions applies zero or more OptionsFunc to the receiving Waveform
// struct, manipulating its properties.
func (w *Waveform) SetOptions(options ...OptionsFunc) error {
for _, o := range options {
// Do not apply nil function arguments
if o == nil {
continue
}
if err := o(w); err != nil {
return err
}
}
return nil
}
// BGColorFunction generates an OptionsFunc which applies the input background
// ColorFunc to an input Waveform struct.
//
// This function is used to apply a variety of color schemes to the background
// of a waveform image, and is called during each drawing loop of the background
// image.
func BGColorFunction(function ColorFunc) OptionsFunc {
return func(w *Waveform) error {
return w.setBGColorFunction(function)
}
}
// SetBGColorFunction applies the input ColorFunc to the receiving Waveform
// struct for background use.
func (w *Waveform) SetBGColorFunction(function ColorFunc) error {
return w.SetOptions(BGColorFunction(function))
}
// setBGColorFunction directly sets the background ColorFunc member of the
// receiving Waveform struct.
func (w *Waveform) setBGColorFunction(function ColorFunc) error {
// Function cannot be nil
if function == nil {
return errBGColorFunctionNil
}
w.bgColorFn = function
return nil
}
// FGColorFunction generates an OptionsFunc which applies the input foreground
// ColorFunc to an input Waveform struct.
//
// This function is used to apply a variety of color schemes to the foreground
// of a waveform image, and is called during each drawing loop of the foreground
// image.
func FGColorFunction(function ColorFunc) OptionsFunc {
return func(w *Waveform) error {
return w.setFGColorFunction(function)
}
}
// SetFGColorFunction applies the input ColorFunc to the receiving Waveform
// struct for foreground use.
func (w *Waveform) SetFGColorFunction(function ColorFunc) error {
return w.SetOptions(FGColorFunction(function))
}
// setFGColorFunction directly sets the foreground ColorFunc member of the
// receiving Waveform struct.
func (w *Waveform) setFGColorFunction(function ColorFunc) error {
// Function cannot be nil
if function == nil {
return errFGColorFunctionNil
}
w.fgColorFn = function
return nil
}
// Resolution generates an OptionsFunc which applies the input resolution
// value to an input Waveform struct.
//
// This value indicates the number of times audio is read and drawn
// as a waveform, per second of audio.
func Resolution(resolution uint) OptionsFunc {
return func(w *Waveform) error {
return w.setResolution(resolution)
}
}
// SetResolution applies the input resolution to the receiving Waveform struct.
func (w *Waveform) SetResolution(resolution uint) error {
return w.SetOptions(Resolution(resolution))
}
// setResolution directly sets the resolution member of the receiving Waveform
// struct.
func (w *Waveform) setResolution(resolution uint) error {
// Resolution cannot be zero
if resolution == 0 {
return errResolutionZero
}
w.resolution = resolution
return nil
}
// SampleFunc generates an OptionsFunc which applies the input SampleReduceFunc
// to an input Waveform struct.
//
// This function is used to compute values from audio samples, for use in
// waveform generation. The function is applied over a slice of float64
// audio samples, reducing them to a single value.
func SampleFunction(function SampleReduceFunc) OptionsFunc {
return func(w *Waveform) error {
return w.setSampleFunction(function)
}
}
// SetSampleFunction applies the input SampleReduceFunc to the receiving Waveform
// struct.
func (w *Waveform) SetSampleFunction(function SampleReduceFunc) error {
return w.SetOptions(SampleFunction(function))
}
// setSampleFunction directly sets the SampleReduceFunc member of the receiving
// Waveform struct.
func (w *Waveform) setSampleFunction(function SampleReduceFunc) error {
// Function cannot be nil
if function == nil {
return errSampleFunctionNil
}
w.sampleFn = function
return nil
}
// Scale generates an OptionsFunc which applies the input X and Y axis scaling
// factors to an input Waveform struct.
//
// This value indicates how a generated waveform image will be scaled, for both
// its X and Y axes.
func Scale(x uint, y uint) OptionsFunc {
return func(w *Waveform) error {
return w.setScale(x, y)
}
}
// SetScale applies the input X and Y axis scaling to the receiving Waveform
// struct.
func (w *Waveform) SetScale(x uint, y uint) error {
return w.SetOptions(Scale(x, y))
}
// setScale directly sets the scaleX and scaleY members of the receiving Waveform
// struct.
func (w *Waveform) setScale(x uint, y uint) error {
// X scale cannot be zero
if x == 0 {
return errScaleXZero
}
// Y scale cannot be zero
if y == 0 {
return errScaleYZero
}
w.scaleX = x
w.scaleY = y
return nil
}
// ScaleClipping generates an OptionsFunc which sets the scaleClipping member
// to true on an input Waveform struct.
//
// This value indicates if the waveform image should be scaled down on its Y-axis
// when clipping thresholds are reached. This can be used to show a more accurate
// waveform when the input audio stream exhibits signs of clipping.
func ScaleClipping() OptionsFunc {
return func(w *Waveform) error {
return w.setScaleClipping(true)
}
}
// SetScaleClipping applies sets the scaleClipping member true for the receiving
// Waveform struct.
func (w *Waveform) SetScaleClipping() error {
return w.SetOptions(ScaleClipping())
}
// setScaleClipping directly sets the scaleClipping member of the receiving Waveform
// struct.
func (w *Waveform) setScaleClipping(scaleClipping bool) error {
w.scaleClipping = scaleClipping
return nil
}
// Sharpness generates an OptionsFunc which applies the input sharpness
// value to an input Waveform struct.
//
// This value indicates the amount of curvature which is applied to a
// waveform image, scaled on its X-axis. A higher value results in steeper
// curves, and a lower value results in more "blocky" curves.
func Sharpness(sharpness uint) OptionsFunc {
return func(w *Waveform) error {
return w.setSharpness(sharpness)
}
}
// SetSharpness applies the input sharpness to the receiving Waveform struct.
func (w *Waveform) SetSharpness(sharpness uint) error {
return w.SetOptions(Sharpness(sharpness))
}
// setSharpness directly sets the sharpness member of the receiving Waveform
// struct.
func (w *Waveform) setSharpness(sharpness uint) error {
w.sharpness = sharpness
return nil
}