- [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;
int pos {0};
int times {0};
int volume {64};
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++) {
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_volume/2);
SDL_MixAudioFormat(stream, channels[i].sound->buffer + channels[i].pos, AUDIO_S16, size, channels[i].volume);
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_volume/2);
SDL_MixAudioFormat(stream + size, channels[i].sound->buffer, AUDIO_S16, len-size, channels[i].volume);
channels[i].pos = len-size;
if (channels[i].times > 0) channels[i].times--;
} else {
@@ -243,7 +244,13 @@ JA_Channel_state JA_GetChannelState(const int channel) {
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) {
JA_volume = volume > 128 ? 128 : volume < 0 ? 0 : volume;
return JA_volume;
}
}