-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendcat.go
191 lines (144 loc) · 2.93 KB
/
sendcat.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
package main
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"github.com/yobert/alsa"
)
func main() {
cards, err := alsa.OpenCards()
if err != nil {
fmt.Println(err)
return
}
defer alsa.CloseCards(cards)
for _, card := range cards {
fmt.Println(card)
if err := beep_card(card); err != nil {
fmt.Println(err)
return
}
}
}
func beep_card(card *alsa.Card) error {
devices, err := card.Devices()
if err != nil {
return err
}
for _, device := range devices {
if device.Type != alsa.PCM || !device.Play {
continue
}
fmt.Println("───", device)
if err := beep_device(device); err != nil {
return err
}
}
return nil
}
type Cmd int
const (
start Cmd = iota
pause
quit
sethz
setamp
setphasea
setphaseb
)
type beepdo struct {
cmd Cmd
value float
}
// Beeper makes a sine wave while awaiting commands
//
func beeper(device *alsa.Device, commands chan beepdo ) {
mytime float
defer device.Close()
}
func negotiate(device *alsa.Device) error {
var err error
if err = device.Open(); err != nil {
return err
}
channels, err := device.NegotiateChannels(1, 2)
if err != nil {
return err
}
rate, err := device.NegotiateRate(44100)
if err != nil {
return err
}
format, err := device.NegotiateFormat(alsa.S16_LE, alsa.S32_LE)
if err != nil {
return err
}
buffer_size, err := device.NegotiateBufferSize(1024, 8192, 16384)
if err != nil {
return err
}
if err = device.Prepare(); err != nil {
return err
}
fmt.Printf("Negotiated parameters: %d channels, %d hz, %v, %d frame buffer\n",
channels, rate, format, buffer_size)
return nil
}
func beep_device(device *alsa.Device) error {
var err error
if err != nil {
fmt.Println(err)
}
if err = device.Open(); err != nil {
return err
}
defer device.Close()
channels, err := device.NegotiateChannels(1, 2)
if err != nil {
return err
}
rate, err := device.NegotiateRate(44100)
if err != nil {
return err
}
format, err := device.NegotiateFormat(alsa.S16_LE, alsa.S32_LE)
if err != nil {
return err
}
buffer_size, err := device.NegotiateBufferSize(1024, 8192, 16384)
if err != nil {
return err
}
if err = device.Prepare(); err != nil {
return err
}
buf := bytes.NewBuffer(nil)
t := 0.0
for {
buf.Reset()
for i := 0; i < buffer_size; i++ {
v := math.Sin(t * 2 * math.Pi * 512) // A4
v *= 0.05 // make a little quieter
switch format {
case alsa.S16_LE:
sample := int16(v * ((1 << 16) - 1))
for c := 0; c < channels; c++ {
binary.Write(buf, binary.LittleEndian, sample)
}
case alsa.S32_LE:
sample := int32(v * ((1 << 32) - 1))
for c := 0; c < channels; c++ {
binary.Write(buf, binary.LittleEndian, sample)
}
default:
return fmt.Errorf("Unhandled sample format: %v", format)
}
t += 1.0 / float64(rate)
}
if err := device.Write(buf.Bytes(), buffer_size); err != nil {
return err
}
}
return nil
}