Esempio n. 1
0
void initSound() {

    printf("Initializing OpenAl \n");

    // Init openAL
    alutInit(0, NULL);

    alGetError(); // clear any error messages

    // Generate buffers, or else no sound will happen!
    alGenBuffers(NUM_BUFFERS, buffer);

    if(alGetError() != AL_NO_ERROR)
    {
        printf("- Error creating buffers !!\n");
        exit(1);
    }
    else
    {
        printf("init() - No errors yet.\n");
    }

    alutLoadWAVFile("..\\res\\Footsteps.wav",&format,&data,&size,&freq,false);
    alBufferData(buffer[0],format,data,size,freq);
    //alutUnloadWAV(format,data,size,freq);

    alGetError(); /* clear error*/
    alGenSources(NUM_SOURCES, source);

    if(alGetError() != AL_NO_ERROR)
    {
        printf("- Error creating sources !!\n");
        exit(2);
    }
    else
    {
        printf("init - no errors after alGenSources\n");
    }

    listenerPos[0] = posX;
    listenerPos[1] = posY;
    listenerPos[2] = posZ;

    source0Pos[0] = posX;
    source0Pos[1] = posY;
    source0Pos[2] = posZ;

    alListenerfv(AL_POSITION,listenerPos);
    alListenerfv(AL_VELOCITY,listenerVel);
    alListenerfv(AL_ORIENTATION,listenerOri);

    alSourcef(source[0], AL_PITCH, 1.0f);
    alSourcef(source[0], AL_GAIN, 1.0f);
    alSourcefv(source[0], AL_POSITION, source0Pos);
    alSourcefv(source[0], AL_VELOCITY, source0Vel);
    alSourcei(source[0], AL_BUFFER,buffer[0]);
    alSourcei(source[0], AL_LOOPING, AL_TRUE);

    printf("Sound ok! \n\n");
}
Esempio n. 2
0
	bool Source::BeginSource(unsigned char group, unsigned char priority, Buffer* buffer, bool setActiveBuffer, bool setLooping)
	{
		if (buffer->bufferId[0] == NO_BUFFER) return false;

		gain_scale = 1.0f;

		if (source == NO_SOURCE)
		{
			source = AllocSource(group,priority);
			if (source != NO_SOURCE)
			{
				alSourcef(sources[source],AL_PITCH,pitch);
				alSourcef(sources[source],AL_GAIN,gain * gain_scale);
				alSourcef(sources[source],AL_ROLLOFF_FACTOR,rolloff);
				alSourcefv(sources[source],AL_POSITION,position);
				alSourcefv(sources[source],AL_VELOCITY,velocity);
				alSourcef(sources[source],AL_REFERENCE_DISTANCE,refDistance);
				alSourcef(sources[source],AL_MAX_DISTANCE,maxDistance);
				alSourcei(sources[source],AL_LOOPING,(setLooping) ? looping : AL_FALSE);
				if (setActiveBuffer) alSourcei(sources[source],AL_BUFFER,buffer->bufferId[buffer->activeBuffer]);
			}
		}

		return HasSource();
	}
