openalbridge/wrappers.c
author koda
Tue, 30 Jun 2009 12:31:32 +0000
changeset 2213 bd51bbf06033
parent 2212 6b5da1a2765a
child 2216 82e7da49c26a
permissions -rw-r--r--
-Smaxx's porting of the library to MSVC compilers -library is now ANSI C -interface headers reworked
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     1
/*
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     2
 * OpenAL Bridge - a simple portable library for OpenAL interface
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     4
 *
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     8
 *
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    12
 * GNU General Public License for more details.
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    13
 *
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    17
 */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    18
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    19
#include "wrappers.h"
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    20
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    21
#ifdef __CPLUSPLUS
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    22
extern "C" {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    23
#endif 
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    24
	
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    25
	extern ALint *Sources;
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    26
	
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    27
	void *Malloc (size_t nbytes) {
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    28
		void *aptr;
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
    29
		if ((aptr = malloc(nbytes)) == NULL) {
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
    30
			fprintf(stderr, "ERROR: not enough memory! malloc() failed\n");
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
    31
			exit(-1);
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
    32
		}
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    33
		return aptr;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    34
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    35
	
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    36
	
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    37
	void *Realloc (void *aptr, size_t nbytes) {
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    38
#ifndef _WIN32
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    39
		aptr = reallocf(aptr, nbytes);
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    40
#else
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    41
		aptr = realloc(aptr, nbytes);
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    42
#endif
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    43
		if (aptr == NULL) {
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    44
			fprintf(stderr, "ERROR: not enough memory! realloc() failed\n");
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    45
#ifdef _WIN32
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    46
			free(aptr);
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    47
#endif
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    48
			exit(-1);
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    49
		}
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    50
		return aptr;
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    51
	}
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    52
	
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    53
	
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    54
	FILE *Fopen (const char *fname, char *mode)	{
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    55
		FILE *fp;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    56
		if ((fp=fopen(fname,mode)) == NULL)
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
    57
			fprintf (stderr, "ERROR: can't open file %s in mode '%s'\n", fname, mode);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    58
		return fp;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    59
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    60
	
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    61
	
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    62
	ALint AlGetError (const char *str) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    63
		ALenum error;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    64
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    65
		error = alGetError();
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    66
		if (error != AL_NO_ERROR) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    67
			fprintf(stderr, str, error);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    68
			return -2;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    69
		} else 
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    70
			return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    71
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    72
	
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    73
	
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    74
#ifndef _WIN32
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    75
	void *helper_fadein(void *tmp) 
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    76
#else
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    77
	void *helper_fadein(void *tmp) 
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    78
#endif
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    79
	{
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    80
		ALfloat gain;
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    81
		ALfloat target_gain;
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    82
		fade_t *fade;
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    83
		int index; 
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    84
		unsigned int quantity; 
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    85
		
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    86
		fade = tmp;
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    87
		index = fade->index;
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    88
		quantity = fade->quantity;
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    89
		free (fade);
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    90
		
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    91
#ifdef DEBUG
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    92
		fprintf(stderr, "Fade-out: index %d quantity %d\n", index, quantity);
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    93
#endif
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    94
		
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
    95
		/*save the volume desired after the fade*/
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    96
		alGetSourcef(Sources[index], AL_GAIN, &target_gain);
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    97
		if (target_gain > 1.0f || target_gain <= 0.0f)
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    98
			target_gain = 1.0f;
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    99
		
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   100
		alSourcePlay(Sources[index]);
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   101
		
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   102
		for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) {
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   103
#ifdef DEBUG
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   104
			fprintf(stderr, "Fade-in: Set gain to: %f\n", gain);
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   105
#endif
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   106
			alSourcef(Sources[index], AL_GAIN, gain);
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   107
			usleep(10000);
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   108
		}
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   109
		
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   110
		AlGetError("ERROR %d: Setting fade in volume\n");
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   111
		
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   112
#ifndef _WIN32
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   113
		pthread_exit(NULL);
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   114
#else
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   115
		_endthread();
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   116
#endif
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   117
	}
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   118
	
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   119
	
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   120
#ifndef _WIN32
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   121
	void *helper_fadeout(void *tmp) 
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   122
#else
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   123
	void *helper_fadeout(void *tmp) 	
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   124
#endif
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   125
	{
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   126
		ALfloat gain;
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   127
		ALfloat old_gain;
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   128
		fade_t *fade;
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   129
		int index; 
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   130
		unsigned int quantity; 
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   131
		
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   132
		fade = tmp;
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   133
		index = fade->index;
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   134
		quantity = fade->quantity;
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   135
		free(fade);
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   136
		
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   137
#ifdef DEBUG
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   138
		fprintf(stderr, "Fade-out: index %d quantity %d\n", index, quantity);
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   139
#endif
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   140
		
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   141
		alGetSourcef(Sources[index], AL_GAIN, &old_gain);
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   142
		
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   143
		for (gain = old_gain; gain >= 0.00f; gain -= (float) quantity/10000) {
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   144
#ifdef DEBUG
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   145
			fprintf(stderr, "Fade-out: Set gain to %f\n", gain);
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   146
#endif
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   147
			alSourcef(Sources[index], AL_GAIN, gain);
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   148
			usleep(10000);
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   149
		}
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   150
		
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   151
		AlGetError("ERROR %d: Setting fade out volume\n");
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   152
		
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
   153
		/*stop that sound and reset its volume*/
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   154
		alSourceStop (Sources[index]);
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   155
		alSourcef (Sources[index], AL_GAIN, old_gain);	
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   156
		
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   157
#ifndef _WIN32
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   158
		pthread_exit(NULL);
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   159
#else
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   160
		_endthread();
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   161
#endif
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   162
	}
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   163
	
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   164
	
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   165
#ifdef __CPLUSPLUS
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   166
}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   167
#endif