Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
app-pyaudioplayer
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
santiago duque
app-pyaudioplayer
Commits
c12f60e6
Commit
c12f60e6
authored
Jan 14, 2025
by
santiago duque
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
now safe to run on mac without GPIOs
parent
d15848b5
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
296 deletions
+30
-296
main copy.py
main copy.py
+0
-211
main.py
main.py
+30
-85
No files found.
main copy.py
deleted
100644 → 0
View file @
d15848b5
import
sys
import
os
from
os.path
import
isfile
,
join
import
pygame
from
gpiozero
import
Button
# import keyboard
from
storage_media
import
USBDeviceHandler
# import time
# import signal
# import keyboard
pygame
.
init
()
winW
=
pygame
.
display
.
Info
()
.
current_w
*
0.2
winH
=
pygame
.
display
.
Info
()
.
current_h
*
0.2
window
=
pygame
.
display
.
set_mode
((
winW
,
winH
))
# Fullscreen = True
# window.fill((205,100,100))
# pygame.display.update()
#clock = pygame.time.Clock()
def
reset_sounds
():
global
sound_array
sound_array
=
[]
setup_sounds
()
def
setup_sounds
():
global
sound_array
global
contentlist
global
sound_dir
print
(
"inside setup"
)
print
(
contentlist
)
for
content
in
contentlist
:
print
(
join
(
sound_dir
,
content
))
if
isfile
(
join
(
sound_dir
,
content
)):
if
(
content
.
endswith
(
".mp3"
)
or
content
.
endswith
(
".wav"
))
and
(
not
content
.
startswith
(
"._"
)
):
sound
=
pygame
.
mixer
.
Sound
(
join
(
sound_dir
,
content
))
sound_array
.
append
(
sound
)
print
(
content
)
print
(
"sound array"
)
print
(
sound_array
)
sound_playing
=
False
def
play_sound
(
index
):
global
sound_playing
global
cut_current_sound
global
current_sound_index
global
channel
global
sound_array
global
btn_array
print
(
'play func called'
)
sound_playing
=
True
if
not
cut_current_sound
:
current_sound_index
=
index
channel
=
sound_array
[
current_sound_index
]
.
play
()
while
channel
.
get_busy
():
pygame
.
time
.
wait
(
100
)
# ms
print
(
"Playing..."
)
print
(
btn_array
[
current_sound_index
]
.
is_held
)
print
(
btn_array
[
current_sound_index
]
.
is_pressed
)
pygame
.
event
.
clear
()
sound_playing
=
False
btn_array
[
current_sound_index
]
.
when_pressed
=
lambda
:
btn_pressed
(
index
)
print
(
"Finished. "
)
""" todo
make sounds stop when new key pressed
else:
if current_sound_index == index:
current_sound_index = index
channel = sound_array[current_sound_index].play()
while channel.get_busy():
pygame.time.wait(100) # ms
print("Playing...pygame.init()")
print("Finished.")
else:
print("STOP.")
"""
def
setup_paths
(
val
):
global
contentlist
global
sound_dir
global
sound_dir_local
global
usb_handler
sound_dir
=
sound_dir_local
usbdisks
=
usb_handler
.
get_usb_devices
()
if
len
(
usbdisks
)
>
0
:
sound_dir
=
usbdisks
[
0
]
.
mountpoint
contentlist
=
os
.
listdir
(
sound_dir
)
contentlist
=
sorted
(
contentlist
)
reset_sounds
()
# prepare local paths in case no USB
sound_dir_local
=
os
.
getcwd
()
+
"/sounds"
sound_dir
=
""
# start USB handler with listener
usb_handler
=
USBDeviceHandler
()
usb_handler
.
on
(
"example_event"
,
setup_paths
)
# prepare sound variables
sound_array
=
[]
cut_current_sound
=
False
channel
=
False
current_sound_index
=
-
1
# run setup
setup_paths
(
False
)
def
btn_pressed
(
index
):
global
btn_array
global
sound_playing
btn_array
[
index
]
.
when_pressed
=
None
#btn_array[index].when_released = lambda: resetBtn(index)
print
(
'keeps pressing'
)
if
(
not
sound_playing
):
print
(
'btn pressed'
)
play_sound
(
index
)
"""
def resetBtn(index):
global btn_array
#if(sound_playing):
print('btn released')
btn_array[index].when_released = None
btn_array[index].when_pressed = lambda: btn_pressed(index) """
btn1
=
Button
(
21
)
btn1
.
when_pressed
=
lambda
:
btn_pressed
(
0
)
#btn1.when_held = None
#btn1.when_released = lambda: resetBtn(0)
btn2
=
Button
(
13
)
btn2
.
when_pressed
=
lambda
:
btn_pressed
(
1
)
#btn2.when_held = None
#btn2.when_released = lambda: resetBtn(1)
btn_array
=
[
btn1
,
btn2
]
run
=
True
while
run
:
#clock.tick(25)
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
QUIT
:
run
=
False
# keyboard module
"""
if keyboard.is_pressed("q"):
print("You Pressed A Key!")
play_sound(0)
elif keyboard.is_pressed("w"):
print("You Pressed A Key!")
play_sound(1)
elif keyboard.is_pressed("e"):
print("You Pressed A Key!")
play_sound(2)
elif keyboard.is_pressed("r"):
print("You Pressed A Key!")
play_sound(3)
"""
if
event
.
type
==
pygame
.
KEYDOWN
:
print
(
"you pressed "
+
pygame
.
key
.
name
(
event
.
key
))
if
event
.
key
==
pygame
.
K_q
:
play_sound
(
0
)
elif
event
.
key
==
pygame
.
K_w
:
play_sound
(
1
)
elif
event
.
key
==
pygame
.
K_e
:
play_sound
(
2
)
elif
event
.
key
==
pygame
.
K_r
:
play_sound
(
3
)
"""
elif event.key == pygame.K_f:
if Fullscreen:
window = pygame.display.set_mode((winW,winH))
Fullscreen = False
else:
window = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
Fullscreen = True
"""
pygame
.
quit
()
sys
.
exit
()
exit
()
# time.sleep(5.5)
# signal.pause()
main.py
View file @
c12f60e6
...
...
@@ -3,28 +3,21 @@ import os
from
os.path
import
isfile
,
join
import
pygame
from
gpiozero
import
Button
from
signal
import
pause
# import keyboard
from
storage_media
import
USBDeviceHandler
# import time
# import signal
# import keyboard
pygame
.
init
()
winW
=
pygame
.
display
.
Info
()
.
current_w
*
0.2
winH
=
pygame
.
display
.
Info
()
.
current_h
*
0.2
window
=
pygame
.
display
.
set_mode
((
winW
,
winH
))
clock
=
pygame
.
time
.
Clock
()
# Fullscreen = True
# window.fill((205,100,100))
# pygame.display.update()
clock
=
pygame
.
time
.
Clock
()
def
reset_sounds
():
global
sound_array
sound_array
=
[]
setup_sounds
()
...
...
@@ -44,33 +37,24 @@ def setup_sounds():
):
sound
=
pygame
.
mixer
.
Sound
(
join
(
sound_dir
,
content
))
sound_array
.
append
(
sound
)
print
(
sound_array
)
sound_playing
=
False
def
play_sound
(
index
):
global
sound_playing
global
cut_current_sound
global
current_sound_index
global
channel
global
sound_array
global
btn_array
print
(
"play func called"
)
sound_playing
=
True
if
not
cut_current_sound
:
current_sound_index
=
index
channel
=
sound_array
[
current_sound_index
]
.
play
()
while
channel
.
get_busy
():
pygame
.
time
.
wait
(
1
0
0
)
# ms
pygame
.
time
.
wait
(
1
5
0
)
# ms
print
(
"Playing..."
)
pygame
.
event
.
clear
()
sound_playing
=
False
#btn_array[current_sound_index].when_pressed = lambda: btn_pressed(index)
print
(
"Finished. "
)
print
(
"Finished playing."
)
""" todo
make sounds stop when new key pressed
...
...
@@ -120,75 +104,48 @@ cut_current_sound = False
channel
=
False
current_sound_index
=
-
1
# run setup
# run
empty
setup
setup_paths
(
False
)
def
btn_pressed
(
index
):
global
btn_array
global
sound_playing
btn_array
[
index
]
.
when_pressed
=
None
# btn_array[index].when_released = lambda: resetBtn(index)
print
(
"keeps pressing"
)
if
not
sound_playing
:
print
(
"btn pressed"
)
play_sound
(
index
)
"""
def resetBtn(index):
global btn_array
#if(sound_playing):
print('btn released')
btn_array[index].when_released = None
btn_array[index].when_pressed = lambda: btn_pressed(index) """
btn1
=
Button
(
21
)
#btn1.when_pressed = lambda: btn_pressed(0)
# btn1.when_held = None
# btn1.when_released = lambda: resetBtn(0)
btn2
=
Button
(
13
)
#btn2.when_pressed = lambda: btn_pressed(1)
# btn2.when_held = None
# btn2.when_released = lambda: resetBtn(1)
"""
DEFINE GPIO BUTTONS
"""
btn1
=
False
btn2
=
False
btn3
=
False
btn4
=
False
btn_array
=
[
btn1
,
btn2
]
try
:
btn1
=
Button
(
21
)
btn2
=
Button
(
13
)
btn3
=
Button
(
5
)
btn4
=
Button
(
27
)
except
Exception
as
X
:
print
(
X
)
# pause()
run
=
True
while
run
:
clock
.
tick
(
25
)
print
(
btn1
.
value
)
# check pins only if they exist
if
btn1
and
btn2
and
btn3
and
btn4
:
if
btn1
.
is_pressed
:
play_sound
(
0
)
elif
btn2
.
is_pressed
:
play_sound
(
1
)
elif
btn3
.
is_pressed
:
play_sound
(
2
)
elif
btn4
.
is_pressed
:
play_sound
(
3
)
# check pygame events
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
QUIT
:
run
=
False
# keyboard module
"""
if keyboard.is_pressed("q"):
print("You Pressed A Key!")
play_sound(0)
elif keyboard.is_pressed("w"):
print("You Pressed A Key!")
play_sound(1)
elif keyboard.is_pressed("e"):
print("You Pressed A Key!")
play_sound(2)
elif keyboard.is_pressed("r"):
print("You Pressed A Key!")
play_sound(3)
"""
if
event
.
type
==
pygame
.
KEYDOWN
:
print
(
"you pressed "
+
pygame
.
key
.
name
(
event
.
key
))
if
event
.
key
==
pygame
.
K_q
:
...
...
@@ -200,19 +157,7 @@ while run:
elif
event
.
key
==
pygame
.
K_r
:
play_sound
(
3
)
"""
elif event.key == pygame.K_f:
if Fullscreen:
window = pygame.display.set_mode((winW,winH))
Fullscreen = False
else:
window = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
Fullscreen = True
"""
pygame
.
quit
()
sys
.
exit
()
exit
()
# time.sleep(5.5)
# signal.pause()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment