From: Mikko Rasa Date: Wed, 13 Feb 2008 22:37:53 +0000 (+0000) Subject: Initial revision X-Git-Tag: 0.9~6 X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=35ea5cc8d1eb8c806885d27441548f630e7b1266;p=libs%2Fal.git Initial revision --- diff --git a/Build b/Build new file mode 100644 index 0000000..678d98d --- /dev/null +++ b/Build @@ -0,0 +1,16 @@ +package "mspal" +{ + version "0.0"; + description "C++ wrapper for OpenAL"; + + require "vorbisfile"; + require "openal"; + require "mspcore"; + + library "mspal" + { + source "source"; + install true; + install_headers "msp/al"; + }; +}; diff --git a/source/buffer.cpp b/source/buffer.cpp new file mode 100644 index 0000000..2a33b83 --- /dev/null +++ b/source/buffer.cpp @@ -0,0 +1,40 @@ +/* $Id$ + +This file is part of libmspal +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Diestributed under the LGPL +*/ + +#include "buffer.h" +#include "sound.h" + +using namespace std; + +namespace Msp { +namespace AL { + +Buffer::Buffer() +{ + alGenBuffers(1, &id); +} + +Buffer::~Buffer() +{ + alDeleteBuffers(1, &id); +} + +void Buffer::data(Format fmt, const void *dt, sizei size, sizei freq) +{ + alBufferData(id, fmt, dt, size, freq); +} + +void Buffer::load_data(const string &fn) +{ + Sound sound; + sound.load(fn); + + data(sound.get_format(), sound.get_data(), sound.get_size(), sound.get_frequency()); +} + +} // namespace AL +} // namespace Msp diff --git a/source/buffer.h b/source/buffer.h new file mode 100644 index 0000000..00d1add --- /dev/null +++ b/source/buffer.h @@ -0,0 +1,36 @@ +/* $Id$ + +This file is part of libmspal +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Diestributed under the LGPL +*/ + +#ifndef MSP_AL_BUFFER_H_ +#define MSP_AL_BUFFER_H_ + +#include +#include +#include "format.h" +#include "types.h" + +namespace Msp { +namespace AL { + +class Buffer +{ +private: + uint id; + +public: + Buffer(); + ~Buffer(); + + uint get_id() const { return id; } + void data(Format, const void *, sizei, sizei); + void load_data(const std::string &); +}; + +} // namespace AL +} // namespace Msp + +#endif diff --git a/source/context.cpp b/source/context.cpp new file mode 100644 index 0000000..aa316fd --- /dev/null +++ b/source/context.cpp @@ -0,0 +1,25 @@ +/* $Id$ + +This file is part of libmspal +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Diestributed under the LGPL +*/ + +#include +#include "context.h" +#include "device.h" + +namespace Msp { +namespace AL { + +Context::Context(Device &dev) +{ + context=alcCreateContext(dev.get_device(), 0); + if(!context) + throw Exception("Couldn't create OpenAL context"); + + alcMakeContextCurrent(context); +} + +} // namespace AL +} // namespace Msp diff --git a/source/context.h b/source/context.h new file mode 100644 index 0000000..93cb0e8 --- /dev/null +++ b/source/context.h @@ -0,0 +1,30 @@ +/* $Id$ + +This file is part of libmspal +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Diestributed under the LGPL +*/ + +#ifndef MSP_AL_CONTEXT_H_ +#define MSP_AL_CONTEXT_H_ + +#include + +namespace Msp { +namespace AL { + +class Device; + +class Context +{ +private: + ALCcontext *context; + +public: + Context(Device &); +}; + +} // namespace AL +} // namespace Msp + +#endif diff --git a/source/device.cpp b/source/device.cpp new file mode 100644 index 0000000..422b7ea --- /dev/null +++ b/source/device.cpp @@ -0,0 +1,31 @@ +/* $Id$ + +This file is part of libmspal +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Diestributed under the LGPL +*/ + +#include +#include "device.h" + +using namespace std; + +namespace Msp { +namespace AL { + +Device::Device() +{ + dev=alcOpenDevice(0); + if(!dev) + throw Exception("Couldn't get OpenAL device"); +} + +Device::Device(const string &spec) +{ + dev=alcOpenDevice(spec.c_str()); + if(!dev) + throw Exception("Couldn't get OpenAL device"); +} + +} // namespace AL +} // namespace Msp diff --git a/source/device.h b/source/device.h new file mode 100644 index 0000000..4c80d9e --- /dev/null +++ b/source/device.h @@ -0,0 +1,31 @@ +/* $Id$ + +This file is part of libmspal +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Diestributed under the LGPL +*/ + +#ifndef MSP_AL_DEVICE_H_ +#define MSP_AL_DEVICE_H_ + +#include +#include + +namespace Msp { +namespace AL { + +class Device +{ +private: + ALCdevice *dev; + +public: + Device(); + Device(const std::string &); + ALCdevice *get_device() { return dev; } +}; + +} // namespace AL +} // namespace Msp + +#endif diff --git a/source/format.h b/source/format.h new file mode 100644 index 0000000..624fb60 --- /dev/null +++ b/source/format.h @@ -0,0 +1,27 @@ +/* $Id$ + +This file is part of libmspal +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Diestributed under the LGPL +*/ + +#ifndef MSP_AL_FORMAT_H_ +#define MSP_AL_FORMAT_H_ + +#include + +namespace Msp { +namespace AL { + +enum Format +{ + MONO8 = AL_FORMAT_MONO8, + MONO16 = AL_FORMAT_MONO16, + STEREO8 = AL_FORMAT_STEREO8, + STEREO16 = AL_FORMAT_STEREO16 +}; + +} // namespace AL +} // namespace Msp + +#endif diff --git a/source/listener.cpp b/source/listener.cpp new file mode 100644 index 0000000..04fc368 --- /dev/null +++ b/source/listener.cpp @@ -0,0 +1,39 @@ +/* $Id$ + +This file is part of libmspal +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Diestributed under the LGPL +*/ + +#include "listener.h" + +namespace Msp { +namespace AL { + +void Listener::attribute(ALenum attr, float v) +{ + alListenerf(attr, v); +} + +void Listener::attribute(ALenum attr, float v0, float v1, float v2) +{ + alListener3f(attr, v0, v1, v2); +} + +void Listener::attribute(ALenum attr, const float *v) +{ + alListenerfv(attr, v); +} + +void Listener::set_position(float x, float y, float z) +{ + attribute(AL_POSITION, x, y, z); +} + +void Listener::set_gain(float g) +{ + attribute(AL_GAIN, g); +} + +} // namespace AL +} // namespace Msp diff --git a/source/listener.h b/source/listener.h new file mode 100644 index 0000000..5725170 --- /dev/null +++ b/source/listener.h @@ -0,0 +1,31 @@ +/* $Id$ + +This file is part of libmspal +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Diestributed under the LGPL +*/ + +#ifndef MSP_AL_LIStENER_H_ +#define MSP_AL_LISTENER_H_ + +#include + +namespace Msp { +namespace AL { + +class Listener +{ +private: + Listener(); +public: + static void attribute(ALenum, float); + static void attribute(ALenum, float, float, float); + static void attribute(ALenum, const float *); + static void set_position(float, float, float); + static void set_gain(float); +}; + +} // namespace AL +} // namespace Msp + +#endif diff --git a/source/sound.cpp b/source/sound.cpp new file mode 100644 index 0000000..bc4db1e --- /dev/null +++ b/source/sound.cpp @@ -0,0 +1,79 @@ +/* $Id$ + +This file is part of libmspal +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Diestributed under the LGPL +*/ + +#include +#include "sound.h" + +using namespace std; + +namespace Msp { +namespace AL { + +Sound::Sound(): + data(0) +{ + ovfile.datasource=0; +} + +Sound::~Sound() +{ + if(ovfile.datasource) + ov_clear(&ovfile); +} + +void Sound::open(const string &fn) +{ + if(ov_fopen(const_cast(fn.c_str()), &ovfile)<0) + throw Exception("Could not open ogg vorbis file"); + + vorbis_info *info=ov_info(&ovfile, -1); + freq=info->rate; + switch(info->channels) + { + case 1: format=MONO16; break; + case 2: format=STEREO16; break; + default: throw Exception("Unsupported number of channels"); + } +} + +void Sound::load_data() +{ + if(data) + throw InvalidState("Data has already been loaded"); + + size=ov_pcm_total(&ovfile, 0)*4; + data=new char[size]; + unsigned pos=0; + while(unsigned len=read(data+pos, size-pos)) + pos+=len; + size=pos; +} + +void Sound::load(const string &fn) +{ + open(fn); + load_data(); +} + +const char *Sound::get_data() const +{ + if(!data) + throw InvalidState("Data has not been loaded"); + return data; +} + +unsigned Sound::read(char *buf, unsigned len) +{ + int section=0; + int res=ov_read(&ovfile, buf, len, 0, 2, 1, §ion); + if(res<0) + throw Exception("Error reading ogg vorbis file"); + return res; +} + +} // namespace AL +} // namespace Msp diff --git a/source/sound.h b/source/sound.h new file mode 100644 index 0000000..0e15a40 --- /dev/null +++ b/source/sound.h @@ -0,0 +1,43 @@ +/* $Id$ + +This file is part of libmspal +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Diestributed under the LGPL +*/ + +#ifndef MSP_AL_SOUND_H_ +#define MSP_AL_SOUND_H_ + +#include +#include "format.h" + +namespace Msp { +namespace AL { + +class Sound +{ +private: + OggVorbis_File ovfile; + unsigned freq; + unsigned size; + char *data; + Format format; + +public: + Sound(); + ~Sound(); + + void open(const std::string &); + void load_data(); + void load(const std::string &); + Format get_format() const { return format; } + unsigned get_frequency() const { return freq; } + unsigned get_size() const { return size; } + const char *get_data() const; + unsigned read(char *, unsigned); +}; + +} // namespace AL +} // namespace Msp + +#endif diff --git a/source/source.cpp b/source/source.cpp new file mode 100644 index 0000000..fec965c --- /dev/null +++ b/source/source.cpp @@ -0,0 +1,67 @@ +/* $Id$ + +This file is part of libmspal +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Diestributed under the LGPL +*/ + +#include "buffer.h" +#include "source.h" + +using namespace std; + +namespace Msp { +namespace AL { + +Source::Source() +{ + alGenSources(1, &id); +} + +Source::~Source() +{ + alDeleteSources(1, &id); +} + +void Source::attribute(ALenum attr, int v) +{ + alSourcei(id, attr, v); +} + +void Source::attribute(ALenum attr, float v) +{ + alSourcef(id, attr, v); +} + +void Source::attribute(ALenum attr, float v0, float v1, float v2) +{ + alSource3f(id, attr, v0, v1, v2); +} + +void Source::attribute(ALenum attr, const float *v) +{ + alSourcefv(id, attr, v); +} + +void Source::set_buffer(Buffer &buffer) +{ + attribute(AL_BUFFER, static_cast(buffer.get_id())); +} + +void Source::queue_buffers(const vector &buffers) +{ + vector ids; + ids.reserve(buffers.size()); + for(vector::const_iterator i=buffers.begin(); i!=buffers.end(); ++i) + ids.push_back((*i)->get_id()); + alSourceQueueBuffers(id, ids.size(), &ids.front()); +} + +void Source::queue_buffer(Buffer &buffer) +{ + uint bid=buffer.get_id(); + alSourceQueueBuffers(id, 1, &bid); +} + +} // namespace AL +} // namespace Msp diff --git a/source/source.h b/source/source.h new file mode 100644 index 0000000..234dcb8 --- /dev/null +++ b/source/source.h @@ -0,0 +1,40 @@ +/* $Id$ + +This file is part of libmspal +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Diestributed under the LGPL +*/ + +#ifndef MSP_AL_SOURCE_H_ +#define MSP_AL_SOURCE_H_ + +#include +#include + +namespace Msp { +namespace AL { + +class Buffer; + +class Source +{ +private: + uint id; + +public: + Source(); + ~Source(); + + void attribute(ALenum, int); + void attribute(ALenum, float); + void attribute(ALenum, float, float, float); + void attribute(ALenum, const float *); + void set_buffer(Buffer &); + void queue_buffers(const std::vector &); + void queue_buffer(Buffer &); +}; + +} // namespace AL +} // namespace Msp + +#endif diff --git a/source/types.h b/source/types.h new file mode 100644 index 0000000..e1f7f01 --- /dev/null +++ b/source/types.h @@ -0,0 +1,20 @@ +/* $Id$ + +This file is part of libmspal +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Diestributed under the LGPL +*/ + +#ifndef MSP_AL_TYPES_H_ +#define MSP_AL_TYPES_H_ + +namespace Msp { +namespace AL { + +typedef unsigned uint; +typedef unsigned sizei; + +} // namespace AL +} // namespace Msp + +#endif