Skip to content

Commit edcbfa3

Browse files
authored
Sound refactor and improvements (luanti-org#12764)
1 parent 8e1af25 commit edcbfa3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2790
-1199
lines changed

LICENSE.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ SmallJoker:
8383
DS:
8484
games/devtest/mods/soundstuff/textures/soundstuff_bigfoot.png
8585
games/devtest/mods/soundstuff/textures/soundstuff_jukebox.png
86+
games/devtest/mods/soundstuff/textures/soundstuff_racecar.png
87+
games/devtest/mods/soundstuff/sounds/soundstuff_sinus.ogg
8688
games/devtest/mods/testtools/textures/testtools_branding_iron.png
8789

8890
License of Minetest source code

builtin/client/misc.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,14 @@ function core.setting_get_pos(name)
55
end
66
return core.string_to_pos(value)
77
end
8+
9+
10+
-- old non-method sound functions
11+
12+
function core.sound_stop(handle, ...)
13+
return handle:stop(...)
14+
end
15+
16+
function core.sound_fade(handle, ...)
17+
return handle:fade(...)
18+
end

builtin/mainmenu/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ local basepath = core.get_builtin_path()
2828
defaulttexturedir = core.get_texturepath_share() .. DIR_DELIM .. "base" ..
2929
DIR_DELIM .. "pack" .. DIR_DELIM
3030

31+
dofile(menupath .. DIR_DELIM .. "misc.lua")
32+
3133
dofile(basepath .. "common" .. DIR_DELIM .. "filterlist.lua")
3234
dofile(basepath .. "fstk" .. DIR_DELIM .. "buttonbar.lua")
3335
dofile(basepath .. "fstk" .. DIR_DELIM .. "dialog.lua")

builtin/mainmenu/misc.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
-- old non-method sound function
3+
4+
function core.sound_stop(handle, ...)
5+
return handle:stop(...)
6+
end

doc/client_lua_api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,9 +754,9 @@ Call these functions only at load time!
754754
* `minetest.sound_play(spec, parameters)`: returns a handle
755755
* `spec` is a `SimpleSoundSpec`
756756
* `parameters` is a sound parameter table
757-
* `minetest.sound_stop(handle)`
757+
* `handle:stop()` or `minetest.sound_stop(handle)`
758758
* `handle` is a handle returned by `minetest.sound_play`
759-
* `minetest.sound_fade(handle, step, gain)`
759+
* `handle:fade(step, gain)` or `minetest.sound_fade(handle, step, gain)`
760760
* `handle` is a handle returned by `minetest.sound_play`
761761
* `step` determines how fast a sound will fade.
762762
Negative step will lower the sound volume, positive step will increase

doc/lua_api.md

Lines changed: 115 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -994,16 +994,21 @@ Only Ogg Vorbis files are supported.
994994
For positional playing of sounds, only single-channel (mono) files are
995995
supported. Otherwise OpenAL will play them non-positionally.
996996

997-
Mods should generally prefix their sounds with `modname_`, e.g. given
997+
Mods should generally prefix their sound files with `modname_`, e.g. given
998998
the mod name "`foomod`", a sound could be called:
999999

10001000
foomod_foosound.ogg
10011001

1002-
Sounds are referred to by their name with a dot, a single digit and the
1003-
file extension stripped out. When a sound is played, the actual sound file
1004-
is chosen randomly from the matching sounds.
1002+
Sound group
1003+
-----------
1004+
1005+
A sound group is the set of all sound files, whose filenames are of the following
1006+
format:
1007+
`<sound-group name>[.<single digit>].ogg`
1008+
When a sound-group is played, one the files in the group is chosen at random.
1009+
Sound files can only be referred to by their sound-group name.
10051010

1006-
When playing the sound `foomod_foosound`, the sound is chosen randomly
1011+
Example: When playing the sound `foomod_foosound`, the sound is chosen randomly
10071012
from the available ones of the following files:
10081013

10091014
* `foomod_foosound.ogg`
@@ -1012,20 +1017,111 @@ from the available ones of the following files:
10121017
* (...)
10131018
* `foomod_foosound.9.ogg`
10141019

1015-
Examples of sound parameter tables:
1020+
`SimpleSoundSpec`
1021+
-----------------
1022+
1023+
Specifies a sound name, gain (=volume), pitch and fade.
1024+
This is either a string or a table.
1025+
1026+
In string form, you just specify the sound name or
1027+
the empty string for no sound.
1028+
1029+
Table form has the following fields:
1030+
1031+
* `name`:
1032+
Sound-group name.
1033+
If == `""`, no sound is played.
1034+
* `gain`:
1035+
Volume (`1.0` = 100%), must be non-negative.
1036+
At the end, OpenAL clamps sound gain to a maximum of `1.0`. By setting gain for
1037+
a positional sound higher than `1.0`, one can increase the radius inside which
1038+
maximal gain is reached.
1039+
Furthermore, gain of positional sounds doesn't increase inside a 1 node radius.
1040+
The gain given here describes the gain at a distance of 3 nodes.
1041+
* `pitch`:
1042+
Applies a pitch-shift to the sound.
1043+
Each factor of `2.0` results in a pitch-shift of +12 semitones.
1044+
Must be positive.
1045+
* `fade`:
1046+
If > `0.0`, the sound is faded in, with this value in gain per second, until
1047+
`gain` is reached.
1048+
1049+
`gain`, `pitch` and `fade` are optional and default to `1.0`, `1.0` and `0.0`.
1050+
1051+
Examples:
1052+
1053+
* `""`: No sound
1054+
* `{}`: No sound
1055+
* `"default_place_node"`: Play e.g. `default_place_node.ogg`
1056+
* `{name = "default_place_node"}`: Same as above
1057+
* `{name = "default_place_node", gain = 0.5}`: 50% volume
1058+
* `{name = "default_place_node", gain = 0.9, pitch = 1.1}`: 90% volume, 110% pitch
1059+
1060+
Sound parameter table
1061+
---------------------
1062+
1063+
Table used to specify how a sound is played:
1064+
1065+
```lua
1066+
{
1067+
gain = 1.0,
1068+
-- Scales the gain specified in `SimpleSoundSpec`.
1069+
1070+
pitch = 1.0,
1071+
-- Overwrites the pitch specified in `SimpleSoundSpec`.
1072+
1073+
fade = 0.0,
1074+
-- Overwrites the fade specified in `SimpleSoundSpec`.
1075+
1076+
start_time = 0.0,
1077+
-- Start with a time-offset into the sound.
1078+
-- The behavior is as if the sound was already playing for this many seconds.
1079+
-- Negative values are relative to the sound's length, so the sound reaches
1080+
-- its end in `-start_time` seconds.
1081+
-- It is unspecified what happens if `loop` is false and `start_time` is
1082+
-- smaller than minus the sound's length.
1083+
1084+
loop = false,
1085+
-- If true, sound is played in a loop.
1086+
1087+
pos = {x = 1, y = 2, z = 3},
1088+
-- Play sound at a position.
1089+
-- Can't be used together with `object`.
1090+
1091+
object = <an ObjectRef>,
1092+
-- Attach the sound to an object.
1093+
-- Can't be used together with `pos`.
1094+
1095+
to_player = name,
1096+
-- Only play for this player.
1097+
-- Can't be used together with `exclude_player`.
1098+
1099+
exclude_player = name,
1100+
-- Don't play sound for this player.
1101+
-- Can't be used together with `to_player`.
1102+
1103+
max_hear_distance = 32,
1104+
-- Only play for players that are at most this far away when the sound
1105+
-- starts playing.
1106+
-- Needs `pos` or `object` to be set.
1107+
-- `32` is the default.
1108+
}
1109+
```
1110+
1111+
Examples:
10161112

10171113
```lua
10181114
-- Play locationless on all clients
10191115
{
10201116
gain = 1.0, -- default
1021-
fade = 0.0, -- default, change to a value > 0 to fade the sound in
1117+
fade = 0.0, -- default
10221118
pitch = 1.0, -- default
10231119
}
10241120
-- Play locationless to one player
10251121
{
10261122
to_player = name,
10271123
gain = 1.0, -- default
1028-
fade = 0.0, -- default, change to a value > 0 to fade the sound in
1124+
fade = 0.0, -- default
10291125
pitch = 1.0, -- default
10301126
}
10311127
-- Play locationless to one player, looped
@@ -1034,17 +1130,18 @@ Examples of sound parameter tables:
10341130
gain = 1.0, -- default
10351131
loop = true,
10361132
}
1037-
-- Play at a location
1133+
-- Play at a location, start the sound at offset 5 seconds
10381134
{
10391135
pos = {x = 1, y = 2, z = 3},
10401136
gain = 1.0, -- default
1041-
max_hear_distance = 32, -- default, uses a Euclidean metric
1137+
max_hear_distance = 32, -- default
1138+
start_time = 5.0,
10421139
}
10431140
-- Play connected to an object, looped
10441141
{
10451142
object = <an ObjectRef>,
10461143
gain = 1.0, -- default
1047-
max_hear_distance = 32, -- default, uses a Euclidean metric
1144+
max_hear_distance = 32, -- default
10481145
loop = true,
10491146
}
10501147
-- Play at a location, heard by anyone *but* the given player
@@ -1055,45 +1152,10 @@ Examples of sound parameter tables:
10551152
}
10561153
```
10571154

1058-
Looped sounds must either be connected to an object or played locationless to
1059-
one player using `to_player = name`.
1060-
1061-
A positional sound will only be heard by players that are within
1062-
`max_hear_distance` of the sound position, at the start of the sound.
1063-
1064-
`exclude_player = name` can be applied to locationless, positional and object-
1065-
bound sounds to exclude a single player from hearing them.
1066-
1067-
`SimpleSoundSpec`
1068-
-----------------
1069-
1070-
Specifies a sound name, gain (=volume) and pitch.
1071-
This is either a string or a table.
1072-
1073-
In string form, you just specify the sound name or
1074-
the empty string for no sound.
1075-
1076-
Table form has the following fields:
1077-
1078-
* `name`: Sound name
1079-
* `gain`: Volume (`1.0` = 100%)
1080-
* `pitch`: Pitch (`1.0` = 100%)
1081-
1082-
`gain` and `pitch` are optional and default to `1.0`.
1083-
1084-
Examples:
1085-
1086-
* `""`: No sound
1087-
* `{}`: No sound
1088-
* `"default_place_node"`: Play e.g. `default_place_node.ogg`
1089-
* `{name = "default_place_node"}`: Same as above
1090-
* `{name = "default_place_node", gain = 0.5}`: 50% volume
1091-
* `{name = "default_place_node", gain = 0.9, pitch = 1.1}`: 90% volume, 110% pitch
1092-
1093-
Special sound files
1094-
-------------------
1155+
Special sound-groups
1156+
--------------------
10951157

1096-
These sound files are played back by the engine if provided.
1158+
These sound-groups are played back by the engine if provided.
10971159

10981160
* `player_damage`: Played when the local player takes damage (gain = 0.5)
10991161
* `player_falling_damage`: Played when the local player takes
@@ -8804,7 +8866,10 @@ Used by `minetest.register_node`.
88048866

88058867
footstep = <SimpleSoundSpec>,
88068868
-- If walkable, played when object walks on it. If node is
8807-
-- climbable or a liquid, played when object moves through it
8869+
-- climbable or a liquid, played when object moves through it.
8870+
-- Sound is played at the base of the object's collision-box.
8871+
-- Gain is multiplied by `0.6`.
8872+
-- For local player, it's played position-less, with normal gain.
88088873

88098874
dig = <SimpleSoundSpec> or "__group",
88108875
-- While digging node.

doc/menu_lua_api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Filesystem
8181
* `core.sound_play(spec, looped)` -> handle
8282
* `spec` = `SimpleSoundSpec` (see `lua_api.md`)
8383
* `looped` = bool
84-
* `core.sound_stop(handle)`
84+
* `handle:stop()` or `core.sound_stop(handle)`
8585
* `core.get_video_drivers()`
8686
* get list of video drivers supported by engine (not all modes are guaranteed to work)
8787
* returns list of available video drivers' settings name and 'friendly' display name

games/devtest/mods/soundstuff/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ local path = minetest.get_modpath("soundstuff") .. "/"
33
dofile(path .. "sound_event_items.lua")
44
dofile(path .. "jukebox.lua")
55
dofile(path .. "bigfoot.lua")
6+
dofile(path .. "racecar.lua")

0 commit comments

Comments
 (0)