openalbridge/openalwrap.c
author koda
Fri, 03 Jul 2009 22:00:32 +0000
changeset 2220 110266ba2ef7
parent 2216 82e7da49c26a
child 2257 7eb31efcfb9b
permissions -rw-r--r--
-new openal api for setting sound position -updates to INSTALL
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
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    19
#include "globals.h"
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    20
#include "wrappers.h"
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    21
#include "alc.h"
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    22
#include "loaders.h"
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    23
#include "endianness.h"
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    24
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    25
#ifdef __CPLUSPLUS
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    26
extern "C" {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    27
#endif 
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    28
	
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
    29
	/*Sources are points emitting sound*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    30
	ALuint *Sources;
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
    31
	/*Buffers hold sound data*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    32
	ALuint *Buffers;
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
    33
	/*index for Sources and Buffers*/
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    34
	ALuint globalindex, globalsize, increment;
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
    35
	/*Position of the source sound*/
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
    36
	ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
    37
	/*Velocity of the source sound*/
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
    38
	ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    39
	
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    40
	ALint openalReady = AL_FALSE;
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    41
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    42
	ALint openal_close(void) {
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    43
		/*Stop all sounds, deallocate all memory and close OpenAL */
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    44
		ALCcontext *context;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    45
		ALCdevice  *device;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    46
		
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    47
		if(openalReady == AL_FALSE)
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    48
		{
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    49
			fprintf(stderr, "ERROR: OpenAL not initialized\n");
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    50
			return AL_FALSE;
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    51
		}
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    52
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    53
		alSourceStopv	(globalsize, Sources);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    54
		alDeleteSources (globalsize, Sources);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    55
		alDeleteBuffers (globalsize, Buffers);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    56
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    57
		free(Sources);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    58
		free(Buffers);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    59
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    60
		context = alcGetCurrentContext();
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    61
		device  = alcGetContextsDevice(context);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    62
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    63
		alcMakeContextCurrent(NULL);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    64
		alcDestroyContext(context);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    65
		alcCloseDevice(device);
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    66
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    67
		openalReady = AL_FALSE;
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    68
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    69
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    70
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    71
	
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    72
	ALint openal_ready(void) {
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    73
		return openalReady;
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    74
	}
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    75
	
2215
1d78579e06c2 regression of previous commit, stdint.h in use
koda
parents: 2213
diff changeset
    76
	ALint openal_init(uint32_t memorysize) {	
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
    77
		/*Initialize an OpenAL contex and allocate memory space for data and buffers*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    78
		ALCcontext *context;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    79
		ALCdevice *device;
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
    80
		const ALCchar *default_device;
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    81
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
    82
		/*Position of the listener*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    83
		ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
    84
		/*Velocity of the listener*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    85
		ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
    86
		/*Orientation of the listener. (first 3 elements are "at", second 3 are "up")*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    87
		ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    88
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    89
		if(openalReady == AL_TRUE)
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    90
		{
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    91
			fprintf(stderr, "ERROR: OpenAL already initialized\n");
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    92
			return AL_FALSE;
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    93
		}
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
    94
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    95
		default_device = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    96
		fprintf(stderr, "Using default device: %s\n", default_device);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    97
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    98
		if ((device = alcOpenDevice(default_device)) == NULL) {
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
    99
			fprintf(stderr, "ERROR: Failed to open sound device\n");
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   100
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   101
		}
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   102
		
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   103
		context = alcCreateContext(device, NULL);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   104
		alcMakeContextCurrent(context);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   105
		alcProcessContext(context);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   106
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   107
		if (AlGetError("ERROR %d: Creating a new contex\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   108
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   109
		
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
   110
		/*allocate memory space for buffers and sources*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   111
		globalsize = memorysize;
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   112
		increment = memorysize;
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   113
		Buffers = (ALuint*) Malloc(sizeof(ALuint)*globalsize);
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   114
		Sources = (ALuint*) Malloc(sizeof(ALuint)*globalsize);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   115
		
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
   116
		/*set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   117
		alListenerf (AL_GAIN,		 1.0f		);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   118
		alListenerfv(AL_POSITION,    ListenerPos);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   119
		alListenerfv(AL_VELOCITY,    ListenerVel);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   120
		alListenerfv(AL_ORIENTATION, ListenerOri);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   121
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   122
		if (AlGetError("ERROR %d: Setting Listener properties\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   123
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   124
		
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   125
		openalReady = AL_TRUE;
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   126
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   127
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   128
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   129
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   130
	
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   131
	
2215
1d78579e06c2 regression of previous commit, stdint.h in use
koda
parents: 2213
diff changeset
   132
	uint8_t helper_realloc (void) {
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   133
		/*expands allocated memory when loading more sound files than expected*/
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   134
		globalsize += increment;
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   135
#ifdef DEBUG
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   136
		fprintf(stderr, "OpenAL: Realloc in process %d\n", globalsize);
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   137
#endif
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   138
		Buffers = (ALuint*) Realloc(Buffers, sizeof(ALuint)*globalsize);
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   139
		Sources = (ALuint*) Realloc(Sources, sizeof(ALuint)*globalsize);
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   140
		
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   141
		return 0;
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   142
	}
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   143
	
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   144
	
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   145
	int openal_loadfile (const char *filename){
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   146
		/*Open a file, load into memory and allocate the Source buffer for playing*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   147
		ALenum format;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   148
		ALsizei bitsize;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   149
		ALsizei freq;
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   150
		char *data;
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   151
		uint32_t fileformat;
2215
1d78579e06c2 regression of previous commit, stdint.h in use
koda
parents: 2213
diff changeset
   152
		ALenum error;
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   153
		FILE *fp;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   154
		
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   155
		if(openalReady == AL_FALSE)
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   156
		{
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   157
			fprintf(stderr, "ERROR: OpenAL not initialized\n");
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   158
			return AL_FALSE;
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   159
		}
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   160
		
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   161
		/*when the buffers are all used, we can expand memory to accept new files*/
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   162
		if (globalindex == globalsize)
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   163
			helper_realloc();
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   164
		
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   165
		/*detect the file format, as written in the first 4 bytes of the header*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   166
		fp = Fopen (filename, "rb");
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   167
		if (fp == NULL)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   168
			return -1;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   169
		error = fread (&fileformat, sizeof(uint32_t), 1, fp);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   170
		fclose (fp);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   171
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   172
		if (error < 0) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   173
			fprintf(stderr, "ERROR: file %s is too short \n", filename);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   174
			return -2;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   175
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   176
		
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
   177
		/*prepare the buffer to receive data*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   178
		alGenBuffers(1, &Buffers[globalindex]);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   179
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   180
		if (AlGetError("ERROR %d: Allocating memory for buffers\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   181
			return -3;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   182
		
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
   183
		/*prepare the source to emit sound*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   184
		alGenSources(1, &Sources[globalindex]);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   185
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   186
		if (AlGetError("ERROR %d: Allocating memory for sources\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   187
			return -4;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   188
				
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   189
		
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
   190
		if (fileformat == 0x5367674F) /*check if ogg*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   191
			error = load_OggVorbis (filename, &format, &data, &bitsize, &freq);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   192
		else {
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
   193
			if (fileformat == 0x46464952) /*check if wav*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   194
				error = load_WavPcm (filename, &format, &data, &bitsize, &freq);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   195
			else {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   196
				fprintf(stderr, "ERROR: File format (%08X) not supported!\n", invert_endianness(fileformat));
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   197
				return -5;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   198
			}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   199
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   200
		
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
   201
		/*copy pcm data in one buffer*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   202
		alBufferData(Buffers[globalindex], format, data, bitsize, freq);
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
   203
		free(data);		/*deallocate data to save memory*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   204
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   205
		if (AlGetError("ERROR %d: Writing data to buffer\n") != AL_TRUE)
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2209
diff changeset
   206
			return -6;
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   207
			
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
   208
		/*set source properties that it will use when it's in playback*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   209
		alSourcei (Sources[globalindex], AL_BUFFER,   Buffers[globalindex]  );
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   210
		alSourcef (Sources[globalindex], AL_PITCH,    1.0f					);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   211
		alSourcef (Sources[globalindex], AL_GAIN,     1.0f					);
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   212
		alSourcefv(Sources[globalindex], AL_POSITION, SourcePos				);
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   213
		alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel				);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   214
		alSourcei (Sources[globalindex], AL_LOOPING,  0						);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   215
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   216
		if (AlGetError("ERROR %d: Setting source properties\n") != AL_TRUE)
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   217
			return -7;
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   218
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   219
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   220
		
2212
6b5da1a2765a -openalbridge headers cleaned
koda
parents: 2211
diff changeset
   221
		/*returns the index of the source you just loaded, increments it and exits*/
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   222
		return globalindex++;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   223
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   224
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   225
	
2215
1d78579e06c2 regression of previous commit, stdint.h in use
koda
parents: 2213
diff changeset
   226
	ALint openal_toggleloop (uint32_t index){
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   227
		/*Set or unset looping mode*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   228
		ALint loop;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   229
		
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   230
		if(openalReady == AL_FALSE)
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   231
		{
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   232
			fprintf(stderr, "ERROR: OpenAL not initialized\n");
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   233
			return AL_FALSE;
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   234
		}
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   235
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   236
		if (index >= globalsize) {
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   237
			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   238
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   239
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   240
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   241
		alGetSourcei (Sources[index], AL_LOOPING, &loop);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   242
		alSourcei (Sources[index], AL_LOOPING, !((uint8_t) loop) & 0x00000001);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   243
		if (AlGetError("ERROR %d: Getting or setting loop property\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   244
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   245
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   246
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   247
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   248
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   249
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   250
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   251
	
2215
1d78579e06c2 regression of previous commit, stdint.h in use
koda
parents: 2213
diff changeset
   252
	ALint openal_setvolume (uint32_t index, uint8_t percentage) {
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   253
		if(openalReady == AL_FALSE)
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   254
		{
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   255
			fprintf(stderr, "ERROR: OpenAL not initialized\n");
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   256
			return AL_FALSE;
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   257
		}
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   258
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   259
		/*Set volume for sound number index*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   260
		if (index >= globalindex) {
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   261
			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   262
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   263
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   264
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   265
		if (percentage > 100)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   266
			percentage = 100;
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   267
		alSourcef (Sources[index], AL_GAIN, (float) percentage/100.0f);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   268
		if (AlGetError("ERROR %d: Setting volume for last sound\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   269
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   270
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   271
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   272
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   273
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   274
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   275
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   276
	
2215
1d78579e06c2 regression of previous commit, stdint.h in use
koda
parents: 2213
diff changeset
   277
	ALint openal_setglobalvolume (uint8_t percentage) {
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   278
		if(openalReady == AL_FALSE)
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   279
		{
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   280
			fprintf(stderr, "ERROR: OpenAL not initialized\n");
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   281
			return AL_FALSE;
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   282
		}
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   283
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   284
		/*Set volume for all sounds*/		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   285
		if (percentage > 100)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   286
			percentage = 100;
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   287
		alListenerf (AL_GAIN, (float) percentage/100.0f);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   288
		if (AlGetError("ERROR %d: Setting global volume\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   289
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   290
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   291
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   292
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   293
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   294
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   295
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   296
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   297
	ALint openal_togglemute () {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   298
		/*Mute or unmute sound*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   299
		ALfloat mute;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   300
		
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   301
		if(openalReady == AL_FALSE)
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   302
		{
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   303
			fprintf(stderr, "ERROR: OpenAL not initialized\n");
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   304
			return AL_FALSE;
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   305
		}
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   306
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   307
		alGetListenerf (AL_GAIN, &mute);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   308
		if (mute > 0) 
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   309
			mute = 0;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   310
		else
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   311
			mute = 1.0;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   312
		alListenerf (AL_GAIN, mute);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   313
		if (AlGetError("ERROR %d: Setting mute property\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   314
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   315
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   316
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   317
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   318
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   319
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   320
	
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   321
	
2215
1d78579e06c2 regression of previous commit, stdint.h in use
koda
parents: 2213
diff changeset
   322
	ALint openal_fade(uint32_t index, uint16_t quantity, uint8_t direction) {
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   323
		/*Fade in or out by calling a helper thread*/
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   324
#ifndef _WIN32
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   325
		pthread_t thread;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   326
#else
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   327
		HANDLE Thread;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   328
		DWORD threadID;
2209
2573d4ff78f9 - bugfix for segfault on fades
koda
parents: 2200
diff changeset
   329
#endif
2573d4ff78f9 - bugfix for segfault on fades
koda
parents: 2200
diff changeset
   330
		fade_t *fade;
2573d4ff78f9 - bugfix for segfault on fades
koda
parents: 2200
diff changeset
   331
		
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   332
		if(openalReady == AL_FALSE)
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   333
		{
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   334
			fprintf(stderr, "ERROR: OpenAL not initialized\n");
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   335
			return AL_FALSE;
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   336
		}
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   337
2209
2573d4ff78f9 - bugfix for segfault on fades
koda
parents: 2200
diff changeset
   338
		fade = (fade_t*) Malloc(sizeof(fade_t));
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   339
		fade->index = index;
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   340
		fade->quantity = quantity;
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   341
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   342
		if (index >= globalindex) {
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   343
			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   344
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   345
		}
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   346
		
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   347
		if (direction == FADE_IN)
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   348
#ifndef _WIN32
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   349
			pthread_create(&thread, NULL, helper_fadein, (void*) fade);
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   350
#else
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   351
			Thread = _beginthread(&helper_fadein, 0, (void*) fade);
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   352
#endif
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   353
		else {
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   354
			if (direction == FADE_OUT)
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   355
#ifndef _WIN32
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   356
				pthread_create(&thread, NULL, helper_fadeout, (void*) fade);
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   357
#else
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   358
				Thread = _beginthread(&helper_fadeout, 0, (void*) fade);
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   359
#endif	
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   360
			else {
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   361
				fprintf(stderr, "ERROR: unknown direction for fade (%d)\n", direction);
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   362
				free(fade);
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   363
				return AL_FALSE;
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   364
			}
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   365
		}
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   366
		
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   367
#ifndef _WIN32
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   368
		pthread_detach(thread);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   369
#endif
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   370
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   371
		alGetError();  /* clear any AL errors beforehand */
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   372
		
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   373
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   374
	}
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   375
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   376
	
2215
1d78579e06c2 regression of previous commit, stdint.h in use
koda
parents: 2213
diff changeset
   377
	ALint openal_fadeout(uint32_t index, uint16_t quantity) {
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   378
		/*wrapper for fadeout*/
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   379
		return openal_fade(index, quantity, FADE_OUT);
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   380
	}
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   381
		
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   382
		
2215
1d78579e06c2 regression of previous commit, stdint.h in use
koda
parents: 2213
diff changeset
   383
	ALint openal_fadein(uint32_t index, uint16_t quantity) {
2213
bd51bbf06033 -Smaxx's porting of the library to MSVC compilers
koda
parents: 2212
diff changeset
   384
		/*wrapper for fadein*/
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   385
		return openal_fade(index, quantity, FADE_IN);
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   386
	}
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   387
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   388
	
2220
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   389
	ALint openal_setposition(uint32_t index, float x, float y, float z) {
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   390
		if(openalReady == AL_FALSE)	{
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   391
			fprintf(stderr, "ERROR: OpenAL not initialized\n");
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   392
			return AL_FALSE;
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   393
		}
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   394
		if (index >= globalindex) {
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   395
			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   396
			return AL_FALSE;
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   397
		}
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   398
		
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   399
		alSource3f(Sources[index], AL_POSITION, x, y, z);
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   400
		if (AlGetError("ERROR %d: setting position for last sound\n") != AL_TRUE)
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   401
			return AL_FALSE;
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   402
		
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   403
		return AL_TRUE;
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   404
	}
2215
1d78579e06c2 regression of previous commit, stdint.h in use
koda
parents: 2213
diff changeset
   405
	ALint openal_playsound(uint32_t index){
2220
110266ba2ef7 -new openal api for setting sound position
koda
parents: 2216
diff changeset
   406
		if(openalReady == AL_FALSE)	{
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   407
			fprintf(stderr, "ERROR: OpenAL not initialized\n");
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   408
			return AL_FALSE;
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   409
		}
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   410
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   411
		/*Play sound number index*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   412
		if (index >= globalindex) {
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   413
			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   414
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   415
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   416
		alSourcePlay(Sources[index]);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   417
		if (AlGetError("ERROR %d: Playing last sound\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   418
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   419
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   420
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   421
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   422
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   423
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   424
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   425
	
2215
1d78579e06c2 regression of previous commit, stdint.h in use
koda
parents: 2213
diff changeset
   426
	ALint openal_pausesound(uint32_t index){
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   427
		if(openalReady == AL_FALSE)
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   428
		{
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   429
			fprintf(stderr, "ERROR: OpenAL not initialized\n");
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   430
			return AL_FALSE;
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   431
		}
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   432
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   433
		/*Pause sound number index*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   434
		if (index >= globalindex) {
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   435
			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   436
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   437
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   438
		alSourcePause(Sources[index]);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   439
		if (AlGetError("ERROR %d: Pausing last sound\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   440
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   441
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   442
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   443
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   444
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   445
	
2215
1d78579e06c2 regression of previous commit, stdint.h in use
koda
parents: 2213
diff changeset
   446
	ALint openal_stopsound(uint32_t index){
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   447
		if(openalReady == AL_FALSE)
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   448
		{
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   449
			fprintf(stderr, "ERROR: OpenAL not initialized\n");
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   450
			return AL_FALSE;
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   451
		}
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2215
diff changeset
   452
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   453
		/*Stop sound number index*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   454
		if (index >= globalindex) {
2211
288360b78f30 - fade in/out functions merged, but kept binary compatibility
koda
parents: 2210
diff changeset
   455
			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   456
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   457
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   458
		alSourceStop(Sources[index]);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   459
		if (AlGetError("ERROR %d: Stopping last sound\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   460
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   461
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   462
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   463
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   464
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   465
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   466
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   467
#ifdef __CPLUSPLUS
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   468
}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   469
#endif