sound_player::sound_player() { alutInit(NULL,NULL); #ifdef __APPLE__ ALsizei size, freq; ALenum format; ALvoid *data; alutLoadWAVFile("sounds/pew.wav", &format, &data, &size, &freq); alBufferData(buffers[PEW], format, data, size, freq); alutUnloadWAV(format,data,size,freq); alutLoadWAVFile("sounds/blast.wav", &format, &data, &size, &freq); alBufferData(buffers[BLAST], format, data, size, freq); alutUnloadWAV(format,data,size,freq); #elif __linux__ || _WIN32 || _WIN64 buffers[PEW] = alutCreateBufferFromFile("sounds/pew.wav"); buffers[BLAST] = alutCreateBufferFromFile("sounds/blast.wav"); #endif //alGenSources(1,&source); alGenSources(SOUNDS,sources); alSourcei(sources[PEW],AL_BUFFER,buffers[PEW]); alSourcei(sources[PEW],AL_LOOPING,AL_FALSE); alSourcei(sources[BLAST],AL_BUFFER,buffers[BLAST]); alSourcei(sources[BLAST],AL_LOOPING,AL_FALSE); }
AudioTrack::AudioTrack(std::string filename, Ogre::Real startTime) : startTime(startTime) { int error; // clear Error alGetError(); std::cout << "Creating the buffers" << std::endl; // Create the buffers alGenBuffers(1, buffers); if ((error = alGetError()) != AL_NO_ERROR) { std::cout << "alGenBuffers : " << error << std::endl; exit(0); } std::cout << "Loading the sound..." << std::endl; ALenum format; ALsizei size; ALsizei freq; ALboolean loop; ALvoid* data; /* ****Problem mit XCode-Update **/ ALbyte* file = (ALbyte*) filename.c_str(); #ifdef OSX alutLoadWAVFile(file, &format, &data, &size, &freq);//, &loop); #else alutLoadWAVFile(file, &format, &data, &size, &freq, &loop); #endif //loadWAVFile(file, &format, &data, &size, &freq);//, &loop); if ((error = alGetError()) != AL_NO_ERROR) { std::cout << "alutLoadWAVFile : " << error << std::endl; // Delete Buffers alDeleteBuffers(1, buffers); exit(0); } std::cout << "Filling Buffer..." << std::endl; alBufferData(buffers[0],format,data,size,freq); if ((error = alGetError()) != AL_NO_ERROR) { std::cout << "alBufferData buffer 0 : " << error << std::endl; // Delete buffers alDeleteBuffers(1, buffers); exit(0); } std::cout << "Freeing Audio-File..." << std::endl; alutUnloadWAV(format,data,size,freq); //unloadWAV(format,data,size,freq); if ((error = alGetError()) != AL_NO_ERROR) { std::cout << "alutUnloadWAV : " << error << std::endl; // Delete buffers alDeleteBuffers(1, buffers); exit(0); } /** ENDE VOM PROBLEM!*/ }
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); } }
void AudioManager::load_sounds() { if (loaded_) { std::cout << "SoundManager: Error - sounds already loaded" << std::endl; return; } // For now, assuming one sound (buffer) per source int sound_count = sound_names_.size(); unsigned int source_indicies[sound_count]; unsigned int buffer_indicies[sound_count]; alGenBuffers(sound_count, buffer_indicies); alGenSources(sound_count, source_indicies); ALenum format; ALsizei size; ALsizei freq; ALboolean loop; ALvoid* data; for (int j = 0; j < sound_count; ++j) { // TODO Evil, evil casting here alutLoadWAVFile(reinterpret_cast<ALbyte*>(const_cast<char*>(sound_names_[j].c_str())), &format, &data, &size, &freq, &loop); alBufferData(buffer_indicies[j], format, data, size, freq); alutUnloadWAV(format, data, size, freq); Sound::ShPtr s(new Sound(sound_names_[j], source_indicies[j], buffer_indicies[j])); sounds_.push_back(s); } }
unsigned int CSoundManager::LoadSoundWAV(std::string name) { ALenum format, error; ALsizei size; ALsizei freq; ALboolean loop; ALvoid* data; alutLoadWAVFile((ALbyte *)name.c_str(), &format, &data, &size, &freq, &loop); if ((error = alGetError()) != AL_NO_ERROR) { CLogManager::Instance()->LogMessage("Error loading sound : " + GetSoundError(error) ); return 0; } alBufferData(m_iBuffers[m_iSoundCount],format,data,size,freq); if ((error = alGetError()) != AL_NO_ERROR) { CLogManager::Instance()->LogMessage("Error loading sound : " + GetSoundError(error) ); return 0; } alutUnloadWAV(format,data,size,freq); if ((error = alGetError()) != AL_NO_ERROR) { CLogManager::Instance()->LogMessage("Error loading sound : " + GetSoundError(error) ); return 0; } return m_iBuffers[m_iSoundCount++]; }
Sound::Sound(string filename, Options* options) { ALenum format; ALsizei size; ALsizei freq; ALboolean loop; ALvoid* data; ALuint error; this->options = options; alGenBuffers(1, &(this->buffer)); if(!checkError(__func__)) return; alutLoadWAVFile((ALbyte*) filename.c_str(), &format, &data, &size, &freq, &loop); if(!checkError(__func__)) return; alBufferData(this->buffer, format, data, size, freq); if(!checkError(__func__)) return; alutUnloadWAV(format,data,size,freq); if(!checkError(__func__)) return; alGenSources(1, &(this->source)); if(!checkError(__func__)) return; alSourcei(source, AL_BUFFER, buffer); if(!checkError(__func__)) return; }
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"); }
system::sound_id system::load_wav(const std::string& filename) { sound_id bad_id = -1; sound_info buffer; ALenum format; ALvoid* data; ALsizei size; ALsizei freq; ALboolean loop; ALuint buf_id = 0; buffer.m_filename = filename; sound_buffers::const_iterator end_it = m_d->m_buffers->end(); for(sound_buffers::const_iterator it = m_d->m_buffers->begin(); it != end_it; ++it) { if(it->second.m_filename == filename) { buf_id = it->first; break; } } if(!buf_id) { alGenBuffers(1, &buffer.m_id); if(!check_al_error()) { return false; } alutLoadWAVFile((ALbyte *)filename.c_str(), &format, &data, &size, &freq, &loop); if (!check_al_error()) { return bad_id; } buffer.m_format = format; buffer.m_rate = freq; alBufferData(buffer.m_id, format, data, size, freq); if (!check_al_error()) { return bad_id; } alutUnloadWAV(format, data, size, freq); if (!check_al_error()) { return bad_id; } m_d->m_buffers->insert(sound_buffers::value_type(buf_id, buffer)); } else { buffer = (*m_d->m_buffers)[buf_id]; } return buffer.m_id; }
OPAL_SOUND_MGR void SoundManager::testSound( const char* wavFile ) { ALuint buffer; ALuint source; ALenum format; ALsizei size; ALvoid* data; ALsizei freq; ALboolean loop; alGenBuffers( 1, &buffer ); if ( checkALError( "testSound()" ) ) return; // This is the same for alutLoadWAVMemory #ifndef MACINTOSH_AL alutLoadWAVFile( (ALbyte*) wavFile, &format, &data, &size, &freq, &loop ); #else alutLoadWAVFile( (ALbyte*) wavFile, &format, &data, &size, &freq ); #endif alBufferData( buffer, format, data, size, freq ); alutUnloadWAV( format, data, size, freq ); alGenSources( 1, &source ); alSourcei( source, AL_BUFFER, buffer ); alSourcef( source, AL_PITCH, 1.0f ); alSourcef( source, AL_GAIN, 1.0f ); #ifndef MACINTOSH_AL alSourcei( source, AL_LOOPING, loop ); #else alSourcei( source, AL_LOOPING, false ); // TODO ! But how to get from file? #endif alSourcePlay( source ); //Or else we risk to destroy the manager too quickly to here anything ! for (int i=0;i<50000; i++) {printf(".");}; }
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; }
void CsAudioManager::LoadBuffer(ALbyte* fileName, int no) { ALenum format; ALsizei size; ALvoid* data; ALsizei freq; ALboolean loop; // Load wav data into a buffer. if (alGetError() != AL_NO_ERROR) return; alutLoadWAVFile(fileName, &format, &data, &size, &freq, &loop); alBufferData(Buffer[no], format, data, size, freq); alutUnloadWAV(format, data, size, freq); }
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; }
OPAL_SOUND_MGR bool SoundManager::loadWAV( std::string filename, ALuint pDestAudioBuffer ) { ALenum format; //for the buffer format ALsizei size; //the bit depth ALsizei freq; //for the frequency of the buffer ALboolean loop; //looped ALvoid* data; //data for the buffer std::string mFullPath = mAudioPath; alGetError(); // Clear Error Code // Load in the WAV file from disk //mFullPath += "\\"; mFullPath += filename; #ifndef MACINTOSH_AL alutLoadWAVFile( (ALbyte*)mFullPath.c_str(), &format, &data, &size, &freq, &loop); #else alutLoadWAVFile( (ALbyte*)mFullPath.c_str(), &format, &data, &size, &freq); #endif if ( checkALError("loadWAV::alutLoadWAVFile: ") ) return false; // Copy the new WAV data into the buffer alBufferData(pDestAudioBuffer, format, data, size, freq); if ( checkALError("loadWAV::alBufferData: ") ) return false; // Unload the WAV file alutUnloadWAV(format, data, size, freq); if ( checkALError("loadWAV::alutUnloadWAV: ") ) return false; return true; }
/* --==================================================================-- -- Function: loadMusic -- Edited By: Brian McGlauflin -- Modified: Mar 5, 2007 -- Parameters: filename of music file to load (char *) -- Returns: -- Description: Loads a musicfile into the manager so it can be played. --==================================================================-- */ ALboolean MusicManager::loadMusic(char* filename) { ALenum format; ALsizei size; ALvoid* data; ALsizei freq; ALboolean loop; alGenBuffers(1, &buffer); if (alGetError() != AL_NO_ERROR) { engine.logger.printlog(" Music: ERROR generating buffer for "); engine.logger.printlnlog(filename); return AL_FALSE; } alutLoadWAVFile(filename, &format, &data, &size, &freq, &loop); alBufferData(buffer, format, data, size, freq); alutUnloadWAV(format, data, size, freq); // Bind buffer with a source. alGenSources(1, &source); if (alGetError() != AL_NO_ERROR) { engine.logger.printlog(" Music: ERROR generating source for "); engine.logger.printlnlog(filename); return AL_FALSE; } 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, sourceVelocity); alSourcei (source, AL_LOOPING, AL_TRUE ); // Do another error check and return. if (alGetError() != AL_NO_ERROR) { engine.logger.printlog(" Music: ERROR setting up source for "); engine.logger.printlnlog(filename); return AL_FALSE; } char str[101]; sprintf_s(str, " Music: Loaded music file '%s'", filename); engine.logger.printlnlog(str); return AL_TRUE; }
static void playFile(const char *fileName) { ALenum format; void *data; ALsizei size; ALsizei frequency; #if !defined(__APPLE__) ALboolean loop; #endif ALuint buffer; ALuint source; ALenum error; ALint status; /* Create an AL buffer from the given sound file. */ alutLoadWAVFile((ALbyte *) "file1.wav", &format, &data, &size, &frequency #if !defined(__APPLE__) , &loop #endif ); alGenBuffers(1, &buffer); alBufferData(buffer, format, data, size, frequency); free(data); /* Generate a single source, attach the buffer to it and start playing. */ alGenSources(1, &source); alSourcei(source, AL_BUFFER, buffer); alSourcePlay(source); /* Normally nothing should go wrong above, but one never knows... */ error = alGetError(); if (error != ALUT_ERROR_NO_ERROR) { fprintf(stderr, "%s\n", alGetString(error)); alutExit(); exit(EXIT_FAILURE); } /* Check every 0.1 seconds if the sound is still playing. */ do { alutSleep(0.1f); alGetSourcei(source, AL_SOURCE_STATE, &status); } while (status == AL_PLAYING); }
/* *************** Load WAV file into Buffer *****************/ int SoundManager::LoadWav(char* file) { if(nBuffers >= MAX_BUFFERS - 1) std::cerr << "Out of sources" << std::endl; ALenum format; ALsizei size; ALsizei frequency; ALboolean loop; ALvoid* data; // Read in WAV data to buffer and delete temporary array used to // load the data. AL_CHECK_ERR(alutLoadWAVFile(reinterpret_cast<ALbyte*>(file), &format, &data, &size, &frequency, &loop)); AL_CHECK_ERR(alBufferData(buffers[nBuffers], format, data, size, frequency)); AL_CHECK_ERR(alutUnloadWAV(format, data, size, frequency)); return nBuffers++; }
int OpenALSampler::loadWaveBuffer (char* _fileName, ALuint _buffer) { int error; ALenum format; ALsizei size; ALsizei freq; ALboolean loop; ALvoid* data; FILE *File=NULL; File=fopen(_fileName,"r"); if (File) { // Carregar arquivo alutLoadWAVFile((ALbyte*) _fileName, &format, &data, &size, &freq, &loop); if ((error = alGetError()) != AL_NO_ERROR) { displayOpenALError("alutLoadWAVFile : ", error); return 0; } } else { printf("\n%s - Arquivo inexistente ou em uso.", _fileName); return 0; } // Carregar dados do arquivo no buffer. alBufferData(_buffer,format,data,size,freq); if ((error = alGetError()) != AL_NO_ERROR) { displayOpenALError("alBufferData :", error); return 0; } // Liberar memoria usada para carregar arquivo. alutUnloadWAV(format,data,size,freq); if ((error = alGetError()) != AL_NO_ERROR) { displayOpenALError("alutUnloadWAV :", error); return 0; } return 1; }
void AudioManager::initAL() { ALfloat listenerPos[]={0.0,0.0,0.0}; ALfloat listenerVel[]={0.0,0.0,0.0}; ALfloat listenerOri[]={0.0,0.0,0.0, 0.0,0.0,0.0}; // "at", then "up" ALsizei size,freq; ALenum format; ALvoid *data; ALboolean loop; //Load sound files which are stored insided "sound" folder in the current directory char* audioFiles[NUM_BUFFERS] = { ".\\sound\\1.wav", ".\\sound\\2.wav", ".\\sound\\3.wav", ".\\sound\\4.wav", ".\\sound\\5.wav", ".\\sound\\6.wav", ".\\sound\\7.wav", ".\\sound\\8.wav", ".\\sound\\9.wav", ".\\sound\\10.wav", ".\\sound\\11.wav", ".\\sound\\12.wav", ".\\sound\\13.wav", ".\\sound\\14.wav", ".\\sound\\15.wav", ".\\sound\\16.wav", ".\\sound\\17.wav", ".\\sound\\18.wav", ".\\sound\\19.wav", ".\\sound\\20.wav", ".\\sound\\21.wav", ".\\sound\\22.wav", ".\\sound\\23.wav", ".\\sound\\24.wav" }; ALuint g_Buffers[NUM_BUFFERS]; // Initialize OpenAL alutInit(0, 0); int alError = alGetError(); // Generate Buffers alGenBuffers(NUM_BUFFERS, g_Buffers); //audio buffers for each chord for (ALuint k=0; k < NUM_BUFFERS; k++) { alutLoadWAVFile(audioFiles[k],&format,&data,&size,&freq,&loop); alBufferData(g_Buffers[k],format,data,size,freq); alutUnloadWAV(format,data,size,freq); } // Generate Sources alGenSources(NUM_BUFFERS,soundClip); for (ALuint r=0; r < NUM_BUFFERS; r++) { alSourcei(soundClip[r], AL_BUFFER, g_Buffers[r]); } }
bool SoundManager::_loadSound(const std::string& file, IdBuffer& buffer) { // Variables to load into. FILE *fd; ALenum format; ALenum error; ALsizei size; ALvoid* data; ALsizei freq; ALboolean loop; // Load wav data into buffers. alGenBuffers(1, &buffer); if((error=alGetError()) != AL_NO_ERROR) { alDeleteBuffers(1,&buffer); SingletonLogMgr::Instance()->AddNewLine("SoundManager::_loadSound","Error: Can't create openAL Buffer (" + GetALErrorString(error,false) + ")",LOGEXCEPTION); return false; } // Check if the file exists if ((fd = fopen(file.c_str(),"r"))==NULL) { alDeleteBuffers(1,&buffer); SingletonLogMgr::Instance()->AddNewLine("SoundManager::_loadSound","Error: Can't open file " + file,LOGEXCEPTION); return false; } else { fclose(fd); } alutLoadWAVFile((ALbyte*)file.c_str(), &format, &data, &size, &freq, &loop); alBufferData(buffer, format, data, size, freq); alutUnloadWAV(format, data, size, freq); if ((error=alGetError()) != AL_NO_ERROR) { alDeleteBuffers(1,&buffer); SingletonLogMgr::Instance()->AddNewLine("SoundManager::_loadSound","Error: Can't load sound file " + file + " (" + GetALErrorString(error,false) + ")",LOGEXCEPTION); return false; } return true; }
Handle<Value> ALUTLoadWAVFileCallback(const Arguments& args) { //if less that nbr of formal parameters then do nothing if (args.Length() < 5) return v8::Undefined(); //get arguments Handle<Array> arg0 = Array::Cast(args[0]); Handle<Array> arg1 = Array::Cast(args[1]); String::Utf8Value value2(args[2]); char* key2 = *value2; void* arg2 = font_[key2]; Handle<Array> arg3 = Array::Cast(args[3]); Handle<Array> arg4 = Array::Cast(args[4]); //make call alutLoadWAVFile((ALbyte*)arg0, (ALenum*)arg1, (void**)arg2, (ALsizei*)arg3, (ALsizei*)arg4); return v8::Undefined(); }
void Sound3D::main1 () { // Initialize OpenAL and clear the error bit. alutInit(NULL, 0); alGetError(); ALenum format; ALsizei size; ALvoid* data; ALsizei freq; ALboolean loop = AL_TRUE; alutLoadWAVFile("BuzzingBee.wav", &format, &data, &size, &freq, &loop); alGenBuffers(1, &Buffer); alBufferData(Buffer, format, data, size, freq); alutUnloadWAV(format, data, size, freq); }
Sound() // : osg::MatrixTransform() // , osg::NodeCallback() { // TODO use inner class for the buffer in order to benefit of RAII in case of exception alGenSources((ALuint)1, &_source); throw_AlError_onfailure(); alSourcef(_source, AL_PITCH, 1); throw_AlError_onfailure(); alSourcef(_source, AL_GAIN, 1); throw_AlError_onfailure(); alSource3f(_source, AL_POSITION, 0, 0, 10); throw_AlError_onfailure(); alSource3f(_source, AL_VELOCITY, 0, 0, 0); throw_AlError_onfailure(); alSourcei(_source, AL_LOOPING, AL_TRUE); alGenBuffers((ALuint)1, &_buffer); throw_AlError_onfailure(); ALsizei size, freq; ALenum format; ALvoid *data; ALboolean loop = AL_FALSE; alutLoadWAVFile((ALbyte *)"test.wav", &format, &data, &size, &freq, &loop); throw_ALUTError_onfailure(); alBufferData(_buffer, format, data, size, freq); throw_AlError_onfailure(); alSourcei(_source, AL_BUFFER, _buffer); throw_AlError_onfailure(); //this->setUpdateCallback(this); }
void CargaSonido ( char *fichero_wav, int identificador ){ /* Variables locales */ ALsizei size = NULL, freq = NULL, bits = NULL, format = NULL; ALenum format1 = NULL; static void *data = NULL; ALboolean loop = AL_FALSE; /* Generamos buffer, le asignamos un identificador y comprobamos errores */ alGenBuffers( 1, &buffer[identificador] ); if ( !alIsBuffer ( buffer[identificador] )){ fprintf ( logs, "error al crear los buffers\n"); exit(1); } /* Cargamos ficheros Wave */ #ifdef _LINUX alutLoadWAV ( fichero_wav, &data, &format, &size, &bits, &freq ); /* Cargamos en memoria */ alBufferData ( buffer[identificador], format, data, size, freq ); /* Almacenamos en buffer */ free (data); /* liberamos */ #endif #ifdef _WIN32 alutLoadWAVFile ( fichero_wav, &format1, &data, &size, &freq, &loop ); alBufferData ( buffer[identificador], format1, data, size, freq ); alutUnLoadWAV ( format1, data, size, freq ); #endif /* Generamos las fuentes de sonido y comprobamos errores */ alGenSources( 1, &source[identificador] ); if ( !alIsSource ( source[identificador])){ fprintf ( logs, "No se pueden crear las fuentes de sonido\n"); exit(1); } /* Pasamos el archivo wav del buffer a la fuente */ alSourcei ( source[identificador], AL_BUFFER, buffer[identificador]); }
int Audio::loadWav(Path file){ engineTime.skipNextFrame(); AudioSource* source=new AudioSource; source->type=AUDIO_WAV; source->filename=file; ALenum format; ALsizei size; ALvoid* data; ALsizei freq; ALboolean l; //discarded // Load wav data into a buffer. alGetError(); alGetError(); alGetError(); alGetError(); alGetError(); alGetError(); alGenBuffers(1, &source->buffer); ALenum error=alGetError(); if(error != AL_NO_ERROR){ logs().audio.write("[AudioObject] Error loading file:"+file.getRelative()+", "+String(alGetString(error))); console().write("audio error: could not load file '"+file.getRelative()+"', "+String(alGetString(error))); return -1; } error=alGetError(); alutLoadWAVFile((ALbyte*)file.getAbsolute().c_str(), &format, &data, &size, &freq, &l); if(format==AL_FORMAT_STEREO8 || format==AL_FORMAT_STEREO16){ source->stereo=true; }else{ source->stereo=false; } alBufferData(source->buffer, format, data, size, freq); alutUnloadWAV(format, data, size, freq); // Bind buffer with a source. error=alGetError(); if(error != AL_NO_ERROR){ logs().audio.write("[AudioObject] Error loading file:"+file.getRelative()+", "+String(alGetString(error))); console().write("audio error: could not loading file '"+file.getRelative()+"', "+String(alGetString(error))); return -1; } alGenSources(1, &source->source); if(error==AL_OUT_OF_MEMORY || error==AL_INVALID_VALUE){ //there's not more resources for sources source->alSourceSet=false; }else if(error!=AL_NO_ERROR){ return -1; }else{ source->alSourceSet=true; alSources.pushBack(source->source); source->sourceIndex=sources.size()-1; alSourcePriority.pushBack(0); while(alSourceUsedBy.size()<=sources.size()){ //note, we haven't actually added this source to the sources list yet, thus the weird index alSourceUsedBy.pushBack(); } alSourceUsedBy[alSources.size()-1]=sources.size(); source->alSourceIndex=alSources.size()-1; } source->sourcePos[0]=source->pos.x; source->sourcePos[1]=source->pos.y; source->sourcePos[2]=source->pos.z; source->sourceVelocity[0]=source->velocity.x; source->sourceVelocity[1]=source->velocity.y; source->sourceVelocity[2]=source->velocity.z; if(source->alSourceSet){ alSourcei (source->source, AL_BUFFER, source->buffer ); alSourcef (source->source, AL_PITCH, 1.0f ); alSourcef (source->source, AL_GAIN, 1.0f ); if(!source->stream){ alSourcei (source->source, AL_LOOPING, source->loop ); } } // Do another error check and return. error=alGetError(); if(error == AL_NO_ERROR){ sources.pushBack(source); source->sourceIndex=sources.size()-1; return sources.size()-1; }else{ logs().audio.write("[AudioObject] Error 2 loading file:"+file.getRelative()); console().write(String("audio wav error: error '")+String(alGetString(error))+"' loading file '"+file.getRelative()+"'"); return -1; } }
struct SoundManager newSoundManager(struct Settings* settings) { int i; struct SoundManager sm; sm.settings = settings; alutInit(NULL, 0); if(alGetError() != AL_NO_ERROR) { printf("newSoundManager: cannot initalize ALUT"); return sm; } alGenBuffers(SOUND_COUNT, sm.buffers); alGenSources(SOUND_SOURCE_COUNT, sm.sources); char filenames[SOUND_COUNT][256] = { "music.wav", "click.wav", "crash.wav", "engine.wav" }; ALfloat zeroVector[3] = {0.0, 0.0, 0.0}; ALfloat zeroVector6[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; for(i=0;i<SOUND_COUNT;i++) { ALenum format; ALsizei size; ALvoid* data; ALsizei freq; ALboolean loop; alutLoadWAVFile(filenames[i], &format, &data, &size, &freq, &loop); alBufferData(sm.buffers[i], format, data, size, freq); alutUnloadWAV(format, data, size, freq); } for(i=0;i<SOUND_SOURCE_COUNT;i++) { if(SOUND_SOURCE_ENGINE1 == i || SOUND_SOURCE_ENGINE2 == i) alSourcei (sm.sources[i], AL_BUFFER, sm.buffers[SOUND_ENGINE]); else alSourcei (sm.sources[i], AL_BUFFER, sm.buffers[i]); alSourcef (sm.sources[i], AL_PITCH, 1.0); if(SOUND_SOURCE_MUSIC == i) alSourcef (sm.sources[i], AL_GAIN, settings->musicVolmue / 100.0); else alSourcef (sm.sources[i], AL_GAIN, settings->fxVolmue / 100.0); alSourcefv(sm.sources[i], AL_POSITION, zeroVector); alSourcefv(sm.sources[i], AL_VELOCITY, zeroVector); if(SOUND_SOURCE_CLICK == i || SOUND_SOURCE_CRASH == i) alSourcei (sm.sources[i], AL_LOOPING, AL_FALSE); else alSourcei (sm.sources[i], AL_LOOPING, AL_TRUE); } alListenerfv(AL_POSITION, zeroVector); alListenerfv(AL_VELOCITY, zeroVector); alListenerfv(AL_ORIENTATION, zeroVector6); return sm; }
Sound::SoundManager::SoundManager() : m_pDevice(alcOpenDevice(nullptr)) { if(!m_pDevice) LogError() << "Could not open audio device"; //m_Enumeration = alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT"); ALCenum error; error = alGetError(); if (error != AL_NO_ERROR) LogError() << error; m_pContext = alcCreateContext(m_pDevice, NULL); if (!alcMakeContextCurrent(m_pContext)) LogError() << "Could not make current context"; ALfloat listenerOri[] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f }; alListener3f(AL_POSITION, 0, 0, 1.0f); error = alGetError(); if (error != AL_NO_ERROR) LogError() << error; alListener3f(AL_VELOCITY, 0, 0, 0); error = alGetError(); if (error != AL_NO_ERROR) LogError() << error; alListenerfv(AL_ORIENTATION, listenerOri); error = alGetError(); if (error != AL_NO_ERROR) LogError() << error; ALuint source; alGenSources((ALuint)1, &source); error = alGetError(); if (error != AL_NO_ERROR) LogError() << error; alSourcef(source, AL_PITCH, 1); error = alGetError(); if (error != AL_NO_ERROR) LogError() << error; alSourcef(source, AL_GAIN, 1); error = alGetError(); if (error != AL_NO_ERROR) LogError() << error; alSource3f(source, AL_POSITION, 0, 0, 0); error = alGetError(); if (error != AL_NO_ERROR) LogError() << error; alSource3f(source, AL_VELOCITY, 0, 0, 0); error = alGetError(); if (error != AL_NO_ERROR) LogError() << error; alSourcei(source, AL_LOOPING, AL_FALSE); error = alGetError(); if (error != AL_NO_ERROR) LogError() << error; ALuint buffer; alGenBuffers((ALuint)1, &buffer); error = alGetError(); if (error != AL_NO_ERROR) LogError() << error; ALsizei size, freq; ALenum format; ALvoid *data; ALboolean loop = AL_FALSE; alutLoadWAVFile(reinterpret_cast<ALbyte *>(const_cast<char *>("test.wav")), &format, &data, &size, &freq, &loop); error = alGetError(); if (error != AL_NO_ERROR) LogError() << error; alBufferData(buffer, format, data, size, freq); alSourcei(source, AL_BUFFER, buffer);alSourcePlay(source); alSourcePlay(source); error = alGetError(); if (error != AL_NO_ERROR) LogError() << error; ALint source_state; alGetSourcei(source, AL_SOURCE_STATE, &source_state); LogInfo() << "Play sound..."; while (source_state == AL_PLAYING) { alGetSourcei(source, AL_SOURCE_STATE, &source_state); error = alGetError(); if (error != AL_NO_ERROR) LogError() << error; LogInfo() << "Play sound..."; } // cleanup context alDeleteSources(1, &source); alDeleteBuffers(1, &buffer); m_pDevice = alcGetContextsDevice(m_pContext); alcMakeContextCurrent(NULL); alcDestroyContext(m_pContext); }
ALboolean loadALData() { // Variables to load into. ALenum format; ALsizei size; ALvoid* data; ALsizei freq; ALboolean loop; // Load wav data into buffers. alGenBuffers(NUM_BUFFERS, Buffers); if (alGetError() != AL_NO_ERROR) return AL_FALSE; alutLoadWAVFile("Data/loop.wav", &format, &data, &size, &freq, &loop); alBufferData(Buffers[LOOP], format, data, size, freq); alutUnloadWAV(format, data, size, freq); alutLoadWAVFile("Data/minestep.wav", &format, &data, &size, &freq, &loop); alBufferData(Buffers[MINE], format, data, size, freq); alutUnloadWAV(format, data, size, freq); alutLoadWAVFile("Data/alarm001.wav", &format, &data, &size, &freq, &loop); alBufferData(Buffers[WARNING], format, data, size, freq); alutUnloadWAV(format, data, size, freq); // Bind buffers into audio sources. alGenSources(NUM_SOURCES, Sources); if (alGetError() != AL_NO_ERROR) return AL_FALSE; alSourcei (Sources[LOOP], AL_BUFFER, Buffers[LOOP] ); alSourcef (Sources[LOOP], AL_PITCH, 1.0 ); alSourcef (Sources[LOOP], AL_GAIN, 1.0 ); alSourcefv(Sources[LOOP], AL_POSITION, SourcePos[LOOP]); alSourcefv(Sources[LOOP], AL_VELOCITY, SourceVel[LOOP]); alSourcei (Sources[LOOP], AL_LOOPING, AL_TRUE ); alSourcei (Sources[MINE], AL_BUFFER, Buffers[MINE] ); alSourcef (Sources[MINE], AL_PITCH, 1.0 ); alSourcef (Sources[MINE], AL_GAIN, 1.0 ); alSourcefv(Sources[MINE], AL_POSITION, SourcePos[MINE]); alSourcefv(Sources[MINE], AL_VELOCITY, SourceVel[MINE]); alSourcei (Sources[MINE], AL_LOOPING, AL_FALSE ); alSourcei (Sources[WARNING], AL_BUFFER, Buffers[WARNING] ); alSourcef (Sources[WARNING], AL_PITCH, 1.0 ); alSourcef (Sources[WARNING], AL_GAIN, 1.0 ); alSourcefv(Sources[WARNING], AL_POSITION, SourcePos[WARNING]); alSourcefv(Sources[WARNING], AL_VELOCITY, SourceVel[WARNING]); alSourcei (Sources[WARNING], AL_LOOPING, AL_FALSE ); // Do another error check and return. if( alGetError() != AL_NO_ERROR) return AL_FALSE; return AL_TRUE; }
INT CLcxSndAL::CreateFromFile(LC_HANDLE p1, LC_HANDLE p2, LC_HANDLE p3, LC_HANDLE p4) { INT hr = LC_EFAIL; char* orgFile = (char*)p1; UINT SndF = 0; // Audio Format UINT SndB = 0; // Audio Bit Rate UINT SndC = 0; // Channels UINT SndHz= 0; // Sampling Rate BYTE* Buff = NULL; // Audio Buffer UINT Size = 0; // Buffer Size char sDest[LC_MAX_PATH2]={0}; char drive[LC_MAX_DRIVE]={0}; char dir [LC_MAX_DIR ]={0}; char fname[LC_MAX_FNAME]={0}; char ext [LC_MAX_EXT ]={0}; char sMedia[LC_MAX_PATH2]={0}; // check reference count if(0 > (CLcxSndAL::m_refSnd+1)) { LOGE("The reference count of sound instance is out of bound.\n"); return LC_EFAIL; } // get the file name and extension LcStr_SplitPath((char*)orgFile, drive, dir, fname, ext); sprintf(sDest, "%s%s%s%s", drive, dir, fname, ".gpsf"); // Load gpsf file in embedded systems // for iphone, android #if !defined(_MSC_VER) && !defined(_PC_LINUX_) if(LC_FAILED(LcSys_ResourcePath(sMedia, sDest))) return LC_CANNT_FIND_RESOURCE; hr = Gps_FileRead(&Buff, &SndF, &SndB, &SndC, &SndHz, &Size, sMedia); if(LC_FAILED(hr)) { SAFE_FREE( Buff ); LOGE("Err: %d, %s \t", __LINE__, __FILE__); LOGE("Read Sound File Failed: %s\n", (char*)orgFile); hr = LC_CANNT_FIND_RESOURCE; } else { if ( 8 == SndB && 1 == SndC) m_iFormat = AL_FORMAT_MONO8; else if(16 == SndB && 1 == SndC) m_iFormat = AL_FORMAT_MONO16; else if( 8 == SndB && 2 == SndC) m_iFormat = AL_FORMAT_STEREO8; else if(16 == SndB && 2 == SndC) m_iFormat = AL_FORMAT_STEREO16; if(0 == m_iFormat) { SAFE_FREE( Buff ); LOGE("Err: %d, %s \t", __LINE__, __FILE__); LOGE("Sound File Format Error: %s\n", (char*)orgFile); hr = LC_INVALID_RESOURCE; } m_uiSize = Size; m_uiFreq = SndHz; } if(LC_FAILED(hr)) return hr; #endif if(LC_FAILED(hr)) { if(LC_FAILED(LcSys_ResourcePath(sMedia, orgFile))) return LC_CANNT_FIND_RESOURCE; // Load Wave File alutLoadWAVFile((ALbyte*)sMedia, (ALenum*)&m_iFormat, (void**)&Buff , (ALsizei*)&m_uiSize, (ALsizei*)&m_uiFreq, (ALboolean*)&m_bLoop); if(NULL == Buff || 0 == m_iFormat || 0 == m_uiSize || 0 == m_uiFreq) { SAFE_FREE( Buff ); LOGE("Err: %d, %s \t", __LINE__, __FILE__); LOGE("Load WAV File Failed\n"); hr = LC_INVALID_RESOURCE; return hr; } Size = m_uiSize; SndHz = m_uiFreq; if(AL_FORMAT_MONO8 == m_iFormat) { SndB = 8; SndC = 1; } else if(AL_FORMAT_MONO16 == m_iFormat) { SndB = 16; SndC = 1; } else if(AL_FORMAT_STEREO8 == m_iFormat) { SndB = 8; SndC = 2; } else if(AL_FORMAT_STEREO16 == m_iFormat){ SndB = 16; SndC = 2; } hr = Gps_FileWrite(Buff, LCX_AV_PCM, SndB, SndC, SndHz, Size, sDest); SAFE_FREE( Buff ); if(LC_FAILED(hr)) { LOGE("Err: %d, %s \t", __LINE__, __FILE__); LOGE("Write Sound File Failed: %s\n", (char*)orgFile); hr = LC_INVALID_RESOURCE; return hr; } // reload hr = Gps_FileRead(&Buff, &SndF, &SndB, &SndC, &SndHz, &Size, sDest); if(LC_FAILED(hr)) { SAFE_FREE( Buff ); LOGE("Err: %d, %s \t", __LINE__, __FILE__); LOGE("Read Sound File Failed: %s\n", (char*)sDest); hr = LC_INVALID_RESOURCE; return hr; } if ( 8 == SndB && 1 == SndC) m_iFormat = AL_FORMAT_MONO8; else if(16 == SndB && 1 == SndC) m_iFormat = AL_FORMAT_MONO16; else if( 8 == SndB && 2 == SndC) m_iFormat = AL_FORMAT_STEREO8; else if(16 == SndB && 2 == SndC) m_iFormat = AL_FORMAT_STEREO16; if(0 == m_iFormat) { SAFE_FREE( Buff ); LOGE("Err: %d, %s \t", __LINE__, __FILE__); LOGE("Sound File Format Error: %s\n", (char*)sMedia); hr = LC_INVALID_RESOURCE; return hr; } } if(0 == CLcxSndAL::m_refSnd) { hr = LcxSmd_ALCDeviceOpen(); if(LC_FAILED(hr)) { SAFE_FREE( Buff ); return hr; } } hr = CreateALSound(Buff); SAFE_FREE( Buff ); if(LC_FAILED(hr)) { if(0 == CLcxSndAL::m_refSnd) LcxSmd_ALCDeviceClose(); } ++CLcxSndAL::m_refSnd; return LC_OK; }
SoundID Audio::addSound(const char *fileName, unsigned int flags){ Sound sound; // Clear error flag alGetError(); const char *ext = strrchr(fileName, '.') + 1; char str[256]; if (stricmp(ext, "ogg") == 0){ FILE *file = fopen(fileName, "rb"); if (file == NULL){ sprintf(str, "Couldn't open \"%s\"", fileName); ErrorMsg(str); return SOUND_NONE; } OggVorbis_File vf; memset(&vf, 0, sizeof(vf)); if (ov_open(file, &vf, NULL, 0) < 0){ fclose(file); sprintf(str, "\"%s\" is not an ogg file", fileName); ErrorMsg(str); return SOUND_NONE; } vorbis_info *vi = ov_info(&vf, -1); int nSamples = (uint) ov_pcm_total(&vf, -1); int nChannels = vi->channels; sound.format = nChannels == 1? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; sound.sampleRate = vi->rate; sound.size = nSamples * nChannels; sound.samples = new short[sound.size]; sound.size *= sizeof(short); int samplePos = 0; while (samplePos < sound.size){ char *dest = ((char *) sound.samples) + samplePos; int bitStream, readBytes = ov_read(&vf, dest, sound.size - samplePos, 0, 2, 1, &bitStream); if (readBytes <= 0) break; samplePos += readBytes; } ov_clear(&vf); } else { ALboolean al_bool; ALvoid *data; alutLoadWAVFile(fileName, &sound.format, &data, &sound.size, &sound.sampleRate, &al_bool); sound.samples = (short *) data; } alGenBuffers(1, &sound.buffer); alBufferData(sound.buffer, sound.format, sound.samples, sound.size, sound.sampleRate); if (alGetError() != AL_NO_ERROR){ alDeleteBuffers(1, &sound.buffer); sprintf(str, "Couldn't open \"%s\"", fileName); ErrorMsg(str); return SOUND_NONE; } return insertSound(sound); }
/* * 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; }