Balance audio a bit.

This commit is contained in:
Sander Schobers 2021-08-14 09:38:30 +02:00
parent aab65a984a
commit 17008871ce
2 changed files with 25 additions and 10 deletions

View File

@ -63,18 +63,23 @@ func (a *app) Init(ctx ui.Context) error {
}) })
a.context.ShowMainMenu(ctx) a.context.ShowMainMenu(ctx)
err := a.context.Audio.LoadSample( if err := a.context.Audio.LoadSample(
"level_completed.mp3", "level_completed.mp3",
"level_game_over.mp3", "level_game_over.mp3",
"level_new_high_score.mp3", "level_new_high_score.mp3",
"menu_interaction.mp3",
"monster_jump.mp3",
"player_collect_heart.mp3", "player_collect_heart.mp3",
"player_collect_star.mp3", "player_collect_star.mp3",
"player_hurt.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", "player_move.mp3",
) ); err != nil {
if err != nil {
log.Printf("failed to load samples; %v\n", err) log.Printf("failed to load samples; %v\n", err)
} }

View File

@ -41,9 +41,13 @@ func NewAudioPlayer(resources ui.Resources, prefix string) *AudioPlayer {
} }
func (p *AudioPlayer) LoadSample(name ...string) error { func (p *AudioPlayer) LoadSample(name ...string) error {
return p.LoadSampleVolume(0, name...)
}
func (p *AudioPlayer) LoadSampleVolume(volume float64, name ...string) error {
var all error var all error
for _, name := range name { 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")) 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") 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] sample, ok := p.Samples[name]
if ok { if ok {
return sample, nil return sample, nil
@ -66,17 +70,22 @@ func (p *AudioPlayer) openSample(name string) (Sample, error) {
buffer := beep.NewBuffer(format) buffer := beep.NewBuffer(format)
buffer.Append(stream) buffer.Append(stream)
sample = Sample{Buffer: buffer, SampleRate: format.SampleRate} sample = Sample{Buffer: buffer, Volume: volume, SampleRate: format.SampleRate}
p.Samples[name] = sample p.Samples[name] = sample
return sample, nil return sample, nil
} }
func (p *AudioPlayer) PlaySample(name string) error { func (p *AudioPlayer) PlaySample(name string) error {
sample, err := p.openSample(name) sample, err := p.openSample(name, 0)
if err != nil { if err != nil {
return err 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 return nil
} }
@ -149,6 +158,7 @@ func (m Music) Finished(player *AudioPlayer) {
type Sample struct { type Sample struct {
*beep.Buffer *beep.Buffer
Volume float64
SampleRate beep.SampleRate SampleRate beep.SampleRate
} }