dungeon_crawler

changeset 56:f9b8bbebc9b3

fixed the music playback
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 20 Sep 2012 10:04:25 +0300
parents 4c427e28ca00
children 508540dae114
files prototype/src/audio/ovstream.cc prototype/src/audio/stream.cc prototype/src/audio/stream.h prototype/src/main.cc
diffstat 4 files changed, 54 insertions(+), 14 deletions(-) [+]
line diff
     1.1 --- a/prototype/src/audio/ovstream.cc	Wed Sep 19 08:19:10 2012 +0300
     1.2 +++ b/prototype/src/audio/ovstream.cc	Thu Sep 20 10:04:25 2012 +0300
     1.3 @@ -62,7 +62,7 @@
     1.4  	long total_read = 0;
     1.5  	while(total_read < bufsz) {
     1.6  		int bitstream;
     1.7 -		long rd = ov_read(&vf, buf->samples + total_read, bufsz, 0, 2, 1, &bitstream);
     1.8 +		long rd = ov_read(&vf, buf->samples + total_read, bufsz - total_read, 0, 2, 1, &bitstream);
     1.9  		if(!rd) {
    1.10  			bufsz = total_read;
    1.11  		} else {
     2.1 --- a/prototype/src/audio/stream.cc	Wed Sep 19 08:19:10 2012 +0300
     2.2 +++ b/prototype/src/audio/stream.cc	Thu Sep 20 10:04:25 2012 +0300
     2.3 @@ -1,4 +1,5 @@
     2.4  #include <stdio.h>
     2.5 +#include <assert.h>
     2.6  #include "stream.h"
     2.7  #include "openal.h"
     2.8  
     2.9 @@ -8,6 +9,7 @@
    2.10  	poll_interval = 250;
    2.11  	done = true;
    2.12  	loop = false;
    2.13 +	volume = 1.0;
    2.14  }
    2.15  
    2.16  AudioStream::~AudioStream()
    2.17 @@ -15,6 +17,21 @@
    2.18  	stop();
    2.19  }
    2.20  
    2.21 +void AudioStream::set_volume(float vol)
    2.22 +{
    2.23 +	volume = vol;
    2.24 +
    2.25 +	std::lock_guard<std::mutex> lock(mutex);
    2.26 +	if(alsrc) {
    2.27 +		alSourcef(alsrc, AL_GAIN, vol);
    2.28 +	}
    2.29 +}
    2.30 +
    2.31 +float AudioStream::get_volume() const
    2.32 +{
    2.33 +	return volume;
    2.34 +}
    2.35 +
    2.36  void AudioStream::play(enum PlayMode mode)
    2.37  {
    2.38  	loop = (mode == PlayMode::loop);
    2.39 @@ -24,12 +41,15 @@
    2.40  
    2.41  void AudioStream::stop()
    2.42  {
    2.43 -	std::lock_guard<std::mutex> lock(mutex);
    2.44 -
    2.45 +	mutex.lock();
    2.46  	if(alsrc) {
    2.47  		done = true;
    2.48  		alSourceStop(alsrc);
    2.49 +		printf("waiting for the music thread to stop\n");
    2.50 +		mutex.unlock();
    2.51  		play_thread.join();
    2.52 +	} else {
    2.53 +		mutex.unlock();
    2.54  	}
    2.55  }
    2.56  
    2.57 @@ -47,6 +67,9 @@
    2.58  	mutex.lock();
    2.59  
    2.60  	alGenSources(1, &alsrc);
    2.61 +	alSourcei(alsrc, AL_LOOPING, AL_FALSE);
    2.62 +	alSourcef(alsrc, AL_GAIN, volume);
    2.63 +
    2.64  	alGenBuffers(num_buffers, albuf);
    2.65  
    2.66  	AudioStreamBuffer *buf = new AudioStreamBuffer;
    2.67 @@ -54,7 +77,6 @@
    2.68  	for(int i=0; i<num_buffers; i++) {
    2.69  		if(more_samples(buf)) {
    2.70  			int bufsz = buf->num_samples * buf->channels * 2;	// 2 is for 16bit samples
    2.71 -			printf("buffer data: %d samples, %d rate, %d channels\n", buf->num_samples, buf->sample_rate, buf->channels);
    2.72  			alBufferData(albuf[i], alformat(buf), buf->samples, bufsz, buf->sample_rate);
    2.73  			if(alGetError()) {
    2.74  				fprintf(stderr, "failed to load sample data into OpenAL buffer\n");
    2.75 @@ -70,30 +92,43 @@
    2.76  	}
    2.77  	// start playback
    2.78  	alSourcePlay(alsrc);
    2.79 -	mutex.unlock();
    2.80  
    2.81  	while(!done) {
    2.82 -		mutex.lock();
    2.83  		/* find out how many (if any) of the queued buffers are
    2.84  		 * done, and free to be reused.
    2.85  		 */
    2.86  		int num_buf_done;
    2.87  		alGetSourcei(alsrc, AL_BUFFERS_PROCESSED, &num_buf_done);
    2.88  		for(int i=0; i<num_buf_done; i++) {
    2.89 +			int err;
    2.90  			// unqueue a buffer...
    2.91  			unsigned int buf_id;
    2.92  			alSourceUnqueueBuffers(alsrc, 1, &buf_id);
    2.93 +			if((err = alGetError())) {
    2.94 +				fprintf(stderr, "failed to unqueue used buffer (error: %x)\n", err);
    2.95 +				num_buf_done = i;
    2.96 +				break;
    2.97 +			}
    2.98 +
    2.99 +			int looping;
   2.100 +			alGetSourcei(alsrc, AL_LOOPING, &looping);
   2.101 +			assert(looping == AL_FALSE);
   2.102 +
   2.103 +			int cur_buf;
   2.104 +			alGetSourcei(alsrc, AL_BUFFER, &cur_buf);
   2.105 +			if((unsigned int)cur_buf == buf_id) {
   2.106 +				continue;
   2.107 +			}
   2.108  
   2.109  			// if there are more data, fill it up and requeue it
   2.110  			if(more_samples(buf)) {
   2.111  				int bufsz = buf->num_samples * buf->channels * 2;	// 2 is for 16bit samples
   2.112 -				printf("buffer data: %d samples, %d rate, %d channels\n", buf->num_samples, buf->sample_rate, buf->channels);
   2.113  				alBufferData(buf_id, alformat(buf), buf->samples, bufsz, buf->sample_rate);
   2.114 -				if(alGetError()) {
   2.115 -					fprintf(stderr, "failed to load sample data into OpenAL buffer\n");
   2.116 +				if((err = alGetError())) {
   2.117 +					fprintf(stderr, "failed to load sample data into OpenAL buffer (error: %x)\n", err);
   2.118  				}
   2.119  
   2.120 -				alSourceQueueBuffers(alsrc, 1, albuf + i);
   2.121 +				alSourceQueueBuffers(alsrc, 1, &buf_id);
   2.122  				if(alGetError()) {
   2.123  					fprintf(stderr, "failed to start streaming audio buffers\n");
   2.124  				}
   2.125 @@ -120,10 +155,10 @@
   2.126  
   2.127  		std::chrono::milliseconds dur{poll_interval};
   2.128  		std::this_thread::sleep_for(dur);
   2.129 +
   2.130 +		mutex.lock();
   2.131  	}
   2.132  
   2.133 -	mutex.lock();
   2.134 -
   2.135  	// done with the data, wait for the source to stop playing before cleanup
   2.136  	int state;
   2.137  	while(alGetSourcei(alsrc, AL_SOURCE_STATE, &state), state == AL_PLAYING) {
     3.1 --- a/prototype/src/audio/stream.h	Wed Sep 19 08:19:10 2012 +0300
     3.2 +++ b/prototype/src/audio/stream.h	Thu Sep 20 10:04:25 2012 +0300
     3.3 @@ -22,6 +22,7 @@
     3.4  	std::thread play_thread;
     3.5  	std::mutex mutex;
     3.6  
     3.7 +	float volume;
     3.8  	bool done, loop;
     3.9  	unsigned int poll_interval;
    3.10  	unsigned int alsrc;
    3.11 @@ -34,6 +35,9 @@
    3.12  	AudioStream();
    3.13  	virtual ~AudioStream();
    3.14  
    3.15 +	void set_volume(float vol);
    3.16 +	float get_volume() const;
    3.17 +
    3.18  	void play(enum PlayMode mode);
    3.19  	void stop();
    3.20  
     4.1 --- a/prototype/src/main.cc	Wed Sep 19 08:19:10 2012 +0300
     4.2 +++ b/prototype/src/main.cc	Thu Sep 20 10:04:25 2012 +0300
     4.3 @@ -109,6 +109,7 @@
     4.4  
     4.5  		music = new OggVorbisStream;
     4.6  		if(music->open(datafile_path("bgtrack.ogg"))) {
     4.7 +			music->set_volume(0.6);
     4.8  			music->play(PlayMode::loop);
     4.9  		} else {
    4.10  			delete music;
    4.11 @@ -254,7 +255,7 @@
    4.12  	}
    4.13  	float dt = (float)(msec - last_upd) / 1000.0;
    4.14  
    4.15 -	float offs = 2.5 * dt;
    4.16 +	float offs = 2.0 * dt;
    4.17  	float dx = 0, dy = 0;
    4.18  
    4.19  	// handle key input
    4.20 @@ -434,7 +435,7 @@
    4.21  	}
    4.22  
    4.23  	if(bnstate[0]) {
    4.24 -		cam.input_rotate(dy * 0.01, dx * 0.01, 0);
    4.25 +		cam.input_rotate(dy * 0.0075, dx * 0.0075, 0);
    4.26  		glutPostRedisplay();
    4.27  	}
    4.28  	if(bnstate[2]) {