author | koda |
Thu, 15 Oct 2009 17:51:35 +0000 | |
changeset 2458 | 14296bd366d2 |
parent 2454 | c8b1fb10003c |
child 2468 | 0b62498c201a |
permissions | -rw-r--r-- |
2445 | 1 |
/* |
2 |
* OpenAL Bridge - a simple portable library for OpenAL interface |
|
3 |
* Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>, |
|
4 |
* Mario Liebisch <mario.liebisch+hw@googlemail.com> |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU Lesser General Public License as published by |
|
8 |
* the Free Software Foundation; version 2 of the License |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU Lesser General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU Lesser General Public License |
|
16 |
* along with this program; if not, write to the Free Software |
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
18 |
*/ |
|
19 |
||
2446 | 20 |
#include "ssound.h" |
21 |
#include "loaders.h" |
|
2445 | 22 |
|
23 |
extern ALuint sources[MAX_SOURCES]; |
|
24 |
extern const ALfloat NV[3]; |
|
25 |
extern const ALfloat LO[6]; |
|
26 |
||
2446 | 27 |
extern char *prog; |
2445 | 28 |
|
29 |
char SSound_load (SSound_t* pSound, const char* cFilename) { |
|
30 |
uint32_t magic; |
|
31 |
ALenum format; |
|
32 |
ALsizei bitsize, freq; |
|
33 |
char *data; |
|
34 |
FILE* fp; |
|
35 |
||
36 |
snprintf(pSound->Filename, 256, "%s", cFilename); |
|
37 |
pSound->source = -1; |
|
38 |
alGenBuffers(1, &pSound->Buffer); |
|
39 |
||
40 |
if(alGetError() != AL_NO_ERROR) { |
|
41 |
fprintf(stderr, "CSound: Couldn't create buffer.\n"); |
|
42 |
return 0; |
|
43 |
} |
|
44 |
||
45 |
fp = fopen(pSound->Filename, "rb"); |
|
46 |
||
47 |
if(!fp) { |
|
48 |
fprintf(stderr, "CSound: Couldn't open file for reading.\n"); |
|
49 |
return 0; |
|
50 |
} |
|
51 |
||
52 |
if(fread(&magic, sizeof(uint32_t), 1, fp) < 1) |
|
53 |
{ |
|
54 |
fclose(fp); |
|
55 |
fprintf(stderr, "CSound: Couldn't read file header.\n"); |
|
56 |
return 0; |
|
57 |
} |
|
58 |
fclose(fp); |
|
59 |
||
60 |
switch (ENDIAN_BIG_32(magic)) { |
|
61 |
case OGG_FILE_FORMAT: |
|
62 |
load_oggvorbis (pSound->Filename, &format, &data, &bitsize, &freq); |
|
63 |
break; |
|
64 |
case WAV_FILE_FORMAT: |
|
65 |
load_wavpcm (pSound->Filename, &format, &data, &bitsize, &freq); |
|
66 |
break; |
|
67 |
default: |
|
68 |
errno = EINVAL; |
|
69 |
err_ret ("(%s) ERROR - File format (%08X) not supported", prog, ENDIAN_BIG_32(magic)); |
|
70 |
return 0; |
|
71 |
break; |
|
72 |
} |
|
73 |
||
74 |
alBufferData(pSound->Buffer, format, data, bitsize, freq); |
|
75 |
if(alGetError() != AL_NO_ERROR) |
|
76 |
{ |
|
77 |
fprintf(stderr, "CSound: Couldn't write buffer data.\n"); |
|
78 |
return 0; |
|
79 |
} |
|
80 |
free(data); |
|
81 |
||
82 |
return 1; |
|
83 |
} |
|
84 |
||
85 |
void SSound_close(SSound_t* pSound) |
|
86 |
{ |
|
87 |
SSound_stop(pSound); |
|
88 |
alDeleteBuffers(1, &pSound->Buffer); |
|
2454
c8b1fb10003c
fix a potential bug when playing two sounds at the same time (or very near)
koda
parents:
2446
diff
changeset
|
89 |
return; |
2445 | 90 |
} |
91 |
||
92 |
void SSound_play(SSound_t* pSound, const char bLoop) { |
|
93 |
int i; |
|
94 |
||
95 |
if(pSound->source == -1) // need a new source |
|
96 |
{ |
|
97 |
int i; |
|
98 |
for(i = 0; i < MAX_SOURCES; i++) |
|
99 |
{ |
|
100 |
ALint state; |
|
101 |
alGetSourcei(sources[i], AL_SOURCE_STATE, &state); |
|
102 |
if(state != AL_PLAYING && state != AL_PAUSED) |
|
103 |
{ |
|
104 |
#ifdef DEBUG |
|
105 |
printf("using source %d (state 0x%x) for buffer.\n", i, state); |
|
106 |
#endif |
|
107 |
alSourceStop(sources[pSound->source]); |
|
108 |
alGetError(); |
|
109 |
break; |
|
110 |
} |
|
111 |
} |
|
112 |
if(i == MAX_SOURCES) // no available source found; skip |
|
113 |
{ |
|
114 |
#ifdef DEBUG |
|
115 |
printf("no source to play buffer %d!\n", i); |
|
116 |
#endif |
|
117 |
return; |
|
118 |
} |
|
119 |
pSound->source = i; |
|
120 |
} |
|
121 |
else // reuse already playing source |
|
122 |
{ |
|
123 |
alSourceStop(sources[pSound->source]); |
|
124 |
} |
|
125 |
alSourcei (sources[pSound->source], AL_BUFFER, pSound->Buffer); |
|
126 |
alSourcef (sources[pSound->source], AL_PITCH, 1.0f); |
|
127 |
alSourcef (sources[pSound->source], AL_GAIN, 1.0f); |
|
128 |
alSourcefv(sources[pSound->source], AL_POSITION, NV ); |
|
129 |
alSourcefv(sources[pSound->source], AL_VELOCITY, NV ); |
|
130 |
alSourcei (sources[pSound->source], AL_LOOPING, bLoop ); |
|
131 |
alSourcePlay(sources[pSound->source]); |
|
132 |
||
133 |
if((i = alGetError()) != AL_NO_ERROR) |
|
134 |
{ |
|
135 |
fprintf(stderr, "CSound: SourcePlay error 0x%4x in source %d\n", i, pSound->source); |
|
136 |
} |
|
137 |
#ifdef DEBUG |
|
138 |
fprintf(stderr, "play %s%s [%d]\n", pSound->Filename, bLoop ? " forever" : " once", pSound->source); |
|
139 |
#endif |
|
2454
c8b1fb10003c
fix a potential bug when playing two sounds at the same time (or very near)
koda
parents:
2446
diff
changeset
|
140 |
usleep(0); |
c8b1fb10003c
fix a potential bug when playing two sounds at the same time (or very near)
koda
parents:
2446
diff
changeset
|
141 |
return; |
2445 | 142 |
} |
143 |
||
144 |
void SSound_pause(const SSound_t* pSound) { |
|
145 |
if(pSound->source == -1) // not playing |
|
146 |
return; |
|
147 |
alSourcePause(sources[pSound->source]); |
|
148 |
#ifdef DEBUG |
|
149 |
fprintf(stderr, "pause %s\n", pSound->Filename); |
|
150 |
#endif |
|
2454
c8b1fb10003c
fix a potential bug when playing two sounds at the same time (or very near)
koda
parents:
2446
diff
changeset
|
151 |
return; |
2445 | 152 |
} |
153 |
||
154 |
void SSound_continue(const SSound_t* pSound) { |
|
155 |
if(pSound->source == -1) // not playing |
|
156 |
return; |
|
157 |
alSourcePlay(sources[pSound->source]); |
|
158 |
#ifdef DEBUG |
|
159 |
fprintf(stderr, "pause %s\n", pSound->Filename); |
|
160 |
#endif |
|
2454
c8b1fb10003c
fix a potential bug when playing two sounds at the same time (or very near)
koda
parents:
2446
diff
changeset
|
161 |
return; |
2445 | 162 |
} |
163 |
||
164 |
void SSound_stop(SSound_t* pSound) { |
|
165 |
if(pSound->source == -1) // not playing |
|
166 |
return; |
|
167 |
alSourceStop(sources[pSound->source]); |
|
168 |
pSound->source = -1; |
|
169 |
#ifdef DEBUG |
|
170 |
fprintf(stderr, "stop %s\n", pSound->Filename); |
|
171 |
#endif |
|
2454
c8b1fb10003c
fix a potential bug when playing two sounds at the same time (or very near)
koda
parents:
2446
diff
changeset
|
172 |
return; |
2445 | 173 |
} |
174 |
||
175 |
void SSound_volume(const SSound_t* pSound, const float fPercentage) { |
|
176 |
if(pSound->source == -1) // not playing |
|
177 |
return; |
|
178 |
alSourcef(sources[pSound->source], AL_GAIN, fPercentage); |
|
2454
c8b1fb10003c
fix a potential bug when playing two sounds at the same time (or very near)
koda
parents:
2446
diff
changeset
|
179 |
return; |
2445 | 180 |
} |