Skip to content

Commit d26766f

Browse files
authored
Add ability to play music from a specific position (#20)
1 parent b643f7d commit d26766f

5 files changed

Lines changed: 146 additions & 115 deletions

File tree

addons/sound_manager/SoundManager.cs

Lines changed: 120 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -5,163 +5,174 @@ namespace NathanHoad
55
{
66
public partial class SoundManager : Node
77
{
8-
private static Node instance;
9-
public static Node Instance
10-
{
11-
get
12-
{
13-
if (instance == null)
14-
{
15-
instance = (Node)Engine.GetSingleton("SoundManager");
16-
}
17-
return instance;
18-
}
19-
}
8+
private static Node instance;
9+
public static Node Instance
10+
{
11+
get
12+
{
13+
if (instance == null)
14+
{
15+
instance = (Node)Engine.GetSingleton("SoundManager");
16+
}
17+
return instance;
18+
}
19+
}
2020

2121

22-
#region Sounds
22+
#region Sounds
2323

24-
public static float GetSoundVolume()
25-
{
26-
return (float)Instance.Call("get_sound_volume");
27-
}
24+
public static float GetSoundVolume()
25+
{
26+
return (float)Instance.Call("get_sound_volume");
27+
}
2828

2929

30-
public static float GetUISoundVolume()
31-
{
32-
return (float)Instance.Call("get_ui_sound_volume");
33-
}
30+
public static float GetUISoundVolume()
31+
{
32+
return (float)Instance.Call("get_ui_sound_volume");
33+
}
3434

3535

36-
public static void SetSoundVolume(float volume)
37-
{
38-
Instance.Call("set_sound_volume", volume);
39-
}
36+
public static void SetSoundVolume(float volume)
37+
{
38+
Instance.Call("set_sound_volume", volume);
39+
}
4040

4141

42-
public static AudioStreamPlayer PlaySound(AudioStream resource, string overrideBus = "")
43-
{
44-
return (AudioStreamPlayer)Instance.Call("play_sound", resource, overrideBus);
45-
}
42+
public static AudioStreamPlayer PlaySound(AudioStream resource, string overrideBus = "")
43+
{
44+
return (AudioStreamPlayer)Instance.Call("play_sound", resource, overrideBus);
45+
}
4646

4747

48-
public static AudioStreamPlayer PlaySoundWithPitch(AudioStream resource, float pitch, string overrideBus = "")
49-
{
50-
return (AudioStreamPlayer)Instance.Call("play_sound_with_pitch", resource, pitch, overrideBus);
51-
}
48+
public static AudioStreamPlayer PlaySoundWithPitch(AudioStream resource, float pitch, string overrideBus = "")
49+
{
50+
return (AudioStreamPlayer)Instance.Call("play_sound_with_pitch", resource, pitch, overrideBus);
51+
}
5252

5353

54-
public static AudioStreamPlayer PlayUISound(AudioStream resource, string overrideBus = "")
55-
{
56-
return (AudioStreamPlayer)Instance.Call("play_ui_sound", resource, overrideBus);
57-
}
54+
public static AudioStreamPlayer PlayUISound(AudioStream resource, string overrideBus = "")
55+
{
56+
return (AudioStreamPlayer)Instance.Call("play_ui_sound", resource, overrideBus);
57+
}
5858

5959

60-
public static AudioStreamPlayer PlayUISoundWithPitch(AudioStream resource, float pitch, string overrideBus = "")
61-
{
62-
return (AudioStreamPlayer)Instance.Call("play_ui_sound_with_pitch", resource, pitch, overrideBus);
63-
}
60+
public static AudioStreamPlayer PlayUISoundWithPitch(AudioStream resource, float pitch, string overrideBus = "")
61+
{
62+
return (AudioStreamPlayer)Instance.Call("play_ui_sound_with_pitch", resource, pitch, overrideBus);
63+
}
6464

6565

66-
public static void SetDefaultSoundBus(string bus)
67-
{
68-
Instance.Call("set_default_sound_bus", bus);
69-
}
66+
public static void SetDefaultSoundBus(string bus)
67+
{
68+
Instance.Call("set_default_sound_bus", bus);
69+
}
7070

7171

72-
public static void SetDefaultUISoundBus(string bus)
73-
{
74-
Instance.Call("set_default_ui_sound_bus", bus);
75-
}
72+
public static void SetDefaultUISoundBus(string bus)
73+
{
74+
Instance.Call("set_default_ui_sound_bus", bus);
75+
}
7676

77-
#endregion
77+
#endregion
7878

7979

80-
#region Music
80+
#region Music
8181

82-
public static float GetMusicVolume()
83-
{
84-
return (float)Instance.Call("get_music_volume");
85-
}
82+
public static float GetMusicVolume()
83+
{
84+
return (float)Instance.Call("get_music_volume");
85+
}
8686

8787

88-
public static void SetMusicVolume(float volume)
89-
{
90-
Instance.Call("set_music_volume", volume);
91-
}
88+
public static void SetMusicVolume(float volume)
89+
{
90+
Instance.Call("set_music_volume", volume);
91+
}
9292

9393

94-
public static AudioStreamPlayer PlayMusic(AudioStream resource, float crossFadeDuration = 0.0f, string overrideBus = "")
95-
{
96-
return (AudioStreamPlayer)Instance.Call("play_music", resource, crossFadeDuration, overrideBus);
97-
}
94+
public static AudioStreamPlayer PlayMusic(AudioStream resource, float crossFadeDuration = 0.0f, string overrideBus = "")
95+
{
96+
return (AudioStreamPlayer)Instance.Call("play_music", resource, crossFadeDuration, overrideBus);
97+
}
9898

9999

100-
public static AudioStreamPlayer PlayMusicAtVolume(AudioStream resource, float volume, float crossFadeDuration = 0.0f, string overrideBus = "")
101-
{
102-
return (AudioStreamPlayer)Instance.Call("play_music_at_volume", resource, volume, crossFadeDuration, overrideBus);
103-
}
100+
public static AudioStreamPlayer PlayMusicFromPosition(AudioStream resource, float position, float crossFadeDuration = 0.0f, string overrideBus = "")
101+
{
102+
return (AudioStreamPlayer)Instance.Call("play_music_from_position", resource, position, crossFadeDuration, overrideBus);
103+
}
104104

105105

106-
public static Array<string> GetMusicTrackHistory()
107-
{
108-
return (Array<string>)Instance.Call("get_music_track_history");
109-
}
106+
public static AudioStreamPlayer PlayMusicAtVolume(AudioStream resource, float volume, float crossFadeDuration = 0.0f, string overrideBus = "")
107+
{
108+
return (AudioStreamPlayer)Instance.Call("play_music_at_volume", resource, volume, crossFadeDuration, overrideBus);
109+
}
110110

111+
public static AudioStreamPlayer PlayMusicFromPositionAtVolume(AudioStream resource, float position, float volume, float crossFadeDuration = 0.0f, string overrideBus = "")
112+
{
113+
return (AudioStreamPlayer)Instance.Call("play_music_from_position_at_volume", resource, position, volume, crossFadeDuration, overrideBus);
114+
}
111115

112-
public static string GetLastPlayedMusicTrack()
113-
{
114-
return (string)Instance.Call("get_last_played_music_track");
115-
}
116116

117+
public static Array<string> GetMusicTrackHistory()
118+
{
119+
return (Array<string>)Instance.Call("get_music_track_history");
120+
}
117121

118-
public static bool IsMusicPlaying(AudioStream resource = null)
119-
{
120-
return (bool)Instance.Call("is_music_playing", resource);
121-
}
122122

123+
public static string GetLastPlayedMusicTrack()
124+
{
125+
return (string)Instance.Call("get_last_played_music_track");
126+
}
123127

124-
public static bool IsMusicTrackPlaying(string resource_path)
125-
{
126-
return (bool)Instance.Call("is_music_track_playing", resource_path);
127-
}
128128

129+
public static bool IsMusicPlaying(AudioStream resource = null)
130+
{
131+
return (bool)Instance.Call("is_music_playing", resource);
132+
}
129133

130-
public static Array<AudioStream> GetCurrentlyPlayingMusic()
131-
{
132-
return (Array<AudioStream>)Instance.Call("get_currently_playing_music");
133-
}
134134

135+
public static bool IsMusicTrackPlaying(string resource_path)
136+
{
137+
return (bool)Instance.Call("is_music_track_playing", resource_path);
138+
}
135139

136-
public static Array<string> GetCurrentlyPlayingTracks()
137-
{
138-
return (Array<string>)Instance.Call("get_currently_playing_tracks");
139-
}
140140

141+
public static Array<AudioStream> GetCurrentlyPlayingMusic()
142+
{
143+
return (Array<AudioStream>)Instance.Call("get_currently_playing_music");
144+
}
141145

142-
public static void PauseMusic(AudioStream resource = null)
143-
{
144-
Instance.Call("pause_music", resource);
145-
}
146146

147+
public static Array<string> GetCurrentlyPlayingTracks()
148+
{
149+
return (Array<string>)Instance.Call("get_currently_playing_tracks");
150+
}
147151

148-
public static void ResumeMusic(AudioStream resource = null)
149-
{
150-
Instance.Call("resume_music", resource);
151-
}
152152

153+
public static void PauseMusic(AudioStream resource = null)
154+
{
155+
Instance.Call("pause_music", resource);
156+
}
153157

154-
public static void StopMusic(float fadeOutDuration = 0.0f)
155-
{
156-
Instance.Call("stop_music", fadeOutDuration);
157-
}
158158

159+
public static void ResumeMusic(AudioStream resource = null)
160+
{
161+
Instance.Call("resume_music", resource);
162+
}
159163

160-
public static void SetDefaultMusicBus(string bus)
161-
{
162-
Instance.Call("set_default_music_bus", bus);
163-
}
164164

165-
#endregion
165+
public static void StopMusic(float fadeOutDuration = 0.0f)
166+
{
167+
Instance.Call("stop_music", fadeOutDuration);
168+
}
169+
170+
171+
public static void SetDefaultMusicBus(string bus)
172+
{
173+
Instance.Call("set_default_music_bus", bus);
174+
}
175+
176+
#endregion
166177
}
167-
}
178+
}

