void SceneManager::run() { sf::Clock clock; int timerThen = clock.restart().asMilliseconds(); while (m_window.isOpen()) { int timerNow = clock.getElapsedTime().asMilliseconds(); while (timerThen < timerNow) { processFade(); if (m_shakeCounter > 0) { m_shakeCounter--; } pollEvents(); if (m_scenes.size() > 0) { m_scenes.back()->update(); } cleanUp(); timerThen += 1000 / config::FPS; } draw(); sf::sleep(sf::milliseconds(timerThen - timerNow)); } }
void LoopMachine::processFadeOut(int groupIx, int loopIx, float frameStartTicks, float frameEndTicks, float fadeStartTicks, float fadeEndTicks, const AudioSourceChannelInfo& bufferToFill) { // MPD: all done. if (frameStartTicks < fadeStartTicks && frameEndTicks > fadeEndTicks) { // this frame is large and completely contains the fadeout period. // in other words, start segment is normal, and then it fades out within this interval // |---------- | // | \ | // | \___________| // pre-f f post-f int numSamples = fixedBpmTransport.ticksToSamples(fadeStartTicks-frameStartTicks); // pre-f processBlock(groupIx, loopIx, 0, numSamples, bufferToFill); // f float startGain = 1; float endGain = 0; int len = fixedBpmTransport.ticksToSamples(fadeEndTicks-fadeStartTicks); processFade(groupIx, loopIx, startGain, endGain, numSamples, len, bufferToFill); // post-f // NOP: we'd be outputting only zeros. } else if (frameEndTicks < fadeStartTicks) { // this frame ends before the start of the fade period: there is no fade in this period. // this means we need to process the block as if it was full volume. processBlock(groupIx, loopIx, 0, bufferToFill.numSamples, bufferToFill); } else if (frameStartTicks < fadeStartTicks && frameEndTicks >= fadeStartTicks) { // the fade period begins somewhere in this frame // this frame is very small compared to the fade time and is completely contained // within it. // |-----|----- | // | | \ | // | | \ | // | | \| // | | \ // | | |\ // | | | \___________ // f start f end int numSamples = fixedBpmTransport.ticksToSamples(fadeStartTicks-frameStartTicks); processBlock(groupIx, loopIx, 0, numSamples, bufferToFill); float startGain = 1; float endGain = (fadeEndTicks-frameEndTicks)/(fadeEndTicks-fadeStartTicks); processFade(groupIx, loopIx, startGain, endGain, numSamples, bufferToFill.numSamples-numSamples, bufferToFill); } else if (frameStartTicks > fadeStartTicks && frameEndTicks > fadeEndTicks) { // the fade period (i.e. the next tick!) end occurs within this frame. // |---------- | | // | \| | // | |\ | // | | \_______|____ int numSamples = fixedBpmTransport.ticksToSamples(fadeEndTicks-frameStartTicks); float startGain = (fadeEndTicks-frameStartTicks)/(fadeEndTicks-fadeStartTicks); float endGain = 0; processFade(groupIx, loopIx, startGain, endGain, 0, numSamples, bufferToFill); // and after that there's silence, so nothing more to do. } else if (frameStartTicks > fadeStartTicks && frameEndTicks < fadeEndTicks) { // this frame is very small compared to the fade time and is completely contained // within it. // |---------- | | // | \| | // | \ | // | | \| // | | \ // | | |\ // | | | \_______|____ float startGain = (fadeEndTicks-frameStartTicks)/(fadeEndTicks-fadeStartTicks); float endGain = (fadeEndTicks-frameEndTicks)/(fadeEndTicks-fadeStartTicks); processFade(groupIx, loopIx, startGain, endGain, 0, bufferToFill.numSamples, bufferToFill); } else { throw AudioEngineException("This really should never happen."); } }
void LoopMachine::processFadeIn(int groupIx, int loopIx, float frameStartTicks, float frameEndTicks, float fadeStartTicks, float fadeEndTicks, const AudioSourceChannelInfo& bufferToFill) { // std::cout << "MPD: CPP: LoopMachine::processFadeIn: starting group " << groupIx << ", sample " << loopIx << std::endl; if (frameStartTicks < fadeStartTicks && frameEndTicks > fadeEndTicks) { // std::cout << "MPD: CPP: LoopMachine::processFadeIn: case 1" << std::endl; // this frame is large and completely contains the fadeout period. // in other words, start segment is normal, and then it fades out within this interval int offset = fixedBpmTransport.ticksToSamples(fadeStartTicks-frameStartTicks); // pre-f // NOP: we'd be outputting only zeros. // f float startGain = 0; float endGain = 1; int len = fixedBpmTransport.ticksToSamples(fadeEndTicks-fadeStartTicks); processFade(groupIx, loopIx, startGain, endGain, offset, len, bufferToFill); // post-f - audio is now at full volume! processBlock(groupIx, loopIx, offset+len, bufferToFill.numSamples-offset-len, bufferToFill); // MPD: done. } else if (frameEndTicks < fadeStartTicks) { // std::cout << "MPD: CPP: LoopMachine::processFadeIn: case 2" << std::endl; // this frame ends before the start of the fade period: there is no fade in this period. // this means we are in nop territory. // MPD: done. } else if (frameStartTicks < fadeStartTicks && frameEndTicks >= fadeStartTicks) { // std::cout << "MPD: CPP: LoopMachine::processFadeIn: case 3" << std::endl; // the fade period begins somewhere in this frame // |-----|----- | // | | \ | // | | \ | // | | \| // | | \ // | | |\ // | | | \___________ // f start f end int offset = fixedBpmTransport.ticksToSamples(fadeStartTicks-frameStartTicks); float startGain = 0; float endGain = (frameEndTicks-fadeStartTicks)/(fadeEndTicks-fadeStartTicks); processFade(groupIx, loopIx, startGain, endGain, offset, bufferToFill.numSamples-offset, bufferToFill); // MPD: done. } else if (frameStartTicks > fadeStartTicks && frameEndTicks > fadeEndTicks) { // std::cout << "MPD: CPP: LoopMachine::processFadeIn: case 4" << std::endl; // the fade period (i.e. the next tick!) end occurs within this frame. // |---------- | | // | \| | // | |\ | // | | \_______|____ int numSamples = fixedBpmTransport.ticksToSamples(fadeEndTicks-frameStartTicks); float startGain = (frameStartTicks-fadeStartTicks)/(fadeEndTicks-fadeStartTicks); float endGain = 1; processFade(groupIx, loopIx, startGain, endGain, 0, numSamples, bufferToFill); // and after that we have to play the sample volle pulle processBlock(groupIx, loopIx, numSamples, bufferToFill.numSamples-numSamples, bufferToFill); // MPD: done. } else if (frameStartTicks > fadeStartTicks && frameEndTicks < fadeEndTicks) { // std::cout << "MPD: CPP: LoopMachine::processFadeIn: case 5" << std::endl; // this frame is very small compared to the fade time and is completely contained // within it. // |---------- | | // | \| | // | \ | // | | \| // | | \ // | | |\ // | | | \_______|____ float startGain = (frameStartTicks-fadeStartTicks)/(fadeEndTicks-fadeStartTicks); float endGain = (frameEndTicks-fadeStartTicks)/(fadeEndTicks-fadeStartTicks); processFade(groupIx, loopIx, startGain, endGain, 0, bufferToFill.numSamples, bufferToFill); } else { throw AudioEngineException("This really should never happen."); } }