-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundmanager.c
148 lines (125 loc) · 2.93 KB
/
soundmanager.c
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* $Id: soundmanager.c 264 2005-11-06 17:43:44Z mrbrown $
*
* Copyright 2003 Kenta Cho. All rights reserved.
*/
/**
* BGM/SE manager(using SDL_mixer).
*
* @version $Revision: 264 $
*/
#include "SDL.h"
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include "SDL_mixer.h"
#include "soundmanager.h"
static int useAudio = 0;
#define MUSIC_NUM 3
static char *musicFileName[MUSIC_NUM] = {
"stg_a.ogg", "stg_b.ogg", "stg_c.ogg",
};
static Mix_Music *music[MUSIC_NUM];
#define CHUNK_NUM 16
static char *chunkFileName[CHUNK_NUM] = {
"laser_start.wav", "laser.wav", "damage.wav", "bomb.wav",
"destroied.wav", "explosion1.wav", "explosion2.wav", "miss.wav", "extend.wav",
"grz.wav", "grzinv.wav",
"shot.wav", "change.wav",
"reflec1.wav", "reflec2.wav", "ref_ready.wav",
};
static Mix_Chunk *chunk[CHUNK_NUM];
static int chunkChannel[CHUNK_NUM] = {
0, 1, 2, 3,
4, 5, 6, 7, 4,
6, 7,
6, 7,
7, 7, 7,
};
void closeSound() {
int i;
if ( !useAudio ) return;
if ( Mix_PlayingMusic() ) {
Mix_HaltMusic();
}
for ( i=0 ; i<MUSIC_NUM ; i++ ) {
if ( music[i] ) {
Mix_FreeMusic(music[i]);
}
}
for ( i=0 ; i<CHUNK_NUM ; i++ ) {
if ( chunk[i] ) {
Mix_FreeChunk(chunk[i]);
}
}
Mix_CloseAudio();
}
// Initialize the sound.
static void loadSounds() {
int i;
char name[32];
for ( i=0 ; i<MUSIC_NUM ; i++ ) {
strcpy(name, "sounds/");
strcat(name, musicFileName[i]);
if ( NULL == (music[i] = Mix_LoadMUS(name)) ) {
fprintf(stderr, "Couldn't load: %s\n", name);
useAudio = 0;
return;
}
}
for ( i=0 ; i<CHUNK_NUM ; i++ ) {
strcpy(name, "sounds/");
strcat(name, chunkFileName[i]);
if ( NULL == (chunk[i] = Mix_LoadWAV(name)) ) {
fprintf(stderr, "Couldn't load: %s\n", name);
useAudio = 0;
return;
}
}
}
void initSound() {
int audio_rate;
Uint16 audio_format;
int audio_channels;
int audio_buffers;
if ( SDL_InitSubSystem(SDL_INIT_AUDIO) < 0 ) {
fprintf(stderr, "Unable to initialize SDL_AUDIO: %s\n", SDL_GetError());
return;
}
audio_rate = 44100;
audio_format = AUDIO_S16;
audio_channels = 1;
audio_buffers = 4096;
if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
return;
} else {
Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
}
useAudio = 1;
loadSounds();
}
// Play/Stop the music/chunk.
void playMusic(int idx) {
if ( !useAudio ) return;
Mix_PlayMusic(music[idx], -1);
}
void fadeMusic() {
if ( !useAudio ) return;
Mix_FadeOutMusic(1280);
}
void stopMusic() {
if ( !useAudio ) return;
if ( Mix_PlayingMusic() ) {
Mix_HaltMusic();
}
}
void playChunk(int idx) {
if ( !useAudio ) return;
Mix_PlayChannel(chunkChannel[idx], chunk[idx], 0);
}
void haltChunk(int idx) {
if ( !useAudio ) return;
Mix_HaltChannel(chunkChannel[idx]);
}