addons/sound_manager/music.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var tweens: Dictionary = {}
55
var track_history: PackedStringArray = []
66

77

8-
func play(resource: AudioStream, volume: float = 0.0, crossfade_duration: float = 0.0, override_bus: String = "") -> AudioStreamPlayer:
8+
func play(resource: AudioStream, position: float = 0.0, volume: float = 0.0, crossfade_duration: float = 0.0, override_bus: String = "") -> AudioStreamPlayer:
99
stop(crossfade_duration * 2)
1010

1111
var player = _get_player_with_music(resource)
@@ -24,7 +24,7 @@ func play(resource: AudioStream, volume: float = 0.0, crossfade_duration: float
2424
if track_history.size() > 50:
2525
track_history.remove_at(50)
2626

27-
player.call_deferred("play")
27+
player.call_deferred("play", position)
2828
return player
2929

3030

addons/sound_manager/sound_manager.gd

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,19 @@ func set_music_volume(volume_between_0_and_1: float) -> void:
9191

9292

9393
func play_music(resource: AudioStream, crossfade_duration: float = 0.0, override_bus: String = "") -> AudioStreamPlayer:
94-
return music.play(resource, 0.0, crossfade_duration, override_bus)
94+
return music.play(resource, 0.0, 0.0, crossfade_duration, override_bus)
95+
96+
97+
func play_music_from_position(resource: AudioStream, position: float = 0.0, crossfade_duration: float = 0.0, override_bus: String = "") -> AudioStreamPlayer:
98+
return music.play(resource, position, 0.0, crossfade_duration, override_bus)
9599

96100

97101
func play_music_at_volume(resource: AudioStream, volume: float = 0.0, crossfade_duration: float = 0.0, override_bus: String = "") -> AudioStreamPlayer:
98-
return music.play(resource, volume, crossfade_duration, override_bus)
102+
return music.play(resource, 0.0, volume, crossfade_duration, override_bus)
103+
104+
105+
func play_music_from_position_at_volume(resource: AudioStream, position: float = 0.0, volume: float = 0.0, crossfade_duration: float = 0.0, override_bus: String = "") -> AudioStreamPlayer:
106+
return music.play(resource, position, volume, crossfade_duration, override_bus)
99107

100108

101109
func get_music_track_history() -> Array:

docs/Music.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,24 @@
1414

1515
This returns the `AudioStreamPlayer` that is playing the sound so you can make any other adjustments you need.
1616

17+
- **`SoundManager.play_music_from_position(resource: AudioStream, position: float = 0.0, crossfade_duration: int = 0, override_bus: String = "") -> AudioStreamPlayer`**
18+
19+
Play some music from a certain point in the audio file with an optional fade in (or crossfade if something is already playing) and optionally specify which audio bus to use.
20+
21+
This returns the `AudioStreamPlayer` that is playing the sound so you can make any other adjustments you need.
22+
1723
- **`SoundManager.play_music_at_volume(resource: AudioStream, volume: float = 0.0, crossfade_duration: int = 0, override_bus: String = "") -> AudioStreamPlayer`**
1824

1925
Play some music at a given volume with an optional fade in (or crossfade if something is already playing) and optionally specify which audio bus to use.
2026

2127
This returns the `AudioStreamPlayer` that is playing the sound so you can make any other adjustments you need.
2228

29+
- **`SoundManager.play_music_from_position_at_volume(resource: AudioStream, position: floatm = 0.0 volume: float = 0.0, crossfade_duration: int = 0, override_bus: String = "") -> AudioStreamPlayer`**
30+
31+
Play some music from a certain point in the audio file at a given volume with an optional fade in (or crossfade if something is already playing) and optionally specify which audio bus to use.
32+
33+
This returns the `AudioStreamPlayer` that is playing the sound so you can make any other adjustments you need.
34+
2335
- **`SoundManager.get_music_track_history() -> Array`**
2436

2537
Get the list of recently played resource paths (capped at 50).

examples/example.tscn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ offset_bottom = 182.0
8080

8181
[node name="PlayMusic" type="Button" parent="Music"]
8282
layout_mode = 0
83-
offset_left = 212.0
83+
offset_left = 229.0
8484
offset_top = 220.0
85-
offset_right = 351.0
85+
offset_right = 368.0
8686
offset_bottom = 251.0
8787
text = "Play music"
8888

0 commit comments

Comments
 (0)