167 lines
3.2 KiB
C++
167 lines
3.2 KiB
C++
#ifdef JA_USESDLMIXER
|
|
#include "jail_audio.h"
|
|
#include <SDL2/SDL.h>
|
|
#include <SDL2/SDL_mixer.h>
|
|
#include <stdio.h>
|
|
|
|
struct JA_Sound_t {}; // Dummy structs
|
|
struct JA_Music_t {};
|
|
|
|
int JA_freq {48000};
|
|
SDL_AudioFormat JA_format {AUDIO_S16};
|
|
Uint8 JA_channels {2};
|
|
int JA_musicVolume = 128;
|
|
int JA_soundVolume = 64;
|
|
bool JA_musicEnabled = true;
|
|
bool JA_soundEnabled = true;
|
|
|
|
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) {
|
|
JA_freq = freq;
|
|
JA_format = format;
|
|
JA_channels = channels;
|
|
Mix_OpenAudio(JA_freq, JA_format, JA_channels, 1024);
|
|
}
|
|
|
|
void JA_Quit() {
|
|
Mix_CloseAudio();
|
|
}
|
|
|
|
JA_Music_t *JA_LoadMusic(const char* filename) {
|
|
return (JA_Music_t*)Mix_LoadMUS(filename);
|
|
}
|
|
|
|
void JA_PlayMusic(JA_Music_t *music, const int loop)
|
|
{
|
|
if (!JA_musicEnabled) return;
|
|
Mix_PlayMusic((Mix_Music*)music, loop);
|
|
Mix_VolumeMusic(JA_musicVolume);
|
|
}
|
|
|
|
void JA_PauseMusic()
|
|
{
|
|
if (!JA_musicEnabled) return;
|
|
Mix_PauseMusic();
|
|
}
|
|
|
|
void JA_ResumeMusic()
|
|
{
|
|
if (!JA_musicEnabled) return;
|
|
Mix_ResumeMusic();
|
|
}
|
|
|
|
void JA_StopMusic()
|
|
{
|
|
if (!JA_musicEnabled) return;
|
|
Mix_HaltMusic();
|
|
}
|
|
|
|
JA_Music_state JA_GetMusicState()
|
|
{
|
|
if (!JA_musicEnabled) return JA_MUSIC_DISABLED;
|
|
|
|
if (Mix_PausedMusic()) {
|
|
return JA_MUSIC_PAUSED;
|
|
} else if (Mix_PlayingMusic()) {
|
|
return JA_MUSIC_PLAYING;
|
|
} else {
|
|
return JA_MUSIC_STOPPED;
|
|
}
|
|
}
|
|
|
|
void JA_DeleteMusic(JA_Music_t *music)
|
|
{
|
|
Mix_FreeMusic((Mix_Music*)music);
|
|
}
|
|
|
|
int JA_SetMusicVolume(int volume)
|
|
{
|
|
JA_musicVolume = volume;
|
|
Mix_VolumeMusic(JA_musicVolume);
|
|
return JA_musicVolume;
|
|
}
|
|
|
|
void JA_EnableMusic(const bool value)
|
|
{
|
|
if (Mix_PlayingMusic()) Mix_HaltMusic();
|
|
JA_musicEnabled = value;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
JA_Sound_t *JA_LoadSound(const char* filename) {
|
|
JA_Sound_t *sound = (JA_Sound_t*)Mix_LoadWAV(filename);
|
|
return sound;
|
|
}
|
|
|
|
int JA_PlaySound(JA_Sound_t *sound, const int loop) {
|
|
if (!JA_soundEnabled) return -1;
|
|
const int channel = Mix_PlayChannel(-1, (Mix_Chunk*)sound, loop);
|
|
Mix_Volume(-1, JA_soundVolume);
|
|
return channel;
|
|
}
|
|
|
|
void JA_DeleteSound(JA_Sound_t *sound)
|
|
{
|
|
Mix_FreeChunk((Mix_Chunk*)sound);
|
|
}
|
|
|
|
void JA_PauseChannel(const int channel)
|
|
{
|
|
if (!JA_soundEnabled) return;
|
|
Mix_Pause(channel);
|
|
}
|
|
|
|
void JA_ResumeChannel(const int channel)
|
|
{
|
|
if (!JA_soundEnabled) return;
|
|
Mix_Resume(channel);
|
|
}
|
|
|
|
void JA_StopChannel(const int channel)
|
|
{
|
|
if (!JA_soundEnabled) return;
|
|
Mix_HaltChannel(channel);
|
|
}
|
|
|
|
JA_Channel_state JA_GetChannelState(const int channel)
|
|
{
|
|
if (!JA_soundEnabled) return JA_SOUND_DISABLED;
|
|
|
|
if (Mix_Paused(channel)) {
|
|
return JA_CHANNEL_PAUSED;
|
|
} else if (Mix_Playing(channel)) {
|
|
return JA_CHANNEL_PLAYING;
|
|
} else {
|
|
return JA_CHANNEL_FREE;
|
|
}
|
|
}
|
|
|
|
int JA_SetSoundVolume(int volume)
|
|
{
|
|
JA_musicVolume = volume;
|
|
Mix_Volume(-1, JA_musicVolume);
|
|
return JA_musicVolume;
|
|
}
|
|
|
|
void JA_EnableSound(const bool value)
|
|
{
|
|
Mix_HaltChannel(-1);
|
|
JA_soundEnabled = value;
|
|
}
|
|
|
|
|
|
int JA_SetVolume(int volume)
|
|
{
|
|
JA_SetSoundVolume(volume);
|
|
return JA_SetMusicVolume(volume);
|
|
}
|
|
|
|
#endif
|