Hey all,
I’m working on a Pygame project where I’m trying to integrate dynamic background music. The goal is to have the music adapt based on the game’s intensity level.
The Problem: The session’s clock isn’t advancing when I run it in a separate thread. As a result, my instruments keep playing indefinitely without considering note lengths, like the session is stuck at time t=0.
What I’ve Tried:
- Creating the SCAMP Session in the Main Thread, Running Music in a Separate Thread:
I set up the Session and instruments in the main thread and started the musical processes in a separate thread. Here’s a simplified code snippet:
from scamp import *
import threading
class MusicController:
def init(self):
self.session = Session(tempo=120)
self.drone_instrument = self.session.new_part(“pad_synth”)
# …more instrument setup…
# Launch musical processes in a separate thread
self.scamp_thread = threading.Thread(target=self.run_scamp)
self.scamp_thread.daemon = True
self.scamp_thread.start()
def run_scamp(self):
try:
self.session.fork(self.play_drone)
# ...more forks...
self.session.wait_forever()
except Exception as e:
print(f"Error: {e}")
# Play methods for the instruments...
Even with this setup, the clock refuses to move, so self.session.wait()
never works as expected.
2. Creating the Session in the Separate Thread:
I also tried setting up the Session directly within the separate thread, but the behavior didn’t change.
Additional Context:
run_as_server(): When I use self.session.run_as_server()
, the clock advances, but SCAMP’s documentation warns against using this for non-interactive purposes.
Main Thread for Pygame: Since Pygame needs to run in the main thread for event handling, SCAMP has to operate in a separate thread.
Also, running SCAMP examples outside Pygame works perfectly fine.
Has anyone dealt with a similar issue where the SCAMP clock doesn’t advance in a multithreaded setup with Pygame? How can I get the SCAMP session’s clock to run properly so note lengths are respected and wait()
calls work?
Any advice would be awesome!
Thanks a ton!