diff --git a/sound/outputs/openal_out.cpp b/sound/outputs/openal_out.cpp index 99b976bc9..e6d0b3ae7 100644 --- a/sound/outputs/openal_out.cpp +++ b/sound/outputs/openal_out.cpp @@ -27,7 +27,13 @@ static void checkALError(const std::string &msg) { ALenum err = alGetError(); if(err != AL_NO_ERROR) - fail("\"" + std::string(alGetString(err)) + "\" while " + msg); + { + const ALchar* errmsg = alGetString(err); + if(errmsg) + fail("\"" + std::string(alGetString(err)) + "\" while " + msg); + else + fail("non-specified error while " + msg + " (did you forget to initialize OpenAL?)"); + } } static void getALFormat(SampleSourcePtr inp, int &fmt, int &rate) diff --git a/sound/outputs/openal_out.h b/sound/outputs/openal_out.h index 335880cda..2ec593665 100644 --- a/sound/outputs/openal_out.h +++ b/sound/outputs/openal_out.h @@ -21,8 +21,13 @@ class OpenAL_Sound : public Sound int *refCnt; public: + /// Read samples from the given input buffer OpenAL_Sound(SampleSourcePtr input); - OpenAL_Sound(ALuint buf, int *ref); // Used for cloning + + /// Play an existing buffer, with a given ref counter. Used + /// internally for cloning. + OpenAL_Sound(ALuint buf, int *ref); + ~OpenAL_Sound(); void play(); diff --git a/sound/sources/audiere_source.cpp b/sound/sources/audiere_source.cpp index 8da4fe3c7..8a6b57ada 100644 --- a/sound/sources/audiere_source.cpp +++ b/sound/sources/audiere_source.cpp @@ -135,6 +135,8 @@ void AudiereSource::setup() int channels, rate; sample->getFormat(channels, rate, fmt); + pullSize = 0; + // Calculate the size of one frame frameSize = GetSampleSize(fmt) * channels; diff --git a/sound/sources/stream_source.h b/sound/sources/stream_source.h index 278f701e8..42791ccdf 100644 --- a/sound/sources/stream_source.h +++ b/sound/sources/stream_source.h @@ -10,10 +10,10 @@ namespace Sound { class Stream2Samples : public SampleSource { int32_t rate, channels, bits; - Stream::StreamPtr inp; + Mangle::Stream::StreamPtr inp; public: - Stream2Samples(Stream::StreamPtr _inp, int32_t _rate, int32_t _channels, int32_t _bits) + Stream2Samples(Mangle::Stream::StreamPtr _inp, int32_t _rate, int32_t _channels, int32_t _bits) : inp(_inp), rate(_rate), channels(_channels), bits(_bits) { isSeekable = inp->isSeekable; @@ -24,7 +24,7 @@ class Stream2Samples : public SampleSource /// Get the sample rate, number of channels, and bits per /// sample. NULL parameters are ignored. - void getInfo(int32_t *_rate, int32_t *_channels, int32_t *_bits) const + void getInfo(int32_t *_rate, int32_t *_channels, int32_t *_bits) { if(_rate) *_rate = rate; if(_channels) *_channels = channels; diff --git a/sound/tests/Makefile b/sound/tests/Makefile index ca7a933c6..75fb1e9e6 100644 --- a/sound/tests/Makefile +++ b/sound/tests/Makefile @@ -1,6 +1,6 @@ GCC=g++ -I../ -all: openal_audiere_test +all: audiere_source_test openal_output_test openal_audiere_test #L_FFMPEG=$(shell pkg-config --libs libavcodec libavformat) L_OPENAL=$(shell pkg-config --libs openal) @@ -9,5 +9,11 @@ L_AUDIERE=-laudiere openal_audiere_test: openal_audiere_test.cpp ../sources/audiere_source.cpp ../outputs/openal_out.cpp ../../stream/clients/audiere_file.cpp $(GCC) $^ -o $@ $(L_AUDIERE) $(L_OPENAL) +openal_output_test: openal_output_test.cpp ../outputs/openal_out.cpp + $(GCC) $^ -o $@ $(L_OPENAL) + +audiere_source_test: audiere_source_test.cpp ../sources/audiere_source.cpp ../../stream/clients/audiere_file.cpp + $(GCC) $^ -o $@ $(L_AUDIERE) + clean: rm *_test diff --git a/sound/tests/audiere_source_test.cpp b/sound/tests/audiere_source_test.cpp new file mode 100644 index 000000000..6dba37b51 --- /dev/null +++ b/sound/tests/audiere_source_test.cpp @@ -0,0 +1,69 @@ +#include + +#include "../../stream/servers/file_stream.h" +#include "../sources/audiere_source.h" +#include "../../stream/filters/buffer_stream.h" + +#include +#include + +using namespace std; +using namespace Mangle::Stream; +using namespace Mangle::Sound; + +// Contents and size of cow.raw +void *orig; +size_t orig_size; + +void run(SampleSourcePtr &src) +{ + size_t ss = src->size(); + assert(ss == orig_size); + + cout << "Source size: " << ss << endl; + int rate, channels, bits; + src->getInfo(&rate, &channels, &bits); + cout << "rate=" << rate << "\nchannels=" << channels + << "\nbits=" << bits << endl; + + cout << "Reading entire buffer into memory\n"; + void *buf = malloc(ss); + src->read(buf, ss); + + cout << "Comparing...\n"; + if(memcmp(buf, orig, ss) != 0) + { + cout << "Oops!\n"; + assert(0); + } + + cout << "Done\n"; +} + +int main() +{ + { + cout << "Reading cow.raw first\n"; + FileStream tmp("cow.raw"); + orig_size = tmp.size(); + cout << "Size: " << orig_size << endl; + orig = malloc(orig_size); + tmp.read(orig, orig_size); + cout << "Done\n"; + } + + { + cout << "\nLoading cow.wav by filename:\n"; + SampleSourcePtr cow_file( new AudiereSource("cow.wav") ); + run(cow_file); + } + + { + cout << "\nLoading cow.wav by stream:\n"; + StreamPtr inp( new FileStream("cow.wav") ); + SampleSourcePtr cow_stream( new AudiereSource(inp) ); + run(cow_stream); + } + + return 0; +} diff --git a/sound/tests/cow.raw b/sound/tests/cow.raw new file mode 100644 index 000000000..c4d155bbf Binary files /dev/null and b/sound/tests/cow.raw differ diff --git a/sound/tests/openal_audiere_test.cpp b/sound/tests/openal_audiere_test.cpp index a39ae3d98..9c49356a2 100644 --- a/sound/tests/openal_audiere_test.cpp +++ b/sound/tests/openal_audiere_test.cpp @@ -1,9 +1,7 @@ #include -#include #include #include "../../stream/servers/file_stream.h" -#include "../../stream/filters/buffer_stream.h" #include "../filters/openal_audiere.h" using namespace std; diff --git a/sound/tests/openal_output_test.cpp b/sound/tests/openal_output_test.cpp new file mode 100644 index 000000000..11b2fc42e --- /dev/null +++ b/sound/tests/openal_output_test.cpp @@ -0,0 +1,46 @@ +#include +#include + +#include "../../stream/servers/file_stream.h" +#include "../sources/stream_source.h" +#include "../outputs/openal_out.h" + +using namespace std; +using namespace Mangle::Stream; +using namespace Mangle::Sound; + +int main() +{ + cout << "Loading cow.raw\n"; + + int rate = 11025; + int chan = 1; + int bits = 16; + + cout << " rate=" << rate << "\n channels=" << chan + << "\n bits=" << bits << endl; + + StreamPtr file( new FileStream("cow.raw") ); + SampleSourcePtr source( new Stream2Samples( file, rate, chan, bits)); + + cout << "Playing\n"; + + // This initializes OpenAL for us, and serves no other purpose. + OpenAL_Factory mg; + + OpenAL_Sound snd(source); + try + { + snd.play(); + + while(snd.isPlaying()) + { + usleep(10000); + } + } + catch(exception &e) + { + cout << " ERROR: " << e.what() << "\n"; + } + return 0; +}