- [NEW] Adaptació de àudio en progrés

This commit is contained in:
2026-04-17 13:46:22 +02:00
parent 9c4c94093c
commit e6d4833e8b
3 changed files with 675 additions and 535 deletions

View File

@@ -6,19 +6,19 @@ namespace mini
{ {
namespace audio namespace audio
{ {
JA_Music_t *current_music = NULL; int current_music = -1;
#define MAX_SOUNDS 50 //#define MAX_SOUNDS 50
JA_Sound_t *sounds[MAX_SOUNDS]; //JA_Sound_t *sounds[MAX_SOUNDS];
void init() { void init() {
JA_Init(48000, SDL_AUDIO_S16, 1); jail::audio::init();
for (int i=0;i<MAX_SOUNDS;++i) sounds[i] = NULL; //for (int i=0;i<MAX_SOUNDS;++i) sounds[i] = NULL;
} }
void quit() { void quit() {
if (current_music != NULL) JA_DeleteMusic(current_music); //if (current_music != NULL) JA_DeleteMusic(current_music);
for (int i=0;i<MAX_SOUNDS;++i) if (sounds[i]!=NULL) JA_DeleteSound(sounds[i]); //for (int i=0;i<MAX_SOUNDS;++i) if (sounds[i]!=NULL) JA_DeleteSound(sounds[i]);
JA_Quit(); jail::audio::quit();
} }
namespace music namespace music
@@ -26,45 +26,45 @@ namespace mini
void play(const char *filename, const int loop) { void play(const char *filename, const int loop) {
int size; int size;
char *buffer = file::getfilebuffer(filename, size); char *buffer = file::getfilebuffer(filename, size);
if (current_music != NULL) JA_DeleteMusic(current_music); if (current_music != -1) jail::audio::music::destroy(current_music);
current_music = JA_LoadMusic((Uint8*)buffer, size); current_music = jail::audio::music::load((uint8_t*)buffer, size);
JA_PlayMusic(current_music, loop); jail::audio::music::play(current_music, loop);
} }
void pause() { void pause() {
JA_PauseMusic(); jail::audio::music::pause();
} }
void resume() { void resume() {
JA_ResumeMusic(); jail::audio::music::resume();
} }
void stop(const int t) { void stop(const int t) {
JA_StopMusic(); jail::audio::music::stop();
} }
namespace pos { namespace pos {
void set(float value) void set(float value)
{ {
JA_SetMusicPosition(value); jail::audio::music::setPosition(value);
} }
float get() float get()
{ {
return JA_GetMusicPosition(); return jail::audio::music::getPosition();
} }
} }
namespace enable { namespace enable {
void set(const bool value) void set(const bool value)
{ {
JA_EnableMusic(value); jail::audio::music::enable(value);
file::setconfigvalue("music", value?"true":"false"); file::setconfigvalue("music", value?"true":"false");
} }
const bool get() const bool get()
{ {
return JA_IsMusicEnabled(); return jail::audio::music::isEnabled();
} }
} }
} }
@@ -74,40 +74,32 @@ namespace mini
int load(const char *filename) { int load(const char *filename) {
int size; int size;
char *buffer = file::getfilebuffer(filename, size); char *buffer = file::getfilebuffer(filename, size);
int i=0; return jail::audio::sound::load((uint8_t*)buffer, size);
while (i<MAX_SOUNDS && sounds[i]!=NULL) i++;
if (i==MAX_SOUNDS) { i=0; JA_DeleteSound(sounds[i]); }
sounds[i]=JA_LoadSound((Uint8*)buffer, size);
return i;
} }
void free(int soundfile) { void free(int soundfile) {
JA_DeleteSound(sounds[soundfile]); return jail::audio::sound::destroy(soundfile);
sounds[soundfile] = NULL;
} }
int play(int soundfile, const int volume) { int play(int soundfile, const int volume) {
const int channel = JA_PlaySound(sounds[soundfile], 0); // [TODO] Ficar el volumen
JA_SetSoundVolume(128); return jail::audio::sound::play(soundfile, 0);
//Mix_Volume(channel, volume!=-1?volume:MIX_MAX_VOLUME);
return channel;
} }
void stop(int soundchannel) { void stop(int soundchannel) {
JA_StopChannel(soundchannel); return jail::audio::sound::channel::stop(soundchannel);
//Mix_HaltChannel(soundchannel);
} }
namespace enable { namespace enable {
void set(const bool value) void set(const bool value)
{ {
JA_EnableSound(value); return jail::audio::sound::enable(value);
file::setconfigvalue("sound", value?"true":"false"); file::setconfigvalue("sound", value?"true":"false");
} }
const bool get() const bool get()
{ {
return JA_IsSoundEnabled(); return jail::audio::sound::isEnabled();
} }
} }
} }

View File

