Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer changes time read #73

Open
sheerun opened this issue Nov 20, 2019 · 0 comments
Open

Buffer changes time read #73

sheerun opened this issue Nov 20, 2019 · 0 comments

Comments

@sheerun
Copy link

sheerun commented Nov 20, 2019

package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/faiface/beep"
	"github.com/faiface/beep/mp3"
	"github.com/faiface/beep/speaker"
)

func main() {
	f, err := os.Open("/Users/sheerun/Music/West/foo.mp3")
	if err != nil {
		log.Fatal(err)
	}
	streamer, format, err := mp3.Decode(f)
	if err != nil {
		log.Fatal(err)
	}
	defer streamer.Close()

	speaker.Init(format.SampleRate, format.SampleRate.N(time.Second))

	done := make(chan bool)
	speaker.Play(beep.Seq(streamer, beep.Callback(func() {
		done <- true
	})))

	speaker.Lock()
	fmt.Println(format.SampleRate.D(streamer.Position()).Round(time.Second))
	speaker.Unlock()

	for {
		select {
		case <-done:
			return
		case <-time.After(time.Second):
			speaker.Lock()
			fmt.Println(format.SampleRate.D(streamer.Position()))
			speaker.Unlock()
		}
	}
}

As you see I've used format.SampleRate.N(time.Second) rather than format.SampleRate.N(time.Second/10) in the readme to prevent issues with streaming audio due to slow CPU.

Now the logs say following:

go run main.go
0s
4s
5s
6s

First of all it seems that 3 seconds are buffered instead of 1

Secondly it seems that reading time depends on size of buffer which is confusing. Could I read position of speaker maybe instead? I'd expect to see:

go run main.go
0s
1s
2s
3s

Regardless of the buffer size

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant