/
api.go
119 lines (104 loc) · 2.39 KB
/
api.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
// Package bind is for modular binding of mix to audio interface
package bind
import (
"io"
"time"
"gopkg.in/mix.v0/bind/hardware/null"
"gopkg.in/mix.v0/bind/opt"
"gopkg.in/mix.v0/bind/sample"
"gopkg.in/mix.v0/bind/sox"
"gopkg.in/mix.v0/bind/spec"
"gopkg.in/mix.v0/bind/wav"
)
// Configure begins streaming to the bound out audio interface, via a callback function
func Configure(s spec.AudioSpec) {
sample.ConfigureOutput(s)
switch useOutput {
case opt.OutputWAV:
wav.ConfigureOutput(s)
case opt.OutputNull:
null.ConfigureOutput(s)
}
}
func IsDirectOutput() bool {
return useOutput == opt.OutputWAV
}
// SetMixNextOutFunc to stream mix out from mix
func SetOutputCallback(fn sample.OutNextCallbackFunc) {
sample.SetOutputCallback(fn)
}
// OutputStart requires a known length
func OutputStart(length time.Duration, out io.Writer) {
switch useOutput {
case opt.OutputWAV:
wav.OutputStart(length, out)
case opt.OutputNull:
// do nothing
}
}
// OutputNext using the configured writer.
func OutputNext(numSamples spec.Tz) {
switch useOutput {
case opt.OutputWAV:
wav.OutputNext(numSamples)
case opt.OutputNull:
// do nothing
}
}
// LoadWAV into a buffer
func LoadWAV(file string) ([]sample.Sample, *spec.AudioSpec) {
switch useLoader {
case opt.InputWAV:
return wav.Load(file)
case opt.InputSOX:
return sox.Load(file)
default:
return make([]sample.Sample, 0), &spec.AudioSpec{}
}
}
// Teardown to close all hardware bindings
func Teardown() {
switch useOutput {
case opt.OutputWAV:
wav.TeardownOutput()
case opt.OutputNull:
// do nothing
}
}
// UseLoader to select the file loading interface
func UseLoader(opt opt.Input) {
useLoader = opt
}
// UseLoaderString to select the file loading interface by string
func UseLoaderString(loader string) {
switch loader {
case string(opt.InputWAV):
useLoader = opt.InputWAV
case string(opt.InputSOX):
useLoader = opt.InputSOX
default:
panic("No such Loader: " + loader)
}
}
// UseOutput to select the outback interface
func UseOutput(opt opt.Output) {
useOutput = opt
}
// UseOutputString to select the outback interface by string
func UseOutputString(output string) {
switch output {
case string(opt.OutputWAV):
useOutput = opt.OutputWAV
case string(opt.OutputNull):
useOutput = opt.OutputNull
default:
panic("No such Output: " + output)
}
}
//
// Private
//
var (
useLoader = opt.InputWAV
useOutput = opt.OutputNull
)