bool SoundData::load(const std::string & filename) { byte signature[4]; bool isWav = false; FILE* fp = fopen(filename.c_str(), "rb"); if (!fp) { return false; } fread(signature, 1, 4, fp); fclose(fp); if (signature[0] == 'R' && signature[1] == 'I' && signature[2] == 'F' && signature[3] == 'F') { isWav = true; } return isWav ? loadWav(filename) : loadOgg(filename); }
int Audio::load(Path file){ // Variables to load into. if(!os().fileExists(file)){ logs().audio.write("[AudioObject] Error, file '"+file.getRelative()+"' doesn't exist"); console().write("audio error: file '"+file.getRelative()+"' doesn't exist"); return -1; } for(int i=0; i<sources.size(); i++){ if(sources[i]->filename==file){ console().write("NOTE: the file "+file.getRelative()+" was already loaded as another audio object. This is not an error, but may not be what was intended"); } } String filetype=file.getExtension(); if(filetype=="wav"){ if(hardware.audio){ return loadWav(file); }else{ return loadFakeWav(file); } }else if(filetype=="ogg"){ #ifdef FEATURE_OGG if(hardware.audio){ #ifdef OGG_STREAM return loadOggStream(file); #else return loadOgg(file); #endif }else{ return loadFakeOgg(file); } #else console().write("audio error: this version of the engine can't open oggs"); return -1; #endif }else{ logs().audio.write("[AudioObject] error: unknown filetype '"+filetype+"' for file '"+file.getRelative()+"'"); console().write("audio error: unknown filetype '"+filetype+"' for file '"+file.getRelative()+"'"); return -1; } }
bool SoundData::load(const byte * buffer, Uint32 bufferSize) { if (!buffer || bufferSize == 0) { return false; } if (buffer[0] == 'R' && buffer[1] == 'I' && buffer[2] == 'F' && buffer[3] == 'F') { return loadWav(buffer, bufferSize); } return loadOgg(buffer, bufferSize); }
unsigned OpenALAudio::load( const fs::path &filename ) { ALuint bufferId = 0; alGenBuffers( 1, &bufferId ); FILE *file = fopen( filename.string().c_str(), "rb" ); if ( !file ) throw OpenALAudioExc( "Error opening " + filename.string() ); string extension = filename.extension().string(); if ( extension == ".ogg" ) { loadOgg( filename, file, bufferId ); } else { throw OpenALAudioExc( "Unknown file format " + extension ); } return bufferId; }
SoundManager::SoundManager(QString soundDirectory) { QString applicationDirectory; QString completeSoundDirectory; char cwd[PATH_MAX]; // Initialize ALUT. if (alutInit(NULL, NULL) == false) { reportALUTError(alutGetError()); } // Get the complete application directory in which we will load sounds from. // We convert to QString since it is more convenient when working with directories. getcwd(cwd, PATH_MAX); applicationDirectory = QString(cwd); // Append the assets directory and the actual sounds directory name. completeSoundDirectory = applicationDirectory .append("/app/native/assets/") .append(soundDirectory); // Create OpenAL buffers from all files in the sound directory. QDir dir(completeSoundDirectory); if (!dir.exists()) { qDebug() << "Cannot find the sounds directory." << completeSoundDirectory; } else { // Set a filter for file listing, only files should be listed. dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot); // Get a directory listing. QFileInfoList list = dir.entryInfoList(); // Traverse and load all the audio files into buffers. for (int i = 0; i < list.size(); ++i) { // Create a file info for each audio file. QFileInfo fileInfo = list.at(i); const char* path = fileInfo.absoluteFilePath().toStdString().c_str(); ALenum error; // Generate buffers to hold audio data. alGenBuffers(1, &mSoundBuffers[fileInfo.fileName()]); error = alGetError(); if (error != AL_NO_ERROR) { reportOpenALError(error); break; } // Load sound file. FILE* file = fopen(path, "rb"); if (!file) { qDebug() << "Failed to load audio file " << path; break; } // Read the file header char header[12]; ALuint bufferId = mSoundBuffers[fileInfo.fileName()]; if (fread(header, 1, 12, file) != 12) { qDebug() << "Invalid header for audio file " << path; alDeleteBuffers(1, &bufferId); goto cleanup; } // Check the file format & load the buffer with audio data. if (memcmp(header, "RIFF", 4) == 0) { if (!loadWav(file, bufferId)) { qDebug() << "Invalid wav file: " << path; alDeleteBuffers(1, &bufferId); goto cleanup; } } else if (memcmp(header, "OggS", 4) == 0) { if (!loadOgg(file, bufferId)) { qDebug() << "Invalid ogg file: " << path; alDeleteBuffers(1, &bufferId); goto cleanup; } } else { qDebug() << "Unsupported audio file: " << path; goto cleanup; } cleanup: if (file) { fclose(file); } } } // Generate a number of sources used to attach buffers and play. alGenSources(SOUNDMANAGER_MAX_NBR_OF_SOURCES, mSoundSources); ALenum error = alGetError(); if (error != AL_NO_ERROR) { reportOpenALError(error); } }
Stream::Stream(const Stream &obj) { loadOgg(obj.sound.c_str()); if (obj.playing()) play(); }