/
music.go
97 lines (78 loc) · 1.77 KB
/
music.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
package plugins
import (
"os"
"os/signal"
"github.com/bobertlo/go-mpg123/mpg123"
"encoding/binary"
"bytes"
"github.com/gordonklaus/portaudio"
"strings"
)
func init() {
p := plugin_music{}
Regist("plugin_music", p)
}
type plugin_music struct {
}
func (this plugin_music) Keyword() []string {
return []string{"古诗","诗词","吟诗"}
}
func (this plugin_music) Flag() bool {
return true
}
func (this plugin_music) Active() bool {
return true
}
/**
控制函数,返回控制指令。
*/
func (this plugin_music) Control(str string) string {
if strings.Contains(str, "播放"){
return CMD_RESET
}
if strings.Contains(str, "暂停"){
return CMD_PAUSE
}
if strings.Contains(str, "停止") || strings.Contains(str, "关闭"){
return CMD_STOP
}
return true
}
func (this plugin_music) Run(msg string) {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, os.Kill)
// create mpg123 decoder instance
decoder, err := mpg123.NewDecoder("")
chk(err)
fileName := "yuanzougaofei.mp3"
chk(decoder.Open(fileName))
defer decoder.Close()
// get audio format information
rate, channels, _ := decoder.GetFormat()
// make sure output format does not change
decoder.FormatNone()
decoder.Format(rate, channels, mpg123.ENC_SIGNED_16)
portaudio.Initialize()
defer portaudio.Terminate()
out := make([]int16, 8192)
stream, err := portaudio.OpenDefaultStream(0, channels, float64(rate), len(out), &out)
chk(err)
defer stream.Close()
chk(stream.Start())
defer stream.Stop()
for PluginControl(){
audio := make([]byte, 2*len(out))
_, err = decoder.Read(audio)
if err == mpg123.EOF {
break
}
chk(err)
chk(binary.Read(bytes.NewBuffer(audio), binary.LittleEndian, out))
chk(stream.Write())
select {
case <-sig:
return
default:
}
}
}