openalbridge/openalwrap.c
changeset 2212 6b5da1a2765a
parent 2211 288360b78f30
child 2213 bd51bbf06033
equal deleted inserted replaced
2211:288360b78f30 2212:6b5da1a2765a
    20 
    20 
    21 #ifdef __CPLUSPLUS
    21 #ifdef __CPLUSPLUS
    22 extern "C" {
    22 extern "C" {
    23 #endif 
    23 #endif 
    24 	
    24 	
    25 	// Sources are points emitting sound.
    25 	/*Sources are points emitting sound*/
    26 	ALuint *Sources;
    26 	ALuint *Sources;
    27 	// Buffers hold sound data.
    27 	/*Buffers hold sound data*/
    28 	ALuint *Buffers;
    28 	ALuint *Buffers;
    29 	//index for Sources and Buffers
    29 	/*index for Sources and Buffers*/
    30 	ALuint globalindex, globalsize;
    30 	ALuint globalindex, globalsize;
    31 	// Position of the source sound.
    31 	/*Position of the source sound*/
    32 	ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };
    32 	ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };
    33 	// Velocity of the source sound.
    33 	/*Velocity of the source sound*/
    34 	ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };
    34 	ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };
    35 	
    35 	
    36 	int increment;
    36 	int increment;
    37 	
    37 	
    38 	ALint openal_close(void) {
    38 	ALint openal_close(void) {
    61 		/* This function initializes an OpenAL contex, allocates memory space for data and prepares OpenAL buffers*/
    61 		/* This function initializes an OpenAL contex, allocates memory space for data and prepares OpenAL buffers*/
    62 		ALCcontext *context;
    62 		ALCcontext *context;
    63 		ALCdevice *device;
    63 		ALCdevice *device;
    64 		const ALCchar *default_device;
    64 		const ALCchar *default_device;
    65 
    65 
    66 		// Position of the listener.
    66 		/*Position of the listener*/
    67 		ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };
    67 		ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };
    68 		// Velocity of the listener.
    68 		/*Velocity of the listener*/
    69 		ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };
    69 		ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };
    70 		// Orientation of the listener. (first 3 elements are "at", second 3 are "up")
    70 		/*Orientation of the listener. (first 3 elements are "at", second 3 are "up")*/
    71 		ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };
    71 		ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };
    72 		
    72 		
    73 		default_device = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
    73 		default_device = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
    74 		fprintf(stderr, "Using default device: %s\n", default_device);
    74 		fprintf(stderr, "Using default device: %s\n", default_device);
    75 		
    75 		
    83 		alcProcessContext(context);
    83 		alcProcessContext(context);
    84 		
    84 		
    85 		if (AlGetError("ERROR %d: Creating a new contex\n") != AL_TRUE)
    85 		if (AlGetError("ERROR %d: Creating a new contex\n") != AL_TRUE)
    86 			return AL_FALSE;
    86 			return AL_FALSE;
    87 		
    87 		
    88 		//allocate memory space for buffers and sources
    88 		/*allocate memory space for buffers and sources*/
    89 		globalsize = memorysize;
    89 		globalsize = memorysize;
    90 		increment = memorysize;
    90 		increment = memorysize;
    91 		Buffers = (ALuint*) Malloc(sizeof(ALuint)*globalsize);
    91 		Buffers = (ALuint*) Malloc(sizeof(ALuint)*globalsize);
    92 		Sources = (ALuint*) Malloc(sizeof(ALuint)*globalsize);
    92 		Sources = (ALuint*) Malloc(sizeof(ALuint)*globalsize);
    93 		
    93 		
    94 		//set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation
    94 		/*set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation*/
    95 		alListenerf (AL_GAIN,		 1.0f		);
    95 		alListenerf (AL_GAIN,		 1.0f		);
    96 		alListenerfv(AL_POSITION,    ListenerPos);
    96 		alListenerfv(AL_POSITION,    ListenerPos);
    97 		alListenerfv(AL_VELOCITY,    ListenerVel);
    97 		alListenerfv(AL_VELOCITY,    ListenerVel);
    98 		alListenerfv(AL_ORIENTATION, ListenerOri);
    98 		alListenerfv(AL_ORIENTATION, ListenerOri);
    99 		
    99 		
   144 		if (error < 0) {
   144 		if (error < 0) {
   145 			fprintf(stderr, "ERROR: file %s is too short \n", filename);
   145 			fprintf(stderr, "ERROR: file %s is too short \n", filename);
   146 			return -2;
   146 			return -2;
   147 		}
   147 		}
   148 		
   148 		
   149 		//prepare the buffers to receive data
   149 		/*prepare the buffer to receive data*/
   150 		alGenBuffers(1, &Buffers[globalindex]);
   150 		alGenBuffers(1, &Buffers[globalindex]);
   151 		
   151 		
   152 		if (AlGetError("ERROR %d: Allocating memory for buffers\n") != AL_TRUE)
   152 		if (AlGetError("ERROR %d: Allocating memory for buffers\n") != AL_TRUE)
   153 			return -3;
   153 			return -3;
   154 		
   154 		
   155 		//prepare the sources to emit sound
   155 		/*prepare the source to emit sound*/
   156 		alGenSources(1, &Sources[globalindex]);
   156 		alGenSources(1, &Sources[globalindex]);
   157 		
   157 		
   158 		if (AlGetError("ERROR %d: Allocating memory for sources\n") != AL_TRUE)
   158 		if (AlGetError("ERROR %d: Allocating memory for sources\n") != AL_TRUE)
   159 			return -4;
   159 			return -4;
   160 				
   160 				
   161 		
   161 		
   162 		if (fileformat == 0x5367674F) //check if ogg
   162 		if (fileformat == 0x5367674F) /*check if ogg*/
   163 			error = load_OggVorbis (filename, &format, &data, &bitsize, &freq);
   163 			error = load_OggVorbis (filename, &format, &data, &bitsize, &freq);
   164 		else {
   164 		else {
   165 			if (fileformat == 0x46464952) //check if wav
   165 			if (fileformat == 0x46464952) /*check if wav*/
   166 				error = load_WavPcm (filename, &format, &data, &bitsize, &freq);
   166 				error = load_WavPcm (filename, &format, &data, &bitsize, &freq);
   167 			else {
   167 			else {
   168 				fprintf(stderr, "ERROR: File format (%08X) not supported!\n", invert_endianness(fileformat));
   168 				fprintf(stderr, "ERROR: File format (%08X) not supported!\n", invert_endianness(fileformat));
   169 				return -5;
   169 				return -5;
   170 			}
   170 			}
   171 		}
   171 		}
   172 		
   172 		
   173 		//copy pcm data in one buffer
   173 		/*copy pcm data in one buffer*/
   174 		alBufferData(Buffers[globalindex], format, data, bitsize, freq);
   174 		alBufferData(Buffers[globalindex], format, data, bitsize, freq);
   175 		free(data);		//deallocate data to save memory
   175 		free(data);		/*deallocate data to save memory*/
   176 		
   176 		
   177 		if (AlGetError("ERROR %d: Writing data to buffer\n") != AL_TRUE)
   177 		if (AlGetError("ERROR %d: Writing data to buffer\n") != AL_TRUE)
   178 			return -6;
   178 			return -6;
   179 			
   179 			
   180 		//source properties that it will use when it's in playback
   180 		/*set source properties that it will use when it's in playback*/
   181 		alSourcei (Sources[globalindex], AL_BUFFER,   Buffers[globalindex]  );
   181 		alSourcei (Sources[globalindex], AL_BUFFER,   Buffers[globalindex]  );
   182 		alSourcef (Sources[globalindex], AL_PITCH,    1.0f					);
   182 		alSourcef (Sources[globalindex], AL_PITCH,    1.0f					);
   183 		alSourcef (Sources[globalindex], AL_GAIN,     1.0f					);
   183 		alSourcef (Sources[globalindex], AL_GAIN,     1.0f					);
   184 		alSourcefv(Sources[globalindex], AL_POSITION, SourcePos				);
   184 		alSourcefv(Sources[globalindex], AL_POSITION, SourcePos				);
   185 		alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel				);
   185 		alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel				);
   188 		if (AlGetError("ERROR %d: Setting source properties\n") != AL_TRUE)
   188 		if (AlGetError("ERROR %d: Setting source properties\n") != AL_TRUE)
   189 			return -7;
   189 			return -7;
   190 		
   190 		
   191 		alGetError();  /* clear any AL errors beforehand */
   191 		alGetError();  /* clear any AL errors beforehand */
   192 		
   192 		
   193 		//returns the index of the source you just loaded, increments it and exits
   193 		/*returns the index of the source you just loaded, increments it and exits*/
   194 		return globalindex++;
   194 		return globalindex++;
   195 	}
   195 	}
   196 	
   196 	
   197 	
   197 	
   198 	ALint openal_toggleloop (int index){
   198 	ALint openal_toggleloop (int index){