Import from neverball-1.3.1.tar.gz
[neverball-archive.git] / putt / audio.c
blobbd0415219cb1aac1fbb746b501a7203e8fb2dbcf
1 /*
2 * Copyright (C) 2003 Robert Kooima
4 * NEVERPUTT is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
15 #include <SDL.h>
16 #include <SDL_mixer.h>
18 #include "config.h"
19 #include "audio.h"
21 /*---------------------------------------------------------------------------*/
23 static int audio_state = 0;
25 static Mix_Music *song;
26 static Mix_Chunk *buff[AUD_COUNT];
27 static int chan[AUD_COUNT];
29 #define CH_MENU 0
30 #define CH_TICK 1
31 #define CH_BUMP 2
32 #define CH_VOICE 3
33 #define CH_COUNT 4
35 /*---------------------------------------------------------------------------*/
37 static void chunk_load(int i, const char *filename, int channel)
39 buff[i] = Mix_LoadWAV(config_data(filename));
40 chan[i] = channel;
43 static void chunk_free(int i)
45 if (buff[i])
47 Mix_FreeChunk(buff[i]);
48 buff[i] = NULL;
52 /*---------------------------------------------------------------------------*/
54 void audio_init(void)
56 int r = config_get_d(CONFIG_AUDIO_RATE);
57 int b = config_get_d(CONFIG_AUDIO_BUFF);
59 if (audio_state == 0)
61 if (Mix_OpenAudio(r, MIX_DEFAULT_FORMAT, 1, b) == 0)
63 chunk_load(AUD_BIRDIE, "snd/birdie.ogg", CH_VOICE);
64 chunk_load(AUD_BOGEY, "snd/bogey.ogg", CH_VOICE);
65 chunk_load(AUD_BUMP, "snd/bink.wav", CH_VOICE);
66 chunk_load(AUD_DOUBLE, "snd/double.ogg", CH_VOICE);
67 chunk_load(AUD_EAGLE, "snd/eagle.ogg", CH_VOICE);
68 chunk_load(AUD_JUMP, "snd/jump.ogg", CH_BUMP);
69 chunk_load(AUD_MENU, "snd/menu.wav", CH_MENU);
70 chunk_load(AUD_ONE, "snd/one.ogg", CH_VOICE);
71 chunk_load(AUD_PAR, "snd/par.ogg", CH_VOICE);
72 chunk_load(AUD_PENALTY, "snd/penalty.ogg", CH_VOICE);
73 chunk_load(AUD_PLAYER1, "snd/player1.ogg", CH_VOICE);
74 chunk_load(AUD_PLAYER2, "snd/player2.ogg", CH_VOICE);
75 chunk_load(AUD_PLAYER3, "snd/player3.ogg", CH_VOICE);
76 chunk_load(AUD_PLAYER4, "snd/player4.ogg", CH_VOICE);
77 chunk_load(AUD_SUCCESS, "snd/success.ogg", CH_VOICE);
79 audio_state = 1;
81 audio_volume(config_get_d(CONFIG_SOUND_VOLUME),
82 config_get_d(CONFIG_MUSIC_VOLUME));
84 else fprintf(stderr, "Sound disabled\n");
88 void audio_play(int i, float v)
90 if (audio_state == 1 && buff[i])
92 Mix_VolumeChunk(buff[i], (int) (v * MIX_MAX_VOLUME));
93 Mix_PlayChannel(chan[i], buff[i], 0);
97 void audio_free(void)
99 if (audio_state == 1)
101 Mix_CloseAudio();
103 chunk_free(AUD_SUCCESS);
104 chunk_free(AUD_PLAYER4);
105 chunk_free(AUD_PLAYER3);
106 chunk_free(AUD_PLAYER2);
107 chunk_free(AUD_PLAYER1);
108 chunk_free(AUD_PENALTY);
109 chunk_free(AUD_PAR);
110 chunk_free(AUD_ONE);
111 chunk_free(AUD_MENU);
112 chunk_free(AUD_JUMP);
113 chunk_free(AUD_EAGLE);
114 chunk_free(AUD_DOUBLE);
115 chunk_free(AUD_BUMP);
116 chunk_free(AUD_BOGEY);
117 chunk_free(AUD_BIRDIE);
119 audio_state = 0;
123 /*---------------------------------------------------------------------------*/
125 void audio_music_play(const char *filename)
127 if (audio_state)
129 audio_music_stop();
131 if ((config_get_d(CONFIG_MUSIC_VOLUME) > 0) &&
132 (song = Mix_LoadMUS(config_data(filename))))
133 Mix_PlayMusic(song, -1);
137 void audio_music_fade(float t)
139 if (audio_state && song && Mix_PlayingMusic())
140 Mix_FadeOutMusic((int) (t * 1000.f));
143 void audio_music_stop(void)
145 if (audio_state)
147 if (Mix_PlayingMusic())
148 Mix_HaltMusic();
150 if (song)
151 Mix_FreeMusic(song);
153 song = NULL;
157 void audio_volume(int s, int m)
159 if (audio_state)
161 Mix_Volume(-1, s * MIX_MAX_VOLUME / 10);
162 Mix_VolumeMusic(m * MIX_MAX_VOLUME / 10);
166 /*---------------------------------------------------------------------------*/