aboutsummaryrefslogtreecommitdiff
path: root/src/sdl/audio.c
blob: ec09050f9821686f5ceb3097b0958e867dd82c13 (plain)
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
#include "audio.h"
#include <SDL/SDL.h>

int music_volume = 4;

void PHL_AudioInit()
{
    SDL_InitSubSystem(SDL_INIT_AUDIO);
    Mix_Init(0);
    Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024);
}

void PHL_AudioClose()
{
    Mix_CloseAudio();
    Mix_Quit();
}

//Same as PHL_LoadSound, but expects a file name without extension
PHL_Music PHL_LoadMusic(char* fname, int loop)
{
    PHL_Music ret;
    ret.loop = loop;
    char buff[4096];
    sprintf(buff, "data/%s.ogg", fname);
    ret.snd = Mix_LoadMUS(buff);
    return ret;
}

PHL_Sound PHL_LoadSound(char* fname)
{
    char buff[4096];
    sprintf(buff, "data/%s", fname);
    return Mix_LoadWAV(buff);
}

void PHL_MusicVolume(float vol)
{
    Mix_VolumeMusic(SDL_MIX_MAXVOLUME*vol);
}

void PHL_PlayMusic(PHL_Music snd)
{
    if(snd.snd)
        Mix_PlayMusic(snd.snd, snd.loop ? -1 : 0);
}

void PHL_PlaySound(PHL_Sound snd, int channel)
{
    Mix_PlayChannel(channel, snd, 0);
}

void PHL_StopMusic()
{
    Mix_HaltMusic();
}

void PHL_StopSound(PHL_Sound snd, int channel)
{
    Mix_HaltChannel(channel);
}

void PHL_FreeMusic(PHL_Music snd)
{
    if(snd.snd)
        Mix_FreeMusic(snd.snd);
    snd.snd = NULL;
}

void PHL_FreeSound(PHL_Sound snd)
{
    Mix_FreeChunk(snd);
}