-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauxloop.py
More file actions
155 lines (125 loc) · 4.33 KB
/
auxloop.py
File metadata and controls
155 lines (125 loc) · 4.33 KB
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
import pyaudio
import wave
import os
import sys
import tkinter as tk
from tkinter import filedialog
import threading
import time
import pygame
from pygame.locals import *
# Music playing class
class WavPlayer(threading.Thread):
CHUNK = 1024
def __init__(self, filepath, loop=True):
super(WavPlayer, self).__init__()
self.filepath = os.path.abspath(filepath)
self.loop = loop
self.end = False
self.hold = False
def run(self):
# Open Wave File and start play!
wf = wave.open(self.filepath, 'rb')
player = pyaudio.PyAudio()
# Open Output Stream (based on PyAudio tutorial)
stream = player.open(format=player.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
# PLAYBACK LOOP
data = wf.readframes(self.CHUNK)
while not self.end:
stream.write(data)
data = wf.readframes(self.CHUNK)
while self.hold:
time.sleep(.1)
if data == b'': # If file is over then rewind.
if not self.loop:
break
wf.rewind()
data = wf.readframes(self.CHUNK)
stream.close()
player.terminate()
# ONLY CALL THIS FUNCTION ONCE
def play(self):
"""
another name for self.start()
"""
self.start()
def pause(self):
"""
pause playback.
"""
self.hold = True
def resume(self):
self.hold = False
def stop(self):
self.end = True
pygame.init()
screen = pygame.display.set_mode((797, 599))
pos = pygame.mouse.get_pos()
# Load images
play_image = pygame.image.load('Sprites/play-button.png').convert_alpha()
pause_image = pygame.image.load('Sprites/pause-button.png').convert_alpha()
upload_image = pygame.image.load('Sprites/upload-file.png').convert_alpha()
# Button class
class Button():
hasfp = False
def __init__(self, x, y, img, scale=1):
self.image = pygame.transform.scale(img, (int(img.get_width() * scale), int(img.get_height() * scale)))
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
self.x = x
self.y = y
self.click = False
self.activate = False
def draw(self):
if self.rect.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1 and not self.click:
self.click = True
screen.blit(self.image, (self.x, self.y))
return True
if pygame.mouse.get_pressed()[0] == 0:
self.click = False
screen.blit(self.image, (self.x, self.y))
return False
# play_button = Button(60, 20, play_image)
# pause_button = Button(100, 20, pause_image)
# upload_button = Button(20, 20, upload_image)
play_buttons = [Button(60, 20*i, play_image) for i in range(8)]
pause_buttons = [Button(100, 20*i, pause_image) for i in range(8)]
upload_buttons = [Button(20, 20*i, upload_image) for i in range(8)]
sounds = [WavPlayer('C:/Users/Austi/stuff/PythonDev/audioPlayer/spool shard new 2 outline.wav', False) for i in range(8)]
sounds[0].loop = True
sounds[1].loop = True
running = True
while running:
pos = pygame.mouse.get_pos()
for i in range(8):
if play_buttons[i].draw() and play_buttons[i].hasfp:
sounds[i].hold = False
if pause_buttons[i].draw() and pause_buttons[i].hasfp:
sounds[i].hold = True
if upload_buttons[i].draw():
print(i)
root = tk.Tk()
filepath = filedialog.askopenfilename()
root.withdraw()
if play_buttons[i].hasfp:
sounds[i].resume()
sounds[i].stop()
sounds[i].join()
sounds[i] = WavPlayer(filepath)
sounds[i].play()
# music.pause()
play_buttons[i].hasfp = True
pause_buttons[i].hasfp = True
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
for i in range(8):
sounds[i].stop()
if play_buttons[i].hasfp:
sounds[i].join()
break
pygame.display.update()