dbf-halloween2015

diff src/audio/ovstream.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/ovstream.cc	Sun Nov 01 00:09:12 2015 +0200
     1.3 @@ -0,0 +1,95 @@
     1.4 +#include <stdio.h>
     1.5 +#include <assert.h>
     1.6 +#include "ovstream.h"
     1.7 +
     1.8 +OggVorbisStream::OggVorbisStream()
     1.9 +{
    1.10 +	vfopen = false;
    1.11 +
    1.12 +	pthread_mutex_init(&vflock, 0);
    1.13 +}
    1.14 +
    1.15 +OggVorbisStream::~OggVorbisStream()
    1.16 +{
    1.17 +	close();
    1.18 +}
    1.19 +
    1.20 +bool OggVorbisStream::open(const char *fname)
    1.21 +{
    1.22 +	close();
    1.23 +
    1.24 +	pthread_mutex_lock(&vflock);
    1.25 +
    1.26 +	if(ov_fopen(fname, &vf) != 0) {
    1.27 +		fprintf(stderr, "failed to open ogg/vorbis stream: %s\n", fname ? fname : "<not found>");
    1.28 +		pthread_mutex_unlock(&vflock);
    1.29 +		return false;
    1.30 +	}
    1.31 +
    1.32 +	vfopen = true;
    1.33 +	pthread_mutex_unlock(&vflock);
    1.34 +	return true;
    1.35 +}
    1.36 +
    1.37 +void OggVorbisStream::close()
    1.38 +{
    1.39 +	pthread_mutex_lock(&vflock);
    1.40 +	if(vfopen) {
    1.41 +		ov_clear(&vf);
    1.42 +		vfopen = false;
    1.43 +	}
    1.44 +	pthread_mutex_unlock(&vflock);
    1.45 +}
    1.46 +
    1.47 +void OggVorbisStream::play(AUDIO_PLAYMODE mode)
    1.48 +{
    1.49 +	if (vfopen)
    1.50 +	{
    1.51 +		AudioStream::play(mode);
    1.52 +	} else {
    1.53 +		fprintf(stderr, "failed to play audio track.\n");
    1.54 +	}
    1.55 +}
    1.56 +
    1.57 +void OggVorbisStream::rewind()
    1.58 +{
    1.59 +	pthread_mutex_lock(&vflock);
    1.60 +	if(vfopen) {
    1.61 +		ov_raw_seek(&vf, 0);
    1.62 +	}
    1.63 +	pthread_mutex_unlock(&vflock);
    1.64 +}
    1.65 +
    1.66 +bool OggVorbisStream::more_samples(AudioStreamBuffer *buf)
    1.67 +{
    1.68 +	pthread_mutex_lock(&vflock);
    1.69 +
    1.70 +	vorbis_info *vinfo = ov_info(&vf, -1);
    1.71 +	buf->channels = vinfo->channels;
    1.72 +	buf->sample_rate = vinfo->rate;
    1.73 +	assert(buf->channels == 2);
    1.74 +	assert(buf->sample_rate == 44100);
    1.75 +
    1.76 +	long bufsz = AUDIO_BUFFER_BYTES;
    1.77 +	long total_read = 0;
    1.78 +	while(total_read < bufsz) {
    1.79 +		int bitstream;
    1.80 +		long rd = ov_read(&vf, buf->samples + total_read, bufsz - total_read, 0, 2, 1, &bitstream);
    1.81 +
    1.82 +		if(!rd) {
    1.83 +			bufsz = total_read;
    1.84 +		} else {
    1.85 +			total_read += rd;
    1.86 +		}
    1.87 +	}
    1.88 +
    1.89 +	if(!total_read) {
    1.90 +		buf->num_samples = 0;
    1.91 +		pthread_mutex_unlock(&vflock);
    1.92 +		return false;
    1.93 +	}
    1.94 +
    1.95 +	buf->num_samples = bufsz / vinfo->channels / 2;
    1.96 +	pthread_mutex_unlock(&vflock);
    1.97 +	return true;
    1.98 +}