mirror of
https://github.com/Almamu/linux-wallpaperengine.git
synced 2025-07-16 14:22:24 +08:00

+ Added support for sound objects Supported formats: MP3, FLAC, OGG Signed-off-by: Alexis Maiquez <almamu@almamu.com>
65 lines
2.0 KiB
C++
65 lines
2.0 KiB
C++
//
|
|
// Created by almamu on 17/05/19.
|
|
//
|
|
|
|
#include <SDL_rwops.h>
|
|
#include <SDL_mixer.h>
|
|
#include "sound.h"
|
|
#include "irrlicht.h"
|
|
|
|
namespace wp
|
|
{
|
|
sound::sound (json json_data, wp::object* parent) : object3d (object3d::Type::Type_Material, parent)
|
|
{
|
|
json::const_iterator cur = json_data.begin ();
|
|
json::const_iterator end = json_data.end ();
|
|
|
|
for (; cur != end; cur ++)
|
|
{
|
|
this->m_filenames.push_back ((*cur).get <std::string> ());
|
|
}
|
|
|
|
this->play ();
|
|
}
|
|
|
|
void sound::play ()
|
|
{
|
|
std::vector<std::string>::const_iterator cur = this->m_filenames.begin ();
|
|
std::vector<std::string>::const_iterator end = this->m_filenames.end ();
|
|
|
|
for (; cur != end; cur ++)
|
|
{
|
|
SDL_RWops* sdlRwops = nullptr;
|
|
Mix_Music* music = nullptr;
|
|
irr::io::IReadFile* readfile = wp::irrlicht::device->getFileSystem ()->createAndOpenFile ((*cur).c_str ());
|
|
int filesize = readfile->getSize ();
|
|
char* filebuffer = new char [filesize];
|
|
|
|
readfile->read (filebuffer, filesize);
|
|
|
|
sdlRwops = SDL_RWFromConstMem(filebuffer, filesize);
|
|
music = Mix_LoadMUS_RW (sdlRwops);
|
|
readfile->drop ();
|
|
|
|
if (music == nullptr)
|
|
{
|
|
wp::irrlicht::device->getLogger ()->log ("Cannot load audio", Mix_GetError (), irr::ELL_ERROR);
|
|
}
|
|
|
|
this->m_bufferReader.push_back (sdlRwops);
|
|
this->m_soundBuffer.push_back (filebuffer);
|
|
this->m_sdl.push_back (music);
|
|
}
|
|
|
|
std::vector<Mix_Music*>::const_iterator mixcur = this->m_sdl.begin ();
|
|
std::vector<Mix_Music*>::const_iterator mixend = this->m_sdl.end ();
|
|
|
|
for (; mixcur != mixend; mixcur ++)
|
|
{
|
|
if (Mix_PlayMusic ((*mixcur), -1) == -1)
|
|
{
|
|
wp::irrlicht::device->getLogger ()->log ("Cannot play audio", Mix_GetError (), irr::ELL_ERROR);
|
|
}
|
|
}
|
|
}
|
|
} |