Advertisement
cherrydeluxe

SFML - Audio Demo

Feb 22nd, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.59 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <SFML/Audio.hpp>
  3. #include <iostream>
  4.  
  5. #define FPS 60
  6.  
  7. using namespace std;
  8.  
  9. // Usage: this.exe musicfilename soundfilename
  10. // Sample files to use: my.mixtape.moe/kpcaxi.wav
  11. //                      my.mixtape.moe/obopyb.wav
  12.  
  13. void help()
  14. {
  15.     cout << "Music Controls:            \n";
  16.     cout << " > M - play/pause          \n";
  17.     cout << " > R - restart             \n";
  18.     cout << " > W - increase volume     \n";
  19.     cout << " > S - decrease volume     \n";
  20.     cout << " > D - increase pitch      \n";
  21.     cout << " > A - decrease pitch      \n";
  22.     cout << "Sound Controls:            \n";
  23.     cout << " > Space - play            \n";
  24.     cout << " > Up    - increase volume \n";
  25.     cout << " > Down  - decrease volume \n";
  26.     cout << " > Right - increase pitch  \n";
  27.     cout << " > Left  - decrease pitch  \n";
  28.     cout << "Press H for help." << endl;
  29. }
  30.  
  31. int main(int argc,  char **argv)
  32. {
  33.     string musicFilename(argv[1]);
  34.     string soundFilename(argv[2]);
  35.  
  36.     sf::Music music;
  37.     if (!music.openFromFile(musicFilename))
  38.     {
  39.         cout << "Error loading music." << endl;
  40.         return 0;
  41.     }
  42.     music.setLoop(true);
  43.  
  44.     float musicPitch = 1.f;
  45.     float musicVol = 100.f;
  46.  
  47.     sf::SoundBuffer buffer;
  48.     if (!buffer.loadFromFile(soundFilename))
  49.     {
  50.         cout << "Error loading sound." << endl;
  51.         return 0;
  52.     }
  53.  
  54.     sf::Sound sounds[32];
  55.     for (int i = 0; i < 32; i++) { sounds[i].setBuffer(buffer); }
  56.  
  57.     int currentsound = 0;
  58.     float soundPitch = 1.f;
  59.     float soundVol = 100.f;
  60.  
  61.     //initialize window.
  62.     sf::RenderWindow window( sf::VideoMode( 800, 600 ), "h" );
  63.     window.setFramerateLimit( FPS );
  64.     window.setKeyRepeatEnabled(false);
  65.     help();
  66.  
  67.     while( window.isOpen() )
  68.     {
  69.         sf::Event event;
  70.         while( window.pollEvent( event ) )
  71.         {
  72.             switch( event.type ) {
  73.                 case sf::Event::Closed:
  74.                     window.close();
  75.                     break;
  76.                 case sf::Event::KeyPressed:
  77.                     if( event.key.code == sf::Keyboard::Escape ) { window.close(); }
  78.                    
  79.                     // Main Controls
  80.  
  81.                     // Help
  82.                     if( event.key.code == sf::Keyboard::H )
  83.                     {
  84.                         help();
  85.                     }
  86.  
  87.                     // Music: Play/Pause
  88.                     if( event.key.code == sf::Keyboard::M )
  89.                     {
  90.                         if( music.getStatus() == sf::SoundSource::Status::Playing )
  91.                         {
  92.                             music.pause();
  93.                             cout << "Music paused." << endl;
  94.                         }
  95.                         else
  96.                         {
  97.                             music.play();
  98.                             cout << "Music playing." << endl;
  99.                         }
  100.                     }
  101.  
  102.                     // Music: Reset
  103.                     if( event.key.code == sf::Keyboard::R )
  104.                     {
  105.                         music.stop();
  106.                         music.play();
  107.                         cout << "Music restarted." << endl;
  108.                     }
  109.  
  110.                     // Music: Decrease Pitch
  111.                     if( event.key.code == sf::Keyboard::A )
  112.                     {
  113.                         if( musicPitch > 0.1f )
  114.                         {
  115.                             musicPitch -= 0.1f;
  116.                             music.setPitch(musicPitch);
  117.                             cout << "Music -Pitch.  Now at " << musicPitch << endl;
  118.                         }
  119.                         else
  120.                         {
  121.                             musicPitch = 0.1f;
  122.                             cout << "Music at minimum pitch." << endl;
  123.                         }
  124.                     }
  125.  
  126.                     // Music: Increase Pitch
  127.                     if( event.key.code == sf::Keyboard::D )
  128.                     {
  129.                         musicPitch += 0.1f;
  130.                         music.setPitch(musicPitch);
  131.                         cout << "Music +Pitch.  Now at " << musicPitch << endl;
  132.                     }
  133.  
  134.                     // Music: Decrease Volume
  135.                     if( event.key.code == sf::Keyboard::S )
  136.                     {
  137.                         if( musicVol > 0.f )
  138.                         {
  139.                             musicVol -= 2.5f;
  140.                             music.setVolume(musicVol);
  141.                             cout << "Music -Volume. Now at " << musicVol << endl;
  142.                         }
  143.                         else
  144.                         {
  145.                             musicVol = 0.f;
  146.                             music.setVolume(musicVol);
  147.                             cout << "Music at minimum volume." << endl;
  148.                         }
  149.                     }
  150.  
  151.                     // Music: Increase Volume
  152.                     if( event.key.code == sf::Keyboard::W )
  153.                     {
  154.                         musicVol += 2.5f;
  155.                         if( musicVol > 100.f )
  156.                         {
  157.                             musicVol = 100.f;
  158.                             music.setVolume(musicVol);
  159.                             cout << "Music at maximum volume." << endl;
  160.                         }
  161.                         else
  162.                         {
  163.                             music.setVolume(musicVol);
  164.                             cout << "Music +Volume. Now at " << musicVol << endl;
  165.                         }
  166.                     }
  167.  
  168.                     // Sound: Play
  169.                     if( event.key.code == sf::Keyboard::Space )
  170.                     {
  171.                         sounds[currentsound].setPitch(soundPitch);
  172.                         sounds[currentsound].setVolume(soundVol);
  173.  
  174.                         cout << "Playing sound on track " << currentsound << endl;
  175.                         sounds[currentsound].play();
  176.                         if( currentsound >= 31 ) { currentsound = 0; }
  177.                         else { currentsound++; }
  178.                     }
  179.  
  180.                     // Sound: Decrease Pitch
  181.                     if( event.key.code == sf::Keyboard::Left )
  182.                     {
  183.                         if( soundPitch > 0.1f )
  184.                         {
  185.                             soundPitch -= 0.1f;
  186.                             cout << "Sound -Pitch.  Now at " << soundPitch << endl;
  187.                         }
  188.                         else
  189.                         {
  190.                             soundPitch = 0.1f;
  191.                             cout << "Sound at minimum pitch." << endl;
  192.                         }
  193.                     }
  194.  
  195.                     // Sound: Increase Pitch
  196.                     if( event.key.code == sf::Keyboard::Right )
  197.                     {
  198.                         soundPitch += 0.1f;
  199.                         cout << "Sound +Pitch.  Now at " << soundPitch << endl;
  200.                     }
  201.  
  202.                     // Sound: Decrease Volume
  203.                     if( event.key.code == sf::Keyboard::Down )
  204.                     {
  205.                         if( soundVol > 0.f )
  206.                         {
  207.                             soundVol -= 2.5f;
  208.                             cout << "Sound -Volume. Now at " << soundVol << endl;
  209.                         }
  210.                         else
  211.                         {
  212.                             soundVol = 0.f;
  213.                             cout << "Sound at minimum volume." << endl;
  214.                         }
  215.                     }
  216.  
  217.                     // Sound: Increase Volume
  218.                     if( event.key.code == sf::Keyboard::Up )
  219.                     {
  220.                         soundVol += 2.5f;
  221.                         if( soundVol > 100.f )
  222.                         {
  223.                             soundVol = 100.f;
  224.                             cout << "Sound at maximum volume." << endl;
  225.                         }
  226.                         else { cout << "Sound +Volume. Now at " << soundVol << endl; }
  227.                     }
  228.  
  229.                     break;
  230.             }
  231.         }
  232.         window.clear();
  233.     }
  234.  
  235.     return 0;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement