Skip to content

Instantly share code, notes, and snippets.

@joegle
Created October 29, 2013 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joegle/7222868 to your computer and use it in GitHub Desktop.
Save joegle/7222868 to your computer and use it in GitHub Desktop.
A server that starts and stops audio recording with websocket messages
package main
import (
"net/http"
"fmt"
"os/exec"
"time"
"code.google.com/p/go.net/websocket"
)
// Echo the data received on the WebSocket.
func EchoServer(ws *websocket.Conn) {
//io.Copy(ws, ws)
c := make(chan bool)
for {
var message string
websocket.Message.Receive(ws, &message)
if message == "Stop" {
c<-true
fmt.Println(message)
}
if message=="Start"{
fmt.Println(message)
go newrecording(c)
}
websocket.Message.Send(ws, "message")
}
}
// This example demonstrates a trivial echo server.
func ExampleHandler() {
http.Handle("/echo", websocket.Handler(EchoServer))
err := http.ListenAndServe(":12345", nil)
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}
func newrecording(c <-chan bool){
filename := fmt.Sprintf("recs/%d.wav",time.Now().Unix())
cmd := exec.Command("arecord","-f","cd","-t","wav",filename)
cmd.Start()
//fmt.Print(cmd.Process.Pid)
select{
case <- c:
cmd.Process.Kill()
}
}
func main(){
ExampleHandler()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment