-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (42 loc) · 989 Bytes
/
main.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
package main
import (
"encoding/binary"
"github.com/gordonklaus/portaudio"
"net/http"
"time"
)
const sampleRate = 44100
const seconds = 1
func main() {
portaudio.Initialize()
defer portaudio.Terminate()
buffer := make([]float32, sampleRate * seconds)
stream, err := portaudio.OpenDefaultStream(1, 0, sampleRate, len(buffer), func(in []float32) {
for i := range buffer {
buffer[i] = in[i]
}
})
chk(err)
chk(stream.Start())
time.Sleep(time.Second * 40)
chk(stream.Stop())
defer stream.Close()
http.HandleFunc("/audio", func(w http.ResponseWriter, r *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
panic("expected http.ResponseWriter to be an http.Flusher")
}
w.Header().Set("Connection", "Keep-Alive")
w.Header().Set("Transfer-Encoding", "chunked")
for true {
binary.Write(w, binary.BigEndian, &buffer)
flusher.Flush() // Trigger "chunked" encoding
return
}
})
}
func chk(err error) {
if err != nil {
panic(err)
}
}