-
Notifications
You must be signed in to change notification settings - Fork 1
/
play.go
171 lines (142 loc) · 3.45 KB
/
play.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
package main
import (
"bytes"
"os"
"runtime"
"strconv"
"sync"
"time"
"github.com/godsic/malgo"
"github.com/rivo/tview"
wav "github.com/youpy/go-wav"
)
type SafeBuffer struct {
b bytes.Buffer
mux sync.Mutex
}
var (
device = new(malgo.Device)
deviceConfig = malgo.DefaultDeviceConfig()
playerCtl = make(chan int)
buffer SafeBuffer
contextConfig = malgo.ContextConfig{ThreadPriority: malgo.ThreadPriorityRealtime,
Alsa: malgo.AlsaContextConfig{UseVerboseDeviceEnumeration: 1},
}
)
func bitsPerSampleToDeviceFormat(bitsPerSample int) malgo.FormatType {
switch bitsPerSample {
case 24:
return malgo.FormatS24
case 32:
return malgo.FormatS32
case 16:
return malgo.FormatS16
default:
return malgo.FormatUnknown
}
}
var (
cardNum int
ctx *malgo.AllocatedContext
d malgo.DeviceInfo
)
func closeCard() {
err := ctx.Uninit()
if err != nil {
vibeLogger.Println(err)
}
ctx.Free()
}
func chooseCard() error {
backends := []malgo.Backend{malgo.BackendAlsa, malgo.BackendWasapi, malgo.BackendNull}
var err error
ctx, err = malgo.InitContext(backends, contextConfig, miniaudioLogger)
if err != nil {
return err
}
devices, _ := ctx.Devices(malgo.Playback)
done := make(chan int)
list := tview.NewList()
for n, dd := range devices {
// fmt.Println(n+1, ": ", s.Name)
list.AddItem(dd.Name(), "", rune(strconv.Itoa(n)[0]), func() { done <- 0 })
}
app.SetRoot(list, true).Draw()
<-done
d = devices[list.GetCurrentItem()]
return nil
}
func initSource() (err error) {
deviceConfig.DeviceType = malgo.Playback
deviceConfig.Playback.DeviceID = &d.ID
deviceConfig.Playback.ShareMode = malgo.Exclusive
deviceConfig.PeriodSizeInMilliseconds = 4
deviceConfig.Periods = 16
deviceConfig.Playback.Channels = uint32(2)
deviceConfig.SampleRate = uint32(source.dev.(*Source).SampleRate)
deviceConfig.Playback.Format = source.dev.(*Source).SampleFormat
deviceConfig.Wasapi.NoAutoConvertSRC = 1
deviceConfig.Wasapi.NoDefaultQualitySRC = 0
deviceConfig.Wasapi.NoHardwareOffloading = 1
deviceConfig.Wasapi.NoAutoStreamRouting = 1
deviceConfig.Alsa.NoMMap = 0
deviceConfig.Alsa.NoAutoResample = 1
deviceConfig.Alsa.NoAutoFormat = 1
deviceConfig.Alsa.NoAutoChannels = 1
deviceConfig.NoClip = 1
deviceConfig.NoPreZeroedOutputBuffer = 0
// This is the function that's used for sending more data to the device for playback.
onData := func(outputSamples, inputSamples []byte, frameCount uint32) {
runtime.LockOSThread()
buffer.mux.Lock()
if *jitter {
tIn := time.Now()
n, _ := buffer.b.Read(outputSamples)
tOut := time.Now()
jd := jitterData{timeIn: tIn, timeOut: tOut, requestedBytes: frameCount, readBytes: n}
timeChannel <- jd
} else {
buffer.b.Read(outputSamples)
}
buffer.mux.Unlock()
runtime.UnlockOSThread()
}
deviceCallbacks := malgo.DeviceCallbacks{
Data: onData,
}
device, err = malgo.InitDevice(ctx.Context, deviceConfig, deviceCallbacks)
if err != nil {
vibeLogger.Println(err)
return err
}
return nil
}
func play() error {
err := device.Start()
if err != nil {
vibeLogger.Fatal(err)
}
return nil
}
func pause() error {
if device.IsStarted() {
err := device.Stop()
if err != nil {
vibeLogger.Fatal(err)
}
}
return nil
}
func loadFileIntoBuffer(fname string) error {
f, err := os.Open(fname)
if err != nil {
return err
}
defer f.Close()
r := wav.NewReader(f)
buffer.mux.Lock()
buffer.b.Reset()
buffer.b.ReadFrom(r)
buffer.mux.Unlock()
return nil
}