openalbridge/openalwrap.c
changeset 2216 82e7da49c26a
parent 2215 1d78579e06c2
child 2220 110266ba2ef7
equal deleted inserted replaced
2215:1d78579e06c2 2216:82e7da49c26a
    35 	/*Position of the source sound*/
    35 	/*Position of the source sound*/
    36 	ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };
    36 	ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };
    37 	/*Velocity of the source sound*/
    37 	/*Velocity of the source sound*/
    38 	ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };
    38 	ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };
    39 	
    39 	
       
    40 	ALint openalReady = AL_FALSE;
    40 	
    41 	
    41 	ALint openal_close(void) {
    42 	ALint openal_close(void) {
    42 		/*Stop all sounds, deallocate all memory and close OpenAL */
    43 		/*Stop all sounds, deallocate all memory and close OpenAL */
    43 		ALCcontext *context;
    44 		ALCcontext *context;
    44 		ALCdevice  *device;
    45 		ALCdevice  *device;
    45 		
    46 		
       
    47 		if(openalReady == AL_FALSE)
       
    48 		{
       
    49 			fprintf(stderr, "ERROR: OpenAL not initialized\n");
       
    50 			return AL_FALSE;
       
    51 		}
       
    52 
    46 		alSourceStopv	(globalsize, Sources);
    53 		alSourceStopv	(globalsize, Sources);
    47 		alDeleteSources (globalsize, Sources);
    54 		alDeleteSources (globalsize, Sources);
    48 		alDeleteBuffers (globalsize, Buffers);
    55 		alDeleteBuffers (globalsize, Buffers);
    49 		
    56 		
    50 		free(Sources);
    57 		free(Sources);
    54 		device  = alcGetContextsDevice(context);
    61 		device  = alcGetContextsDevice(context);
    55 		
    62 		
    56 		alcMakeContextCurrent(NULL);
    63 		alcMakeContextCurrent(NULL);
    57 		alcDestroyContext(context);
    64 		alcDestroyContext(context);
    58 		alcCloseDevice(device);
    65 		alcCloseDevice(device);
    59 		return AL_TRUE;
    66 
    60 	}
    67 		openalReady = AL_FALSE;
    61 	
    68 
       
    69 		return AL_TRUE;
       
    70 	}
       
    71 	
       
    72 	ALint openal_ready(void) {
       
    73 		return openalReady;
       
    74 	}
    62 	
    75 	
    63 	ALint openal_init(uint32_t memorysize) {	
    76 	ALint openal_init(uint32_t memorysize) {	
    64 		/*Initialize an OpenAL contex and allocate memory space for data and buffers*/
    77 		/*Initialize an OpenAL contex and allocate memory space for data and buffers*/
    65 		ALCcontext *context;
    78 		ALCcontext *context;
    66 		ALCdevice *device;
    79 		ALCdevice *device;
    70 		ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };
    83 		ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };
    71 		/*Velocity of the listener*/
    84 		/*Velocity of the listener*/
    72 		ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };
    85 		ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };
    73 		/*Orientation of the listener. (first 3 elements are "at", second 3 are "up")*/
    86 		/*Orientation of the listener. (first 3 elements are "at", second 3 are "up")*/
    74 		ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };
    87 		ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };
    75 		
    88 
       
    89 		if(openalReady == AL_TRUE)
       
    90 		{
       
    91 			fprintf(stderr, "ERROR: OpenAL already initialized\n");
       
    92 			return AL_FALSE;
       
    93 		}
       
    94 
    76 		default_device = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
    95 		default_device = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
    77 		fprintf(stderr, "Using default device: %s\n", default_device);
    96 		fprintf(stderr, "Using default device: %s\n", default_device);
    78 		
    97 		
    79 		if ((device = alcOpenDevice(default_device)) == NULL) {
    98 		if ((device = alcOpenDevice(default_device)) == NULL) {
    80 			fprintf(stderr, "ERROR: Failed to open sound device\n");
    99 			fprintf(stderr, "ERROR: Failed to open sound device\n");
   101 		alListenerfv(AL_ORIENTATION, ListenerOri);
   120 		alListenerfv(AL_ORIENTATION, ListenerOri);
   102 		
   121 		
   103 		if (AlGetError("ERROR %d: Setting Listener properties\n") != AL_TRUE)
   122 		if (AlGetError("ERROR %d: Setting Listener properties\n") != AL_TRUE)
   104 			return AL_FALSE;
   123 			return AL_FALSE;
   105 		
   124 		
       
   125 		openalReady = AL_TRUE;
       
   126 
   106 		alGetError();  /* clear any AL errors beforehand */
   127 		alGetError();  /* clear any AL errors beforehand */
   107 		return AL_TRUE;
   128 		return AL_TRUE;
   108 	}
   129 	}
   109 	
   130 	
   110 	
   131 	
   119 		
   140 		
   120 		return 0;
   141 		return 0;
   121 	}
   142 	}
   122 	
   143 	
   123 	
   144 	
   124 	uint32_t openal_loadfile (const char *filename){
   145 	int openal_loadfile (const char *filename){
   125 		/*Open a file, load into memory and allocate the Source buffer for playing*/
   146 		/*Open a file, load into memory and allocate the Source buffer for playing*/
   126 		ALenum format;
   147 		ALenum format;
   127 		ALsizei bitsize;
   148 		ALsizei bitsize;
   128 		ALsizei freq;
   149 		ALsizei freq;
   129 		char *data;
   150 		char *data;
   130 		uint32_t fileformat;
   151 		uint32_t fileformat;
   131 		ALenum error;
   152 		ALenum error;
   132 		FILE *fp;
   153 		FILE *fp;
   133 		
   154 		
       
   155 		if(openalReady == AL_FALSE)
       
   156 		{
       
   157 			fprintf(stderr, "ERROR: OpenAL not initialized\n");
       
   158 			return AL_FALSE;
       
   159 		}
   134 		
   160 		
   135 		/*when the buffers are all used, we can expand memory to accept new files*/
   161 		/*when the buffers are all used, we can expand memory to accept new files*/
   136 		if (globalindex == globalsize)
   162 		if (globalindex == globalsize)
   137 			helper_realloc();
   163 			helper_realloc();
   138 		
   164 		
   199 	
   225 	
   200 	ALint openal_toggleloop (uint32_t index){
   226 	ALint openal_toggleloop (uint32_t index){
   201 		/*Set or unset looping mode*/
   227 		/*Set or unset looping mode*/
   202 		ALint loop;
   228 		ALint loop;
   203 		
   229 		
       
   230 		if(openalReady == AL_FALSE)
       
   231 		{
       
   232 			fprintf(stderr, "ERROR: OpenAL not initialized\n");
       
   233 			return AL_FALSE;
       
   234 		}
       
   235 
   204 		if (index >= globalsize) {
   236 		if (index >= globalsize) {
   205 			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
   237 			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
   206 			return AL_FALSE;
   238 			return AL_FALSE;
   207 		}
   239 		}
   208 		
   240 		
   216 		return AL_TRUE;
   248 		return AL_TRUE;
   217 	}
   249 	}
   218 	
   250 	
   219 	
   251 	
   220 	ALint openal_setvolume (uint32_t index, uint8_t percentage) {
   252 	ALint openal_setvolume (uint32_t index, uint8_t percentage) {
       
   253 		if(openalReady == AL_FALSE)
       
   254 		{
       
   255 			fprintf(stderr, "ERROR: OpenAL not initialized\n");
       
   256 			return AL_FALSE;
       
   257 		}
       
   258 
   221 		/*Set volume for sound number index*/
   259 		/*Set volume for sound number index*/
   222 		if (index >= globalindex) {
   260 		if (index >= globalindex) {
   223 			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
   261 			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
   224 			return AL_FALSE;
   262 			return AL_FALSE;
   225 		}
   263 		}
   235 		return AL_TRUE;
   273 		return AL_TRUE;
   236 	}
   274 	}
   237 	
   275 	
   238 	
   276 	
   239 	ALint openal_setglobalvolume (uint8_t percentage) {
   277 	ALint openal_setglobalvolume (uint8_t percentage) {
       
   278 		if(openalReady == AL_FALSE)
       
   279 		{
       
   280 			fprintf(stderr, "ERROR: OpenAL not initialized\n");
       
   281 			return AL_FALSE;
       
   282 		}
       
   283 
   240 		/*Set volume for all sounds*/		
   284 		/*Set volume for all sounds*/		
   241 		if (percentage > 100)
   285 		if (percentage > 100)
   242 			percentage = 100;
   286 			percentage = 100;
   243 		alListenerf (AL_GAIN, (float) percentage/100.0f);
   287 		alListenerf (AL_GAIN, (float) percentage/100.0f);
   244 		if (AlGetError("ERROR %d: Setting global volume\n") != AL_TRUE)
   288 		if (AlGetError("ERROR %d: Setting global volume\n") != AL_TRUE)
   252 	
   296 	
   253 	ALint openal_togglemute () {
   297 	ALint openal_togglemute () {
   254 		/*Mute or unmute sound*/
   298 		/*Mute or unmute sound*/
   255 		ALfloat mute;
   299 		ALfloat mute;
   256 		
   300 		
       
   301 		if(openalReady == AL_FALSE)
       
   302 		{
       
   303 			fprintf(stderr, "ERROR: OpenAL not initialized\n");
       
   304 			return AL_FALSE;
       
   305 		}
       
   306 
   257 		alGetListenerf (AL_GAIN, &mute);
   307 		alGetListenerf (AL_GAIN, &mute);
   258 		if (mute > 0) 
   308 		if (mute > 0) 
   259 			mute = 0;
   309 			mute = 0;
   260 		else
   310 		else
   261 			mute = 1.0;
   311 			mute = 1.0;
   277 		HANDLE Thread;
   327 		HANDLE Thread;
   278 		DWORD threadID;
   328 		DWORD threadID;
   279 #endif
   329 #endif
   280 		fade_t *fade;
   330 		fade_t *fade;
   281 		
   331 		
       
   332 		if(openalReady == AL_FALSE)
       
   333 		{
       
   334 			fprintf(stderr, "ERROR: OpenAL not initialized\n");
       
   335 			return AL_FALSE;
       
   336 		}
       
   337 
   282 		fade = (fade_t*) Malloc(sizeof(fade_t));
   338 		fade = (fade_t*) Malloc(sizeof(fade_t));
   283 		fade->index = index;
   339 		fade->index = index;
   284 		fade->quantity = quantity;
   340 		fade->quantity = quantity;
   285 		
   341 		
   286 		if (index >= globalindex) {
   342 		if (index >= globalindex) {
   329 		return openal_fade(index, quantity, FADE_IN);
   385 		return openal_fade(index, quantity, FADE_IN);
   330 	}
   386 	}
   331 
   387 
   332 	
   388 	
   333 	ALint openal_playsound(uint32_t index){
   389 	ALint openal_playsound(uint32_t index){
       
   390 		if(openalReady == AL_FALSE)
       
   391 		{
       
   392 			fprintf(stderr, "ERROR: OpenAL not initialized\n");
       
   393 			return AL_FALSE;
       
   394 		}
       
   395 
   334 		/*Play sound number index*/
   396 		/*Play sound number index*/
   335 		if (index >= globalindex) {
   397 		if (index >= globalindex) {
   336 			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
   398 			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
   337 			return AL_FALSE;
   399 			return AL_FALSE;
   338 		}
   400 		}
   345 		return AL_TRUE;
   407 		return AL_TRUE;
   346 	}
   408 	}
   347 	
   409 	
   348 	
   410 	
   349 	ALint openal_pausesound(uint32_t index){
   411 	ALint openal_pausesound(uint32_t index){
       
   412 		if(openalReady == AL_FALSE)
       
   413 		{
       
   414 			fprintf(stderr, "ERROR: OpenAL not initialized\n");
       
   415 			return AL_FALSE;
       
   416 		}
       
   417 
   350 		/*Pause sound number index*/
   418 		/*Pause sound number index*/
   351 		if (index >= globalindex) {
   419 		if (index >= globalindex) {
   352 			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
   420 			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
   353 			return AL_FALSE;
   421 			return AL_FALSE;
   354 		}
   422 		}
   359 		return AL_TRUE;
   427 		return AL_TRUE;
   360 	}
   428 	}
   361 	
   429 	
   362 	
   430 	
   363 	ALint openal_stopsound(uint32_t index){
   431 	ALint openal_stopsound(uint32_t index){
       
   432 		if(openalReady == AL_FALSE)
       
   433 		{
       
   434 			fprintf(stderr, "ERROR: OpenAL not initialized\n");
       
   435 			return AL_FALSE;
       
   436 		}
       
   437 
   364 		/*Stop sound number index*/
   438 		/*Stop sound number index*/
   365 		if (index >= globalindex) {
   439 		if (index >= globalindex) {
   366 			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
   440 			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
   367 			return AL_FALSE;
   441 			return AL_FALSE;
   368 		}
   442 		}