Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. /*
  2.  * main.cpp
  3.  *
  4.  *  Created on: Aug 9, 2015
  5.  *      Author: cobaltergeist
  6.  */
  7.  
  8. #include <iostream>
  9. #include <SFML/System.hpp>
  10. #include <SFML/Audio.hpp>
  11. #include <signal.h>
  12.  
  13. //#define HAS_LOOP_PTS
  14.  
  15. bool run = true;
  16.  
  17. void sigHand(int signum)
  18. {
  19.     run = false;
  20. }
  21.  
  22. int main(int argc, char ** argv)
  23. {
  24.     signal(SIGINT, sigHand);
  25.  
  26.     if (argc < 2)
  27.     {
  28.         std::cerr << "Usage: " << argv[0] << ", file, [seek_start], [loop_begin, loop_end]" << std::endl;
  29.         return 1;
  30.     }
  31.  
  32.     // argv[1]: Filename
  33.     sf::Music music;
  34.     if (!music.openFromFile(argv[1]))
  35.     {
  36.         std::cerr << "File Invalid or Not Found." << std::endl;
  37.         return 1;
  38.     }
  39.  
  40.     std::cout << "File Duration: " << music.getDuration().asSeconds() << " sec." << std::endl;
  41.     music.setLoop(true);
  42.  
  43.     // argv[2]: Starting Position in Seconds
  44.     if (argc >= 3)
  45.         music.setPlayingOffset(sf::seconds(atof(argv[2])));
  46.  
  47. #ifdef HAS_LOOP_PTS
  48.     // argv[3] and argv[4]: Loop Points in Seconds
  49.     sf::Music::LoopPoints loop;
  50.     if (argc >= 5)
  51.     {
  52.         // Attempt to set the loop points
  53.         loop = sf::Music::LoopPoints(sf::seconds(atof(argv[3])), sf::seconds(atof(argv[4])));
  54.         if (loop.length != sf::Time::Zero
  55.                 && loop.begin < music.getDuration()
  56.                 && loop.length <= music.getDuration()) {
  57.             music.setLoopPoints(loop);
  58.         }
  59.         else
  60.         {
  61.             // Just get the loop points on failure, so the correct ones appear.
  62.             std::cout << "Invalid Loop Point Range. Using Default." << std::endl;
  63.             loop = music.getLoopPoints();
  64.         }
  65.     }
  66.     else
  67.     {
  68.         // Just get the loop points if none provided.
  69.         loop = music.getLoopPoints();
  70.     }
  71.  
  72.     // Markers
  73.     int loopBeginIndex = static_cast<int>(50.0f * (loop.begin.asSeconds() / music.getDuration().asSeconds()));
  74.     int loopEndIndex = (loopBeginIndex + static_cast<int>(50.0f * (loop.length.asSeconds() / music.getDuration().asSeconds())));
  75.     if (loopEndIndex > 50)
  76.         loopEndIndex %= 50;
  77. #else
  78.     int loopBeginIndex = 0;
  79.     int loopEndIndex = 50;
  80. #endif
  81.  
  82.     // Announcement
  83.     std::cout << std::endl;
  84.     std::cout << "File Range:    '[]'" << std::endl;
  85.     std::cout << "Loop Range:    '()'" << std::endl;
  86.     std::cout << "Play Position: '|'" << std::endl;
  87.     std::cout << std::endl;
  88.  
  89.     // Loop and display the sound position.
  90.     music.play();
  91.     while (run)
  92.     {
  93.         int playPos = static_cast<int>(50.0f * music.getPlayingOffset().asSeconds() / music.getDuration().asSeconds());
  94.         std::cout << "\rPlaying [";
  95.         for (int i = 0; i < 50; ++i)
  96.         {
  97.             // Fill a bar representing the sound duration with loop points and the play position.
  98.             if (i == loopBeginIndex)
  99.                 std::cout << '(';
  100.  
  101.             std::cout << ((i == playPos) ? '|' : ' ');
  102.  
  103.             if (i == loopEndIndex)
  104.                 std::cout << ')';
  105.         }
  106.         // Just in case.
  107.         if (loopBeginIndex >= 50)
  108.             std::cout << '(';
  109.         if (playPos >= 50)
  110.             std::cout << '|';
  111.         if (loopEndIndex >= 50)
  112.             std::cout << ')';
  113.  
  114.         std::cout << "] " << music.getPlayingOffset().asSeconds() << "sec        " << std::flush;
  115.         sf::sleep(sf::seconds(0.05f));
  116.     }
  117.     std::cout << std::endl;
  118.  
  119.     std::cout << "Done!" << std::endl;
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement