Skip to content

Commit

Permalink
refactor music player, remove exp module
Browse files Browse the repository at this point in the history
  • Loading branch information
zc2638 committed Apr 17, 2022
1 parent 08406e4 commit 6f6dc08
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 80 deletions.
8 changes: 6 additions & 2 deletions cmd/ddshop/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/zc2638/ddshop/asserts"
"github.com/zc2638/ddshop/core/ddmc"
"github.com/zc2638/ddshop/pkg/notice"
"github.com/zc2638/ddshop/pkg/notice/music"
"github.com/zc2638/ddshop/pkg/regular"
)

Expand Down Expand Up @@ -83,10 +84,13 @@ func NewRootCommand() *cobra.Command {
return errors.New("请输入用户Cookie.\n你可以执行此命令 `ddshop --cookie xxx` 或者 `DDSHOP_COOKIE=xxx ddshop`")
}

mp3Entry, err := music.NewMP3(asserts.NoticeMP3, 180)
if err != nil {
logrus.Warning("提醒歌曲解析失败: %v", err)
}
bark := notice.NewBark(&cfg.Bark)
pushPlus := notice.NewPushPlus(&cfg.PushPlus)
music := notice.NewMusic(asserts.NoticeMP3, 180)
noticeIns := notice.New(notice.NewLog(), bark, pushPlus, music)
noticeIns := notice.New(notice.NewLog(), bark, pushPlus, mp3Entry)

session, err := ddmc.NewSession(&cfg.DDMC, noticeIns)
if err != nil {
Expand Down
12 changes: 3 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ go 1.17

require (
github.com/AlecAivazis/survey/v2 v2.3.4
github.com/faiface/beep v1.1.0
github.com/go-resty/resty/v2 v2.7.0
github.com/google/uuid v1.3.0
github.com/hajimehoshi/go-mp3 v0.3.3
github.com/hajimehoshi/oto/v2 v2.0.3
github.com/pkgms/go v0.0.0-20220316065414-13c40cbbea1a
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.7.1
github.com/tidwall/gjson v1.14.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
)

require (
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/hajimehoshi/go-mp3 v0.3.3 // indirect
github.com/hajimehoshi/oto v1.0.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
Expand All @@ -29,19 +27,15 @@ require (
github.com/mitchellh/go-homedir v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/afero v1.1.2 // indirect
github.com/spf13/cast v1.3.0 // indirect
github.com/spf13/jwalterweatherman v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
golang.org/x/exp/shiny v0.0.0-20220407100705-7b9b53b0aca4 // indirect
golang.org/x/image v0.0.0-20220412021310-99f80d0ecbab // indirect
golang.org/x/mobile v0.0.0-20220407111146-e579adbbc4a2 // indirect
golang.org/x/net v0.0.0-20220407224826-aac1ed45d8e3 // indirect
golang.org/x/sys v0.0.0-20220412015802-83041a38b14a // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.51.0 // indirect
Expand Down
52 changes: 7 additions & 45 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/notice/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewLog() Engine {
type log struct{}

func (l *log) Name() string {
return "log"
return "Log"
}

func (l *log) Send(title, body string) error {
Expand Down
51 changes: 29 additions & 22 deletions pkg/notice/music.go → pkg/notice/music/music.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,51 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package notice
package music

import (
"bytes"
"io"
"runtime"
"time"

"github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker"
"github.com/hajimehoshi/go-mp3"
"github.com/hajimehoshi/oto/v2"
)

func NewMusic(b []byte, sec int) Engine {
return &music{b: b, sec: sec}
}

type music struct {
b []byte
sec int
func NewMP3(b []byte, sec int) (*MP3, error) {
decoder, err := mp3.NewDecoder(bytes.NewReader(b))
if err != nil {
return nil, err
}
return &MP3{
decoder: decoder,
sec: sec,
}, nil
}

func (m *music) Name() string {
return "music"
type MP3 struct {
decoder *mp3.Decoder
sec int
}

func (m *music) Send(title, body string) error {
rc := io.NopCloser(bytes.NewReader(m.b))
streamer, format, err := mp3.Decode(rc)
func (m *MP3) Play() error {
c, _, err := oto.NewContext(m.decoder.SampleRate(), 2, 2)
if err != nil {
return err
}
defer streamer.Close()

if err := speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10)); err != nil {
return err
}
speaker.Play(streamer)
player := c.NewPlayer(m.decoder)
player.Play()

// 异步放歌,需要等待
time.Sleep(time.Duration(m.sec) * time.Second)
runtime.KeepAlive(player)
return nil
}

func (m *MP3) Name() string {
return "Music"
}

func (m *MP3) Send(_, _ string) error {
return m.Play()
}
19 changes: 19 additions & 0 deletions pkg/notice/music/music_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Package music
// Created by zc on 2022/4/17.
package music

import (
"testing"

"github.com/zc2638/ddshop/asserts"
)

func TestNewMP3(t *testing.T) {
player, err := NewMP3(asserts.NoticeMP3, 180)
if err != nil {
return
}
if err := player.Play(); err != nil {
t.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion pkg/notice/pushplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type pushPlus struct {
}

func (p *pushPlus) Name() string {
return "pushplus"
return "PushPlus"
}

func (p *pushPlus) Send(title, body string) error {
Expand Down

0 comments on commit 6f6dc08

Please sign in to comment.