- [NEW] int JA_SetChannelVolume(const int channel, int volume)

This commit is contained in:
2022-11-24 15:40:02 +01:00
parent 6d8921b160
commit cc7ef78d59
2 changed files with 12 additions and 3 deletions

View File

@@ -14,6 +14,7 @@ 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 };
}; };
@@ -55,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, JA_volume/2); SDL_MixAudioFormat(stream, channels[i].sound->buffer + channels[i].pos, AUDIO_S16, size, channels[i].volume);
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, JA_volume/2); SDL_MixAudioFormat(stream + size, channels[i].sound->buffer, AUDIO_S16, len-size, channels[i].volume);
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 {
@@ -243,6 +244,12 @@ JA_Channel_state JA_GetChannelState(const int channel) {
return channels[channel].state; return channels[channel].state;
} }
int JA_SetChannelVolume(const int channel, int volume) {
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return JA_CHANNEL_INVALID;
channels[channel].volume = volume > 128 ? 128 : volume < 0 ? 0 : volume;
return channels[channel].volume;
}
int JA_SetVolume(int volume) { int JA_SetVolume(int volume) {
JA_volume = volume > 128 ? 128 : volume < 0 ? 0 : volume; JA_volume = volume > 128 ? 128 : volume < 0 ? 0 : volume;
return JA_volume; return JA_volume;

View File

@@ -25,6 +25,8 @@ 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);
void JA_DeleteSound(JA_Sound sound); void JA_DeleteSound(JA_Sound sound);
int JA_SetVolume(int volume); int JA_SetVolume(int volume);