From 17008871ce6896a77a7e6e067876b35db9928269 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Sat, 14 Aug 2021 09:38:30 +0200 Subject: [PATCH] Balance audio a bit. --- cmd/tins2021/app.go | 15 ++++++++++----- cmd/tins2021/audio.go | 20 +++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/cmd/tins2021/app.go b/cmd/tins2021/app.go index b8981d1..fc6c4d7 100644 --- a/cmd/tins2021/app.go +++ b/cmd/tins2021/app.go @@ -63,18 +63,23 @@ func (a *app) Init(ctx ui.Context) error { }) a.context.ShowMainMenu(ctx) - err := a.context.Audio.LoadSample( + if err := a.context.Audio.LoadSample( "level_completed.mp3", "level_game_over.mp3", "level_new_high_score.mp3", - "menu_interaction.mp3", - "monster_jump.mp3", "player_collect_heart.mp3", "player_collect_star.mp3", "player_hurt.mp3", + ); err != nil { + log.Printf("failed to load samples; %v\n", err) + } + if err := a.context.Audio.LoadSampleVolume(-1, "menu_interaction.mp3"); err != nil { + log.Printf("failed to load samples; %v\n", err) + } + if err := a.context.Audio.LoadSampleVolume(-2, + "monster_jump.mp3", "player_move.mp3", - ) - if err != nil { + ); err != nil { log.Printf("failed to load samples; %v\n", err) } diff --git a/cmd/tins2021/audio.go b/cmd/tins2021/audio.go index 859d648..e253101 100644 --- a/cmd/tins2021/audio.go +++ b/cmd/tins2021/audio.go @@ -41,9 +41,13 @@ func NewAudioPlayer(resources ui.Resources, prefix string) *AudioPlayer { } func (p *AudioPlayer) LoadSample(name ...string) error { + return p.LoadSampleVolume(0, name...) +} + +func (p *AudioPlayer) LoadSampleVolume(volume float64, name ...string) error { var all error for _, name := range name { - if _, err := p.openSample(name); err != nil { + if _, err := p.openSample(name, volume); err != nil { all = ut.ErrCombine(all, errors.Wrap(err, "failed to open sample")) } } @@ -53,7 +57,7 @@ func (p *AudioPlayer) LoadSample(name ...string) error { return errors.WithMessage(all, "failed to open one or more samples") } -func (p *AudioPlayer) openSample(name string) (Sample, error) { +func (p *AudioPlayer) openSample(name string, volume float64) (Sample, error) { sample, ok := p.Samples[name] if ok { return sample, nil @@ -66,17 +70,22 @@ func (p *AudioPlayer) openSample(name string) (Sample, error) { buffer := beep.NewBuffer(format) buffer.Append(stream) - sample = Sample{Buffer: buffer, SampleRate: format.SampleRate} + sample = Sample{Buffer: buffer, Volume: volume, SampleRate: format.SampleRate} p.Samples[name] = sample return sample, nil } func (p *AudioPlayer) PlaySample(name string) error { - sample, err := p.openSample(name) + sample, err := p.openSample(name, 0) if err != nil { return err } - speaker.Play(p.resample(sample.Stream(), sample.SampleRate)) + speaker.Play(&effects.Volume{ + Streamer: p.resample(sample.Stream(), sample.SampleRate), + Base: 2, + Volume: float64(sample.Volume), + Silent: false, + }) return nil } @@ -149,6 +158,7 @@ func (m Music) Finished(player *AudioPlayer) { type Sample struct { *beep.Buffer + Volume float64 SampleRate beep.SampleRate }