Skip to content

Commit

Permalink
key sound only chart is now available
Browse files Browse the repository at this point in the history
v0.6.1
  • Loading branch information
hndada committed Jul 23, 2023
1 parent 25a8c0d commit 4c69c89
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
51 changes: 44 additions & 7 deletions audios/musicplayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const defaultSampleRate beep.SampleRate = 44100
const quality = 4

func init() {
speaker.Init(defaultSampleRate, defaultSampleRate.N(time.Second/30))
speaker.Init(defaultSampleRate, defaultSampleRate.N(time.Second/20))
}

func NewMusicPlayer(f beep.StreamSeekCloser, format beep.Format, ratio float64) MusicPlayer {
Expand Down Expand Up @@ -61,6 +61,9 @@ func NewMusicPlayerFromFile(fsys fs.FS, name string, ratio float64) (MusicPlayer
}

func (mp *MusicPlayer) Play() {
if mp.IsEmpty() {
return
}
if mp.played {
return
}
Expand All @@ -78,24 +81,46 @@ func (mp *MusicPlayer) Rewind() {

func (mp MusicPlayer) IsEmpty() bool { return mp.streamer == nil }

func (mp MusicPlayer) IsPlayed() bool { return mp.played }
func (mp MusicPlayer) IsPlayed() bool {
if mp.IsEmpty() {
return false
}
return mp.played
}

func (mp MusicPlayer) Time() time.Duration {
if mp.IsEmpty() {
return 0
}
return defaultSampleRate.D(mp.streamer.Position())
}
func (mp MusicPlayer) Duration() time.Duration {
if mp.IsEmpty() {
return 0
}
return defaultSampleRate.D(mp.streamer.Len())
}

func (mp MusicPlayer) PlaybackRate() float64 { return mp.resampler.Ratio() }
func (mp MusicPlayer) PlaybackRate() float64 {
if mp.IsEmpty() {
return 1
}
return mp.resampler.Ratio()
}

func (mp *MusicPlayer) SetPlaybackRate(ratio float64) {
if mp.IsEmpty() {
return
}
speaker.Lock()
mp.resampler.SetRatio(ratio)
speaker.Unlock()
}

func (mp *MusicPlayer) SetVolume(vol float64) {
if mp.IsEmpty() {
return
}
speaker.Lock()
mp.volume.Volume = beepVolume(vol)
if vol <= 0.001 { // 0.1%
Expand All @@ -106,23 +131,35 @@ func (mp *MusicPlayer) SetVolume(vol float64) {
speaker.Unlock()
}

func (mp MusicPlayer) IsPaused() bool { return mp.ctrl.Paused }
func (mp MusicPlayer) IsPaused() bool {
if mp.IsEmpty() {
return false
}
return mp.ctrl.Paused
}

func (mp *MusicPlayer) Pause() {
if mp.IsEmpty() {
return
}
speaker.Lock()
mp.ctrl.Paused = true
speaker.Unlock()
}

func (mp *MusicPlayer) Resume() {
if mp.IsEmpty() {
return
}
speaker.Lock()
mp.ctrl.Paused = false
speaker.Unlock()
}

func (mp *MusicPlayer) Close() {
speaker.Clear()
if mp != nil && mp.streamer != nil {
mp.streamer.Close()
if mp.IsEmpty() {
return
}
speaker.Clear()
mp.streamer.Close()
}
18 changes: 8 additions & 10 deletions mode/piano/sceneplay.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ func NewScenePlay(cfg *Config, assets map[int]*Asset, fsys fs.FS, name string, r
}

const ratio = 1
s.musicPlayer, err = audios.NewMusicPlayerFromFile(fsys, s.MusicFilename, ratio)
if err != nil {
return
}
s.musicPlayer, _ = audios.NewMusicPlayerFromFile(fsys, s.MusicFilename, ratio)
s.SetMusicVolume(*s.MusicVolume)

s.SoundMap = audios.NewSoundMap(fsys, s.DefaultHitSoundFormat, s.SoundVolume)
Expand Down Expand Up @@ -189,6 +186,12 @@ func (s *ScenePlay) Update() any {
var worstJudgment mode.Judgment
kas := s.readInput()
for _, ka := range kas {
// Todo: solve this
if len(ka.KeyActions) != s.KeyCount {
fmt.Println("len(ka.KeyActions) != s.KeyCount")
continue
}

missed := s.Scorer.flushStagedNotes(ka.Time)
if missed {
worstJudgment = s.miss()
Expand Down Expand Up @@ -270,11 +273,6 @@ func (s *ScenePlay) tryPlayMusic() {

// Todo: set all sample volumes in advance?
func (s ScenePlay) playSounds(ka input.KeyboardAction) {
if len(ka.KeyActions) != s.KeyCount {
fmt.Println("len(ka.KeyActions) != s.KeyCount")
return
}

for k, n := range s.stagedNotes {
if n == nil {
continue
Expand Down Expand Up @@ -364,7 +362,7 @@ func (s *ScenePlay) Resume() {
}

func (s ScenePlay) Finish() any {
s.musicPlayer.Close()
// s.musicPlayer.Close()
if s.Keyboard != nil {
s.Keyboard.Close()
}
Expand Down
5 changes: 4 additions & 1 deletion scene/choose/musicplayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ type PreviewMusicPlayer struct {

func (s *Scene) updatePreviewMusic() {
// It is fine to call Close at blank MusicPlayer.
s.MusicPlayer.Close()
if s.MusicPlayer != nil {
s.MusicPlayer.Close()
}

c := s.chart()
fsys := c.MusicFS
name := c.MusicFilename
mp, _ := audios.NewMusicPlayerFromFile(fsys, name, 1)
mp.SetVolume(s.MusicVolume)
// MusicPlayer should be pointer so that it plays only once.
s.PreviewMusicPlayer = PreviewMusicPlayer{
MusicPlayer: &mp,
StartTime: time.Now(),
Expand Down
2 changes: 1 addition & 1 deletion scene/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewConfig() *Config {

MusicVolume: 0.60,
SoundVolume: 0.60,
MusicOffset: 0,
MusicOffset: -20,
BackgroundBrightness: 0.6,
DebugPrint: true,
Replay: false,
Expand Down

0 comments on commit 4c69c89

Please sign in to comment.