forked from JailDoctor/JailAudio
- [NEW] Afegits grups de sons, per a fer el volum de cada grup independent.
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#define JA_MAX_SIMULTANEOUS_CHANNELS 5
|
#define JA_MAX_SIMULTANEOUS_CHANNELS 5
|
||||||
|
#define JA_MAX_GROUPS 5
|
||||||
|
|
||||||
struct JA_Sound_t
|
struct JA_Sound_t
|
||||||
{
|
{
|
||||||
@@ -18,6 +19,7 @@ struct JA_Channel_t
|
|||||||
JA_Sound_t *sound { nullptr };
|
JA_Sound_t *sound { nullptr };
|
||||||
int pos { 0 };
|
int pos { 0 };
|
||||||
int times { 0 };
|
int times { 0 };
|
||||||
|
int group { 0 };
|
||||||
SDL_AudioStream *stream { nullptr };
|
SDL_AudioStream *stream { nullptr };
|
||||||
JA_Channel_state state { JA_CHANNEL_FREE };
|
JA_Channel_state state { JA_CHANNEL_FREE };
|
||||||
};
|
};
|
||||||
@@ -40,7 +42,7 @@ JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS];
|
|||||||
|
|
||||||
SDL_AudioSpec JA_audioSpec { SDL_AUDIO_S16, 2, 48000 };
|
SDL_AudioSpec JA_audioSpec { SDL_AUDIO_S16, 2, 48000 };
|
||||||
float JA_musicVolume { 1.0f };
|
float JA_musicVolume { 1.0f };
|
||||||
float JA_soundVolume { 0.5f };
|
float JA_soundVolume[JA_MAX_GROUPS];
|
||||||
bool JA_musicEnabled { true };
|
bool JA_musicEnabled { true };
|
||||||
bool JA_soundEnabled { true };
|
bool JA_soundEnabled { true };
|
||||||
SDL_AudioDeviceID sdlAudioDevice { 0 };
|
SDL_AudioDeviceID sdlAudioDevice { 0 };
|
||||||
@@ -51,43 +53,6 @@ int fade_start_time;
|
|||||||
int fade_duration;
|
int fade_duration;
|
||||||
int fade_initial_volume;
|
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)
|
Uint32 JA_UpdateCallback(void *userdata, SDL_TimerID timerID, Uint32 interval)
|
||||||
{
|
{
|
||||||
@@ -153,7 +118,8 @@ void JA_Init(const int freq, const SDL_AudioFormat format, const int num_channel
|
|||||||
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, &JA_audioSpec);
|
||||||
SDL_Log( (sdlAudioDevice==0) ? "Failed to initialize SDL audio!\n" : "OK!\n");
|
SDL_Log( (sdlAudioDevice==0) ? "Failed to initialize SDL audio!\n" : "OK!\n");
|
||||||
for (int i=0;i<JA_MAX_SIMULTANEOUS_CHANNELS;++i) channels[i].state = JA_CHANNEL_FREE;
|
for (int i=0; i<JA_MAX_SIMULTANEOUS_CHANNELS; ++i) channels[i].state = JA_CHANNEL_FREE;
|
||||||
|
for (int i=0; i<JA_MAX_GROUPS; ++i) JA_soundVolume[i] = 0.5f;
|
||||||
//SDL_PauseAudioDevice(sdlAudioDevice);
|
//SDL_PauseAudioDevice(sdlAudioDevice);
|
||||||
JA_timerID = SDL_AddTimer(30, JA_UpdateCallback, nullptr);
|
JA_timerID = SDL_AddTimer(30, JA_UpdateCallback, nullptr);
|
||||||
}
|
}
|
||||||
@@ -345,7 +311,7 @@ JA_Sound_t *JA_LoadSound(const char* filename)
|
|||||||
return sound;
|
return sound;
|
||||||
}
|
}
|
||||||
|
|
||||||
int JA_PlaySound(JA_Sound_t *sound, const int loop)
|
int JA_PlaySound(JA_Sound_t *sound, const int loop, const int group)
|
||||||
{
|
{
|
||||||
if (!JA_soundEnabled) return -1;
|
if (!JA_soundEnabled) return -1;
|
||||||
|
|
||||||
@@ -360,13 +326,13 @@ int JA_PlaySound(JA_Sound_t *sound, const int loop)
|
|||||||
channels[channel].state = JA_CHANNEL_PLAYING;
|
channels[channel].state = JA_CHANNEL_PLAYING;
|
||||||
channels[channel].stream = SDL_CreateAudioStream(&channels[channel].sound->spec, &JA_audioSpec);
|
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_PutAudioStreamData(channels[channel].stream, channels[channel].sound->buffer, channels[channel].sound->length);
|
||||||
SDL_SetAudioStreamGain(channels[channel].stream, JA_soundVolume);
|
SDL_SetAudioStreamGain(channels[channel].stream, JA_soundVolume[group]);
|
||||||
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream);
|
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream);
|
||||||
|
|
||||||
return channel;
|
return channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop)
|
int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop, const int group)
|
||||||
{
|
{
|
||||||
if (!JA_soundEnabled) return -1;
|
if (!JA_soundEnabled) return -1;
|
||||||
|
|
||||||
@@ -379,7 +345,7 @@ int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop)
|
|||||||
channels[channel].state = JA_CHANNEL_PLAYING;
|
channels[channel].state = JA_CHANNEL_PLAYING;
|
||||||
channels[channel].stream = SDL_CreateAudioStream(&channels[channel].sound->spec, &JA_audioSpec);
|
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_PutAudioStreamData(channels[channel].stream, channels[channel].sound->buffer, channels[channel].sound->length);
|
||||||
SDL_SetAudioStreamGain(channels[channel].stream, JA_soundVolume);
|
SDL_SetAudioStreamGain(channels[channel].stream, JA_soundVolume[group]);
|
||||||
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream);
|
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream);
|
||||||
|
|
||||||
return channel;
|
return channel;
|
||||||
@@ -477,15 +443,19 @@ JA_Channel_state JA_GetChannelState(const int channel)
|
|||||||
return channels[channel].state;
|
return channels[channel].state;
|
||||||
}
|
}
|
||||||
|
|
||||||
float JA_SetSoundVolume(float volume)
|
float JA_SetSoundVolume(float volume, const int group)
|
||||||
{
|
{
|
||||||
JA_soundVolume = SDL_clamp( volume, 0.0f, 1.0f );
|
const float v = SDL_clamp( volume, 0.0f, 1.0f );
|
||||||
|
for (int i = 0; i < JA_MAX_GROUPS; ++i) {
|
||||||
|
if (group==-1 || group==i) JA_soundVolume[i]=v;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
|
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
|
||||||
if ( (channels[i].state == JA_CHANNEL_PLAYING) || (channels[i].state == JA_CHANNEL_PAUSED) )
|
if ( ((channels[i].state == JA_CHANNEL_PLAYING) || (channels[i].state == JA_CHANNEL_PAUSED)) &&
|
||||||
SDL_SetAudioStreamGain(channels[i].stream, JA_soundVolume);
|
((group==-1) || (channels[i].group==group)) )
|
||||||
|
SDL_SetAudioStreamGain(channels[i].stream, JA_soundVolume[i]);
|
||||||
|
|
||||||
return JA_soundVolume;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
void JA_EnableSound(const bool value)
|
void JA_EnableSound(const bool value)
|
||||||
|
|||||||
@@ -28,14 +28,14 @@ void JA_EnableMusic(const bool value);
|
|||||||
JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length);
|
JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length);
|
||||||
JA_Sound_t *JA_LoadSound(Uint8* buffer, Uint32 length);
|
JA_Sound_t *JA_LoadSound(Uint8* buffer, Uint32 length);
|
||||||
JA_Sound_t *JA_LoadSound(const char* filename);
|
JA_Sound_t *JA_LoadSound(const char* filename);
|
||||||
int JA_PlaySound(JA_Sound_t *sound, const int loop = 0);
|
int JA_PlaySound(JA_Sound_t *sound, const int loop = 0, const int group=0);
|
||||||
int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop = 0);
|
int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop = 0, const int group=0);
|
||||||
void JA_PauseChannel(const int channel);
|
void JA_PauseChannel(const int channel);
|
||||||
void JA_ResumeChannel(const int channel);
|
void JA_ResumeChannel(const int channel);
|
||||||
void JA_StopChannel(const int channel);
|
void JA_StopChannel(const int channel);
|
||||||
JA_Channel_state JA_GetChannelState(const int channel);
|
JA_Channel_state JA_GetChannelState(const int channel);
|
||||||
void JA_DeleteSound(JA_Sound_t *sound);
|
void JA_DeleteSound(JA_Sound_t *sound);
|
||||||
float JA_SetSoundVolume(float volume);
|
float JA_SetSoundVolume(float volume, const int group=0);
|
||||||
void JA_EnableSound(const bool value);
|
void JA_EnableSound(const bool value);
|
||||||
|
|
||||||
float JA_SetVolume(float volume);
|
float JA_SetVolume(float volume);
|
||||||
|
|||||||
Reference in New Issue
Block a user