-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaudio_sdl2mixer_ext.cpp
78 lines (65 loc) · 2.11 KB
/
audio_sdl2mixer_ext.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifdef HAVE_SDL2_MIXER_EXT
#include <malloc.h>
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer_ext.h>
#include "engine.h"
#include "games.h"
extern "C" {
void audio_sdl2_init() {
SDL_Init(SDL_INIT_AUDIO);
Mix_Init(MIX_INIT_OGG);
Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 1024);
Mix_AllocateChannels(52);
}
void audio_sdl2_pause() {
Mix_PauseMusicStreamAll();
}
void audio_sdl2_resume() {
Mix_ResumeMusicStreamAll();
}
void *audio_sdl2_track_play(const char *fname, int looping, float vol, int *handle) {
char path[256];
sprintf(path, "%s/%s.wav.ogg", AUDIO_FOLDER, fname);
Mix_Music *res = Mix_LoadMUS(path);
Mix_VolumeMusicStream(res, (int)(vol * config.music_volume * 128.0f));
Mix_PlayMusicStream(res, looping ? -1 : 1);
*handle = (int)res;
return res;
}
void *audio_sdl2_voice_track_play(const char *fname, int looping, float vol, int *handle) {
char path[256];
sprintf(path, "%s/%s.wav.ogg", AUDIO_FOLDER, fname);
Mix_Music *res = Mix_LoadMUS(path);
Mix_VolumeMusicStream(res, (int)(vol * 128.0f));
Mix_PlayMusicStream(res, looping ? -1 : 1);
*handle = (int)res;
return res;
}
void audio_sdl2_track_stop(void *s, int handle) {
Mix_HaltMusicStream((Mix_Music *)s);
Mix_FreeMusic((Mix_Music *)s);
}
void audio_sdl2_track_set_volume(int h, float vol) {
Mix_VolumeMusicStream((Mix_Music *)h, (int)(vol * config.music_volume * 128.0f));
}
float audio_sdl2_track_fade(int h, float volume_start, float volume_end, uint32_t time_start, uint32_t time_end) {
float volume_delta = volume_end - volume_start;
float time_delta = time_end - time_start;
float delta = cur_delta - time_start;
float new_volume = volume_start + (delta / time_delta) * volume_delta;
Mix_VolumeMusicStream((Mix_Music *)h, (int)(new_volume * config.music_volume * 128.0f));
return new_volume;
}
void *audio_sdl2_sound_load(const char *fname) {
return Mix_LoadWAV(fname);
}
void audio_sdl2_sound_play(void *s) {
Mix_VolumeChunk((Mix_Chunk *)s, (int)(config.master_volume * 128.0f));
Mix_PlayChannel(-1, (Mix_Chunk *)s, 0);
}
void audio_sdl2_set_global_volume(float vol) {
Mix_VolumeMusicGeneral((int)(vol * 128.0f));
}
}
#endif