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