sound.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // Created by almamu on 17/05/19.
  3. //
  4. #include <SDL_rwops.h>
  5. #include <SDL_mixer.h>
  6. #include "sound.h"
  7. #include "irrlicht.h"
  8. namespace WallpaperEngine
  9. {
  10. sound::sound (json json_data, WallpaperEngine::object* parent) : object3d (object3d::Type::Type_Material, parent)
  11. {
  12. json::const_iterator cur = json_data.begin ();
  13. json::const_iterator end = json_data.end ();
  14. for (; cur != end; cur ++)
  15. {
  16. this->m_filenames.push_back ((*cur).get <std::string> ());
  17. }
  18. this->play ();
  19. }
  20. void sound::play ()
  21. {
  22. std::vector<std::string>::const_iterator cur = this->m_filenames.begin ();
  23. std::vector<std::string>::const_iterator end = this->m_filenames.end ();
  24. for (; cur != end; cur ++)
  25. {
  26. SDL_RWops* sdlRwops = nullptr;
  27. Mix_Music* music = nullptr;
  28. irr::io::IReadFile* readfile = WallpaperEngine::irrlicht::device->getFileSystem ()->createAndOpenFile ((*cur).c_str ());
  29. int filesize = readfile->getSize ();
  30. char* filebuffer = new char [filesize];
  31. readfile->read (filebuffer, filesize);
  32. sdlRwops = SDL_RWFromConstMem(filebuffer, filesize);
  33. music = Mix_LoadMUS_RW (sdlRwops);
  34. readfile->drop ();
  35. if (music == nullptr)
  36. {
  37. WallpaperEngine::irrlicht::device->getLogger ()->log ("Cannot load audio", Mix_GetError (), irr::ELL_ERROR);
  38. }
  39. this->m_bufferReader.push_back (sdlRwops);
  40. this->m_soundBuffer.push_back (filebuffer);
  41. this->m_sdl.push_back (music);
  42. }
  43. std::vector<Mix_Music*>::const_iterator mixcur = this->m_sdl.begin ();
  44. std::vector<Mix_Music*>::const_iterator mixend = this->m_sdl.end ();
  45. for (; mixcur != mixend; mixcur ++)
  46. {
  47. if (Mix_PlayMusic ((*mixcur), -1) == -1)
  48. {
  49. WallpaperEngine::irrlicht::device->getLogger ()->log ("Cannot play audio", Mix_GetError (), irr::ELL_ERROR);
  50. }
  51. }
  52. }
  53. }