@@ -6,26 +6,21 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <stdio.h> #include <stdio.h>
#include <vector>
#define JA_MAX_SIMULTANEOUS_CHANNELS 5 // structs i variables
// =============================
struct JA_Sound_t namespace jail
{ {
SDL_AudioSpec spec { SDL_AUDIO_S16, 2, 48000 }; namespace audio
Uint32 length { 0 };
Uint8 *buffer { NULL };
};
struct JA_Channel_t
{ {
JA_Sound_t *sound { nullptr }; static SDL_AudioSpec audioSpec { SDL_AUDIO_S16, 2, 48000 };
int pos { 0 }; SDL_AudioDeviceID sdlAudioDevice { 0 };
int times { 0 }; SDL_TimerID timerID { 0 };
SDL_AudioStream *stream { nullptr };
JA_Channel_state state { JA_CHANNEL_FREE };
};
struct JA_Music_t namespace music
{
struct music_t
{ {
SDL_AudioSpec spec { SDL_AUDIO_S16, 2, 48000 }; SDL_AudioSpec spec { SDL_AUDIO_S16, 2, 48000 };
Uint32 length { 0 }; Uint32 length { 0 };
@@ -34,162 +29,193 @@ struct JA_Music_t
int pos { 0 }; int pos { 0 };
int times { 0 }; int times { 0 };
SDL_AudioStream *stream { nullptr }; SDL_AudioStream *stream { nullptr };
JA_Music_state state { JA_MUSIC_INVALID }; music::state state { music::state::invalid };
}; };
JA_Music_t *current_music { nullptr }; static int current { -1 };
JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS]; static std::vector<music_t> musics;
static float volume { 1.0f };
static bool enabled { true };
SDL_AudioSpec JA_audioSpec { SDL_AUDIO_S16, 2, 48000 }; namespace fade
float JA_musicVolume { 1.0f };
float JA_soundVolume { 0.5f };
bool JA_musicEnabled { true };
bool JA_soundEnabled { true };
SDL_AudioDeviceID sdlAudioDevice { 0 };
SDL_TimerID JA_timerID { 0 };
bool fading = false;
int fade_start_time;
int fade_duration;
int fade_initial_volume;
/*
void audioCallback(void * userdata, uint8_t * stream, int len) {
SDL_memset(stream, 0, len);
if (current_music != NULL && current_music->state == JA_MUSIC_PLAYING) {
const int size = SDL_min(len, current_music->samples*2-current_music->pos);
SDL_MixAudioFormat(stream, (Uint8*)(current_music->output+current_music->pos), AUDIO_S16, size, JA_musicVolume);
current_music->pos += size/2;
if (size < len) {
if (current_music->times != 0) {
SDL_MixAudioFormat(stream+size, (Uint8*)current_music->output, AUDIO_S16, len-size, JA_musicVolume);
current_music->pos = (len-size)/2;
if (current_music->times > 0) current_music->times--;
} else {
current_music->pos = 0;
current_music->state = JA_MUSIC_STOPPED;
}
}
}
// Mixar els channels mi amol
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
if (channels[i].state == JA_CHANNEL_PLAYING) {
const int size = SDL_min(len, channels[i].sound->length - channels[i].pos);
SDL_MixAudioFormat(stream, channels[i].sound->buffer + channels[i].pos, AUDIO_S16, size, JA_soundVolume);
channels[i].pos += size;
if (size < len) {
if (channels[i].times != 0) {
SDL_MixAudioFormat(stream + size, channels[i].sound->buffer, AUDIO_S16, len-size, JA_soundVolume);
channels[i].pos = len-size;
if (channels[i].times > 0) channels[i].times--;
} else {
JA_StopChannel(i);
}
}
}
}
}
*/
Uint32 JA_UpdateCallback(void *userdata, SDL_TimerID timerID, Uint32 interval)
{ {
if (JA_musicEnabled && current_music && current_music->state == JA_MUSIC_PLAYING) static bool fading = false;
static int start_time;
static int duration;
static int initial_volume;
}
}
namespace sound
{ {
if (fading) { struct sound_t
{
SDL_AudioSpec spec { SDL_AUDIO_S16, 2, 48000 };
Uint32 length { 0 };
Uint8 *buffer { NULL };
};
static std::vector<sound_t> sounds;
static float volume { 0.5f };
static bool enabled { true };
namespace channel
{
struct channel_t
{
int sound { -1 };
int pos { 0 };
int times { 0 };
SDL_AudioStream *stream { nullptr };
channel::state state { channel::state::free };
};
static std::vector<channel_t> channels;
}
}
}
}
// Funcions
// ==================
namespace jail
{
namespace audio
{
static void updateMusic()
{
if (!music::enabled) return;
if (music::current < 0 || music::current > music::musics.size()) return;
auto &m = music::musics[music::current];
if (m.state != music::state::playing) return;
if (music::fade::fading) {
int time = SDL_GetTicks(); int time = SDL_GetTicks();
if (time > (fade_start_time+fade_duration)) { if (time > (music::fade::start_time + music::fade::duration)) {
fading = false; music::fade::fading = false;
JA_StopMusic(); music::stop();
return 30; return;
} else { } else {
const int time_passed = time - fade_start_time; const int time_passed = time - music::fade::start_time;
const float percent = (float)time_passed / (float)fade_duration; const float percent = (float)time_passed / (float)music::fade::duration;
SDL_SetAudioStreamGain(current_music->stream, 1.0 - percent); SDL_SetAudioStreamGain(m.stream, 1.0 - percent);
} }
} }
if (current_music->times != 0) if (m.times != 0)
{ {
if (SDL_GetAudioStreamAvailable(current_music->stream) < int(current_music->length/2)) { if (SDL_GetAudioStreamAvailable(m.stream) < int(m.length/2)) {
SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length); SDL_PutAudioStreamData(m.stream, m.buffer, m.length);
} }
if (current_music->times>0) current_music->times--; if (m.times>0) m.times--;
} }
else else
{ {
if (SDL_GetAudioStreamAvailable(current_music->stream) == 0) JA_StopMusic(); if (SDL_GetAudioStreamAvailable(m.stream) == 0) music::stop();
} }
} }
if (JA_soundEnabled) static void updateSound()
{ {
for (int i=0; i < JA_MAX_SIMULTANEOUS_CHANNELS; ++i) if (sound::enabled)
if (channels[i].state == JA_CHANNEL_PLAYING)
{ {
if (channels[i].times != 0) for (int i=0; i < sound::channel::channels.size(); ++i) {
auto &c = sound::channel::channels[i];
if (c.state == sound::channel::state::playing)
{ {
if (SDL_GetAudioStreamAvailable(channels[i].stream) < int(channels[i].sound->length/2)) if (c.times != 0)
SDL_PutAudioStreamData(channels[i].stream, channels[i].sound->buffer, channels[i].sound->length); {
if (channels[i].times>0) channels[i].times--; auto &s = sound::sounds[c.sound];
if (SDL_GetAudioStreamAvailable(c.stream) < int(s.length/2))
SDL_PutAudioStreamData(c.stream, s.buffer, s.length);
if (c.times>0) c.times--;
} }
} }
else else
{ {
if (SDL_GetAudioStreamAvailable(channels[i].stream) == 0) JA_StopChannel(i); if (SDL_GetAudioStreamAvailable(c.stream) == 0) sound::channel::stop(i);
} }
}
}
} }
Uint32 updateCallback(void *userdata, SDL_TimerID timerID, Uint32 interval)
{
updateMusic();
updateSound();
return 30; return 30;
} }
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) void init()
{ {
#ifdef DEBUG #ifdef DEBUG
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG); SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG);
#endif #endif
JA_audioSpec = {format, channels, freq }; audioSpec = {SDL_AUDIO_S16, 1, 48000 };
if (!sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice); if (!sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice);
sdlAudioDevice = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &JA_audioSpec); sdlAudioDevice = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audioSpec);
if (!sdlAudioDevice) { if (!sdlAudioDevice) {
log_msg(LOG_FAIL, "Failed to initialize SDL audio: %s\n", SDL_GetError()); log_msg(LOG_FAIL, "Failed to initialize SDL audio: %s\n", SDL_GetError());
} else { } else {
log_msg(LOG_OK, "Audio subsytem initialized\n"); log_msg(LOG_OK, "Audio subsytem initialized\n");
} }
//SDL_PauseAudioDevice(sdlAudioDevice); //SDL_PauseAudioDevice(sdlAudioDevice);
JA_timerID = SDL_AddTimer(30, JA_UpdateCallback, nullptr); timerID = SDL_AddTimer(30, updateCallback, nullptr);
} }
void JA_Quit() void quit()
{ {
if (JA_timerID) SDL_RemoveTimer(JA_timerID); if (timerID) SDL_RemoveTimer(timerID);
for (int i=0; i<music::musics.size();++i) music::destroy(i);
music::musics.clear();
for (int i=0; i<sound::channel::channels.size();++i) sound::channel::stop(i);
sound::channel::channels.clear();
for (int i=0; i<sound::sounds.size();++i) sound::destroy(i);
sound::sounds.clear();
if (!sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice); if (!sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice);
sdlAudioDevice = 0; sdlAudioDevice = 0;
} }
JA_Music_t *JA_LoadMusic(Uint8* buffer, Uint32 length) float setVolume(float vol)
{ {
JA_Music_t *music = new JA_Music_t(); sound::setVolume(music::setVolume(vol) / 2.0f);
return music::volume;
}
namespace music
{
int load(const uint8_t* buffer, uint32_t length)
{
int music = 0;
while (music < musics.size() && musics[music].state != state::invalid) { music++; }
if (music == musics.size()) musics.emplace_back();
auto &m = musics[music];
int chan, samplerate; int chan, samplerate;
short *output; short *output;
music->length = stb_vorbis_decode_memory(buffer, length, &chan, &samplerate, &output) * chan * 2; m.length = stb_vorbis_decode_memory(buffer, length, &chan, &samplerate, &output) * chan * 2;
music->spec.channels = chan; m.spec.channels = chan;
music->spec.freq = samplerate; m.spec.freq = samplerate;
music->spec.format = SDL_AUDIO_S16; m.spec.format = SDL_AUDIO_S16;
music->buffer = (Uint8*)SDL_malloc(music->length); m.buffer = (uint8_t*)SDL_malloc(m.length);
SDL_memcpy(music->buffer, output, music->length); SDL_memcpy(m.buffer, output, m.length);
free(output); free(output);
music->pos = 0; m.pos = 0;
music->state = JA_MUSIC_STOPPED; m.state = state::stopped;
return music; return music;
} }
JA_Music_t *JA_LoadMusic(const char* filename) int load(const char* filename)
{ {
// [RZC 28/08/22] Carreguem primer el arxiu en memòria i després el descomprimim. Es algo més rapid. // [RZC 28/08/22] Carreguem primer el arxiu en memòria i després el descomprimim. Es algo més rapid.
FILE *f = fopen(filename, "rb"); FILE *f = fopen(filename, "rb");
@@ -200,310 +226,417 @@ JA_Music_t *JA_LoadMusic(const char* filename)
if (fread(buffer, fsize, 1, f)!=1) return NULL; if (fread(buffer, fsize, 1, f)!=1) return NULL;
fclose(f); fclose(f);
JA_Music_t *music = JA_LoadMusic(buffer, fsize); int music = load(buffer, fsize);
free(buffer); free(buffer);
return music; return music;
} }
void JA_PlayMusic(JA_Music_t *music, const int loop) void play(int mus, int loop)
{ {
if (!JA_musicEnabled) return; if (!music::enabled) return;
stop();
JA_StopMusic(); if (mus < 0 || mus >= musics.size()) {
log_msg(LOG_FAIL, "music::play: Illegal music handle: %i\n", mus);
current_music = music; return;
current_music->pos = 0;
current_music->state = JA_MUSIC_PLAYING;
current_music->times = loop;
current_music->stream = SDL_CreateAudioStream(&current_music->spec, &JA_audioSpec);
if (!SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length)) log_msg(LOG_FAIL, "SDL_PutAudioStreamData failed!\n");
SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume);
if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) log_msg(LOG_FAIL, "SDL_BindAudioStream failed!\n");
//SDL_ResumeAudioStreamDevice(current_music->stream);
} }
void JA_PauseMusic() current = mus;
{ auto &m = musics[current];
if (!JA_musicEnabled) return; m.pos = 0;
if (!current_music || current_music->state == JA_MUSIC_INVALID) return; m.state = state::playing;
m.times = loop;
current_music->state = JA_MUSIC_PAUSED; m.stream = SDL_CreateAudioStream(&m.spec, &audioSpec);
//SDL_PauseAudioStreamDevice(current_music->stream); if (!SDL_PutAudioStreamData(m.stream, m.buffer, m.length)) log_msg(LOG_FAIL, "SDL_PutAudioStreamData failed!\n");
SDL_UnbindAudioStream(current_music->stream); SDL_SetAudioStreamGain(m.stream, volume);
if (!SDL_BindAudioStream(sdlAudioDevice, m.stream)) log_msg(LOG_FAIL, "SDL_BindAudioStream failed!\n");
//SDL_ResumeAudioStreamDevice(current->stream);
} }
void JA_ResumeMusic() void pause()
{ {
if (!JA_musicEnabled) return; if (!music::enabled) return;
if (!current_music || current_music->state == JA_MUSIC_INVALID) return; if (current<0 || current>musics.size()) {
log_msg(LOG_FAIL, "music::pause: Illegal music handle: %i\n", current);
current_music->state = JA_MUSIC_PLAYING; return;
//SDL_ResumeAudioStreamDevice(current_music->stream); }
SDL_BindAudioStream(sdlAudioDevice, current_music->stream); auto &m = musics[current];
if (m.state == state::invalid) {
log_msg(LOG_FAIL, "music::pause: Invalidated music handle: %i\n", current);
return;
} }
void JA_StopMusic() m.state = state::paused;
{ //SDL_PauseAudioStreamDevice(current->stream);
if (!JA_musicEnabled) return; SDL_UnbindAudioStream(m.stream);
if (!current_music || current_music->state == JA_MUSIC_INVALID) return;
current_music->pos = 0;
current_music->state = JA_MUSIC_STOPPED;
//SDL_PauseAudioStreamDevice(current_music->stream);
SDL_DestroyAudioStream(current_music->stream);
current_music->stream = nullptr;
} }
void JA_FadeOutMusic(const int milliseconds) void resume()
{ {
if (!JA_musicEnabled) return; if (!music::enabled) return;
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return; if (current<0 || current>musics.size()) {
log_msg(LOG_FAIL, "music::resume: Illegal music handle: %i\n", current);
fading = true; return;
fade_start_time = SDL_GetTicks(); }
fade_duration = milliseconds; auto &m = musics[current];
fade_initial_volume = JA_musicVolume; if (m.state == state::invalid) {
log_msg(LOG_FAIL, "music::resume: Invalidated music handle: %i\n", current);
return;
} }
JA_Music_state JA_GetMusicState() m.state = state::playing;
{ //SDL_ResumeAudioStreamDevice(current->stream);
if (!JA_musicEnabled) return JA_MUSIC_DISABLED; SDL_BindAudioStream(sdlAudioDevice, m.stream);
if (!current_music) return JA_MUSIC_INVALID;
return current_music->state;
} }
void JA_DeleteMusic(JA_Music_t *music) void stop()
{ {
if (current_music == music) current_music = nullptr; if (!music::enabled) return;
SDL_free(music->buffer); if (current<0 || current>musics.size()) {
if (music->stream) SDL_DestroyAudioStream(music->stream); log_msg(LOG_FAIL, "music::stop: Illegal music handle: %i\n", current);
delete music; return;
}
auto &m = musics[current];
if (m.state == state::invalid) {
log_msg(LOG_FAIL, "music::stop: Invalidated music handle: %i\n", current);
return;
} }
float JA_SetMusicVolume(float volume) m.pos = 0;
{ m.state = state::stopped;
JA_musicVolume = SDL_clamp( volume, 0.0f, 1.0f ); //SDL_PauseAudioStreamDevice(current->stream);
if (current_music) SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume); SDL_DestroyAudioStream(m.stream);
return JA_musicVolume; m.stream = nullptr;
} }
void JA_SetMusicPosition(float value) void fadeOut(int milliseconds)
{ {
if (!current_music) return; if (!music::enabled) return;
current_music->pos = value * current_music->spec.freq; if (current<0 || current>musics.size()) {
log_msg(LOG_FAIL, "music::fadeOut: Illegal music handle: %i\n", current);
return;
}
auto &m = musics[current];
if (m.state == state::invalid) {
log_msg(LOG_FAIL, "music::fadeOut: Invalidated music handle: %i\n", current);
return;
} }
float JA_GetMusicPosition() fade::fading = true;
{ fade::start_time = SDL_GetTicks();
if (!current_music) return 0; fade::duration = milliseconds;
return float(current_music->pos)/float(current_music->spec.freq); fade::initial_volume = volume;
} }
void JA_EnableMusic(const bool value) music::state getState()
{ {
if ( !value && current_music && (current_music->state==JA_MUSIC_PLAYING) ) JA_StopMusic(); if (!music::enabled) return music::state::disabled;
if (current<0 || current>musics.size()) return state::invalid;
JA_musicEnabled = value; return musics[current].state;
} }
const bool JA_IsMusicEnabled() void destroy(int mus)
{ {
return JA_musicEnabled; if (current == mus) current = -1;
if (mus<0 || mus>musics.size()) {
log_msg(LOG_FAIL, "music::destroy: Illegal music handle: %i\n", mus);
return;
}
auto &m = musics[mus];
SDL_free(m.buffer);
m.buffer = nullptr;
if (m.stream) SDL_DestroyAudioStream(m.stream);
m.stream = nullptr;
m.state = state::invalid;
} }
float setVolume(float vol)
JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length)
{ {
JA_Sound_t *sound = new JA_Sound_t(); volume = SDL_clamp( vol, 0.0f, 1.0f );
sound->buffer = buffer;
sound->length = length; if (current<0 || current>musics.size()) return vol;
return sound; auto &m = musics[current];
if (m.state == state::invalid) return vol;
SDL_SetAudioStreamGain(m.stream, volume);
return volume;
} }
JA_Sound_t *JA_LoadSound(uint8_t* buffer, uint32_t size) void setPosition(float value)
{ {
JA_Sound_t *sound = new JA_Sound_t(); if (!music::enabled) return;
SDL_LoadWAV_IO(SDL_IOFromMem(buffer, size),1, &sound->spec, &sound->buffer, &sound->length); if (current<0 || current>musics.size()) {
log_msg(LOG_FAIL, "music::setPosition: Illegal music handle: %i\n", current);
return sound; return;
}
auto &m = musics[current];
if (m.state == state::invalid) {
log_msg(LOG_FAIL, "music::setPosition: Invalidated music handle: %i\n", current);
return;
} }
JA_Sound_t *JA_LoadSound(const char* filename) m.pos = value * m.spec.freq;
{
JA_Sound_t *sound = new JA_Sound_t();
SDL_LoadWAV(filename, &sound->spec, &sound->buffer, &sound->length);
return sound;
} }
int JA_PlaySound(JA_Sound_t *sound, const int loop) float getPosition()
{ {
if (!JA_soundEnabled) return -1; if (!music::enabled) return 0;
if (current<0 || current>musics.size()) {
int channel = 0; log_msg(LOG_FAIL, "music::getPosition: Illegal music handle: %i\n", current);
while (channel < JA_MAX_SIMULTANEOUS_CHANNELS && channels[channel].state != JA_CHANNEL_FREE) { channel++; } return 0;
if (channel == JA_MAX_SIMULTANEOUS_CHANNELS) channel = 0; }
JA_StopChannel(channel); auto &m = musics[current];
if (m.state == state::invalid) {
channels[channel].sound = sound; log_msg(LOG_FAIL, "music::getPosition: Invalidated music handle: %i\n", current);
channels[channel].times = loop; return 0;
channels[channel].pos = 0;
channels[channel].state = JA_CHANNEL_PLAYING;
channels[channel].stream = SDL_CreateAudioStream(&channels[channel].sound->spec, &JA_audioSpec);
SDL_PutAudioStreamData(channels[channel].stream, channels[channel].sound->buffer, channels[channel].sound->length);
SDL_SetAudioStreamGain(channels[channel].stream, JA_soundVolume);
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream);
return channel;
} }
int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop) return float(m.pos)/float(m.spec.freq);
{
if (!JA_soundEnabled) return -1;
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return -1;
JA_StopChannel(channel);
channels[channel].sound = sound;
channels[channel].times = loop;
channels[channel].pos = 0;
channels[channel].state = JA_CHANNEL_PLAYING;
channels[channel].stream = SDL_CreateAudioStream(&channels[channel].sound->spec, &JA_audioSpec);
SDL_PutAudioStreamData(channels[channel].stream, channels[channel].sound->buffer, channels[channel].sound->length);
SDL_SetAudioStreamGain(channels[channel].stream, JA_soundVolume);
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream);
return channel;
} }
void JA_DeleteSound(JA_Sound_t *sound) void enable(bool value)
{ {
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { if (!value && music::enabled && current>=0 && current<musics.size() && musics[current].state==state::playing) stop();
if (channels[i].sound == sound) JA_StopChannel(i); music::enabled = value;
}
SDL_free(sound->buffer);
delete sound;
} }
void JA_PauseChannel(const int channel) bool isEnabled()
{ {
if (!JA_soundEnabled) return; return enabled;
}
if (channel == -1) }
namespace sound
{ {
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) int create(uint8_t* buffer, uint32_t length)
if (channels[i].state == JA_CHANNEL_PLAYING)
{ {
channels[i].state = JA_CHANNEL_PAUSED; int snd = 0;
while (snd < sounds.size() && sounds[snd].buffer) { snd++; }
if (snd == sounds.size()) sounds.emplace_back();
auto &s = sounds[snd];
s.buffer = buffer;
s.length = length;
return snd;
}
int load(uint8_t* buffer, uint32_t size)
{
int snd = 0;
while (snd < sounds.size() && sounds[snd].buffer) { snd++; }
if (snd == sounds.size()) sounds.emplace_back();
auto &s = sounds[snd];
SDL_LoadWAV_IO(SDL_IOFromMem(buffer, size),1, &s.spec, &s.buffer, &s.length);
return snd;
}
int load(const char* filename)
{
int snd = 0;
while (snd < sounds.size() && sounds[snd].buffer) { snd++; }
if (snd == sounds.size()) sounds.emplace_back();
auto &s = sounds[snd];
SDL_LoadWAV(filename, &s.spec, &s.buffer, &s.length);
return snd;
}
int play(int snd, int loop)
{
if (!sound::enabled) return -1;
if (snd<0 || snd>sounds.size()) {
log_msg(LOG_FAIL, "sound::play: Illegal sound handle: %i\n", snd);
return -1;
}
auto &s = sounds[snd];
if (!s.buffer) {
log_msg(LOG_FAIL, "sound::play: Invalid sound: %i\n", snd);
return -1;
}
int chan = 0;
while (chan < channel::channels.size() && channel::channels[chan].state != channel::state::free) { chan++; }
if (chan == channel::channels.size()) channel::channels.emplace_back();
channel::stop(chan);
auto &c = channel::channels[chan];
c.sound = snd;
c.times = loop;
c.pos = 0;
c.state = channel::state::playing;
c.stream = SDL_CreateAudioStream(&s.spec, &audioSpec);
SDL_PutAudioStreamData(c.stream, s.buffer, s.length);
SDL_SetAudioStreamGain(c.stream, volume);
SDL_BindAudioStream(sdlAudioDevice, c.stream);
return chan;
}
int playOnChannel(int snd, int chan, int loop)
{
if (!sound::enabled) return -1;
if (snd<0 || snd>sounds.size()) {
log_msg(LOG_FAIL, "sound::playOnChannel: Illegal sound handle: %i\n", snd);
return -1;
}
auto &s = sounds[snd];
if (!s.buffer) {
log_msg(LOG_FAIL, "sound::playOnChannel: Invalid sound: %i\n", snd);
return -1;
}
if (chan<0 || chan>channel::channels.size()) {
log_msg(LOG_FAIL, "sound::playOnChannel: Illegal channel handle: %i\n", chan);
return -1;
}
channel::stop(chan);
auto &c = channel::channels[chan];
c.sound = snd;
c.times = loop;
c.pos = 0;
c.state = channel::state::playing;
c.stream = SDL_CreateAudioStream(&s.spec, &audioSpec);
SDL_PutAudioStreamData(c.stream, s.buffer, s.length);
SDL_SetAudioStreamGain(c.stream, volume);
SDL_BindAudioStream(sdlAudioDevice, c.stream);
return chan;
}
void destroy(int snd)
{
for (int i = 0; i < channel::channels.size(); i++) {
if (channel::channels[i].sound == snd) channel::stop(i);
}
if (snd<0 || snd>sounds.size()) {
log_msg(LOG_FAIL, "sound::destroy: Illegal sound handle: %i\n", snd);
return;
}
auto &s = sounds[snd];
SDL_free(s.buffer);
}
float setVolume(float vol)
{
volume = SDL_clamp( vol, 0.0f, 1.0f );
for (int i = 0; i < channel::channels.size(); i++) {
auto &c = channel::channels[i];
if ( (c.state == channel::state::playing) || (c.state == channel::state::paused) )
SDL_SetAudioStreamGain(c.stream, volume);
}
return volume;
}
void enable(bool value)
{
if (!value && sound::enabled) {
for (int i = 0; i < channel::channels.size(); i++) {
if (channel::channels[i].state == channel::state::playing) channel::stop(i);
}
}
enabled = value;
}
bool isEnabled()
{
return enabled;
}
namespace channel
{
void pause(const int chan)
{
if (!sound::enabled) return;
if (chan == -1)
{
for (int i = 0; i < channels.size(); i++)
if (channels[i].state == state::playing)
{
channels[i].state = state::paused;
//SDL_PauseAudioStreamDevice(channels[i].stream); //SDL_PauseAudioStreamDevice(channels[i].stream);
SDL_UnbindAudioStream(channels[i].stream); SDL_UnbindAudioStream(channels[i].stream);
} }
} }
else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) else if (chan >= 0 && chan < channels.size())
{ {
if (channels[channel].state == JA_CHANNEL_PLAYING) if (channels[chan].state == state::playing)
{ {
channels[channel].state = JA_CHANNEL_PAUSED; channels[chan].state = state::paused;
//SDL_PauseAudioStreamDevice(channels[channel].stream); //SDL_PauseAudioStreamDevice(channels[channel].stream);
SDL_UnbindAudioStream(channels[channel].stream); SDL_UnbindAudioStream(channels[chan].stream);
} }
} }
} }
void JA_ResumeChannel(const int channel) void resume(int chan)
{ {
if (!JA_soundEnabled) return; if (!sound::enabled) return;
if (channel == -1) if (chan == -1)
{ {
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) for (int i = 0; i < channels.size(); i++)
if (channels[i].state == JA_CHANNEL_PAUSED) if (channels[i].state == state::paused)
{ {
channels[i].state = JA_CHANNEL_PLAYING; channels[i].state = state::playing;
//SDL_ResumeAudioStreamDevice(channels[i].stream); //SDL_ResumeAudioStreamDevice(channels[i].stream);
SDL_BindAudioStream(sdlAudioDevice, channels[i].stream); SDL_BindAudioStream(sdlAudioDevice, channels[i].stream);
} }
} }
else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) else if (chan >= 0 && chan < channels.size())
{ {
if (channels[channel].state == JA_CHANNEL_PAUSED) if (channels[chan].state == state::paused)
{ {
channels[channel].state = JA_CHANNEL_PLAYING; channels[chan].state = state::playing;
//SDL_ResumeAudioStreamDevice(channels[channel].stream); //SDL_ResumeAudioStreamDevice(channels[channel].stream);
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream); SDL_BindAudioStream(sdlAudioDevice, channels[chan].stream);
} }
} }
} }
void JA_StopChannel(const int channel) void stop(int chan)
{ {
if (!JA_soundEnabled) return; if (!sound::enabled) return;
if (channel == -1) if (chan == -1)
{ {
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { for (int i = 0; i < channels.size(); i++) {
if (channels[i].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[i].stream); if (channels[i].state != state::free) SDL_DestroyAudioStream(channels[i].stream);
channels[i].stream = nullptr; channels[i].stream = nullptr;
channels[i].state = JA_CHANNEL_FREE; channels[i].state = state::free;
channels[i].pos = 0; channels[i].pos = 0;
channels[i].sound = NULL; channels[i].sound = -1;
} }
} }
else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) else if (chan >= 0 && chan < channels.size())
{ {
if (channels[channel].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[channel].stream); if (channels[chan].state != state::free) SDL_DestroyAudioStream(channels[chan].stream);
channels[channel].stream = nullptr; channels[chan].stream = nullptr;
channels[channel].state = JA_CHANNEL_FREE; channels[chan].state = state::free;
channels[channel].pos = 0; channels[chan].pos = 0;
channels[channel].sound = NULL; channels[chan].sound = -1;
} }
} }
JA_Channel_state JA_GetChannelState(const int channel) channel::state getState(int chan)
{ {
if (!JA_soundEnabled) return JA_SOUND_DISABLED; if (!sound::enabled) return state::disabled;
if (chan < 0 || chan >= channels.size()) return state::invalid;
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return JA_CHANNEL_INVALID; return channels[chan].state;
return channels[channel].state;
} }
float JA_SetSoundVolume(float volume)
{
JA_soundVolume = SDL_clamp( volume, 0.0f, 1.0f );
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
if ( (channels[i].state == JA_CHANNEL_PLAYING) || (channels[i].state == JA_CHANNEL_PAUSED) )
SDL_SetAudioStreamGain(channels[i].stream, JA_soundVolume);
return JA_soundVolume;
} }
void JA_EnableSound(const bool value)
{
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
{
if (channels[i].state == JA_CHANNEL_PLAYING) JA_StopChannel(i);
} }
JA_soundEnabled = value;
} }
const bool JA_IsSoundEnabled()
{
return JA_soundEnabled;
}
float JA_SetVolume(float volume)
{
JA_SetSoundVolume(JA_SetMusicVolume(volume) / 2.0f);
return JA_musicVolume;
} }
#endif #endif

