Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * main.cpp
- *
- * Created on: Aug 9, 2015
- * Author: cobaltergeist
- */
- #include <iostream>
- #include <SFML/System.hpp>
- #include <SFML/Audio.hpp>
- #include <signal.h>
- //#define HAS_LOOP_PTS
- bool run = true;
- void sigHand(int signum)
- {
- run = false;
- }
- int main(int argc, char ** argv)
- {
- signal(SIGINT, sigHand);
- if (argc < 2)
- {
- std::cerr << "Usage: " << argv[0] << ", file, [seek_start], [loop_begin, loop_end]" << std::endl;
- return 1;
- }
- // argv[1]: Filename
- sf::Music music;
- if (!music.openFromFile(argv[1]))
- {
- std::cerr << "File Invalid or Not Found." << std::endl;
- return 1;
- }
- std::cout << "File Duration: " << music.getDuration().asSeconds() << " sec." << std::endl;
- music.setLoop(true);
- // argv[2]: Starting Position in Seconds
- if (argc >= 3)
- music.setPlayingOffset(sf::seconds(atof(argv[2])));
- #ifdef HAS_LOOP_PTS
- // argv[3] and argv[4]: Loop Points in Seconds
- sf::Music::LoopPoints loop;
- if (argc >= 5)
- {
- // Attempt to set the loop points
- loop = sf::Music::LoopPoints(sf::seconds(atof(argv[3])), sf::seconds(atof(argv[4])));
- if (loop.length != sf::Time::Zero
- && loop.begin < music.getDuration()
- && loop.length <= music.getDuration()) {
- music.setLoopPoints(loop);
- }
- else
- {
- // Just get the loop points on failure, so the correct ones appear.
- std::cout << "Invalid Loop Point Range. Using Default." << std::endl;
- loop = music.getLoopPoints();
- }
- }
- else
- {
- // Just get the loop points if none provided.
- loop = music.getLoopPoints();
- }
- // Markers
- int loopBeginIndex = static_cast<int>(50.0f * (loop.begin.asSeconds() / music.getDuration().asSeconds()));
- int loopEndIndex = (loopBeginIndex + static_cast<int>(50.0f * (loop.length.asSeconds() / music.getDuration().asSeconds())));
- if (loopEndIndex > 50)
- loopEndIndex %= 50;
- #else
- int loopBeginIndex = 0;
- int loopEndIndex = 50;
- #endif
- // Announcement
- std::cout << std::endl;
- std::cout << "File Range: '[]'" << std::endl;
- std::cout << "Loop Range: '()'" << std::endl;
- std::cout << "Play Position: '|'" << std::endl;
- std::cout << std::endl;
- // Loop and display the sound position.
- music.play();
- while (run)
- {
- int playPos = static_cast<int>(50.0f * music.getPlayingOffset().asSeconds() / music.getDuration().asSeconds());
- std::cout << "\rPlaying [";
- for (int i = 0; i < 50; ++i)
- {
- // Fill a bar representing the sound duration with loop points and the play position.
- if (i == loopBeginIndex)
- std::cout << '(';
- std::cout << ((i == playPos) ? '|' : ' ');
- if (i == loopEndIndex)
- std::cout << ')';
- }
- // Just in case.
- if (loopBeginIndex >= 50)
- std::cout << '(';
- if (playPos >= 50)
- std::cout << '|';
- if (loopEndIndex >= 50)
- std::cout << ')';
- std::cout << "] " << music.getPlayingOffset().asSeconds() << "sec " << std::flush;
- sf::sleep(sf::seconds(0.05f));
- }
- std::cout << std::endl;
- std::cout << "Done!" << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement