Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. #include <fmod.hpp>
  2. #include <fmod_errors.h>
  3. #include <iostream>
  4. #include <thread>
  5. #include <atomic>
  6. #include <string>
  7. #include <algorithm>
  8. #include <vector>
  9. #include <sstream>
  10.  
  11. std::atomic< float > g_flVolume = 1.0f;
  12. std::atomic_bool g_bPause = false;
  13.  
  14. class FMODManager
  15. {
  16. public:
  17.     FMODManager()
  18.     {
  19.         m_pSystem = nullptr;
  20.     }
  21.     virtual ~FMODManager()
  22.     {
  23.         if ( m_pSystem )
  24.         {
  25.             m_pSystem->release();
  26.         }
  27.     }
  28.  
  29.     bool Init()
  30.     {
  31.         if ( CheckError( FMOD::System_Create( &m_pSystem ) ) != FMOD_OK )
  32.             return false;
  33.  
  34.         if ( CheckError( m_pSystem->init( 32, FMOD_INIT_NORMAL, nullptr ) ) != FMOD_OK )
  35.             return false;
  36.        
  37.         return true;
  38.     }
  39.  
  40.     FMOD::System *GetSystem()
  41.     {
  42.         return m_pSystem;
  43.     }
  44.  
  45.     void Update()
  46.     {
  47.         if ( m_pSystem )
  48.             m_pSystem->update();
  49.     }
  50.  
  51.     static FMOD_RESULT CheckError( FMOD_RESULT result )
  52.     {
  53.         if ( result != FMOD_OK )
  54.         {
  55.             std::cout << "[FMOD]: " << FMOD_ErrorString( result ) << std::endl;
  56.         }
  57.  
  58.         return result;
  59.     }
  60.  
  61. private:
  62.     FMOD::System *m_pSystem;
  63. };
  64.  
  65. static FMODManager s_FMODManager;
  66.  
  67. void PollUser( std::atomic_bool &bFinished )
  68. {
  69.     while ( !bFinished )
  70.     {
  71.         std::string command;
  72.         std::getline( std::cin, command );
  73.  
  74.         std::vector< std::string > tokens;
  75.         std::istringstream ss( command );
  76.         std::string token;
  77.  
  78.         while ( ss >> token )
  79.             tokens.push_back( token );
  80.  
  81.         for ( std::string &str : tokens )
  82.             std::transform( str.begin(), str.end(), str.begin(), ::tolower );
  83.  
  84.         if ( tokens[0] == "quit" || tokens[0] == "exit" )
  85.             bFinished = true;
  86.         else if ( ( tokens[0] == "volume" || tokens[0] == "vol" ) && tokens.size() > 1 )
  87.         {
  88.             g_flVolume = std::min( std::max( ( float )atof( tokens[1].c_str() ), 0.0f ) / 100.0f, 1.0f );
  89.         }
  90.         // It's VERY important that we check for volume and vol above this if statement, as they check for tokens.size(), hence we don't need to check for it here
  91.         else if ( tokens[0] == "printvol" || tokens[0] == "printvolume" || tokens[0] == "vol" || tokens[0] == "volume" )
  92.             std::cout << "Volume: " << g_flVolume << std::endl;
  93.         else if ( tokens[0] == "pause" )
  94.             g_bPause = true;
  95.         else if ( tokens[0] == "unpause" || tokens[0] == "continue" )
  96.             g_bPause = false;
  97.         else
  98.             std::cout << "Invalid command!\n";
  99.        
  100.     }
  101. }
  102.  
  103. int main ( int argc, char *argv[] )
  104. {
  105.     if ( argc <= 1 )
  106.     {
  107.         std::cout << "Usage:\n\tFMODPlayer.exe [FILE]\n\n";
  108.         system( "PAUSE" );
  109.         return 0;
  110.     }
  111.  
  112.     if ( !s_FMODManager.Init() )
  113.     {
  114.         system( "PAUSE" );
  115.         return 1;
  116.     }
  117.  
  118.     FMOD::Channel *pChannel = nullptr;
  119.     FMOD::Sound *pSound = nullptr;
  120.  
  121.     if ( FMODManager::CheckError( s_FMODManager.GetSystem()->createStream( argv[1], FMOD_LOOP_NORMAL , nullptr, &pSound ) ) != FMOD_OK )
  122.     {
  123.         system( "PAUSE" );
  124.         return 1;
  125.     }
  126.  
  127.     if ( FMODManager::CheckError( s_FMODManager.GetSystem()->playSound( pSound, nullptr, false, &pChannel ) ) != FMOD_OK )
  128.     {
  129.         system( "PAUSE" );
  130.         return 1;
  131.     }
  132.  
  133.     std::atomic_bool bFinished = false;
  134.  
  135.     std::thread myThread( PollUser, std::ref( bFinished ) );
  136.     myThread.detach();
  137.  
  138.     using namespace std::chrono_literals;
  139.  
  140.     while ( !bFinished )
  141.     {
  142.         s_FMODManager.Update();
  143.  
  144.         pChannel->setVolume( g_flVolume );
  145.         pChannel->setPaused( g_bPause );
  146.  
  147.         std::this_thread::sleep_for( 1ns );
  148.     }
  149.  
  150.     if ( pChannel )
  151.         pChannel->stop();
  152.  
  153.     if ( pSound )
  154.         pSound->release();
  155.  
  156.     system( "PAUSE" );
  157.  
  158.     return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement