Resample audio files

This commit is contained in:
Trevor Slocum 2021-10-11 00:52:48 -07:00
parent 7b407ab94a
commit d54ebb995c
11 changed files with 101 additions and 28 deletions

BIN
assets/audio/bat.mp3 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
assets/audio/playerdie.mp3 Normal file

Binary file not shown.

BIN
assets/audio/playerhurt.mp3 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -10,6 +10,7 @@ const (
SoundGunshot = iota
SoundVampireDie1
SoundVampireDie2
SoundBat
SoundPlayerHurt
SoundPlayerDie
SoundGib

View File

@ -36,8 +36,6 @@ type gameCreep struct {
health int
killScore int
sync.Mutex
}
@ -50,9 +48,14 @@ func (c *gameCreep) queueNextAction() {
}
func (c *gameCreep) doNextAction() {
c.queueNextAction()
randMovementA := (rand.Float64() - 0.5) / 7
randMovementB := (rand.Float64() - 0.5) / 7
if rand.Intn(7) == 0 {
dx, dy := deltaXY(c.x, c.y, c.player.x, c.player.y)
seekDistance := 7.0
if (dx < seekDistance && dy < seekDistance) || rand.Intn(7) == 0 {
// Seek player.
c.moveX = c.x - c.player.x
if c.moveX < 0 {
@ -66,6 +69,8 @@ func (c *gameCreep) doNextAction() {
} else {
c.moveY = math.Abs(randMovementB) * -1
}
c.nextAction *= 2
} else {
c.moveX = randMovementA
c.moveY = randMovementB
@ -81,8 +86,6 @@ func (c *gameCreep) doNextAction() {
} else if c.y >= float64(c.level.h-3) && c.moveY < 0 {
c.moveY *= 1
}
c.queueNextAction()
}
func (c *gameCreep) Update() {
@ -112,3 +115,16 @@ func (c *gameCreep) Position() (float64, float64) {
defer c.Unlock()
return c.x, c.y
}
func (c *gameCreep) killScore() int {
switch c.creepType {
case TypeVampire:
return 50
case TypeBat:
return 125
case TypeGhost:
return 75
default:
return 0
}
}

102
game.go
View File

@ -28,6 +28,15 @@ var flashImage *ebiten.Image
var numberPrinter = message.NewPrinter(language.English)
// TODO move into switch in playSound
const (
gunshotVolume = 0.2
vampireDieVolume = 0.15
batDieVolume = 1.5
playerHurtVolume = 0.4
playerDieVolume = 1.6
)
var startButtons = []ebiten.StandardGamepadButton{
ebiten.StandardGamepadButtonRightBottom,
ebiten.StandardGamepadButtonRightRight,
@ -88,8 +97,10 @@ type game struct {
op *ebiten.DrawImageOptions
audioContext *audio.Context
nextSound map[int]int
soundBuffer map[int][]*audio.Player
nextSound []int
soundBuffer [][]*audio.Player
lastBatSound time.Time
gamepadIDs []ebiten.GamepadID
gamepadIDsBuf []ebiten.GamepadID
@ -102,7 +113,7 @@ type game struct {
cpuProfile *os.File
}
const sampleRate = 48000
const sampleRate = 44100
// NewGame returns a new isometric demo game.
func NewGame() (*game, error) {
@ -113,8 +124,8 @@ func NewGame() (*game, error) {
mousePanY: math.MinInt32,
op: &ebiten.DrawImageOptions{},
soundBuffer: make(map[int][]*audio.Player),
nextSound: make(map[int]int),
soundBuffer: make([][]*audio.Player, 6),
nextSound: make([]int, 6),
activeGamepad: -1,
}
@ -175,36 +186,49 @@ func (g *game) loadAssets() error {
flashImage = ebiten.NewImageFromImage(img)
g.soundBuffer[SoundGunshot] = make([]*audio.Player, 4)
g.soundBuffer[SoundVampireDie1] = make([]*audio.Player, 4)
g.soundBuffer[SoundVampireDie2] = make([]*audio.Player, 4)
g.soundBuffer[SoundBat] = make([]*audio.Player, 4)
g.soundBuffer[SoundPlayerHurt] = make([]*audio.Player, 4)
g.soundBuffer[SoundPlayerDie] = make([]*audio.Player, 4)
for i := 0; i < 4; i++ {
stream, err := loadMP3(g.audioContext, "assets/audio/gunshot.mp3")
if err != nil {
return err
}
g.soundBuffer[SoundGunshot] = append(g.soundBuffer[SoundGunshot], stream)
g.soundBuffer[SoundGunshot][i] = stream
stream, err = loadMP3(g.audioContext, "assets/audio/vampiredie1.mp3")
if err != nil {
return err
}
g.soundBuffer[SoundVampireDie1] = append(g.soundBuffer[SoundVampireDie1], stream)
g.soundBuffer[SoundVampireDie1][i] = stream
stream, err = loadMP3(g.audioContext, "assets/audio/vampiredie2.mp3")
if err != nil {
return err
}
g.soundBuffer[SoundVampireDie2] = append(g.soundBuffer[SoundVampireDie2], stream)
g.soundBuffer[SoundVampireDie2][i] = stream
stream, err = loadWav(g.audioContext, "assets/audio/hurt.wav")
stream, err = loadMP3(g.audioContext, "assets/audio/bat.mp3")
if err != nil {
return err
}
g.soundBuffer[SoundPlayerHurt] = append(g.soundBuffer[SoundPlayerHurt], stream)
g.soundBuffer[SoundBat][i] = stream
stream, err = loadMP3(g.audioContext, "assets/audio/die.mp3")
stream, err = loadMP3(g.audioContext, "assets/audio/playerhurt.mp3")
if err != nil {
return err
}
g.soundBuffer[SoundPlayerDie] = append(g.soundBuffer[SoundPlayerDie], stream)
g.soundBuffer[SoundPlayerHurt][i] = stream
stream, err = loadMP3(g.audioContext, "assets/audio/playerdie.mp3")
if err != nil {
return err
}
g.soundBuffer[SoundPlayerDie][i] = stream
}
f, err = assetsFS.Open("assets/creeps/vampire.png")
@ -254,7 +278,6 @@ func (g *game) newCreep(creepType int) *gameCreep {
level: g.currentLevel,
player: g.player,
health: 1,
killScore: 50,
}
}
@ -275,6 +298,9 @@ func (g *game) reset() error {
g.player.x = float64(rand.Intn(108))
g.player.y = float64(rand.Intn(108))
// Remove projectiles.
g.projectiles = nil
// Spawn creeps.
g.creeps = make([]*gameCreep, 1000)
addedCreeps := make(map[string]bool)
@ -339,7 +365,7 @@ func (g *game) Update() error {
if g.player.health <= 0 && !g.godMode {
// Game over.
if ebiten.IsKeyPressed(ebiten.KeyEnter) {
if ebiten.IsKeyPressed(ebiten.KeyEnter) || (g.activeGamepad != -1 && ebiten.IsStandardGamepadButtonPressed(g.activeGamepad, ebiten.StandardGamepadButtonCenterRight)) {
err := g.reset()
if err != nil {
return err
@ -347,7 +373,6 @@ func (g *game) Update() error {
g.gameOverTime = time.Time{}
}
// TODO or button start on gamepad
return nil
}
@ -403,9 +428,9 @@ func (g *game) Update() error {
}
if g.player.health == 2 {
g.playSound(SoundPlayerHurt, 0.4)
g.playSound(SoundPlayerHurt, playerHurtVolume/2)
} else if g.player.health == 1 {
g.playSound(SoundPlayerHurt, 0.8)
g.playSound(SoundPlayerHurt, playerHurtVolume)
}
g.addBloodSplatter(g.player.x, g.player.y)
@ -416,12 +441,15 @@ func (g *game) Update() error {
g.gameOverTime = time.Now()
// Play die sound.
err := g.playSound(SoundPlayerDie, 1.6)
err := g.playSound(SoundPlayerDie, playerDieVolume)
if err != nil {
// TODO return err
panic(err)
}
}
} else if c.creepType == TypeBat && (dx <= 12 && dy <= 7) && rand.Intn(166) == 6 && time.Since(g.lastBatSound) >= 100*time.Millisecond {
g.playSound(SoundBat, batDieVolume)
g.lastBatSound = time.Now()
}
}
@ -557,7 +585,7 @@ func (g *game) Update() error {
g.player.weapon.lastFire = time.Now()
// Play gunshot sound.
err := g.playSound(SoundGunshot, 0.4)
err := g.playSound(SoundGunshot, gunshotVolume)
if err != nil {
return err
}
@ -835,15 +863,43 @@ func (g *game) hurtCreep(c *gameCreep, damage int) error {
}
// Killed creep.
g.player.score += c.killScore
g.player.score += c.killScore()
// Play vampire die sound.
dieSound := SoundVampireDie1
var volume float64
var dieSound int
dieSound = SoundVampireDie1
if rand.Intn(2) == 1 {
dieSound = SoundVampireDie2
}
// TODO set volume to distance
err := g.playSound(dieSound, 0.15)
volume = vampireDieVolume
/*
if c.creepType == TypeBat {
dieSound = SoundBat
volume = batDieVolume
} else {
dieSound = SoundVampireDie1
if rand.Intn(2) == 1 {
dieSound = SoundVampireDie2
}
volume = vampireDieVolume
}
*/
dx, dy := deltaXY(g.player.x, g.player.y, c.x, c.y)
distance := dx
if dy > dx {
distance = dy
}
if distance > 9 {
volume *= 0.7
} else if distance > 6 {
volume *= 0.85
}
err := g.playSound(dieSound, volume)
if err != nil {
return err
}