This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_file_flac.go
157 lines (138 loc) · 3.24 KB
/
device_file_flac.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
// +build flac
package gosound
import (
"bufio"
"context"
"errors"
"os"
"github.com/mewkiz/flac"
"github.com/mewkiz/flac/frame"
"github.com/mewkiz/flac/meta"
"github.com/gotracker/gomixing/mixing"
)
type fileDeviceFlac struct {
fileDevice
mix mixing.Mixer
samplesPerSecond int
f *os.File
w *bufio.Writer
}
func newFileFlacDevice(settings Settings) (Device, error) {
fd := fileDeviceFlac{
fileDevice: fileDevice{
device: device{
onRowOutput: settings.OnRowOutput,
},
},
mix: mixing.Mixer{
Channels: settings.Channels,
BitsPerSample: settings.BitsPerSample,
},
samplesPerSecond: settings.SamplesPerSecond,
}
f, err := os.OpenFile(settings.Filepath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
return nil, err
}
if f == nil {
return nil, errors.New("unexpected file error")
}
fd.f = f
return &fd, nil
}
// Play starts the wave output device playing
func (d *fileDeviceFlac) Play(in <-chan *PremixData) error {
return d.PlayWithCtx(context.Background(), in)
}
// PlayWithCtx starts the wave output device playing
func (d *fileDeviceFlac) PlayWithCtx(ctx context.Context, in <-chan *PremixData) error {
w := bufio.NewWriter(d.f)
d.w = w
// Encode FLAC stream.
si := &meta.StreamInfo{
BlockSizeMin: 16,
BlockSizeMax: 65535,
SampleRate: uint32(d.samplesPerSecond),
NChannels: uint8(d.mix.Channels),
BitsPerSample: uint8(d.mix.BitsPerSample),
}
enc, err := flac.NewEncoder(w, si)
if err != nil {
return err
}
defer enc.Close()
panmixer := mixing.GetPanMixer(d.mix.Channels)
if panmixer == nil {
return errors.New("invalid pan mixer - check channel count")
}
var channels frame.Channels
switch d.mix.Channels {
case 1:
channels = frame.ChannelsMono
case 2:
channels = frame.ChannelsLR
case 4:
channels = frame.ChannelsLRLsRs
}
myCtx, cancel := context.WithCancel(ctx)
defer cancel()
for {
select {
case <-myCtx.Done():
return myCtx.Err()
case row, ok := <-in:
if !ok {
return nil
}
mixedData := d.mix.FlattenToInts(panmixer, row.SamplesLen, row.Data, row.MixerVolume)
subframes := make([]*frame.Subframe, d.mix.Channels)
for i := range subframes {
subframe := &frame.Subframe{
SubHeader: frame.SubHeader{
Pred: frame.PredVerbatim,
},
Samples: mixedData[i],
NSamples: row.SamplesLen,
}
subframes[i] = subframe
}
for _, subframe := range subframes {
sample := subframe.Samples[0]
constant := true
for _, s := range subframe.Samples[1:] {
if sample != s {
constant = false
}
}
if constant {
subframe.SubHeader.Pred = frame.PredConstant
}
}
fr := &frame.Frame{
Header: frame.Header{
HasFixedBlockSize: false,
BlockSize: uint16(row.SamplesLen),
SampleRate: uint32(d.samplesPerSecond),
Channels: channels,
BitsPerSample: uint8(d.mix.BitsPerSample),
},
Subframes: subframes,
}
if err := enc.WriteFrame(fr); err != nil {
return err
}
if d.onRowOutput != nil {
d.onRowOutput(KindFile, row)
}
}
}
}
// Close closes the wave output device
func (d *fileDeviceFlac) Close() {
d.w.Flush()
d.w = nil
d.f.Close()
}
func init() {
fileDeviceMap[".flac"] = newFileFlacDevice
}