72 lines
954 B
Go
72 lines
954 B
Go
package audio
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"time"
|
|
"../icecast"
|
|
)
|
|
|
|
// type stream_t struct {
|
|
// filepath string
|
|
// bitrate int
|
|
// channels int
|
|
// encoding_src string
|
|
// encoding_dst string
|
|
// }
|
|
|
|
func Play(filename string) {
|
|
|
|
sig := make(chan os.Signal, 1)
|
|
signal.Notify(sig, os.Interrupt, os.Kill)
|
|
|
|
samplerate := 44100
|
|
bytes_per_sample := 1
|
|
channels := 1
|
|
bytes_per_sec := int(samplerate) * channels * bytes_per_sample
|
|
|
|
r, e := os.Open(filename)
|
|
chk(e)
|
|
defer r.Close()
|
|
|
|
_, er := icecast.Connect()
|
|
chk(er)
|
|
|
|
audio := make([]byte, 2 * 1024)
|
|
|
|
dt := time.Second * time.Duration(len(audio)) / time.Duration(bytes_per_sec)
|
|
|
|
now := time.Now()
|
|
|
|
for {
|
|
|
|
n, err := r.Read(audio)
|
|
if n == 0 {
|
|
break
|
|
}
|
|
chk(err)
|
|
|
|
icecast.Send(audio)
|
|
|
|
select {
|
|
case <-sig:
|
|
return
|
|
default:
|
|
}
|
|
|
|
time.Sleep(dt)
|
|
|
|
}
|
|
|
|
fmt.Println(time.Duration(time.Now().Sub(now)))
|
|
|
|
}
|
|
|
|
|
|
func chk(err error) {
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|