void LLAudioChannelOpenAL::update3DPosition()
{
	if(!mCurrentSourcep)
	{
		return;
	}
	if (mCurrentSourcep->isAmbient())
	{
		alSource3f(ALSource, AL_POSITION, 0.0, 0.0, 0.0);
		alSource3f(ALSource, AL_VELOCITY, 0.0, 0.0, 0.0);
		//alSource3f(ALSource, AL_DIRECTION, 0.0, 0.0, 0.0);
		alSourcef (ALSource, AL_ROLLOFF_FACTOR, 0.0);
		alSourcei (ALSource, AL_SOURCE_RELATIVE, AL_TRUE);
	}
	else
	{
		LLVector3 float_pos;
		float_pos.setVec(mCurrentSourcep->getPositionGlobal());
		alSourcefv(ALSource, AL_POSITION, float_pos.mV);
		//llinfos << "LLAudioChannelOpenAL::update3DPosition() Velocity: " << mCurrentSourcep->getVelocity() << llendl;
		alSourcefv(ALSource, AL_VELOCITY, mCurrentSourcep->getVelocity().mV);
		//alSource3f(ALSource, AL_DIRECTION, 0.0, 0.0, 0.0);
		alSourcef (ALSource, AL_ROLLOFF_FACTOR, 1.0);
		alSourcei (ALSource, AL_SOURCE_RELATIVE, AL_FALSE);
	}
	//llinfos << "LLAudioChannelOpenAL::update3DPosition() Gain: " << mCurrentSourcep->getGain() << llendl;
	alSourcef(ALSource, AL_GAIN, mCurrentSourcep->getGain());
}
Esempio n. 4
0
static void init( const ALbyte *fname )
{
	ALfloat zeroes[] = { 0.0f, 0.0f, 0.0f };
	ALfloat position[] = { 2.0f, 0.0f, 4.0f };
	ALuint boom;
	int i = 0;

	alListenerfv( AL_POSITION, zeroes );

	boom = CreateBufferFromFile( fname );

	alGenSources( NUMSOURCES, multis );

	alSourcefv( multis[0], AL_POSITION, position );
	alSourcei( multis[0], AL_BUFFER, boom );
	alSourcef( multis[i], AL_MAX_GAIN, 1.0 );
	alSourcef( multis[i], AL_REFERENCE_DISTANCE, 1.0 );

	for ( i = 1; i < NUMSOURCES; i++ ) {
		position[0] = -2.0f * i;
		position[1] = 0.0;
		position[2] = -4.0f * i;

		alSourcefv( multis[i], AL_POSITION, position );
		alSourcei( multis[i], AL_BUFFER, boom );
		alSourcef( multis[i], AL_MAX_GAIN, 0 );
		alSourcef( multis[i], AL_REFERENCE_DISTANCE, 0.1 );
	}
}
Esempio n. 5
0
static void init( const ALbyte *fname )
{
	ALfloat zeroes[] = { 0.0f, 0.0f, 0.0f };
	ALfloat back[] = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f };
	ALfloat front[] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f };
	ALfloat position[] = { 0.0f, 0.0f, -4.0f };
	ALuint boom;

	start = time( NULL );

	alListenerfv( AL_POSITION, zeroes );
	alListenerfv( AL_VELOCITY, zeroes );
	alListenerfv( AL_ORIENTATION, front );

	boom = CreateBufferFromFile( fname );

	alGenSources( 1, &movingSource );

	alSourcei( movingSource, AL_BUFFER, boom );
	alSourcei( movingSource, AL_LOOPING, AL_FALSE );
	alSourcefv( movingSource, AL_POSITION, position );
	alSourcefv( movingSource, AL_VELOCITY, zeroes );
	alSourcefv( movingSource, AL_ORIENTATION, back );
	alSourcef( movingSource, AL_PITCH, 1.0f );
}
Esempio n. 6
0
bool sound::open(const std::string& filename, bool looped)
{
    std::ifstream a(filename.c_str());
    if(!a.is_open()) { 
        return false;
    }
    a.close();
    
    ALuint source_id = m_source_id;
    alGenSources(1, &source_id);
    m_source_id = source_id;
    if(!system::get_instance()->check_al_error()) {
        return false;
    }
    
    ALfloat pos[] = { (ALfloat) m_x, (ALfloat) m_y, (ALfloat) m_z };
    ALfloat vel[] = { 0.0f, 0.0f, 0.0f };
    
    alSourcef (m_source_id, AL_PITCH,    1.0f);
    alSourcef (m_source_id, AL_GAIN,     1.0f);
    alSourcefv(m_source_id, AL_POSITION, pos);
    alSourcefv(m_source_id, AL_VELOCITY, vel);
    alSourcei (m_source_id, AL_LOOPING,  looped);
    
    system::sound_id id = system::get_instance()->load_wav(filename);
    if(id == -1) {
        return false;
    }
    
    alSourcei(m_source_id, AL_BUFFER, id);
    
    return true;
}
Esempio n. 7
0
		void Sound::play()
		{

			/**
			 * Need to make sure everything is up to date
			 */
			update();
		
			/**
			 * Source creation
			 * XXX To update
			 */
			ALuint source;
			alGenSources(1, &source);
			
			ALfloat sourcePos[] = { 0.0, 0.0, 0.0 };
			ALfloat sourceVel[] = { 0.0, 0.0, 0.0 };
			
			alSourcef (source, AL_PITCH,    1.0f     );
			alSourcef (source, AL_GAIN,     1.0f     );
			alSourcefv(source, AL_POSITION, sourcePos);
			alSourcefv(source, AL_VELOCITY, sourceVel);
			// -----------
			
			/**
			 * The sound is played
			 * The sound is also added to the play list
			 */
			alSourcei(source, AL_BUFFER, buffer);
			alSourcePlay(source);
			source_list.push_back(source);
		}
