3 Commits

Author SHA1 Message Date
bf1925bb3a Update 'readme.md' 2022-11-24 16:12:43 +01:00
861bb5e7f9 Merge branch 'master' of https://gitea.sustancia.synology.me/JailDoctor/JailAudio 2022-11-24 16:10:36 +01:00
debd80f6af - Removed volume per channel
- [NEW] JA_SetSoundVolume sets volume for all sound effects
- [NEW] JA_SetMusicVolume sets volume for the music
- JA_SetVolume works as always: sets volume for both, with sounds volume halved.
2022-11-24 16:10:33 +01:00
3 changed files with 26 additions and 16 deletions

View File

@@ -14,7 +14,6 @@ struct JA_Channel_t {
JA_Sound sound; JA_Sound sound;
int pos {0}; int pos {0};
int times {0}; int times {0};
int volume {64};
JA_Channel_state state { JA_CHANNEL_FREE }; JA_Channel_state state { JA_CHANNEL_FREE };
}; };
@@ -32,18 +31,19 @@ JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS];
int JA_freq {48000}; int JA_freq {48000};
SDL_AudioFormat JA_format {AUDIO_S16}; SDL_AudioFormat JA_format {AUDIO_S16};
Uint8 JA_channels {2}; Uint8 JA_channels {2};
int JA_volume = 128; int JA_musicvolume = 128;
int JA_soundvolume = 64;
SDL_AudioDeviceID sdlAudioDevice = 0; SDL_AudioDeviceID sdlAudioDevice = 0;
void audioCallback(void * userdata, uint8_t * stream, int len) { void audioCallback(void * userdata, uint8_t * stream, int len) {
SDL_memset(stream, 0, len); SDL_memset(stream, 0, len);
if (current_music != NULL && current_music->state == JA_MUSIC_PLAYING) { if (current_music != NULL && current_music->state == JA_MUSIC_PLAYING) {
const int size = SDL_min(len, current_music->samples*2-current_music->pos); 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_volume); SDL_MixAudioFormat(stream, (Uint8*)(current_music->output+current_music->pos), AUDIO_S16, size, JA_musicvolume);
current_music->pos += size/2; current_music->pos += size/2;
if (size < len) { if (size < len) {
if (current_music->times != 0) { if (current_music->times != 0) {
SDL_MixAudioFormat(stream+size, (Uint8*)current_music->output, AUDIO_S16, len-size, JA_volume); SDL_MixAudioFormat(stream+size, (Uint8*)current_music->output, AUDIO_S16, len-size, JA_musicvolume);
current_music->pos = (len-size)/2; current_music->pos = (len-size)/2;
if (current_music->times > 0) current_music->times--; if (current_music->times > 0) current_music->times--;
} else { } else {
@@ -56,11 +56,11 @@ void audioCallback(void * userdata, uint8_t * stream, int len) {
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) { if (channels[i].state == JA_CHANNEL_PLAYING) {
const int size = SDL_min(len, channels[i].sound->length - channels[i].pos); 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, channels[i].volume); SDL_MixAudioFormat(stream, channels[i].sound->buffer + channels[i].pos, AUDIO_S16, size, JA_soundvolume);
channels[i].pos += size; channels[i].pos += size;
if (size < len) { if (size < len) {
if (channels[i].times != 0) { if (channels[i].times != 0) {
SDL_MixAudioFormat(stream + size, channels[i].sound->buffer, AUDIO_S16, len-size, channels[i].volume); SDL_MixAudioFormat(stream + size, channels[i].sound->buffer, AUDIO_S16, len-size, JA_soundvolume);
channels[i].pos = len-size; channels[i].pos = len-size;
if (channels[i].times > 0) channels[i].times--; if (channels[i].times > 0) channels[i].times--;
} else { } else {
@@ -244,13 +244,18 @@ JA_Channel_state JA_GetChannelState(const int channel) {
return channels[channel].state; return channels[channel].state;
} }
int JA_SetChannelVolume(const int channel, int volume) { int JA_SetSoundVolume(int volume) {
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return JA_CHANNEL_INVALID; JA_soundvolume = volume > 128 ? 128 : volume < 0 ? 0 : volume;
channels[channel].volume = volume > 128 ? 128 : volume < 0 ? 0 : volume; return JA_soundvolume;
return channels[channel].volume; }
int JA_SetMusicVolume(int volume) {
JA_musicvolume = volume > 128 ? 128 : volume < 0 ? 0 : volume;
return JA_musicvolume;
} }
int JA_SetVolume(int volume) { int JA_SetVolume(int volume) {
JA_volume = volume > 128 ? 128 : volume < 0 ? 0 : volume; JA_musicvolume = volume > 128 ? 128 : volume < 0 ? 0 : volume;
return JA_volume; JA_soundvolume = JA_musicvolume/2;
return JA_musicvolume;
} }

View File

@@ -16,6 +16,7 @@ void JA_PauseMusic();
void JA_ResumeMusic(); void JA_ResumeMusic();
void JA_StopMusic(); void JA_StopMusic();
int JA_SetVolume(int volume); int JA_SetVolume(int volume);
int JA_SetMusicVolume(int volume);
JA_Music_state JA_GetMusicState(); JA_Music_state JA_GetMusicState();
void JA_DeleteMusic(JA_Music music); void JA_DeleteMusic(JA_Music music);
@@ -23,10 +24,10 @@ JA_Sound JA_NewSound(Uint8* buffer, Uint32 length);
JA_Sound JA_LoadSound(const char* filename); JA_Sound JA_LoadSound(const char* filename);
int JA_PlaySound(JA_Sound sound, const int loop = 0); int JA_PlaySound(JA_Sound sound, const int loop = 0);
void JA_DeleteSound(JA_Sound sound); void JA_DeleteSound(JA_Sound sound);
int JA_SetSoundVolume(int volume);
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);
int JA_SetChannelVolume(const int channel, int volume);

View File

@@ -8,6 +8,9 @@ Inicialitza el subsistema de audio. Es pot especificar la _frequència_, _format
### `void JA_Quit();` ### `void JA_Quit();`
Tanquem la paraeta. Tanquem la paraeta.
### `int JA_SetVolume(int volume);`
Canvia el volum general. Es un valor entre 0 i 128. Per defecte està a 128. La música al 100%, el só al 50%.
## Música ## Música
### `JA_Music JA_LoadMusic(const char* filename);` ### `JA_Music JA_LoadMusic(const char* filename);`
@@ -25,7 +28,7 @@ Continúa la música per on shavia pausat.
### `void JA_StopMusic();` ### `void JA_StopMusic();`
Pos això... Pos això...
### `int JA_SetVolume(int volume);` ### `int JA_SetMusicVolume(int volume);`
Canvia el volum de la música. Es un valor entre 0 i 128. Per defecte està a 128. Canvia el volum de la música. Es un valor entre 0 i 128. Per defecte està a 128.
### `JA_Music_state JA_GetMusicState();` ### `JA_Music_state JA_GetMusicState();`
@@ -48,6 +51,9 @@ Fa sonar un só prèviament carregat. Se li pot dir que faça loop (`-1` fa loop
### `void JA_DeleteSound(JA_Sound sound);` ### `void JA_DeleteSound(JA_Sound sound);`
Esborra de la memòria un só prèviament carregat. Si el só estava sonant en eixe moment, pos el para, ningún problema. Esborra de la memòria un só prèviament carregat. Si el só estava sonant en eixe moment, pos el para, ningún problema.
### `int JA_SetSoundVolume(int volume);`
Especifica el volum dels efecte de só. Es un valor entre 0 i 128. Per defecte està a 64.
## Canals de só ## Canals de só
### `void JA_PauseChannel(const int channel);` ### `void JA_PauseChannel(const int channel);`
@@ -62,5 +68,3 @@ Para un canal i el deixa lliure. Si s'especifica `-1`, para tots els canals que
### `JA_Channel_state JA_GetChannelState(const int channel);` ### `JA_Channel_state JA_GetChannelState(const int channel);`
Torna el estat del canal especificat, que pot ser `JA_CHANNEL_INVALID`, `JA_CHANNEL_FREE`,`JA_CHANNEL_PLAYING` o `JA_CHANNEL_PAUSED`. Torna el estat del canal especificat, que pot ser `JA_CHANNEL_INVALID`, `JA_CHANNEL_FREE`,`JA_CHANNEL_PLAYING` o `JA_CHANNEL_PAUSED`.
### `int JA_SetChannelVolume(const int channel, int volume);`
Especifica el volum de un canal. Es un valor entre 0 i 128. Per defecte està a 64.