Skip to content

Commit

Permalink
Merge pull request #67 from jmillerv/44-log-duration-of-currently-pla…
Browse files Browse the repository at this point in the history
…ying-content

Add Duration to Logs
  • Loading branch information
jmillerv authored Aug 27, 2023
2 parents 097415d + 210e4cb commit 14fac46
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
run: go build -v ./...

- name: Test
run: go test -v ./...
run: export CI=true && go test -v ./...
# configuration: https://github.com/golangci/golangci-lint-action
golangci:
name: lint
Expand Down
76 changes: 70 additions & 6 deletions content/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"

Expand All @@ -16,15 +17,16 @@ import (
"github.com/faiface/beep/vorbis"
"github.com/faiface/beep/wav"
"github.com/h2non/filetype"
"github.com/hcl/audioduration"
log "github.com/sirupsen/logrus"
)

const (
wavFile = "wav"
mp3File = "mp3"
oggFile = "oggs"
flacFile = "flac"
sampleRateTime = 10
wavFile string = "wav"
mp3File string = "mp3"
oggFile string = "oggs"
flacFile string = "flac"
sampleRateTime = 10
)

type LocalFile struct {
Expand Down Expand Up @@ -78,7 +80,7 @@ func (l *LocalFile) Play() error {
}
}

log.Infof("playing file buffer from %v", l.Path)
log.WithField("estimated duration", l.getEstimatedFileDuration()).Infof("playing file buffer from %v", l.Path)

err = speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/sampleRateTime))
if err != nil {
Expand Down Expand Up @@ -164,3 +166,65 @@ func (l *LocalFile) getFileType(buf []byte) string {

return ""
}

// getEstimatedFileDuration uses github.com/hcl/audioduration to determine duration of multiple file types.
func (l *LocalFile) getEstimatedFileDuration() string { //nolint:cyclop // this is fine in this instance
switch l.fileType {
case wavFile:
return "library doesn't support estimating wav files."

case mp3File:
file, err := os.Open(l.Path)
defer file.Close()

if err != nil {
log.WithError(err).Error("failed to get wav file stats")

return ""
}

duration, err := audioduration.Duration(file, audioduration.TypeMp3)
if err != nil {
log.WithError(err).Error("unable to determine duration")
}

return strconv.FormatFloat(duration, 'f', 2, 64)

case oggFile:
file, err := os.Open(l.Path)
defer file.Close()

if err != nil {
log.WithError(err).Error("failed to get wav file stats")

return ""
}

duration, err := audioduration.Duration(file, audioduration.TypeOgg)
if err != nil {
log.WithError(err).Error("unable to determine duration")
}

return strconv.FormatFloat(duration, 'f', 2, 64)

case flacFile:
file, err := os.Open(l.Path)
defer file.Close()

if err != nil {
log.WithError(err).Error("failed to get wav file stats")

return ""
}

duration, err := audioduration.Duration(file, audioduration.TypeFlac)
if err != nil {
log.WithError(err).Error("unable to determine duration")
}

return strconv.FormatFloat(duration, 'f', 2, 64)

default:
return "unknown file type: can't determine duration"
}
}
96 changes: 91 additions & 5 deletions content/file_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// nolint:TODO https://github.com/jmillerv/go-dj/issues/16
package content_test
package content

import (
"io"
"os"
"testing"

"github.com/faiface/beep"
"github.com/jmillerv/go-dj/content"
"github.com/stretchr/testify/assert"
)

const (
envCI = "CI"
)

func TestLocalFile_Get(t *testing.T) {
Expand All @@ -28,7 +32,7 @@ func TestLocalFile_Get(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &content.LocalFile{
l := &LocalFile{
Name: tt.fields.Name,
Content: tt.fields.Content,
Path: tt.fields.Path,
Expand Down Expand Up @@ -58,7 +62,7 @@ func TestLocalFile_Play(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &content.LocalFile{
l := &LocalFile{
Name: tt.fields.Name,
Content: tt.fields.Content,
Path: tt.fields.Path,
Expand Down Expand Up @@ -88,7 +92,7 @@ func TestLocalFile_Stop(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &content.LocalFile{
l := &LocalFile{
Name: tt.fields.Name,
Content: tt.fields.Content,
Path: tt.fields.Path,
Expand All @@ -99,3 +103,85 @@ func TestLocalFile_Stop(t *testing.T) {
})
}
}

func TestLocalFile_getEstimatedFileDuration(t *testing.T) {
if os.Getenv(envCI) == "true" {
t.Skipf("CI detected, skipping")
}
type fields struct {
File *LocalFile
decodeReader func(r io.Reader) (s beep.StreamSeekCloser, format beep.Format, err error)
decodeReadCloser func(rc io.ReadCloser) (s beep.StreamSeekCloser, format beep.Format, err error)
}

tests := []struct {
name string
fields fields
want string
}{
{
name: "wav file",
fields: fields{
File: &LocalFile{
Name: "CantinaBand3.wav",
Path: "../static/CantinaBand3.wav",
fileType: wavFile,
},
},
want: "library doesn't support estimating wav files.",
},
{
name: "mp3 file",
fields: fields{
File: &LocalFile{
Name: "piano_six_seconds.mp3",
Path: "../static/piano_six_seconds.mp3",
Content: nil,
fileType: mp3File,
},
},
want: "6.37",
},
{
name: "ogg file",
fields: fields{
File: &LocalFile{
Name: "Example.ogg",
Path: "../static/Example.ogg",
fileType: oggFile,
},
},
want: "6.10",
},
{
name: "FLAC file",
fields: fields{
File: &LocalFile{
Name: "JosefSuk-Meditation.flac",
Path: "../static/JosefSuk-Meditation.flac",
Content: nil,
fileType: flacFile,
},
},
want: "405.45",
},
{
name: "default",
fields: fields{
File: &LocalFile{fileType: "mp4"},
},
want: "unknown file type: can't determine duration",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &LocalFile{
Name: tt.fields.File.Name,
Path: tt.fields.File.Path,
fileType: tt.fields.File.fileType,
}

assert.Equalf(t, tt.want, l.getEstimatedFileDuration(), "getEstimatedFileDuration()")
})
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/faiface/beep v1.1.0
github.com/h2non/filetype v1.1.3
github.com/hcl/audioduration v0.0.0-20221028095105-c8039191ae43
github.com/jmillerv/go-utilities v0.0.0-20211009175413-077cd5304cea
github.com/mmcdole/gofeed v1.1.3
github.com/pkg/errors v0.9.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/hcl/audioduration v0.0.0-20221028095105-c8039191ae43 h1:rXo3ryzLSBpdTeoDKzd1Iz9eDO33M6mcfLFIOOQCqHw=
github.com/hcl/audioduration v0.0.0-20221028095105-c8039191ae43/go.mod h1:ATjLP2ak34LrSbG6oRxL1dO+yNwZnhSPSNxnlEuVwYM=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/icza/bitio v1.0.0 h1:squ/m1SHyFeCA6+6Gyol1AxV9nmPPlJFT8c2vKdj3U8=
Expand Down

0 comments on commit 14fac46

Please sign in to comment.