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
6ebecb8b
Commit
6ebecb8b
authored
Jan 13, 2025
by
santiago duque
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
usb swaping now working on mac, also playing from usb
parent
2c8e241e
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
111 additions
and
33 deletions
+111
-33
main.py
main.py
+65
-26
storage_media.py
storage_media.py
+46
-7
No files found.
main.py
View file @
6ebecb8b
import
os
import
os
from
os.path
import
isfile
,
join
from
os.path
import
isfile
,
join
import
pygame
import
pygame
import
keyboard
# import keyboard
from
storage_media
import
USBDeviceHandler
from
storage_media
import
USBDeviceHandler
# import time
# import time
# import signal
# import signal
# import keyboard
# import keyboard
pygame
.
init
()
pygame
.
init
()
winW
=
pygame
.
display
.
Info
()
.
current_w
*
0.
9
winW
=
pygame
.
display
.
Info
()
.
current_w
*
0.
2
winH
=
pygame
.
display
.
Info
()
.
current_h
*
0.
9
winH
=
pygame
.
display
.
Info
()
.
current_h
*
0.
2
window
=
pygame
.
display
.
set_mode
((
winW
,
winH
))
window
=
pygame
.
display
.
set_mode
((
winW
,
winH
))
#Fullscreen = True
#
Fullscreen = True
#window.fill((205,100,100))
#
window.fill((205,100,100))
#pygame.display.update()
#
pygame.display.update()
#clock = pygame.time.Clock()
#
clock = pygame.time.Clock()
dir_path
=
os
.
getcwd
()
def
reset_sounds
():
sound_dir
=
os
.
getcwd
()
+
"/sounds"
global
sound_array
contentlist
=
os
.
listdir
(
sound_dir
)
sound_array
=
[]
cut_current_sound
=
False
channel
=
False
current_sound_index
=
-
1
contentlist
=
sorted
(
contentlist
)
sound_array
=
[]
usb_handler
=
USBDeviceHandler
()
setup_sounds
()
def
setup_sounds
():
def
setup_sounds
():
global
sound_array
global
contentlist
global
sound_dir
print
(
"inside setup"
)
print
(
contentlist
)
for
content
in
contentlist
:
for
content
in
contentlist
:
print
(
join
(
sound_dir
,
content
))
if
isfile
(
join
(
sound_dir
,
content
)):
if
isfile
(
join
(
sound_dir
,
content
)):
if
content
.
endswith
(
".mp3"
)
or
content
.
endswith
(
".wav"
):
if
(
content
.
endswith
(
".mp3"
)
or
content
.
endswith
(
".wav"
))
and
(
not
content
.
startswith
(
"._"
)
):
sound
=
pygame
.
mixer
.
Sound
(
join
(
sound_dir
,
content
))
sound
=
pygame
.
mixer
.
Sound
(
join
(
sound_dir
,
content
))
sound_array
.
append
(
sound
)
sound_array
.
append
(
sound
)
print
(
content
)
print
(
content
)
print
(
"sound array"
)
print
(
sound_array
)
print
(
sound_array
)
...
@@ -45,6 +51,7 @@ def play_sound(index):
...
@@ -45,6 +51,7 @@ def play_sound(index):
global
cut_current_sound
global
cut_current_sound
global
current_sound_index
global
current_sound_index
global
channel
global
channel
global
sound_array
if
not
cut_current_sound
:
if
not
cut_current_sound
:
current_sound_index
=
index
current_sound_index
=
index
...
@@ -72,18 +79,50 @@ def play_sound(index):
...
@@ -72,18 +79,50 @@ def play_sound(index):
"""
"""
setup_sounds
()
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
)
run
=
True
run
=
True
while
run
:
while
run
:
#clock.tick(30)
#
clock.tick(30)
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
QUIT
:
if
event
.
type
==
pygame
.
QUIT
:
run
=
False
run
=
False
# keyboard module
# keyboard module
'''
"""
if keyboard.is_pressed("q"):
if keyboard.is_pressed("q"):
print("You Pressed A Key!")
print("You Pressed A Key!")
play_sound(0)
play_sound(0)
...
@@ -96,7 +135,7 @@ while run:
...
@@ -96,7 +135,7 @@ while run:
elif keyboard.is_pressed("r"):
elif keyboard.is_pressed("r"):
print("You Pressed A Key!")
print("You Pressed A Key!")
play_sound(3)
play_sound(3)
'''
"""
if
event
.
type
==
pygame
.
KEYDOWN
:
if
event
.
type
==
pygame
.
KEYDOWN
:
print
(
"you pressed "
+
pygame
.
key
.
name
(
event
.
key
))
print
(
"you pressed "
+
pygame
.
key
.
name
(
event
.
key
))
if
event
.
key
==
pygame
.
K_q
:
if
event
.
key
==
pygame
.
K_q
:
...
@@ -108,7 +147,7 @@ while run:
...
@@ -108,7 +147,7 @@ while run:
elif
event
.
key
==
pygame
.
K_r
:
elif
event
.
key
==
pygame
.
K_r
:
play_sound
(
3
)
play_sound
(
3
)
'''
"""
elif event.key == pygame.K_f:
elif event.key == pygame.K_f:
if Fullscreen:
if Fullscreen:
window = pygame.display.set_mode((winW,winH))
window = pygame.display.set_mode((winW,winH))
...
@@ -116,7 +155,7 @@ while run:
...
@@ -116,7 +155,7 @@ while run:
else:
else:
window = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
window = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
Fullscreen = True
Fullscreen = True
'''
"""
pygame
.
quit
()
pygame
.
quit
()
...
...
storage_media.py
View file @
6ebecb8b
from
glob
import
glob
from
glob
import
glob
from
subprocess
import
check_output
,
CalledProcessError
# from subprocess import check_output, CalledProcessError
import
platform
import
platform
import
os
# import os
import
psutil
import
time
from
usbmonitor
import
USBMonitor
from
usbmonitor
import
USBMonitor
from
usbmonitor.attributes
import
ID_MODEL
,
ID_MODEL_ID
,
ID_VENDOR_ID
,
DEVNAME
from
usbmonitor.attributes
import
ID_MODEL
,
ID_MODEL_ID
,
ID_VENDOR_ID
,
DEVNAME
class
USBDeviceHandler
:
class
ObjectWithEvents
(
object
):
callbacks
=
None
def
on
(
self
,
event_name
,
callback
):
print
(
'calling ON'
)
if
self
.
callbacks
is
None
:
self
.
callbacks
=
{}
if
event_name
not
in
self
.
callbacks
:
self
.
callbacks
[
event_name
]
=
[
callback
]
else
:
self
.
callbacks
[
event_name
]
.
append
(
callback
)
def
trigger
(
self
,
event_name
):
print
(
'calling TRIGGER'
)
if
self
.
callbacks
is
not
None
and
event_name
in
self
.
callbacks
:
for
callback
in
self
.
callbacks
[
event_name
]:
callback
(
self
)
class
USBDeviceHandler
(
ObjectWithEvents
):
def
__init__
(
self
):
def
__init__
(
self
):
print
(
"init USB handler"
)
self
.
monitor
=
USBMonitor
()
self
.
monitor
=
USBMonitor
()
self
.
startUSBmonitor
()
self
.
startUSBmonitor
()
...
@@ -24,18 +49,24 @@ class USBDeviceHandler:
...
@@ -24,18 +49,24 @@ class USBDeviceHandler:
def
on_connect
(
self
,
device_id
,
device_info
):
def
on_connect
(
self
,
device_id
,
device_info
):
print
(
"Connected: "
+
self
.
device_info_str
(
device_info
=
device_info
))
print
(
"Connected: "
+
self
.
device_info_str
(
device_info
=
device_info
))
print
(
self
.
get_usb_devices
())
print
(
self
.
get_usb_devices
())
self
.
trigger
(
'example_event'
)
def
on_disconnect
(
self
,
device_id
,
device_info
):
def
on_disconnect
(
self
,
device_id
,
device_info
):
print
(
"Disconnected: "
+
self
.
device_info_str
(
device_info
=
device_info
))
print
(
"Disconnected: "
+
self
.
device_info_str
(
device_info
=
device_info
))
print
(
self
.
get_usb_devices
())
print
(
self
.
get_usb_devices
())
self
.
trigger
(
'example_event'
)
def
get_usb_devices
(
self
):
def
get_usb_devices
(
self
):
# delay the reading for 1 second, i guess until it is mounted as partition?
time
.
sleep
(
2
)
current_os
=
platform
.
system
()
current_os
=
platform
.
system
()
print
(
current_os
)
print
(
current_os
)
disks
=
psutil
.
disk_partitions
()
# MacOS
# MacOS
if
current_os
==
"Darwin"
:
if
current_os
==
"Darwin"
:
os
.
chdir
(
"/Volumes"
)
os_drive_path
=
"/Volumes"
"""os.chdir("/Volumes")
# then do some listing
# then do some listing
List = os.listdir()
List = os.listdir()
i = 0
i = 0
...
@@ -47,12 +78,12 @@ class USBDeviceHandler:
...
@@ -47,12 +78,12 @@ class USBDeviceHandler:
List[i] = "/Volumes/" + List[i]
List[i] = "/Volumes/" + List[i]
i += 1
i += 1
return
List
return List
"""
# Linux
# Linux
elif
current_os
==
"Linux"
:
elif
current_os
==
"Linux"
:
os_drive_path
=
""
def
get_linux_usb_devices
():
"""
def get_linux_usb_devices():
sdb_devices = map(os.path.realpath, glob("/sys/block/sd*"))
sdb_devices = map(os.path.realpath, glob("/sys/block/sd*"))
usb_devices = (dev for dev in sdb_devices if "usb" in dev.split("/")[5])
usb_devices = (dev for dev in sdb_devices if "usb" in dev.split("/")[5])
return dict((os.path.basename(dev), dev) for dev in usb_devices)
return dict((os.path.basename(dev), dev) for dev in usb_devices)
...
@@ -62,3 +93,11 @@ class USBDeviceHandler:
...
@@ -62,3 +93,11 @@ class USBDeviceHandler:
is_usb = lambda path: any(dev in path for dev in devices)
is_usb = lambda path: any(dev in path for dev in devices)
usb_info = (line for line in output if is_usb(line.split()[0]))
usb_info = (line for line in output if is_usb(line.split()[0]))
return [(info.split()[0], info.split()[2]) for info in usb_info]
return [(info.split()[0], info.split()[2]) for info in usb_info]
"""
usb_disks
=
[]
for
disk
in
disks
:
if
(
os_drive_path
in
disk
.
mountpoint
)
and
(
disk
.
fstype
==
"exfat"
):
usb_disks
.
append
(
disk
)
return
usb_disks
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