song.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * This file is part of Soft Mood (https://notabug.org/alkeon/soft-mood).
  3. * Copyright (c) 2019 Alejandro "alkeon" Castilla
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "song.h"
  18. song::song(const string& file){
  19. Mix_VolumeMusic(0);
  20. _music = Mix_LoadWAV(file.c_str());
  21. if(!_music)
  22. cout << "Mixer couldn't load song: " << file << endl;
  23. Mix_VolumeChunk(_music, 0);
  24. if(!_music)
  25. cout << "Mixer couldn't change volume" << endl;
  26. _channel = Mix_PlayChannel(-1, _music, -1);
  27. if(_channel < 0)
  28. cout << "Mixer couldn't get a channel" << endl;
  29. }
  30. void song::set_volume(int volume){
  31. if(volume == 0 && Mix_Paused(_channel) == 1)
  32. Mix_Pause(_channel);
  33. else {
  34. _volume = volume;
  35. Mix_VolumeChunk(_music, _volume);
  36. }
  37. }
  38. void song::change_song(const string& file, int volume){
  39. Mix_HaltChannel(_channel);
  40. Mix_VolumeMusic(0);
  41. _music = Mix_LoadWAV(file.c_str());
  42. if(!_music)
  43. cout << "Mixer couldn't load song: " << file << endl;
  44. Mix_VolumeChunk(_music, volume);
  45. if(!_music)
  46. cout << "Mixer couldn't change volume" << endl;
  47. _channel = Mix_PlayChannel(_channel, _music, -1);
  48. if(_channel < 0)
  49. cout << "Mixer couldn't get a channel" << endl;
  50. }