# HG changeset patch # User John Tsiombikas # Date 1348124665 -10800 # Node ID f9b8bbebc9b33a16cd2a6a168be581c03fe79c5d # Parent 4c427e28ca0085c0c01d7c217241ef1576eb82fa fixed the music playback diff -r 4c427e28ca00 -r f9b8bbebc9b3 prototype/src/audio/ovstream.cc --- a/prototype/src/audio/ovstream.cc Wed Sep 19 08:19:10 2012 +0300 +++ b/prototype/src/audio/ovstream.cc Thu Sep 20 10:04:25 2012 +0300 @@ -62,7 +62,7 @@ long total_read = 0; while(total_read < bufsz) { int bitstream; - long rd = ov_read(&vf, buf->samples + total_read, bufsz, 0, 2, 1, &bitstream); + long rd = ov_read(&vf, buf->samples + total_read, bufsz - total_read, 0, 2, 1, &bitstream); if(!rd) { bufsz = total_read; } else { diff -r 4c427e28ca00 -r f9b8bbebc9b3 prototype/src/audio/stream.cc --- a/prototype/src/audio/stream.cc Wed Sep 19 08:19:10 2012 +0300 +++ b/prototype/src/audio/stream.cc Thu Sep 20 10:04:25 2012 +0300 @@ -1,4 +1,5 @@ #include +#include #include "stream.h" #include "openal.h" @@ -8,6 +9,7 @@ poll_interval = 250; done = true; loop = false; + volume = 1.0; } AudioStream::~AudioStream() @@ -15,6 +17,21 @@ stop(); } +void AudioStream::set_volume(float vol) +{ + volume = vol; + + std::lock_guard lock(mutex); + if(alsrc) { + alSourcef(alsrc, AL_GAIN, vol); + } +} + +float AudioStream::get_volume() const +{ + return volume; +} + void AudioStream::play(enum PlayMode mode) { loop = (mode == PlayMode::loop); @@ -24,12 +41,15 @@ void AudioStream::stop() { - std::lock_guard lock(mutex); - + mutex.lock(); if(alsrc) { done = true; alSourceStop(alsrc); + printf("waiting for the music thread to stop\n"); + mutex.unlock(); play_thread.join(); + } else { + mutex.unlock(); } } @@ -47,6 +67,9 @@ mutex.lock(); alGenSources(1, &alsrc); + alSourcei(alsrc, AL_LOOPING, AL_FALSE); + alSourcef(alsrc, AL_GAIN, volume); + alGenBuffers(num_buffers, albuf); AudioStreamBuffer *buf = new AudioStreamBuffer; @@ -54,7 +77,6 @@ for(int i=0; inum_samples * buf->channels * 2; // 2 is for 16bit samples - printf("buffer data: %d samples, %d rate, %d channels\n", buf->num_samples, buf->sample_rate, buf->channels); alBufferData(albuf[i], alformat(buf), buf->samples, bufsz, buf->sample_rate); if(alGetError()) { fprintf(stderr, "failed to load sample data into OpenAL buffer\n"); @@ -70,30 +92,43 @@ } // start playback alSourcePlay(alsrc); - mutex.unlock(); while(!done) { - mutex.lock(); /* find out how many (if any) of the queued buffers are * done, and free to be reused. */ int num_buf_done; alGetSourcei(alsrc, AL_BUFFERS_PROCESSED, &num_buf_done); for(int i=0; inum_samples * buf->channels * 2; // 2 is for 16bit samples - printf("buffer data: %d samples, %d rate, %d channels\n", buf->num_samples, buf->sample_rate, buf->channels); alBufferData(buf_id, alformat(buf), buf->samples, bufsz, buf->sample_rate); - if(alGetError()) { - fprintf(stderr, "failed to load sample data into OpenAL buffer\n"); + if((err = alGetError())) { + fprintf(stderr, "failed to load sample data into OpenAL buffer (error: %x)\n", err); } - alSourceQueueBuffers(alsrc, 1, albuf + i); + alSourceQueueBuffers(alsrc, 1, &buf_id); if(alGetError()) { fprintf(stderr, "failed to start streaming audio buffers\n"); } @@ -120,10 +155,10 @@ std::chrono::milliseconds dur{poll_interval}; std::this_thread::sleep_for(dur); + + mutex.lock(); } - mutex.lock(); - // done with the data, wait for the source to stop playing before cleanup int state; while(alGetSourcei(alsrc, AL_SOURCE_STATE, &state), state == AL_PLAYING) { diff -r 4c427e28ca00 -r f9b8bbebc9b3 prototype/src/audio/stream.h --- a/prototype/src/audio/stream.h Wed Sep 19 08:19:10 2012 +0300 +++ b/prototype/src/audio/stream.h Thu Sep 20 10:04:25 2012 +0300 @@ -22,6 +22,7 @@ std::thread play_thread; std::mutex mutex; + float volume; bool done, loop; unsigned int poll_interval; unsigned int alsrc; @@ -34,6 +35,9 @@ AudioStream(); virtual ~AudioStream(); + void set_volume(float vol); + float get_volume() const; + void play(enum PlayMode mode); void stop(); diff -r 4c427e28ca00 -r f9b8bbebc9b3 prototype/src/main.cc --- a/prototype/src/main.cc Wed Sep 19 08:19:10 2012 +0300 +++ b/prototype/src/main.cc Thu Sep 20 10:04:25 2012 +0300 @@ -109,6 +109,7 @@ music = new OggVorbisStream; if(music->open(datafile_path("bgtrack.ogg"))) { + music->set_volume(0.6); music->play(PlayMode::loop); } else { delete music; @@ -254,7 +255,7 @@ } float dt = (float)(msec - last_upd) / 1000.0; - float offs = 2.5 * dt; + float offs = 2.0 * dt; float dx = 0, dy = 0; // handle key input @@ -434,7 +435,7 @@ } if(bnstate[0]) { - cam.input_rotate(dy * 0.01, dx * 0.01, 0); + cam.input_rotate(dy * 0.0075, dx * 0.0075, 0); glutPostRedisplay(); } if(bnstate[2]) {