Fixed Single-loop mode playing multiple times on fast speeds.

This commit is contained in:
2025-10-29 08:55:36 +02:00
parent 06891e8d82
commit c9f1e8e4ae
2 changed files with 24 additions and 33 deletions

View File

@@ -74,7 +74,7 @@ BellEngine::~BellEngine() {
* *
*/ */
void BellEngine::begin() { void BellEngine::begin() {
LOG_DEBUG("Initializing BellEngine with high-precision timing"); LOG_DEBUG("Initializing BellEngine...");
// Create engine task with HIGHEST priority on dedicated Core 1 // Create engine task with HIGHEST priority on dedicated Core 1
// This ensures maximum performance and timing precision // This ensures maximum performance and timing precision
@@ -88,7 +88,7 @@ void BellEngine::begin() {
1 // 💻 Pin to Core 1 (dedicated) 1 // 💻 Pin to Core 1 (dedicated)
); );
LOG_INFO("BellEngine initialized - Ready for MAXIMUM PRECISION! 🎯"); LOG_INFO("BellEngine initialized !");
} }
/** /**
@@ -120,18 +120,18 @@ void BellEngine::start() {
return; // ⛔ Early exit if no melody data return; // ⛔ Early exit if no melody data
} }
LOG_INFO("🚀 BellEngine IGNITION - Starting precision playback"); LOG_INFO("🚀 BellEngine Ignition - Starting precision playback");
_emergencyStop.store(false); // ✅ Clear any emergency stop state _emergencyStop.store(false); // ✅ Clear any emergency stop state
_engineRunning.store(true); // ✅ Activate the engine atomically _engineRunning.store(true); // ✅ Activate the engine atomically
} }
void BellEngine::stop() { void BellEngine::stop() {
LOG_INFO("BellEngine stopping gracefully"); LOG_INFO("BellEngine - Stopping Gracefully");
_engineRunning.store(false); _engineRunning.store(false);
} }
void BellEngine::emergencyStop() { void BellEngine::emergencyStop() {
LOG_INFO("🛑 EMERGENCY STOP ACTIVATED"); LOG_INFO("BellEngine 🛑 EMERGENCY STOP ACTIVATED");
_emergencyStop.store(true); _emergencyStop.store(true);
_engineRunning.store(false); _engineRunning.store(false);
emergencyShutdown(); emergencyShutdown();
@@ -239,6 +239,10 @@ void BellEngine::playbackLoop() {
// Mark segment completion and notify Player // Mark segment completion and notify Player
_player.segmentCmpltTime = millis(); _player.segmentCmpltTime = millis();
_player.onMelodyLoopCompleted(); // 🔥 Notify Player that melody actually finished! _player.onMelodyLoopCompleted(); // 🔥 Notify Player that melody actually finished!
if ((_player.continuous_loop && _player.segment_duration == 0) || _player.total_duration == 0) {
vTaskDelay(pdMS_TO_TICKS(500)); //Give Player time to pause/stop
LOG_VERBOSE("Melody loop completed for SINGLE Mode - waiting for Player to handle pause/stop");
}
LOG_DEBUG("🎵 Melody loop completed with PRECISION"); LOG_DEBUG("🎵 Melody loop completed with PRECISION");
} }

View File

@@ -135,9 +135,13 @@ void Player::stop() {
isPlaying = false; isPlaying = false;
// Set STOPPING status - actual stop message will be sent when BellEngine finishes // Set STOPPING status - actual stop message will be sent when BellEngine finishes
if (isPaused) {
setStatus(PlayerStatus::STOPPED);
LOG_DEBUG("Plbck: STOP from PAUSED state");
} else {
setStatus(PlayerStatus::STOPPING); setStatus(PlayerStatus::STOPPING);
LOG_DEBUG("Plbck: SOFT STOP (waiting for melody to complete)"); LOG_DEBUG("Plbck: SOFT STOP (waiting for melody to complete)");
}
// NOTE: The actual "stop" message is now sent in onMelodyLoopCompleted() // NOTE: The actual "stop" message is now sent in onMelodyLoopCompleted()
// when the BellEngine actually finishes the current loop // when the BellEngine actually finishes the current loop
} }
@@ -294,13 +298,7 @@ void Player::durationTimerCallback(TimerHandle_t xTimer) {
// Check if it's time to stop playback // Check if it's time to stop playback
bool Player::timeToStop(unsigned long now) { bool Player::timeToStop(unsigned long now) {
if (isPlaying && !infinite_play && total_duration == 0) { if (isPlaying && !infinite_play) {
if (now > startTime){}
LOG_DEBUG("(Single Loop Run Seelected) Soft Stopping.");
return true;
}
}
else if (isPlaying && !infinite_play) {
uint64_t stopTime = startTime + total_duration; uint64_t stopTime = startTime + total_duration;
if (now >= stopTime) { if (now >= stopTime) {
LOG_DEBUG("(TimerFunction) Total Run Duration Reached. Soft Stopping."); LOG_DEBUG("(TimerFunction) Total Run Duration Reached. Soft Stopping.");
@@ -374,16 +372,6 @@ void Player::onMelodyLoopCompleted() {
// Check if it's time to pause playback // Check if it's time to pause playback
bool Player::timeToPause(unsigned long now) { bool Player::timeToPause(unsigned long now) {
if (isPlaying && continuous_loop) { if (isPlaying && continuous_loop) {
// Special case: segment_duration = 0 means "one loop only"
if (segment_duration == 0) {
// Only pause after first loop completes (segmentCmpltTime updated)
if (segmentCmpltTime > segmentStartTime && !isPaused) {
LOG_DEBUG("(TimerFunction) One-loop segment completed. Pausing.");
pauseTime = now;
return true;
}
} else {
// Normal duration-based pausing
uint64_t timeToPause = segmentStartTime + segment_duration; uint64_t timeToPause = segmentStartTime + segment_duration;
LOG_DEBUG("PTL: %llu // NOW: %lu", timeToPause, now); LOG_DEBUG("PTL: %llu // NOW: %lu", timeToPause, now);
if (now >= timeToPause && !isPaused) { if (now >= timeToPause && !isPaused) {
@@ -392,7 +380,6 @@ bool Player::timeToPause(unsigned long now) {
return true; return true;
} }
} }
}
return false; return false;
} }