Handle<Value> ALUTLoadMemoryFromFileCallback(const Arguments& args) { //if less that nbr of formal parameters then do nothing if (args.Length() < 4) return v8::Undefined(); //get arguments String::Utf8Value value0(args[0]); char* arg0 = *value0; Handle<Array> arg1 = Array::Cast(args[1]); Handle<Array> arg2 = Array::Cast(args[2]); Handle<Array> arg3 = Array::Cast(args[3]); //make call alutLoadMemoryFromFile((const char*)arg0, (ALenum*)arg1, (ALsizei*)arg2, (ALfloat*)arg3); return v8::Undefined(); }
Sound::Sound() { // Init openAL alutInit(0, NULL); // Clear Error Code (so we can catch any new errors) if (alGetError() != AL_NO_ERROR) std::cerr << "Error in Sound class!\n"; alGenBuffers(NUM_SOUNDS, buffers_); alGenSources(NUM_SOUNDS, source_); if (alGetError() != AL_NO_ERROR) std::cerr << "Error in Sound class!\n"; std::cout << "Loading sound files"; float zeros[3] = {0.0f, 0.0f, 0.0f}; for (unsigned int i = 0; i < NUM_SOUNDS; ++i) { ALenum format; ALsizei size; ALfloat freq; ALvoid* data = alutLoadMemoryFromFile( SoundFiles[i].c_str(), &format, &size, &freq); alBufferData(buffers_[i],format,data,size,freq); //free(data); if (alGetError() != AL_NO_ERROR) std::cerr << "Error in Sound class!\n"; alSourcei(source_[i], AL_BUFFER, buffers_[i]); alSourcefv (source_[i], AL_VELOCITY, zeros); alSourcefv (source_[i], AL_DIRECTION, zeros); std::cout << ".."; } std::cout << "done." << std::endl; alListenerfv(AL_POSITION,zeros); alListenerfv(AL_VELOCITY,zeros); float orientation[6] = { 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f }; alListenerfv(AL_ORIENTATION,orientation); }
//based on http://ffainelli.github.io/openal-example/ Sound::Sound(std::string path) { int LOAD_OK = 1; ALenum format; ALsizei size; ALvoid* data; ALfloat freq; if(alGetError() != AL_NO_ERROR) LOAD_OK = 0; audioBuffer = 0; audioSource = 0; if(LOAD_OK == 1) { alGenBuffers(1,&audioBuffer); if (alGetError() != AL_NO_ERROR) LOAD_OK = 0; } if(LOAD_OK == 1) { alGenSources(1,&audioSource); if(alGetError() != AL_NO_ERROR) LOAD_OK = 0; } if(LOAD_OK == 1) { data = alutLoadMemoryFromFile(path.c_str(),&format,&size,&freq); LOAD_OK = (data!=NULL) ? 1:0; } if(LOAD_OK == 1) { alBufferData(audioBuffer, format, data, size, (ALsizei)freq); if(alGetError() != AL_NO_ERROR) LOAD_OK = 0; } if(LOAD_OK == 1) { // Unload the WAV file. It's not needed now. alutUnloadWAV(format, data, size,(ALsizei)freq); if(alGetError() != AL_NO_ERROR) LOAD_OK = 0; } if(LOAD_OK == 1) { alSourcei(audioSource,AL_BUFFER,audioBuffer); if (alGetError()!=AL_NO_ERROR) LOAD_OK = 0; } if(LOAD_OK == 1) { // Position of the source of the sound. ALfloat sourcePosition[] = { 0.0f, 0.0f, 0.0f }; // Velocity of the source of the sound. ALfloat sourceVelocity[] = { 0.0f, 0.0f, 0.0f }; alSourcef(audioSource, AL_PITCH, 1.0f); alSourcef(audioSource, AL_GAIN, 1.0f); alSourcefv(audioSource, AL_POSITION, sourcePosition); alSourcefv(audioSource, AL_VELOCITY, sourceVelocity); if(alGetError() != AL_NO_ERROR) LOAD_OK = 0; } if(!LOAD_OK) { printf("Failed to load sound: %s\n", path.c_str()); loaded = false; } else { loaded = true; } }