-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfloatbeat.go
More file actions
185 lines (148 loc) · 4.42 KB
/
Copy pathfloatbeat.go
File metadata and controls
185 lines (148 loc) · 4.42 KB
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
package main
import (
"github.com/gordonklaus/portaudio"
"github.com/colourcountry/d4"
"github.com/gorilla/websocket"
"net/http"
"encoding/json"
"fmt"
"flag"
"os"
"bufio"
"strings"
"io"
"io/ioutil"
"log"
)
const sampleRate = 11025
var filename *string = flag.String("file", "", "Source file to read")
var LIVE *Floatbeat
func chk(err error) {
if err != nil {
panic(err)
}
}
type Floatbeat struct {
d4.Machine
*portaudio.Stream
*websocket.Conn
}
type Message struct {
Cmd string "cmd"
Body string "body"
Value float64 "value"
}
func findImport(name string) (string, error) {
name = strings.ToLower(name)
filename := "lib/"+name+".d4"
log.Printf("Reading imported file %s", filename)
s, err := ioutil.ReadFile(filename)
if err != nil {
return "", fmt.Errorf("Couldn't read import %s", name)
}
return string(s), nil
}
func newFloatbeat(in io.Reader, sampleRate float64) *Floatbeat {
m, err := d4.NewMachine(in, sampleRate, 1.0, 10.0, findImport, 1)
chk(err)
s := &Floatbeat{m, nil, nil}
s.Stream, err = portaudio.OpenDefaultStream(0, 1, sampleRate, 0, s.processAudio)
chk(err)
return s
}
func (f *Floatbeat) setMachine(m d4.Machine) {
f.Machine = m
}
func (f *Floatbeat) setConn(c *websocket.Conn) {
f.Conn = c
}
func (f *Floatbeat) processAudio(out [][]float32) {
//fmt.Println("Need",len(out[0]),"bytes from",f.Machine)
err := f.Machine.Fill32(out[0]) // FIXME: multiple workers currently broken
if (err != nil) {
if (f.Conn != nil) {
_ = f.Conn.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("%s", err)));
} else {
fmt.Println(err)
}
}
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func print_binary(s []byte) {
fmt.Printf("Received b:");
for n := 0;n < len(s);n++ {
fmt.Printf("%d,",s[n]);
}
fmt.Printf("\n");
}
func wsHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
LIVE.setConn(conn)
if err != nil {
//log.Println(err)
return
}
for {
messageType, p, err := conn.ReadMessage()
//fmt.Println("Got message", string(p), messageType)
if err != nil || messageType != websocket.TextMessage {
continue
}
var msg Message
err = json.Unmarshal(p, &msg)
if err != nil {
_ = conn.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("JSON error: %s", err)));
continue
}
//fmt.Println("Got command",msg.Cmd,"body =",msg.Body,"value =",msg.Value)
switch msg.Cmd {
case "set":
err := LIVE.Machine.Set(msg.Body, msg.Value)
if err != nil {
_ = conn.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("%s", err)));
continue
}
case "code":
m, err := d4.CloneMachine(strings.NewReader(msg.Body), LIVE.Machine)
if err != nil {
_ = conn.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("%s", err)));
continue
}
_, err = m.Run()
if err != nil {
_ = conn.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("Not installing: %s", err)));
continue
}
//fmt.Println("Installing %s",m)
LIVE.setMachine(m)
_ = conn.WriteMessage(websocket.TextMessage, []byte("OK"));
default:
_ = conn.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("Read error: unknown command %s", msg.Cmd)));
continue
}
}
}
func main() {
flag.Parse()
portaudio.Initialize()
defer portaudio.Terminate()
var in io.Reader
if *filename != "" {
opened_file, err := os.OpenFile(*filename, os.O_RDONLY, 0755); chk(err)
log.Println( "Opened file", *filename )
in = bufio.NewReader( opened_file )
} else {
in = strings.NewReader( "440 Hz t*sin." )
}
LIVE = newFloatbeat(in,sampleRate)
defer LIVE.Stream.Close()
chk(LIVE.Start())
http.HandleFunc("/control", wsHandler)
http.Handle("/", http.FileServer(http.Dir(".")))
err := http.ListenAndServe(":8044", nil)
chk(err)
chk(LIVE.Stop())
}