Esempio n. 8
0
OPAL_SOUND_MGR bool SoundManager::setSoundPosition( unsigned int audioID, Vector3 position,
												   Vector3 velocity, Vector3 direction )
{
	if ( audioID >= MAX_AUDIO_SOURCES || !mAudioSourceInUse[ audioID ] )
		return false;

	// Set the position
	ALfloat pos[] = { position.x, position.y, position.z };

	alSourcefv( mAudioSources[ audioID ], AL_POSITION, pos );

	if ( checkALError( "setSound::alSourcefv:AL_POSITION" ) )
		return false;

	// Set the veclocity
	ALfloat vel[] = { velocity.x, velocity.y, velocity.z };

	alSourcefv( mAudioSources[ audioID ], AL_VELOCITY, vel );

	if ( checkALError( "setSound::alSourcefv:AL_VELOCITY" ) )
		return false;

	// Set the direction
	ALfloat dir[] = { velocity.x, velocity.y, velocity.z };

	alSourcefv( mAudioSources[ audioID ], AL_DIRECTION, dir );

	if ( checkALError( "setSound::alSourcefv:AL_DIRECTION" ) )
		return false;

	return true;
}
Esempio n. 9
0
REBOOL REWAVSound::loadPcmWavData(const REUByte * data, ALuint * buffer, ALuint * source)
{
	*buffer = 0;
	*source = 0;
	ALenum format = 0;
	ALsizei freq = 0;
	REUInt32 subChunk1Size = 0;
	REUInt32 subChunk2Size = 0;
	REUInt32 fileOffset = 16;
	REUInt16 audioFormat = 0; 
	REUInt16 channels = 0;
	REUInt16 bps = 0;
	char str4[5] = { 0 };
	memcpy(&subChunk1Size, &data[fileOffset], sizeof(REUInt32));
	fileOffset += sizeof(REUInt32);
	if (subChunk1Size >= 16)
	{
		memcpy(&audioFormat, &data[fileOffset], sizeof(REUInt16));
		fileOffset += sizeof(REUInt16);
		if (audioFormat == 1)
		{
			memcpy(&channels, &data[fileOffset],sizeof(REUInt16));
			fileOffset += sizeof(REUInt16);
			
			memcpy(&freq, &data[fileOffset], sizeof(REUInt32));
			fileOffset += sizeof(REUInt32) + 6;
			
			memcpy(&bps, &data[fileOffset], sizeof(REUInt16));
			fileOffset += sizeof(REUInt16);
			if (channels == 1)
			{
				format = (bps == 8) ? AL_FORMAT_MONO8 : AL_FORMAT_MONO16;
			}
			else
			{
				format = (bps == 8) ? AL_FORMAT_STEREO8 : AL_FORMAT_STEREO16;
			}
			memcpy(&str4, &data[fileOffset], 4);
			fileOffset += 4;
			if (strcmp(str4, "data") == 0)
			{
				memcpy(&subChunk2Size, &data[fileOffset], sizeof(REUInt32));
				fileOffset += 4;
				alGenBuffers(1, buffer);
				alBufferData(*buffer, format, &data[fileOffset], subChunk2Size, freq);
				alGenSources(1, source);
				ALfloat sourcePos[] = {0.0f, 0.0f, 0.0f};
				ALfloat sourceVel[] = {0.0f, 0.0f, 0.0f};
				alSourcef(*source, AL_PITCH, 1.0f);
				alSourcef(*source, AL_GAIN, 1.0f);
				alSourcefv(*source, AL_POSITION, sourcePos);
				alSourcefv(*source, AL_VELOCITY, sourceVel);
				alSourcei(*source, AL_LOOPING, false); 
				alSourcei(*source, AL_BUFFER, *buffer);
				return true;
			}
		}
	}
	return false;
}
Esempio n. 10
0
static void init( void ) {
	ALfloat zeroes[]   = { 0.0f, 0.0f,  0.0f };
	ALfloat back[]     = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f };
	ALfloat front[]    = { 0.0f, 0.0f,  1.0f, 0.0f, 1.0f, 0.0f };
	ALfloat position[] = { -10.0f, 0.0f, 4.0f };
	ALsizei size;
	ALsizei bits;
	ALsizei freq;
	ALsizei format;
	int filelen;

	start = time(NULL);

	alListenerfv(AL_POSITION, zeroes );
	alListenerfv(AL_ORIENTATION, front );

	alGenBuffers( 1, &stereo);
	alGenSources( 1, moving_source);

	alSourcefv( moving_source[0], AL_POSITION, position );
	alSourcefv( moving_source[0], AL_ORIENTATION, back );
	alSourcei(  moving_source[0], AL_BUFFER,      stereo );
	alSourcei(  moving_source[0], AL_LOOPING,     AL_FALSE);

	return;
}
Esempio n. 11
0
void CSound::Play(int i)
{		
	ALfloat		mPos[3] = {0.0, 0.0, 0.0};
	ALfloat		mVel[3] = {0.0, 0.0, 0.0};

	
	/* Тон звука; Усиление с расстоянием */
	alSourcef  (source[i], AL_PITCH, 1.0f);
	alSourcef  (source[i], AL_GAIN, 1.0f);
	alSourcefv (source[i], AL_POSITION, mPos);
	alSourcefv (source[i], AL_VELOCITY, mVel);
	/* Зациклен ли? */
	alSourcef (source[i], AL_LOOPING, AL_FALSE);
	alSourcei( source[i], AL_BUFFER, buffer[i] );
	alSourcePlay( source[i] );

// 	ALint status;
// 	do
// 	{
// 		alutSleep (0.1f);
// 		alGetSourcei (source, AL_SOURCE_STATE, &status);
// 	} 
// 	while (status == AL_PLAYING);
// 
// 	alSourceStop (source);
// 

}
Esempio n. 12
0
bool LoadData()
{
	// 载入变量.
	const ALsizei size = 800;
	ALsizei freq = 8000;
	ALboolean loop = 1; // 循环播放
	// 使用一段正弦波作数据
	short data[800];
	alGenBuffers(1, &Buffer);
	float max = SHRT_MAX / 4;
	float rad = 0;
	for (int e = 0; e < 800; e++)
	{
		data[e] = (short)(max * cosf(rad));
		rad += 1.f;
	}
	// 载入WAV数据
	alBufferData(Buffer, AL_FORMAT_MONO16, data, size, freq);
	alGenSources(1, &Source);

	// 源声音的位置
	ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };
	// 源声音的速度
	ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };

	alSourcei(Source, AL_BUFFER, Buffer);
	alSourcef(Source, AL_PITCH, 1.0f);
	alSourcef(Source, AL_GAIN, 1.0f);
	alSourcefv(Source, AL_POSITION, SourcePos);
	alSourcefv(Source, AL_VELOCITY, SourceVel);
	alSourcei(Source, AL_LOOPING, loop);

	return true;
}
Esempio n. 13
0
static void Messaging_HandleMessage(PP_Instance pp_instance,
                                    struct PP_Var message) {
  if (g_MyState.ready == 0)
    return;
  if (message.type != PP_VARTYPE_STRING)
    return;

  uint32_t len;
  const char* str = g_MyState.var_interface->VarToUtf8(message, &len);
  int array_edit = 0;
  float* array = NULL;
  int array_index = 0;

  if (strstr(str, "source_pos")) {
    array_edit = 1;
    array = g_MyState.source_pos;
  } else if (strstr(str, "source_vel")) {
    array_edit = 1;
    array = g_MyState.source_vel;
  } else if (strstr(str, "listener_pos")) {
    array_edit = 1;
    array = g_MyState.listener_pos;
  } else if (strstr(str, "listener_vel")) {
    array_edit = 1;
    array = g_MyState.listener_vel;
  } else if (strstr(str, "pitch")) {
    double val = atof(strstr(str, "= ") + 2);
    g_MyState.pitch = val;
  } else if (strstr(str, "gain")) {
    double val = atof(strstr(str, "= ") + 2);
    g_MyState.gain = val;
  }

  if (array_edit) {
    if (strstr(str, "_x")) {
      array_index = 0;
    } else if (strstr(str, "_y")) {
      array_index = 1;
    } else if (strstr(str, "_z")) {
      array_index = 2;
    }
    double val = atof(strstr(str, "= ") + 2);
    array[array_index] = val;
  }

  alSourcefv(g_MyState.source, AL_POSITION, g_MyState.source_pos);
  assert(alGetError() == AL_NO_ERROR);
  alSourcefv(g_MyState.source, AL_VELOCITY, g_MyState.source_vel);
  assert(alGetError() == AL_NO_ERROR);
  alListenerfv(AL_POSITION, g_MyState.listener_pos);
  assert(alGetError() == AL_NO_ERROR);
  alListenerfv(AL_VELOCITY, g_MyState.listener_vel);
  assert(alGetError() == AL_NO_ERROR);
  alSourcef(g_MyState.source, AL_PITCH, g_MyState.pitch);
  assert(alGetError() == AL_NO_ERROR);
  alSourcef(g_MyState.source, AL_GAIN, g_MyState.gain);
  assert(alGetError() == AL_NO_ERROR);
}
Esempio n. 14
0
static void play() {
	ALCdevice *dev;
	ALCcontext *ctx;
	unsigned src;
	unsigned bufs[2];
	unsigned buf;
	int v;
	float zero[3];

	memset(zero, 0, sizeof zero);

	dev = alcOpenDevice(NULL);
	ctx = alcCreateContext(dev, NULL);
	alcMakeContextCurrent(ctx);

	alGenSources(1, &src);
	alGenBuffers(2, bufs);

	alSourcef(src, AL_PITCH, 1.0f);
	alSourcef(src, AL_GAIN, 1.0f);
	alSourcefv(src, AL_POSITION, zero);
	alSourcefv(src, AL_VELOCITY, zero);
	alSourcei(src, AL_SOURCE_RELATIVE, AL_TRUE);
	alSourcei(src, AL_LOOPING, AL_FALSE);

	ima_init(&ima_state);

	queue(src, bufs[0]);
	queue(src, bufs[1]);

	alSourcePlay(src);

	for (;;) {
		alGetSourcei(src, AL_BUFFERS_PROCESSED, &v);

		while (v-- > 0) {
			alSourceUnqueueBuffers(src, 1, &buf);
			queue(src, buf);
		}

		alGetSourcei(src, AL_SOURCE_STATE, &v);

		if (v == AL_STOPPED)
			break;

		usleep(1000);
	}

        alDeleteSources(1, &src);
        alDeleteBuffers(2, bufs);
        alcMakeContextCurrent(NULL);
        alcDestroyContext(ctx);
        alcCloseDevice(dev);
}
Esempio n. 15
0
	void OALSoundBuffer::Play(bool loop)
	{
		ALuint source = this->FreeSource();

		alSourcefv(source, AL_POSITION, &pos_[0]);
		alSourcefv(source, AL_VELOCITY, &vel_[0]);
		alSourcefv(source, AL_DIRECTION, &dir_[0]);
		alSourcei(source, AL_LOOPING, loop);

		alSourcePlay(source);
	}
