dbf-halloween2015

diff src/audio/stream.cc @ 0:50683c78264e

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 00:09:12 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/audio/stream.cc	Sun Nov 01 00:09:12 2015 +0200
     1.3 @@ -0,0 +1,264 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdint.h>
     1.6 +#include <assert.h>
     1.7 +#include "openal.h"
     1.8 +#include "stream.h"
     1.9 +#include "timer.h"
    1.10 +
    1.11 +static ALenum alformat(AudioStreamBuffer *buf)
    1.12 +{
    1.13 +	return buf->channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
    1.14 +}
    1.15 +
    1.16 +AudioStream::AudioStream()
    1.17 +{
    1.18 +	alsrc = 0;
    1.19 +	poll_interval = 25;
    1.20 +	done = true;
    1.21 +	loop = false;
    1.22 +	volume = 1.0;
    1.23 +	pitch = 1.0;
    1.24 +
    1.25 +	pthread_mutex_init(&mutex, 0);
    1.26 +}
    1.27 +
    1.28 +AudioStream::~AudioStream()
    1.29 +{
    1.30 +	stop();
    1.31 +}
    1.32 +
    1.33 +bool AudioStream::open(const char *fname)
    1.34 +{
    1.35 +	return false;
    1.36 +}
    1.37 +
    1.38 +void AudioStream::close()
    1.39 +{
    1.40 +}
    1.41 +
    1.42 +void AudioStream::set_volume(float vol)
    1.43 +{
    1.44 +	if(vol < 0.0) vol = 0.0;
    1.45 +	if(vol > 1.0) vol = 1.0;
    1.46 +
    1.47 +	volume = vol;
    1.48 +
    1.49 +	pthread_mutex_lock(&mutex);
    1.50 +	if(alsrc) {
    1.51 +		alSourcef(alsrc, AL_GAIN, vol);
    1.52 +	}
    1.53 +	pthread_mutex_unlock(&mutex);
    1.54 +}
    1.55 +
    1.56 +float AudioStream::get_volume() const
    1.57 +{
    1.58 +	return volume;
    1.59 +}
    1.60 +void AudioStream::set_pitch(float pitch)
    1.61 +{
    1.62 +	if(pitch < 0.0) pitch = 0.0;
    1.63 +	if(pitch > 1.0) pitch = 1.0;
    1.64 +
    1.65 +	this->pitch = pitch;
    1.66 +
    1.67 +	pthread_mutex_lock(&mutex);
    1.68 +	if(alsrc) {
    1.69 +		alSourcef(alsrc, AL_PITCH, pitch);
    1.70 +	}
    1.71 +	pthread_mutex_unlock(&mutex);
    1.72 +}
    1.73 +
    1.74 +float AudioStream::get_pitch() const
    1.75 +{
    1.76 +	return pitch;
    1.77 +}
    1.78 +
    1.79 +
    1.80 +static void *thread_func(void *arg)
    1.81 +{
    1.82 +	AudioStream *astr = (AudioStream*)arg;
    1.83 +	astr->poll_loop();
    1.84 +	return 0;
    1.85 +}
    1.86 +
    1.87 +void AudioStream::play(AUDIO_PLAYMODE mode)
    1.88 +{
    1.89 +	loop = (mode == AUDIO_PLAYMODE_LOOP);
    1.90 +	done = false;
    1.91 +
    1.92 +	if(pthread_create(&play_thread, 0, thread_func, this) != 0) {
    1.93 +		fprintf(stderr, "failed to create music playback thread\n");
    1.94 +	}
    1.95 +}
    1.96 +
    1.97 +void AudioStream::stop()
    1.98 +{
    1.99 +	pthread_mutex_lock(&mutex);
   1.100 +
   1.101 +	if(alsrc) {
   1.102 +		done = true;
   1.103 +		//alSourceStop(alsrc);
   1.104 +		printf("waiting for the music thread to stop\n");
   1.105 +		pthread_mutex_unlock(&mutex);
   1.106 +		pthread_join(play_thread, 0);
   1.107 +	} else {
   1.108 +		pthread_mutex_unlock(&mutex);
   1.109 +	}
   1.110 +}
   1.111 +
   1.112 +// gets an array of buffers and returns the index of the one matching id
   1.113 +static inline int find_buffer(unsigned int id, unsigned int *barr, int num)
   1.114 +{
   1.115 +	for(int i=0; i<num; i++) {
   1.116 +		if(barr[i] == id) {
   1.117 +			return i;
   1.118 +		}
   1.119 +	}
   1.120 +	return -1;
   1.121 +}
   1.122 +
   1.123 +
   1.124 +static int queued_idx_list[AUDIO_NUM_BUFFERS];
   1.125 +static int queued_idx_head = 0;
   1.126 +static int queued_idx_tail = 0;
   1.127 +
   1.128 +#define BUFQ_UNQUEUE() \
   1.129 +	do { \
   1.130 +		queued_idx_tail = (queued_idx_tail + 1) % AUDIO_NUM_BUFFERS; \
   1.131 +	} while(0)
   1.132 +
   1.133 +
   1.134 +#define BUFQ_QUEUE(idx)	\
   1.135 +	do { \
   1.136 +		queued_idx_head = (queued_idx_head + 1) % AUDIO_NUM_BUFFERS; \
   1.137 +		queued_idx_list[queued_idx_head] = idx; \
   1.138 +	} while(0)
   1.139 +
   1.140 +// thread function
   1.141 +void AudioStream::poll_loop()
   1.142 +{
   1.143 +	long prev_msec = -1000;
   1.144 +	unsigned int albuf[AUDIO_NUM_BUFFERS];
   1.145 +
   1.146 +	pthread_mutex_lock(&mutex);
   1.147 +	alGenSources(1, &alsrc);
   1.148 +	alSourcei(alsrc, AL_LOOPING, AL_FALSE);
   1.149 +	alSourcef(alsrc, AL_GAIN, volume);
   1.150 +	alSourcef(alsrc, AL_PITCH, pitch);
   1.151 +	alGenBuffers(AUDIO_NUM_BUFFERS, albuf);
   1.152 +	AudioStreamBuffer *buf = new AudioStreamBuffer;
   1.153 +
   1.154 +	assert(alGetError() == 0);
   1.155 +	for(int i=0; i<AUDIO_NUM_BUFFERS; i++) {
   1.156 +		if(more_samples(buf)) {
   1.157 +			int bufsz = buf->num_samples * buf->channels * 2;       // 2 is for 16bit samples
   1.158 +			alBufferData(albuf[i], alformat(buf), buf->samples, bufsz, buf->sample_rate);
   1.159 +
   1.160 +			if(alGetError()) {
   1.161 +				fprintf(stderr, "failed to load sample data into OpenAL buffer\n");
   1.162 +			}
   1.163 +
   1.164 +			alSourceQueueBuffers(alsrc, 1, albuf + i);
   1.165 +			BUFQ_QUEUE(i);
   1.166 +
   1.167 +			if(alGetError()) {
   1.168 +				fprintf(stderr, "failed to start streaming audio buffers\n");
   1.169 +			}
   1.170 +		} else {
   1.171 +			break;
   1.172 +		}
   1.173 +	}
   1.174 +
   1.175 +	// start playback
   1.176 +	alSourcePlay(alsrc);
   1.177 +	while(!done) {
   1.178 +		/* find out how many (if any) of the queued buffers are
   1.179 +		* done, and free to be reused.
   1.180 +		*/
   1.181 +		int num_buf_done;
   1.182 +		alGetSourcei(alsrc, AL_BUFFERS_PROCESSED, &num_buf_done);
   1.183 +		for(int i=0; i<num_buf_done; i++) {
   1.184 +			int err;
   1.185 +			// unqueue a buffer...
   1.186 +			unsigned int buf_id;
   1.187 +			alSourceUnqueueBuffers(alsrc, 1, &buf_id);
   1.188 +
   1.189 +			if((err = alGetError())) {
   1.190 +				fprintf(stderr, "failed to unqueue used buffer (error: %x)\n", err);
   1.191 +				num_buf_done = i;
   1.192 +				break;
   1.193 +			}
   1.194 +			BUFQ_UNQUEUE();
   1.195 +
   1.196 +			// find out which one of our al buffers we just unqueued
   1.197 +			int bidx = find_buffer(buf_id, albuf, AUDIO_NUM_BUFFERS);
   1.198 +			assert(bidx != -1);
   1.199 +
   1.200 +			int looping;
   1.201 +
   1.202 +			alGetSourcei(alsrc, AL_LOOPING, &looping);
   1.203 +			assert(looping == AL_FALSE);
   1.204 +			/*if((unsigned int)cur_buf == buf_id) {
   1.205 +				continue;
   1.206 +			}*/
   1.207 +
   1.208 +			// if there are more data, fill it up and requeue it
   1.209 +			if(more_samples(buf)) {
   1.210 +				int bufsz = buf->num_samples * buf->channels * 2;       // 2 is for 16bit samples
   1.211 +				alBufferData(buf_id, alformat(buf), buf->samples, bufsz, buf->sample_rate);
   1.212 +				if((err = alGetError())) {
   1.213 +					fprintf(stderr, "failed to load sample data into OpenAL buffer (error: %x)\n", err);
   1.214 +				}
   1.215 +
   1.216 +				alSourceQueueBuffers(alsrc, 1, &buf_id);
   1.217 +				if(alGetError()) {
   1.218 +					fprintf(stderr, "failed to start streaming audio buffers\n");
   1.219 +				}
   1.220 +				BUFQ_QUEUE(bidx);
   1.221 +			} else {
   1.222 +				// no more data...
   1.223 +				if(loop) {
   1.224 +					printf("audio stream looping...\n");
   1.225 +					rewind();
   1.226 +				} else {
   1.227 +					done = true;
   1.228 +				}
   1.229 +			}
   1.230 +		}
   1.231 +
   1.232 +		if(num_buf_done) {
   1.233 +			// make sure playback didn't stop
   1.234 +			int state;
   1.235 +			alGetSourcei(alsrc, AL_SOURCE_STATE, &state);
   1.236 +			if(state != AL_PLAYING) {
   1.237 +				alSourcePlay(alsrc);
   1.238 +			}
   1.239 +		}
   1.240 +
   1.241 +		pthread_mutex_unlock(&mutex);
   1.242 +		long msec = get_time_msec();
   1.243 +		long dt = msec - prev_msec;
   1.244 +		prev_msec = msec;
   1.245 +
   1.246 +		if(dt < (long)poll_interval) {
   1.247 +			sleep_msec(poll_interval - dt);
   1.248 +		} else {
   1.249 +			sched_yield();
   1.250 +		}
   1.251 +		pthread_mutex_lock(&mutex);
   1.252 +	}
   1.253 +
   1.254 +
   1.255 +	// done with the data, wait for the source to stop playing before cleanup
   1.256 +	int state;
   1.257 +	while(alGetSourcei(alsrc, AL_SOURCE_STATE, &state), state == AL_PLAYING) {
   1.258 +		sched_yield();
   1.259 +	}
   1.260 +
   1.261 +	alDeleteBuffers(AUDIO_NUM_BUFFERS, albuf);
   1.262 +	alDeleteSources(1, &alsrc);
   1.263 +	alsrc = 0;
   1.264 +	pthread_mutex_unlock(&mutex);
   1.265 +
   1.266 +	delete buf;
   1.267 +}