173 lines
4.2 KiB
C++
173 lines
4.2 KiB
C++
#include "jaudio.h"
|
|
#include <SDL2/SDL.h>
|
|
#include <SDL2/SDL_mixer.h>
|
|
#include <stdio.h>
|
|
#include "jfile.h"
|
|
#include <vector>
|
|
#include "config.h"
|
|
|
|
namespace audio
|
|
{
|
|
struct sound_cache_t
|
|
{
|
|
std::string name;
|
|
Mix_Chunk *chunk;
|
|
};
|
|
|
|
char *music_buffer = nullptr;
|
|
Mix_Music *music = nullptr;
|
|
std::vector<sound_cache_t> sounds;
|
|
std::string musica_actual = "";
|
|
|
|
// Inicialitza el sistema de só
|
|
void init()
|
|
{
|
|
//int result =
|
|
Mix_Init(MIX_INIT_OGG);// | MIX_INIT_WAVPACK);
|
|
// Al final he ficat la configuració automàtica i au. Si en el futur necesitem canviar-ho pos se canvia
|
|
//result =
|
|
Mix_OpenAudio(48000, AUDIO_S16, 2, 512);
|
|
}
|
|
|
|
// Tanca el sistema de só (no shit, sherlock)
|
|
void quit()
|
|
{
|
|
for (auto sound : sounds) Mix_FreeChunk(sound.chunk);
|
|
sounds.clear();
|
|
Mix_CloseAudio();
|
|
}
|
|
|
|
// Comença a reproduïr la música en questió
|
|
void playMusic(const std::string filename, const int loop)
|
|
{
|
|
if (!config::isMusicEnabled()) return;
|
|
// Si hi havia musica carregada, alliberem memòria
|
|
if (music_buffer) free(music_buffer);
|
|
if (music) Mix_FreeMusic(music);
|
|
|
|
int size;
|
|
music_buffer = file::getFileBuffer(filename, size);
|
|
auto rwops = SDL_RWFromMem(music_buffer, size);
|
|
auto mus = Mix_LoadMUS_RW(rwops, 1);
|
|
//free(music_buffer); // [RZC 03/10/2024] Si allibere el buffer, no funciona la música. Porca miseria! Per aixó l'allibere abans de carregar si ja estava usat.
|
|
Mix_PlayMusic((Mix_Music *)mus, loop);
|
|
musica_actual = filename;
|
|
}
|
|
|
|
// Pausa la música que està sonant ara
|
|
void pauseMusic()
|
|
{
|
|
Mix_PauseMusic();
|
|
}
|
|
|
|
// Continua la música pausada
|
|
void resumeMusic()
|
|
{
|
|
if (!config::isMusicEnabled()) return;
|
|
Mix_ResumeMusic();
|
|
}
|
|
|
|
// Para la música que estava sonant
|
|
void stopMusic()
|
|
{
|
|
Mix_HaltMusic();
|
|
musica_actual = "";
|
|
}
|
|
|
|
// Obté el estat actual de la música
|
|
const music_state getMusicState()
|
|
{
|
|
if (Mix_PausedMusic())
|
|
{
|
|
return MUSIC_PAUSED;
|
|
}
|
|
else if (Mix_PlayingMusic())
|
|
{
|
|
return MUSIC_PLAYING;
|
|
}
|
|
else
|
|
{
|
|
return MUSIC_STOPPED;
|
|
}
|
|
}
|
|
|
|
// Obté el nom de la música actual
|
|
const std::string getCurrentMusic()
|
|
{
|
|
return musica_actual;
|
|
}
|
|
|
|
|
|
|
|
// Carrega un só des d'un arxiu WAV
|
|
void loadSound(const std::string filename)
|
|
{
|
|
int size;
|
|
char *buffer = file::getFileBuffer(filename, size);
|
|
auto chunk = Mix_LoadWAV_RW(SDL_RWFromMem(buffer, size), 1);
|
|
if (!chunk) printf("ERROR: %s\n", SDL_GetError());
|
|
sound_cache_t sound = { filename, chunk };
|
|
sounds.push_back(sound);
|
|
}
|
|
|
|
// Comença a reproduïr el só especificat
|
|
const int playSound(const std::string name, const int priority, const int loop)
|
|
{
|
|
if (priority < config::getSoundMode()) return -1;
|
|
for (auto sound : sounds)
|
|
{
|
|
if (sound.name == name) return Mix_PlayChannel(-1, sound.chunk, loop);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// Pausa un canal en el que s'estava reproduïnt un só
|
|
void pauseChannel(const int channel)
|
|
{
|
|
if (channel == -1) return;
|
|
Mix_Pause(channel);
|
|
}
|
|
|
|
// Continua un canal pausat
|
|
void resumeChannel(const int channel)
|
|
{
|
|
if (channel == -1) return;
|
|
Mix_Resume(channel);
|
|
}
|
|
|
|
// Para un canal que estava reproduïnt un só
|
|
void stopChannel(const int channel)
|
|
{
|
|
if (channel == -1) return;
|
|
Mix_HaltChannel(channel);
|
|
}
|
|
|
|
// Para tots els canals
|
|
void stopAllChannel()
|
|
{
|
|
Mix_HaltChannel(-1);
|
|
}
|
|
|
|
// Obté l'estat d'un canal
|
|
const channel_state getChannelState(const int channel)
|
|
{
|
|
if (Mix_Paused(channel))
|
|
{
|
|
return CHANNEL_PAUSED;
|
|
}
|
|
else if (Mix_Playing(channel))
|
|
{
|
|
return CHANNEL_PLAYING;
|
|
}
|
|
else
|
|
{
|
|
return CHANNEL_FREE;
|
|
}
|
|
}
|
|
|
|
// Estableix el volum general
|
|
const int setVolume(int volume)
|
|
{
|
|
return Mix_Volume(-1, volume);
|
|
}
|
|
} |