1
1
import asyncio
2
2
from twitchio .ext import sounds
3
+ from typing import Optional
3
4
4
5
5
6
class AudioQueueManager :
6
7
"""
7
8
Manages a queue of audio files to be played sequentially with optional repeat and pause functionalities.
8
9
9
- Attributes:
10
- queue (asyncio.Queue[str]): A queue to hold paths of audio files to be played.
11
- is_playing (bool): Indicates whether an audio file is currently being played.
12
- repeat_queue (bool): If True, adds the current playing audio file back to the queue after playing.
13
- queue_paused (bool): If True, pauses the processing of the queue.
14
- player (sounds.AudioPlayer): An instance of AudioPlayer to play audio files.
15
- current_sound (str): Path of the currently playing audio file.
10
+ Attributes
11
+ ----------
12
+ queue: asyncio.Queue[:class:`str`]
13
+ A queue to hold paths of audio files to be played.
14
+ is_playing: :class:`bool`
15
+ Indicates whether an audio file is currently being played.
16
+ repeat_queue: :class:`bool`
17
+ If True, adds the current playing audio file back to the queue after playing.
18
+ queue_paused: :class:`bool`
19
+ If True, pauses the processing of the queue.
20
+ player: :class:`sounds.AudioPlayer`
21
+ An instance of AudioPlayer to play audio files.
22
+ current_sound: :class:`str`
23
+ Path of the currently playing audio file.
16
24
"""
17
25
18
- def __init__ (self ) :
26
+ def __init__ (self , repeat_queue : Optional [ bool ] = True ) -> None :
19
27
"""
20
28
Initializes an instance of AudioQueueManager with an empty queue and default settings.
29
+
30
+ Parameters
31
+ ----------
32
+ repeat_queue: Optional[:class:`bool`]
33
+ If True, adds the current playing audio file back to the queue after playing, by default True
21
34
"""
22
35
self .queue : asyncio .Queue [str ] = asyncio .Queue ()
23
36
self .is_playing : bool = False
24
- self .repeat_queue : bool = True
37
+ self .repeat_queue : bool = repeat_queue if repeat_queue is not None else True
25
38
self .queue_paused : bool = False
26
- self .player : sounds .AudioPlayer = sounds .AudioPlayer (
27
- callback = self .player_done )
39
+ self .player : sounds .AudioPlayer = sounds .AudioPlayer (callback = self .player_done )
28
40
self .current_sound : str = ""
29
41
30
42
async def player_done (self ) -> None :
31
43
"""
44
+ |coro|
45
+
32
46
Callback method called when the player finishes playing an audio file.
33
47
Resets the is_playing flag and marks the current task as done in the queue.
34
48
"""
35
- await asyncio .sleep (0.1 )
36
49
self .is_playing = False
37
50
self .queue .task_done ()
38
51
39
52
async def add_audio (self , sound_path : str ) -> None :
40
53
"""
54
+ |coro|
55
+
41
56
Adds a new audio file to the queue.
42
57
43
- Args:
44
- sound_path (str): Path of the audio file to add to the queue.
58
+ Parameters
59
+ ----------
60
+ sound_path: :class:`str`
61
+ Path of the audio file to add to the queue.
45
62
"""
46
- await asyncio .sleep (0.1 )
47
63
await self .queue .put (sound_path )
48
64
49
65
async def play_next (self ) -> None :
50
66
"""
67
+ |coro|
68
+
51
69
Plays the next audio file in the queue if the queue is not empty and not paused.
52
70
Sets the is_playing flag, retrieves the next audio file from the queue, and plays it.
53
71
If repeat_queue is True, adds the current audio file back to the queue after playing.
54
72
"""
55
- await asyncio .sleep (0.1 )
56
73
if not self .queue .empty () and not self .queue_paused :
57
74
self .is_playing = True
58
75
sound_path = await self .queue .get ()
@@ -62,74 +79,71 @@ async def play_next(self) -> None:
62
79
if self .repeat_queue :
63
80
await self .queue .put (self .current_sound )
64
81
65
- async def skip_audio (self ) -> None :
82
+ def skip_audio (self ) -> None :
66
83
"""
67
84
Stops the currently playing audio file if there is one.
68
85
"""
69
- await asyncio .sleep (0.1 )
70
86
if self .is_playing :
71
87
self .player .stop ()
72
88
self .is_playing = False
73
89
74
- async def stop_audio (self ) -> None :
90
+ def stop_audio (self ) -> None :
75
91
"""
76
92
Stops the currently playing audio file.
77
93
Resets the playing flag but leaves the queue intact.
78
94
"""
79
- await asyncio .sleep (0.1 )
80
95
if self .is_playing :
81
96
self .player .stop ()
82
97
self .is_playing = False
83
98
84
- async def pause_audio (self ) -> None :
99
+ def pause_audio (self ) -> None :
85
100
"""
86
101
Pauses the currently playing audio file.
87
102
"""
88
- await asyncio .sleep (0.1 )
89
103
self .player .pause ()
90
104
91
- async def resume_audio (self ) -> None :
105
+ def resume_audio (self ) -> None :
92
106
"""
93
107
Resumes the currently paused audio file.
94
108
"""
95
- await asyncio .sleep (0.1 )
96
109
self .player .resume ()
97
110
98
111
async def clear_queue (self ) -> None :
99
112
"""
113
+ |coro|
114
+
100
115
Clears all audio files from the queue.
101
116
"""
102
- await asyncio .sleep (0.1 )
103
117
while not self .queue .empty ():
104
118
await self .queue .get ()
105
119
self .queue .task_done ()
106
120
107
- async def pause_queue (self ) -> None :
121
+ def pause_queue (self ) -> None :
108
122
"""
109
123
Pauses the processing of the queue.
110
124
"""
111
- await asyncio .sleep (0.1 )
112
125
self .queue_paused = True
113
126
114
- async def resume_queue (self ) -> None :
127
+ def resume_queue (self ) -> None :
115
128
"""
116
129
Resumes the processing of the queue.
117
130
"""
118
- await asyncio .sleep (0.1 )
119
131
self .queue_paused = False
120
132
121
- async def get_queue_contents (self ) -> list :
133
+ def get_queue_contents (self ) -> list [ str ] :
122
134
"""
123
- Retrieves the current contents of the queue.
135
+ Retrieves the current contents of the queue as a list .
124
136
125
- Returns:
126
- list: List of paths of audio files in the queue.
137
+ Returns
138
+ -------
139
+ List[:class:`str`]
127
140
"""
128
- await asyncio .sleep (0.1 )
129
141
return list (self .queue ._queue )
130
142
131
143
async def queue_loop (self ) -> None :
132
144
"""
145
+ |coro|
146
+
133
147
Continuously checks the queue and plays the next audio file if not currently playing and not paused.
134
148
"""
135
149
try :
0 commit comments