forked from JailDoctor/JailAudio
Afegit "int JA_SetVolume(int volume);"
This commit is contained in:
@@ -30,16 +30,17 @@ JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS];
|
||||
int JA_freq {48000};
|
||||
SDL_AudioFormat JA_format {AUDIO_S16};
|
||||
Uint8 JA_channels {2};
|
||||
int JA_volume = 128;
|
||||
|
||||
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_memcpy(stream, current_music->output+current_music->pos, size);
|
||||
SDL_MixAudioFormat(stream, (Uint8*)(current_music->output+current_music->pos), AUDIO_S16, size, JA_volume);
|
||||
current_music->pos += size/2;
|
||||
if (size < len) {
|
||||
if (current_music->times != 0) {
|
||||
SDL_memcpy(stream+size, current_music->output, len-size);
|
||||
SDL_MixAudioFormat(stream+size, (Uint8*)current_music->output, AUDIO_S16, len-size, JA_volume);
|
||||
current_music->pos = (len-size)/2;
|
||||
if (current_music->times > 0) current_music->times--;
|
||||
} else {
|
||||
@@ -52,11 +53,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, 64);
|
||||
SDL_MixAudioFormat(stream, channels[i].sound->buffer + channels[i].pos, AUDIO_S16, size, JA_volume/2);
|
||||
channels[i].pos += size;
|
||||
if (size < len) {
|
||||
if (channels[i].times != 0) {
|
||||
SDL_MixAudioFormat(stream + size, channels[i].sound->buffer, AUDIO_S16, len-size, 64);
|
||||
SDL_MixAudioFormat(stream + size, channels[i].sound->buffer, AUDIO_S16, len-size, JA_volume/2);
|
||||
channels[i].pos = len-size;
|
||||
if (channels[i].times > 0) channels[i].times--;
|
||||
} else {
|
||||
@@ -218,3 +219,7 @@ JA_Channel_state JA_GetChannelState(const int channel) {
|
||||
return channels[channel].state;
|
||||
}
|
||||
|
||||
int JA_SetVolume(int volume) {
|
||||
JA_volume = volume > 128 ? 128 : volume < 0 ? 0 : volume;
|
||||
return JA_volume;
|
||||
}
|
||||
@@ -25,3 +25,5 @@ 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 sound);
|
||||
|
||||
int JA_SetVolume(int volume);
|
||||
|
||||
8
main.cpp
8
main.cpp
@@ -16,7 +16,7 @@ int main(int argc, char **argv) {
|
||||
int channel = -1;
|
||||
|
||||
JA_PlayMusic(music, true);
|
||||
|
||||
int volume = 128;
|
||||
bool should_exit = false;
|
||||
while(!should_exit) {
|
||||
while(SDL_PollEvent(&event)) {
|
||||
@@ -41,6 +41,12 @@ int main(int argc, char **argv) {
|
||||
case SDL_SCANCODE_6: // Si pulsem la tecla '6' stopem definitivament el wav infinit
|
||||
JA_StopChannel(channel);
|
||||
break;
|
||||
case SDL_SCANCODE_UP:
|
||||
volume = JA_SetVolume(volume+16);
|
||||
break;
|
||||
case SDL_SCANCODE_DOWN:
|
||||
volume = JA_SetVolume(volume-16);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user