Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fmod.hpp>
- #include <fmod_errors.h>
- #include <iostream>
- #include <thread>
- #include <atomic>
- #include <string>
- #include <algorithm>
- #include <vector>
- #include <sstream>
- std::atomic< float > g_flVolume = 1.0f;
- std::atomic_bool g_bPause = false;
- class FMODManager
- {
- public:
- FMODManager()
- {
- m_pSystem = nullptr;
- }
- virtual ~FMODManager()
- {
- if ( m_pSystem )
- {
- m_pSystem->release();
- }
- }
- bool Init()
- {
- if ( CheckError( FMOD::System_Create( &m_pSystem ) ) != FMOD_OK )
- return false;
- if ( CheckError( m_pSystem->init( 32, FMOD_INIT_NORMAL, nullptr ) ) != FMOD_OK )
- return false;
- return true;
- }
- FMOD::System *GetSystem()
- {
- return m_pSystem;
- }
- void Update()
- {
- if ( m_pSystem )
- m_pSystem->update();
- }
- static FMOD_RESULT CheckError( FMOD_RESULT result )
- {
- if ( result != FMOD_OK )
- {
- std::cout << "[FMOD]: " << FMOD_ErrorString( result ) << std::endl;
- }
- return result;
- }
- private:
- FMOD::System *m_pSystem;
- };
- static FMODManager s_FMODManager;
- void PollUser( std::atomic_bool &bFinished )
- {
- while ( !bFinished )
- {
- std::string command;
- std::getline( std::cin, command );
- std::vector< std::string > tokens;
- std::istringstream ss( command );
- std::string token;
- while ( ss >> token )
- tokens.push_back( token );
- for ( std::string &str : tokens )
- std::transform( str.begin(), str.end(), str.begin(), ::tolower );
- if ( tokens[0] == "quit" || tokens[0] == "exit" )
- bFinished = true;
- else if ( ( tokens[0] == "volume" || tokens[0] == "vol" ) && tokens.size() > 1 )
- {
- g_flVolume = std::min( std::max( ( float )atof( tokens[1].c_str() ), 0.0f ) / 100.0f, 1.0f );
- }
- // 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
- else if ( tokens[0] == "printvol" || tokens[0] == "printvolume" || tokens[0] == "vol" || tokens[0] == "volume" )
- std::cout << "Volume: " << g_flVolume << std::endl;
- else if ( tokens[0] == "pause" )
- g_bPause = true;
- else if ( tokens[0] == "unpause" || tokens[0] == "continue" )
- g_bPause = false;
- else
- std::cout << "Invalid command!\n";
- }
- }
- int main ( int argc, char *argv[] )
- {
- if ( argc <= 1 )
- {
- std::cout << "Usage:\n\tFMODPlayer.exe [FILE]\n\n";
- system( "PAUSE" );
- return 0;
- }
- if ( !s_FMODManager.Init() )
- {
- system( "PAUSE" );
- return 1;
- }
- FMOD::Channel *pChannel = nullptr;
- FMOD::Sound *pSound = nullptr;
- if ( FMODManager::CheckError( s_FMODManager.GetSystem()->createStream( argv[1], FMOD_LOOP_NORMAL , nullptr, &pSound ) ) != FMOD_OK )
- {
- system( "PAUSE" );
- return 1;
- }
- if ( FMODManager::CheckError( s_FMODManager.GetSystem()->playSound( pSound, nullptr, false, &pChannel ) ) != FMOD_OK )
- {
- system( "PAUSE" );
- return 1;
- }
- std::atomic_bool bFinished = false;
- std::thread myThread( PollUser, std::ref( bFinished ) );
- myThread.detach();
- using namespace std::chrono_literals;
- while ( !bFinished )
- {
- s_FMODManager.Update();
- pChannel->setVolume( g_flVolume );
- pChannel->setPaused( g_bPause );
- std::this_thread::sleep_for( 1ns );
- }
- if ( pChannel )
- pChannel->stop();
- if ( pSound )
- pSound->release();
- system( "PAUSE" );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement