/******************************************************************************* Copyright (c) 2009, Charles McGarvey All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include #include #include #include #include #include "Mippleton.hh" #include "Sound.hh" namespace Mf { struct Sound::Impl { static ALenum getAudioFormat(const Sound_AudioInfo& audioInfo) { if (audioInfo.format == AUDIO_U8 || audioInfo.format == AUDIO_S8) { if (audioInfo.channels == 1) return AL_FORMAT_MONO8; else return AL_FORMAT_STEREO8; } else { if (audioInfo.channels == 1) return AL_FORMAT_MONO16; else return AL_FORMAT_STEREO16; } } struct Buffer : public Mippleton { Buffer(const std::string& name) : Mippleton(name), object(0) {} ~Buffer() { alDeleteBuffers(1, &object); } void loadFromFile(const std::string& filePath, bool stream) { if (object != 0) return; Sound_Sample* sound = Sound_NewSampleFromFile(filePath.c_str(), NULL, 8096); if (!sound) { std::cerr << "could not load sound from file" << std::endl; exit(1); } unsigned decoded = Sound_DecodeAll(sound); if (decoded == 0) { std::cout << "decoded no bytes" << std::endl; exit(1); } std::cerr << "buffer size: " << sound->buffer_size << std::endl; std::cerr << "channels: " << (int)sound->actual.channels << std::endl; std::cerr << "format: " << sound->actual.format << std::endl; std::cerr << "frequency: " << sound->actual.rate << std::endl; alGenBuffers(1, &object); alBufferData(object, getAudioFormat(sound->actual), sound->buffer, sound->buffer_size, sound->actual.rate); Sound_FreeSample(sound); } ALuint object; //ALfloat location[] = {0.0f, 0.0f, 0.0f}; //ALfloat location2[] = {0.0f, 0.0f, 0.0f}; //ALfloat orient[] = {0.0f, 0.0f, -1.0f, 0.0, 1.0, 0.0}; //alListenerfv(AL_POSITION, location); //alListenerfv(AL_VELOCITY, location); //alListenerfv(AL_VELOCITY, orient); }; Impl(const std::string& name, bool stream = false) : buffer_(Buffer::retain(name), Buffer::release) { if (!stream) buffer_->loadFromFile(Sound::getPath(name), stream); else buffer_->loadFromFile(SoundStream::getPath(name), stream); ALfloat location[] = {0.0f, 0.0f, 0.0f}; alGenSources(1, &source_); alSourcei(source_, AL_BUFFER, buffer_->object); alSourcef(source_, AL_PITCH, 1.0f); alSourcef(source_, AL_GAIN, 1.0f); alSourcefv(source_, AL_POSITION, location); alSourcefv(source_, AL_VELOCITY, location); alSourcei(source_, AL_LOOPING, AL_FALSE); } ~Impl() { alDeleteSources(1, &source_); } void update() { } boost::shared_ptr buffer_; ALuint source_; }; Sound::Sound(const std::string& name) : // pass through impl_(new Sound::Impl(name)) {} void Sound::play() { alSourceRewind(impl_->source_); alSourcePlay(impl_->source_); } std::string Sound::getPath(const std::string& name) { std::string path = Resource::getPath("sounds/" + name + ".ogg"); return path; } //############################################################################## SoundStream::SoundStream(const std::string& name) // pass through //impl_(name, true) {} { impl_ = boost::shared_ptr(new Sound::Impl(name, true)); } void SoundStream::update(Scalar t, Scalar dt) { // pass through impl_->update(); } std::string SoundStream::getPath(const std::string& name) { std::string path = Resource::getPath("sounds/" + name + ".xm"); return path; } } // namespace Mf /** vim: set ts=4 sw=4 tw=80: *************************************************/