Skip to content

Commit ae20bd0

Browse files
committed
Add audio.Sfx, audio.Pat and audio.Sync()
1 parent 4019a14 commit ae20bd0

File tree

5 files changed

+74
-61
lines changed

5 files changed

+74
-61
lines changed

audio/audio.go

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// Sound effects and music can be changed using functions: SetSfx and SetMusic.
88
package audio
99

10+
import "fmt"
11+
1012
// Sfx starts playing sound effect with given sfxNo on specified channel.
1113
//
1214
// sfxNo is the number of sound effect to play (0-63). sfxNo=-1 stops
@@ -47,36 +49,6 @@ func GetStat() Stat {
4749
return system.Stat()
4850
}
4951

50-
// SetSfx updates the sound effect. sfxNo is 0-63. Updating sfx number which
51-
// is higher than 63 does not do anything.
52-
//
53-
// SoundEffect parameters are clamped when out of range.
54-
// For example, sfx note volume equal 8 will be silently clamped to 7.
55-
func SetSfx(sfxNo int, e SoundEffect) {
56-
system.SetSfx(sfxNo, e)
57-
}
58-
59-
// GetSfx returns sfx with given number. sfxNo is 0-63. Trying to get
60-
// sfx number higher than 63 will result in returning empty SoundEffect (zero-value).
61-
func GetSfx(sfxNo int) SoundEffect {
62-
return system.GetSfx(sfxNo)
63-
}
64-
65-
// SetMusic updates the music pattern. patternNo is 0-63. Updating pattern number which
66-
// is higher than 63 does not do anything.
67-
//
68-
// Pattern parameters are clamped when out of range.
69-
// For example, pattern sfx number equal 64 will be silently clamped to 63.
70-
func SetMusic(patternNo int, p Pattern) {
71-
system.SetMusic(patternNo, p)
72-
}
73-
74-
// GetMusic returns music pattern with given number. patterNo is 0-63. Trying to get
75-
// pattern number higher than 63 will result in returning empty Pattern (zero-value).
76-
func GetMusic(patterNo int) Pattern {
77-
return system.GetMusic(patterNo)
78-
}
79-
8052
// SaveAudio stores audio system state to byte slice. State is stored in binary form.
8153
// The format is described in Synthesizer.Save source code.
8254
func SaveAudio() ([]byte, error) {
@@ -122,6 +94,12 @@ type SoundEffect struct {
12294
Buzz bool // Not implemented yet.
12395
}
12496

97+
func (s SoundEffect) String() string {
98+
return fmt.Sprintf(
99+
"{Speed:%d LoopStart:%d LoopStop:%d Detune:%d Reverb:%d Dampen:%d Noiz:%t Buzz:%t Notes:(%d)[%+v %+v %+v ...]}",
100+
s.Speed, s.LoopStart, s.LoopStop, s.Detune, s.Reverb, s.Dampen, s.Noiz, s.Buzz, len(s.Notes), s.Notes[0], s.Notes[1], s.Notes[2])
101+
}
102+
125103
func (s SoundEffect) noteAt(no int) Note {
126104
var note Note
127105
if no < len(s.Notes) {
@@ -278,9 +256,23 @@ type System interface {
278256
Sfx(sfxNo int, channel Channel, offset, length int)
279257
Music(patterNo int, fadeMs int, channelMask byte)
280258
Stat() Stat
259+
// SetSfx updates the sound effect. sfxNo is 0-63. Updating sfx number which
260+
// is higher than 63 does not do anything.
261+
//
262+
// SoundEffect parameters are clamped when out of range.
263+
// For example, sfx note volume equal 8 will be silently clamped to 7.
281264
SetSfx(sfxNo int, e SoundEffect)
265+
// GetSfx returns sfx with given number. sfxNo is 0-63. Trying to get
266+
// sfx number higher than 63 will result in returning empty SoundEffect (zero-value).
282267
GetSfx(sfxNo int) SoundEffect
268+
// SetMusic updates the music pattern. patternNo is 0-63. Updating pattern number which
269+
// is higher than 63 does not do anything.
270+
//
271+
// Pattern parameters are clamped when out of range.
272+
// For example, pattern sfx number equal 64 will be silently clamped to 63.
283273
SetMusic(patternNo int, _ Pattern)
274+
// GetMusic returns music pattern with given number. patterNo is 0-63. Trying to get
275+
// pattern number higher than 63 will result in returning empty Pattern (zero-value).
284276
GetMusic(patterNo int) Pattern
285277
Save() ([]byte, error)
286278
Load([]byte) error

audio/memory.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// (c) 2022-2023 Jacek Olszak
2+
// This code is licensed under MIT license (see LICENSE for details)
3+
4+
package audio
5+
6+
var (
7+
SFX [64]SoundEffect
8+
Pat [64]Pattern
9+
)
10+
11+
func Sync() {
12+
for i, sfx := range SFX {
13+
system.SetSfx(i, sfx)
14+
}
15+
for i, pattern := range Pat {
16+
system.SetMusic(i, pattern)
17+
}
18+
}

devtools/internal/lib/github_com-elgopher-pi-audio.go

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

devtools/scripting.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package devtools
55

66
import (
77
"fmt"
8-
8+
"github.com/elgopher/pi/audio"
99
"github.com/elgopher/pi/devtools/internal/help"
1010
"github.com/elgopher/pi/devtools/internal/interpreter"
1111
"github.com/elgopher/pi/devtools/internal/snapshot"
@@ -67,6 +67,7 @@ func evaluateNextCommandFromTerminal() {
6767

6868
switch result {
6969
case interpreter.GoCodeExecuted:
70+
audio.Sync() // Quality Of Live
7071
snapshot.Take()
7172
case interpreter.Resumed:
7273
resumeGame()

examples/audio/audio.go

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,43 @@ import (
77
)
88

99
func main() {
10-
sfx := audio.SoundEffect{
11-
Notes: [32]audio.Note{
12-
{
13-
Pitch: audio.PitchG3,
14-
Instrument: audio.InstrumentNoise,
15-
Volume: 7,
16-
},
17-
},
18-
Speed: 255,
10+
audio.SFX[0].Notes[0] = audio.Note{
11+
Pitch: audio.PitchG3,
12+
Instrument: audio.InstrumentNoise,
13+
Volume: 7,
1914
}
20-
audio.SetSfx(0, sfx)
15+
audio.SFX[0].Speed = 255
16+
audio.Sync()
17+
18+
sfxNote0 := &audio.SFX[0].Notes[0]
2119

2220
pi.Update = func() {
2321
if pi.Btnp(pi.X) {
2422
audio.Sfx(0, audio.Channel0, 0, 0)
2523
}
2624

27-
if pi.Btnp(pi.Up) && sfx.Notes[0].Pitch < 255 {
28-
sfx.Notes[0].Pitch += 1
29-
audio.SetSfx(0, sfx)
30-
audio.Sfx(0, audio.Channel0, 0, 0)
25+
if pi.Btnp(pi.Up) && sfxNote0.Pitch < 255 {
26+
sfxNote0.Pitch += 1
27+
playSfx0()
3128
}
32-
if pi.Btnp(pi.Down) && sfx.Notes[0].Pitch > 0 {
33-
sfx.Notes[0].Pitch -= 1
34-
audio.SetSfx(0, sfx)
35-
audio.Sfx(0, audio.Channel0, 0, 0)
29+
if pi.Btnp(pi.Down) && sfxNote0.Pitch > 0 {
30+
sfxNote0.Pitch -= 1
31+
playSfx0()
3632
}
37-
if pi.Btnp(pi.Right) && sfx.Notes[0].Instrument < audio.InstrumentPhaser {
38-
sfx.Notes[0].Instrument += 1
39-
audio.SetSfx(0, sfx)
40-
audio.Sfx(0, audio.Channel0, 0, 0)
33+
if pi.Btnp(pi.Right) && sfxNote0.Instrument < audio.InstrumentPhaser {
34+
sfxNote0.Instrument += 1
35+
playSfx0()
4136
}
42-
if pi.Btnp(pi.Left) && sfx.Notes[0].Instrument > 0 {
43-
sfx.Notes[0].Instrument -= 1
44-
audio.SetSfx(0, sfx)
45-
audio.Sfx(0, audio.Channel0, 0, 0)
37+
if pi.Btnp(pi.Left) && sfxNote0.Instrument > 0 {
38+
sfxNote0.Instrument -= 1
39+
playSfx0()
4640
}
4741
}
4842

4943
ebitengine.MustRun()
5044
}
45+
46+
func playSfx0() {
47+
audio.Sync() // first send changed sfx to audio system
48+
audio.Sfx(0, audio.Channel0, 0, 0)
49+
}

0 commit comments

Comments
 (0)