Sound Demo

Screen shot of using sound demo
sound_demo.py
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""
Starting Template

Once you have learned how to use classes, you can begin your program with this
template.

If Python and Arcade are installed, this example can be run from the command line with:
python -m arcade.examples.starting_template
"""
import arcade
import typing

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Starting Template"
BUTTON_SIZE = 30


class SoundButton(arcade.SpriteSolidColor):
    """ Button, click-for-sound """
    def __init__(self, sound_file, pan, volume):
        super().__init__(BUTTON_SIZE, BUTTON_SIZE, arcade.color.WHITE)
        self.sound = arcade.Sound(sound_file)
        self.pan = pan
        self.volume = volume

    def play(self):
        """ Play """
        self.sound.play(pan=self.pan, volume=self.volume)

    


class AudioStreamButton(arcade.SpriteSolidColor):
    """ Button, click-for-streaming-sound """
    def __init__(self, sound_file, pan, volume):
        super().__init__(BUTTON_SIZE, BUTTON_SIZE, arcade.color.WHITE)
        self.sound = arcade.Sound(sound_file, streaming=True)
        self.pan = pan
        self.volume = volume

    def play(self):
        """ Play """
        self.sound.play(pan=self.pan, volume=self.volume)

    def volume_up(self):
        vol = self.sound.get_volume()
        self.sound.set_volume(vol+.1)
        print(f"Volume: {self.sound.get_volume()}")

    def stream_position(self):
        print(f"Current position: {self.sound.get_stream_position()}")


class MyGame(arcade.Window):
    """
    Main application class.

    NOTE: Go ahead and delete the methods you don't need.
    If you do need a method, delete the 'pass' and replace it
    with your own code. Don't leave 'pass' in this program.
    """

    def __init__(self, width, height, title):
        super().__init__(width, height, title)

        arcade.set_background_color(arcade.color.AMAZON)

        self.button_sprites = None

    def setup(self):
        self.button_sprites = arcade.SpriteList()

        y = SCREEN_HEIGHT / 2 + 150
        volume = 0.1
        button = AudioStreamButton(":resources:music/funkyrobot.mp3", pan=-1.0, volume=volume)
        button.center_x = BUTTON_SIZE
        button.center_y = y
        self.button_sprites.append(button)

        y = SCREEN_HEIGHT / 2 + 50
        volume = 0.1
        button = SoundButton(":resources:sounds/upgrade4.wav", pan=-1.0, volume=volume)
        button.center_x = BUTTON_SIZE
        button.center_y = y
        self.button_sprites.append(button)

        button = SoundButton(":resources:sounds/upgrade4.wav", pan=-0.5, volume=volume)
        button.center_x = SCREEN_WIDTH / 4
        button.center_y = y
        self.button_sprites.append(button)

        button = SoundButton(":resources:sounds/upgrade4.wav", pan=0, volume=volume)
        button.center_x = SCREEN_WIDTH / 2
        button.center_y = y
        self.button_sprites.append(button)

        button = SoundButton(":resources:sounds/upgrade4.wav", pan=0.5, volume=volume)
        button.center_x = SCREEN_WIDTH / 4 * 3
        button.center_y = y
        self.button_sprites.append(button)

        button = SoundButton(":resources:sounds/upgrade4.wav", pan=1, volume=volume)
        button.center_x = SCREEN_WIDTH - BUTTON_SIZE
        button.center_y = y
        self.button_sprites.append(button)

        y = SCREEN_HEIGHT / 2
        volume = 0.5
        button = SoundButton(":resources:sounds/upgrade4.wav", pan=-1.0, volume=volume)
        button.center_x = BUTTON_SIZE
        button.center_y = y
        self.button_sprites.append(button)

        button = SoundButton(":resources:sounds/upgrade4.wav", pan=-0.5, volume=volume)
        button.center_x = SCREEN_WIDTH / 4
        button.center_y = y
        self.button_sprites.append(button)

        button = SoundButton(":resources:sounds/upgrade4.wav", pan=0, volume=volume)
        button.center_x = SCREEN_WIDTH / 2
        button.center_y = y
        self.button_sprites.append(button)

        button = SoundButton(":resources:sounds/upgrade4.wav", pan=0.5, volume=volume)
        button.center_x = SCREEN_WIDTH / 4 * 3
        button.center_y = y
        self.button_sprites.append(button)

        button = SoundButton(":resources:sounds/upgrade4.wav", pan=1, volume=volume)
        button.center_x = SCREEN_WIDTH - BUTTON_SIZE
        button.center_y = y
        self.button_sprites.append(button)

        y = SCREEN_HEIGHT / 2 - 50
        volume = 1
        button = SoundButton(":resources:sounds/upgrade4.wav", pan=-1.0, volume=volume)
        button.center_x = BUTTON_SIZE
        button.center_y = y
        self.button_sprites.append(button)

        button = SoundButton(":resources:sounds/upgrade4.wav", pan=-0.5, volume=volume)
        button.center_x = SCREEN_WIDTH / 4
        button.center_y = y
        self.button_sprites.append(button)

        button = SoundButton(":resources:sounds/upgrade4.wav", pan=0, volume=volume)
        button.center_x = SCREEN_WIDTH / 2
        button.center_y = y
        self.button_sprites.append(button)

        button = SoundButton(":resources:sounds/upgrade4.wav", pan=0.5, volume=volume)
        button.center_x = SCREEN_WIDTH / 4 * 3
        button.center_y = y
        self.button_sprites.append(button)

        button = SoundButton(":resources:sounds/upgrade4.wav", pan=1, volume=volume)
        button.center_x = SCREEN_WIDTH - BUTTON_SIZE
        button.center_y = y
        self.button_sprites.append(button)

    def on_draw(self):
        """
        Render the screen.
        """

        # This command should happen before we start drawing. It will clear
        # the screen to the background color, and erase what we drew last frame.
        arcade.start_render()

        # Call draw() on all your sprite lists below
        self.button_sprites.draw()

    def on_update(self, delta_time):
        """
        All the logic to move, and the game logic goes here.
        Normally, you'll call update() on the sprite lists that
        need it.
        """
        self.button_sprites.update()

    def on_key_press(self, key, key_modifiers):
        """
        Called whenever a key on the keyboard is pressed.

        For a full list of keys, see:
        http://arcade.academy/arcade.key.html
        """
        pass

    def on_key_release(self, key, key_modifiers):
        """
        Called whenever the user lets off a previously pressed key.
        """
        pass

    def on_mouse_motion(self, x, y, delta_x, delta_y):
        """
        Called whenever the mouse moves.
        """
        pass

    def on_mouse_press(self, x, y, button, key_modifiers):
        """
        Called when the user presses a mouse button.
        """
        hit_sprites = arcade.get_sprites_at_point((x, y), self.button_sprites)
        for sprite in hit_sprites:
            button_sprite = typing.cast(SoundButton, sprite)
            if button == arcade.MOUSE_BUTTON_LEFT:
                button_sprite.play()
            elif button == arcade.MOUSE_BUTTON_RIGHT: # right click to increase volume on currently playing sound
                if button_sprite.sound.voice_handle:
                    button_sprite.volume_up()
                    button_sprite.stream_position()
            

    def on_mouse_release(self, x, y, button, key_modifiers):
        """
        Called when a user releases a mouse button.
        """
        pass


def main():
    """ Main method """
    game = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    game.setup()
    arcade.run()


if __name__ == "__main__":
    main()