Esempio n. 16
0
void play_sound(sound_source_t *source, sound_buffer_t *buffer)
{
    float pos[] = { source->Position[0], source->Position[1], 0.0f };
    float vel[] = { source->Velocity[0], source->Velocity[1], 0.0f };
    alSourcef(source->Id , AL_GAIN     , source->Gain);
    alSourcef(source->Id , AL_PITCH    , source->Pitch);
    alSourcei(source->Id , AL_LOOPING  , source->Loop);
    alSourcefv(source->Id, AL_POSITION , pos);
    alSourcefv(source->Id, AL_VELOCITY , vel);
    alSourcei(source->Id , AL_BUFFER   , buffer->Id);
    alSourcePlay(source->Id);
}
Esempio n. 17
0
void PlaySound(const std::string& name, float gain)
{
	const static ALfloat fz[] = { 0.0f, 0.0f, 0.0f };
	ALuint buf    = GetSound(name);
	ALuint source = GetFreeSource();
	LOG("Sound", LOG_NOTICE, "playing sound %s on channel %d", name.c_str(), source);
	alSourcei(source, AL_BUFFER, buf);
	alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
	alSourcef(source, AL_GAIN, gain);
	alSourcefv(source, AL_POSITION, fz);
	alSourcefv(source, AL_VELOCITY, fz);
	alSourcePlay(source);
}
Esempio n. 18
0
static void ChannelSetParms(int channel, V3XVECTOR *pos, V3XVECTOR *velocity, V3XRANGE *fRange)
{
  // TODO: Get this working.
  if (pos)
  alSourcefv(g_pchannels[channel].src, AL_POSITION, (ALfloat *)pos);
  if (velocity)
  alSourcefv(g_pchannels[channel].src, AL_VELOCITY, (ALfloat *)velocity);
  if (fRange != NULL)
  {
    // TODO: Pass minimum distance into OpenAL.
    alSourcef(g_pchannels[channel].src, AL_MAX_DISTANCE, fRange->max);
  }
}
Esempio n. 19
0
/*
* ALboolean LoadALData()
*
*	This function will load our sample data from the disk using the Alut
*	utility and send the data into OpenAL as a buffer. A source is then
*	also created to play that buffer.
*/
ALboolean LoadALData()
{
	// Variables to load into.

	ALenum format;
	ALsizei size;
	ALvoid* data;
	ALsizei freq;
	ALboolean loop;

	// Load wav data into a buffer.

	alGenBuffers(1, &Buffer);

	if(alGetError() != AL_NO_ERROR)
		return AL_FALSE;
	// yea yea i hard coded the sound wave
	// alutLoadWAVFile is deprecate 
	//alutLoadWAVFile("FancyPants.wav", &format, &data, &size, &freq, &loop);

	/* Create an AL buffer from the given sound file. */
	Buffer = alutCreateBufferFromFile ("hyperwars.wav");

	// not need see the above func read right into the buffer save a step.
    //alBufferData(Buffer, format, data, size, freq);

  // alutUnloadWAV is deprecate 

  //alutUnloadWAV(format, data, size, freq);

	// Bind the buffer with the source.

	alGenSources(1, &Source);

	if(alGetError() != AL_NO_ERROR)
		return AL_FALSE;

	alSourcei (Source, AL_BUFFER,   Buffer   );
	alSourcef (Source, AL_PITCH,    1.0      );
	alSourcef (Source, AL_GAIN,     1.0      );
	alSourcefv(Source, AL_POSITION, SourcePos);
	alSourcefv(Source, AL_VELOCITY, SourceVel);
//	alSourcei (Source, AL_LOOPING,  loop     );

	// Do another error check and return.

	if(alGetError() == AL_NO_ERROR)
		return AL_TRUE;

	return AL_FALSE;
}
Esempio n. 20
0
int loadWavFile(char* emplacementFichierWav) {
	// Partie OpenAL

	alutInit(NULL, 0);
	alGetError();

	// Variables to load into.
	ALenum format;
	ALsizei size;
	ALvoid* data;
	ALsizei freq;
	ALboolean loop;
	// Load wav data into a buffer.
	alGenBuffers(1, &Buffer);

	if(alGetError() != AL_NO_ERROR)
	{
		printf("Error loading data.");
		return 0;
	}

	//Loading wave file
	alutLoadWAVFile((ALbyte*)emplacementFichierWav, &format, &data, &size,&freq, &loop);
	alBufferData(Buffer, format, data, size, freq);
	alutUnloadWAV(format, data, size, freq);

	// Bind the buffer with the source.
	alGenSources(1, &Source);

	if(alGetError() != AL_NO_ERROR)
	{
		printf("Error loading data.");
		return 0;
	}


	alSourcei (Source, AL_BUFFER,   Buffer   );
	alSourcef (Source, AL_PITCH,    1.0      );
	alSourcef (Source, AL_GAIN,     1.0      );

	//Mise en place de la position de la source
	alSourcefv(Source, AL_POSITION, SourcePos);
	alSourcefv(Source, AL_VELOCITY, SourceVel);
	alSourcei (Source, AL_LOOPING,  loop     );

	//Mise en place de la position
	alListenerfv(AL_POSITION,    ListenerPos);
	alListenerfv(AL_VELOCITY,    ListenerVel);
	alListenerfv(AL_ORIENTATION, ListenerOri);
	return 1;
}
Esempio n. 21
0
//Play a sound - 2D sound (action) - AN ACTION IS NOT MODIFIED DURING PLAYING
bool SoundManager::PlayAction(const std::string& action)
{
	if (mSoundON)
	{	
		//Check if there is an action registered
		BuffersMap::iterator it = mBuffers.find(action);
		if (it==mBuffers.end())
		{
			SingletonLogMgr::Instance()->AddNewLine("SoundManager::PlayAction","Action " + action + " not found",LOGEXCEPTION);
			return false;
		}
		IdBuffer idBuffer = (*it).second;
		int index;
		ALfloat vector[3] = { 0.0, 0.0, 0.0 }; //2D SOUND
			
		index = _getSource(false);
	
		if (index==-1)
			return false;
		
		//Store last action played
		mSources[index].actionplayed = action;
			
		alSourcei (mSources[index].source, AL_BUFFER, idBuffer);
		alSourcei (mSources[index].source, AL_SOURCE_RELATIVE, AL_TRUE);
		alSourcef (mSources[index].source, AL_PITCH, 1.0);
		alSourcef (mSources[index].source, AL_GAIN, 1.0);
		alSourcefv(mSources[index].source, AL_POSITION, vector);
		alSourcefv(mSources[index].source, AL_VELOCITY, vector);
		alSourcefv(mSources[index].source, AL_DIRECTION, vector);
		alSourcei (mSources[index].source, AL_LOOPING, AL_FALSE);
		
		//Check errors
		ALenum error(alGetError());
		if(error != AL_NO_ERROR)
		{
			SingletonLogMgr::Instance()->AddNewLine("SoundManager::PlayAction","Failure while Setting source attributes!(" + GetALErrorString(error,false) + ")",LOGEXCEPTION);
		}	

		alSourcePlay(mSources[index].source);

		//Check errors
		error = alGetError();
		if(error  != AL_NO_ERROR)
		{
			SingletonLogMgr::Instance()->AddNewLine("SoundManager::PlayAction","Failure while playing source!(" + GetALErrorString(error,false) + ")",LOGEXCEPTION);
		}
	}
	return true;
}
ALboolean SoundManager::loadALData()
{
      // Variables to load into.

  ALenum format;
  ALsizei size;
  ALvoid* data;
  ALsizei freq;
  ALboolean loop;

   // Load wav data into a buffer.

  alGenBuffers(1, &Buffer);

  if(alGetError() != AL_NO_ERROR)
    return AL_FALSE;

  alutLoadWAVFile((ALbyte*)file.c_str(), &format, &data, &size, &freq, &loop);
  alBufferData(Buffer, format, data, size, freq);
  alutUnloadWAV(format, data, size, freq);

      // Bind the buffer with the source.

  alGenSources(1, &Source);

  if(alGetError() != AL_NO_ERROR)
  {
    std::cerr<<"Error loading data."<<std::endl;
    return AL_FALSE;
  }

  alSourcei (Source, AL_BUFFER,   Buffer   );
  alSourcef (Source, AL_PITCH,    1.0      );
  alSourcef (Source, AL_GAIN,     2.0      );
  alSourcefv(Source, AL_POSITION, SourcePos);
  alSourcefv(Source, AL_VELOCITY, SourceVel);
  alSourcei (Source, AL_LOOPING,  loop     );

      // Do another error check and return.

  if(alGetError() == AL_NO_ERROR)
  {
    std::cerr<<"Successfully loaded data."<<std::endl;
    return AL_TRUE;
  }

  std::cerr<<"Error loading data."<<std::endl;

  return AL_FALSE;
}
Esempio n. 23
0
static void iterate( void )
{
	static ALfloat speed[3] = { 0.0, 0.0, 0.0 };

	speed[2] += .0005;

	srcposition[0] += speed[0];
	srcposition[1] += speed[1];
	srcposition[2] += speed[2];

	alSourcefv( movingSource, AL_VELOCITY, speed );
	alSourcefv( movingSource, AL_POSITION, srcposition );
	microSleep( 20000 );
}
Esempio n. 24
0
void reproducir_sonido ( int identificador, ALfloat src_pos[3], ALfloat src_vel[3],
		      ALfloat ganancia, ALfloat pitch,ALfloat distancia_maxima, ALboolean repeticion ){

	/* Configuramos la fuente con los parametros */
	alSourcefv ( source[identificador], AL_POSITION, src_pos); /* Posicion */
	alSourcefv ( source[identificador], AL_VELOCITY, src_vel); /* Velocidad */
	alSourcef ( source[identificador], AL_GAIN, ganancia); /* Ganancia */
	alSourcef ( source[identificador], AL_PITCH, pitch); /* Frecuencia */
	alSourcei ( source[identificador], AL_LOOPING, repeticion); /* Repeticion */
	alSourcef ( source[identificador],AL_MAX_DISTANCE,distancia_maxima);/* Distancia a partir de la cual no se oye */

	/* Hacemos que suene */
	alSourcePlay ( source[identificador] );
}
Esempio n. 25
0
void PlaySoundPositional(const std::string& name, vec2 pos, vec2 vel, float gain)
{
	ALfloat fpos[] = {pos.X(), pos.Y(), 0.0f};
	ALfloat fvel[] = {vel.X(), vel.Y(), 0.0f};
	ALuint buf    = GetSound(name);
	ALuint source = GetFreeSource();
	alSourcei(source, AL_BUFFER, buf);
	alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE);
	alSourcefv(source, AL_POSITION, fpos);
	alSourcefv(source, AL_VELOCITY, fvel);
	alSourcef(source, AL_REFERENCE_DISTANCE, 50.0);
	alSourcef(source, AL_GAIN, gain);
	alSourcePlay(source);
}
bool IKeOpenALAudioDevice::CreateSoundBuffer( WAVEFORMATEX* wfx, IKeSoundBuffer** sound_buffer )
{
    IKeOpenALSoundBuffer* sb;
    ALenum error = alGetError();
    
    /* Allocate a new sound buffer instance */
    (*sound_buffer) = new IKeOpenALSoundBuffer;
    sb = static_cast<IKeOpenALSoundBuffer*>( *sound_buffer );
    
    /* Save audio format */
    memmove( &sb->wfx, wfx, sizeof( WAVEFORMATEX ) );
    
    /* Generate a sound buffer and source */
    alGenBuffers( 1, &sb->buffer );
    error = alGetError();
	if( error != AL_NO_ERROR )
	{
		sb->Destroy();
		DISPDBG( KE_ERROR, "Error creating buffer (0x" << error << ")!\n" );
		return false;
	}

    alGenSources( 1, &sb->source );
    error = alGetError();
	if( error != AL_NO_ERROR )
	{
		sb->Destroy();
		DISPDBG( KE_ERROR, "Error creating source (0x" << error << ")!\n" );
		return false;
	}

    /* Default 3D sound position and velocity */
    //memset( sb->position, 0, sizeof( float ) * 3 );
	//memset( sb->velocity, 0, sizeof( float ) * 3 );
    sb->position = nv::vec3f( 0.0f, 0.0f, 0.0f );
    sb->velocity = nv::vec3f( 0.0f, 0.0f, 0.0f );
    
	/* Set the sound source's default attributes */
    sb->volume = 1.0f;
    sb->pitch = 1.0f;
	alSourcei( sb->source, AL_BUFFER, sb->buffer );
	alSourcef( sb->source, AL_PITCH,  sb->volume );
	alSourcef( sb->source, AL_GAIN,	 sb->pitch );
	alSourcefv( sb->source, AL_POSITION, sb->position._array );
	alSourcefv( sb->source, AL_VELOCITY, sb->velocity._array );
	alSourcei( sb->source, AL_LOOPING, 0 );
    
    return true;
}
Esempio n. 27
0
// ----------------------------------------------------------------------------
void ofOpenALSoundPlayer::play(){
	std::unique_lock<std::mutex> lock(mutex);
	int err = glGetError();

	// if the sound is set to multiplay, then create new sources,
	// do not multiplay on loop or we won't be able to stop it
	if (bMultiPlay && !bLoop){
		sources.resize(sources.size()+channels);
		alGetError(); // Clear error.
		alGenSources(channels, &sources[sources.size()-channels]);
		err = alGetError();
		if (err != AL_NO_ERROR){
			ofLogError("ofOpenALSoundPlayer") << "play(): couldn't create multiplay stereo sources: "
			<< (int) err << " " << getALErrorString(err);
			return;
		}
		for(int i=0;i<channels;i++){
			alSourcei (sources[sources.size()-channels+i], AL_BUFFER,   buffers[i]   );
			// only stereo panning
			if(i==0){
				float pos[3] = {-1,0,0};
				alSourcefv(sources[sources.size()-channels+i],AL_POSITION,pos);
			}else{
				float pos[3] = {1,0,0};
				alSourcefv(sources[sources.size()-channels+i],AL_POSITION,pos);
			}
		    alSourcef (sources[sources.size()-channels+i], AL_ROLLOFF_FACTOR,  0.0);
		    alSourcei (sources[sources.size()-channels+i], AL_SOURCE_RELATIVE, AL_TRUE);
		}

		err = glGetError();
		if (err != AL_NO_ERROR){
			ofLogError("ofOpenALSoundPlayer") << "play(): couldn't assign multiplay buffers: "
			<< (int) err << " " << getALErrorString(err);
			return;
		}
	}
	alSourcePlayv(channels,&sources[sources.size()-channels]);

	if(bMultiPlay){
		ofAddListener(ofEvents().update,this,&ofOpenALSoundPlayer::update);
	}
	if(isStreaming){
		setPosition(0);
		stream_end = false;
		startThread(true);
	}

}
Esempio n. 28
0
void NMS_Audio::LoadWav(char* sFileName,char* sSoundName,ALfloat* pSourcePos,ALfloat* pSourceVel,float fPitch,float fGain,bool loop)
{

	//Check if the name is already inside our map, if it is, generate an error
	std::map<char* ,sourceStruct>::iterator iter = sourceMap.find(sSoundName);
	if( iter != sourceMap.end() ) 
	{
		throw 0;
	}


    // Buffer id and error checking variable.
	sourceStruct source;
    ALenum result;

	//Define the position of the source of the sound
	source.fSourcePos=pSourcePos;
	source.fSourceVel=pSourceVel;
	source.sSourceName=sSoundName;

    // Generate a buffer. Check that it was created successfully.
	source.iBufferID=NMS_SOUNDMANAGER.LoadWav(sFileName,sSoundName);

    if ((result = alGetError()) != AL_NO_ERROR)
	{
		LOG.write("NMS_Audio::LoadWav -> Impossible to create the stream for the sound file!\n",LOG_ERROR);
        throw 0;
	}

    // Generate a source.
	alGenSources(1,&source.iSourceID);

    if ((result = alGetError()) != AL_NO_ERROR)
	{
		LOG.write("NMS_Audio::LoadWav -> Impossible to create the source for the sound file!\n",LOG_ERROR);
        throw 0;
	}

    // Setup the source properties.
	alSourcei (source.iSourceID, AL_BUFFER,   source.iBufferID   );
	alSourcef (source.iSourceID, AL_PITCH,    fPitch   );
    alSourcef (source.iSourceID, AL_GAIN,     fGain    );
    alSourcefv(source.iSourceID, AL_POSITION, source.fSourcePos);
    alSourcefv(source.iSourceID, AL_VELOCITY, source.fSourceVel);
    alSourcei (source.iSourceID, AL_LOOPING,  loop     );

	sourceMap[sSoundName]=source;
}
Esempio n. 29
0
void init(int ini)
{
  if(ini) {
    alutInit(0, NULL);
  }

  alListenerfv(AL_POSITION,listenerPos);
  alListenerfv(AL_VELOCITY,listenerVel);
  alListenerfv(AL_ORIENTATION,listenerOri);
  
  alGetError(); // clear any error messages

  // Generate buffers, or else no sound will happen!
  alGenBuffers(NUM_BUFFERS, buffer);

  if(alGetError() != AL_NO_ERROR) {
      kerror(TRUE, "- Error creating buffers !!\n");
  }
    
  alutLoadWAVFile(reinterpret_cast<ALbyte*>(const_cast<char *>(HOME_DIR"1.wav")),&format,&data,&size,&freq,0);
  alBufferData(buffer[0],format,data,size,freq);
  alutUnloadWAV(format,data,size,freq);
  
  alutLoadWAVFile(reinterpret_cast<ALbyte*>(const_cast<char *>(HOME_DIR"2.wav")),&format,&data,&size,&freq,0);
  alBufferData(buffer[1],format,data,size,freq);
  alutUnloadWAV(format,data,size,freq);

  alutLoadWAVFile(reinterpret_cast<ALbyte*>(const_cast<char *>(HOME_DIR"3.wav")),&format,&data,&size,&freq,0);
  alBufferData(buffer[2],format,data,size,freq);
  alutUnloadWAV(format,data,size,freq);

  alGetError(); /* clear error */
  alGenSources(NUM_SOURCES, source);

  if(alGetError() != AL_NO_ERROR) {
    kerror(TRUE,"- Error creating sources !!\n");
  }

  int i;
  for(i = 0; i < NUM_SOURCES; i++) {
    alSourcef(source[i],AL_PITCH,1.0f);
    alSourcef(source[i],AL_GAIN,1.0f);
    alSourcefv(source[i],AL_POSITION,pos);
    alSourcefv(source[i],AL_VELOCITY,vel);
    alSourcei(source[i],AL_BUFFER,buffer[i]);
    alSourcei(source[i],AL_LOOPING,AL_TRUE);
  }
}
Esempio n. 30
-1
/*
 * ALboolean LoadALData()
 *
 *	This function will load our sample data from the disk using the Alut
 *	utility and send the data into OpenAL as a buffer. A source is then
 *	also created to play that buffer.
 */
ALboolean LoadALData()
{
    // Variables to load into.
    ALenum format;
    ALsizei size;
    ALvoid* data;
    ALsizei freq;
    ALboolean loop;
    // Load wav data into a buffer.
    alGenBuffers(1, &Buffer);
    if(alGetError() != AL_NO_ERROR)
        return AL_FALSE;
    alutLoadWAVFile((ALbyte*)"wavdata/FancyPants.wav", &format, &data, &size, &freq, &loop);
    alBufferData(Buffer, format, data, size, freq);
    alutUnloadWAV(format, data, size, freq);
    // Bind the buffer with the source.
    alGenSources(1, &Source);
    if(alGetError() != AL_NO_ERROR)
        return AL_FALSE;
    alSourcei (Source, AL_BUFFER,   Buffer   );
    alSourcef (Source, AL_PITCH,    1.0      );
    alSourcef (Source, AL_GAIN,     1.0      );
    alSourcefv(Source, AL_POSITION, SourcePos);
    alSourcefv(Source, AL_VELOCITY, SourceVel);
    alSourcei (Source, AL_LOOPING,  loop     );
    // Do another error check and return.
    if(alGetError() == AL_NO_ERROR)
        return AL_TRUE;
    return AL_FALSE;
}