View File

@@ -1,42 +1,57 @@
#pragma once #pragma once
#include <SDL3/SDL.h> #include <stdint.h>
enum JA_Channel_state { JA_CHANNEL_INVALID, JA_CHANNEL_FREE, JA_CHANNEL_PLAYING, JA_CHANNEL_PAUSED, JA_SOUND_DISABLED }; namespace jail
enum JA_Music_state { JA_MUSIC_INVALID, JA_MUSIC_PLAYING, JA_MUSIC_PAUSED, JA_MUSIC_STOPPED, JA_MUSIC_DISABLED }; {
namespace audio
{
void init(/*const int freq, const SDL_AudioFormat format, const int channels*/);
void quit();
struct JA_Sound_t; namespace music
struct JA_Music_t; {
//struct JA_Music_t;
enum state { invalid, playing, paused, stopped, disabled };
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels); int load(const char* filename);
void JA_Quit(); int load(const uint8_t* buffer, uint32_t length);
void play(int mus, int loop = -1);
void pause();
void resume();
void stop();
void fadeOut(int milliseconds);
state getState();
void destroy(int mus);
float setVolume(float vol);
void setPosition(float value);
float getPosition();
void enable(bool value);
bool isEnabled();
}
JA_Music_t *JA_LoadMusic(const char* filename); namespace sound
JA_Music_t *JA_LoadMusic(Uint8* buffer, Uint32 length); {
void JA_PlayMusic(JA_Music_t *music, const int loop = -1); //struct JA_Sound_t;
void JA_PauseMusic(); int create(uint8_t* buffer, uint32_t length);
void JA_ResumeMusic(); int load(uint8_t* buffer, uint32_t length);
void JA_StopMusic(); int load(const char* filename);
void JA_FadeOutMusic(const int milliseconds); int play(int snd, int loop = 0);
JA_Music_state JA_GetMusicState(); int playOnChannel(int snd, int chan, int loop = 0);
void JA_DeleteMusic(JA_Music_t *music); void destroy(int snd);
float JA_SetMusicVolume(float volume); float setVolume(float vol);
void JA_SetMusicPosition(float value); void enable(bool value);
float JA_GetMusicPosition(); bool isEnabled();
void JA_EnableMusic(const bool value);
const bool JA_IsMusicEnabled(); namespace channel
{
enum state { invalid, free, playing, paused, disabled };
void pause(int chan);
void resume(int chan);
void stop(int chan);
state getState(int chan);
}
}
}
}
JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length);
JA_Sound_t *JA_LoadSound(Uint8* buffer, Uint32 length);
JA_Sound_t *JA_LoadSound(const char* filename);
int JA_PlaySound(JA_Sound_t *sound, const int loop = 0);
int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop = 0);
void JA_PauseChannel(const int channel);
void JA_ResumeChannel(const int channel);
void JA_StopChannel(const int channel);
JA_Channel_state JA_GetChannelState(const int channel);
void JA_DeleteSound(JA_Sound_t *sound);
float JA_SetSoundVolume(float volume);
void JA_EnableSound(const bool value);
const bool JA_IsSoundEnabled();
float JA_SetVolume(float volume);