dbf-halloween2015
annotate 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 |
rev | line source |
---|---|
nuclear@0 | 1 #include <stdio.h> |
nuclear@0 | 2 #include <assert.h> |
nuclear@0 | 3 #include "ovstream.h" |
nuclear@0 | 4 |
nuclear@0 | 5 OggVorbisStream::OggVorbisStream() |
nuclear@0 | 6 { |
nuclear@0 | 7 vfopen = false; |
nuclear@0 | 8 |
nuclear@0 | 9 pthread_mutex_init(&vflock, 0); |
nuclear@0 | 10 } |
nuclear@0 | 11 |
nuclear@0 | 12 OggVorbisStream::~OggVorbisStream() |
nuclear@0 | 13 { |
nuclear@0 | 14 close(); |
nuclear@0 | 15 } |
nuclear@0 | 16 |
nuclear@0 | 17 bool OggVorbisStream::open(const char *fname) |
nuclear@0 | 18 { |
nuclear@0 | 19 close(); |
nuclear@0 | 20 |
nuclear@0 | 21 pthread_mutex_lock(&vflock); |
nuclear@0 | 22 |
nuclear@0 | 23 if(ov_fopen(fname, &vf) != 0) { |
nuclear@0 | 24 fprintf(stderr, "failed to open ogg/vorbis stream: %s\n", fname ? fname : "<not found>"); |
nuclear@0 | 25 pthread_mutex_unlock(&vflock); |
nuclear@0 | 26 return false; |
nuclear@0 | 27 } |
nuclear@0 | 28 |
nuclear@0 | 29 vfopen = true; |
nuclear@0 | 30 pthread_mutex_unlock(&vflock); |
nuclear@0 | 31 return true; |
nuclear@0 | 32 } |
nuclear@0 | 33 |
nuclear@0 | 34 void OggVorbisStream::close() |
nuclear@0 | 35 { |
nuclear@0 | 36 pthread_mutex_lock(&vflock); |
nuclear@0 | 37 if(vfopen) { |
nuclear@0 | 38 ov_clear(&vf); |
nuclear@0 | 39 vfopen = false; |
nuclear@0 | 40 } |
nuclear@0 | 41 pthread_mutex_unlock(&vflock); |
nuclear@0 | 42 } |
nuclear@0 | 43 |
nuclear@0 | 44 void OggVorbisStream::play(AUDIO_PLAYMODE mode) |
nuclear@0 | 45 { |
nuclear@0 | 46 if (vfopen) |
nuclear@0 | 47 { |
nuclear@0 | 48 AudioStream::play(mode); |
nuclear@0 | 49 } else { |
nuclear@0 | 50 fprintf(stderr, "failed to play audio track.\n"); |
nuclear@0 | 51 } |
nuclear@0 | 52 } |
nuclear@0 | 53 |
nuclear@0 | 54 void OggVorbisStream::rewind() |
nuclear@0 | 55 { |
nuclear@0 | 56 pthread_mutex_lock(&vflock); |
nuclear@0 | 57 if(vfopen) { |
nuclear@0 | 58 ov_raw_seek(&vf, 0); |
nuclear@0 | 59 } |
nuclear@0 | 60 pthread_mutex_unlock(&vflock); |
nuclear@0 | 61 } |
nuclear@0 | 62 |
nuclear@0 | 63 bool OggVorbisStream::more_samples(AudioStreamBuffer *buf) |
nuclear@0 | 64 { |
nuclear@0 | 65 pthread_mutex_lock(&vflock); |
nuclear@0 | 66 |
nuclear@0 | 67 vorbis_info *vinfo = ov_info(&vf, -1); |
nuclear@0 | 68 buf->channels = vinfo->channels; |
nuclear@0 | 69 buf->sample_rate = vinfo->rate; |
nuclear@0 | 70 assert(buf->channels == 2); |
nuclear@0 | 71 assert(buf->sample_rate == 44100); |
nuclear@0 | 72 |
nuclear@0 | 73 long bufsz = AUDIO_BUFFER_BYTES; |
nuclear@0 | 74 long total_read = 0; |
nuclear@0 | 75 while(total_read < bufsz) { |
nuclear@0 | 76 int bitstream; |
nuclear@0 | 77 long rd = ov_read(&vf, buf->samples + total_read, bufsz - total_read, 0, 2, 1, &bitstream); |
nuclear@0 | 78 |
nuclear@0 | 79 if(!rd) { |
nuclear@0 | 80 bufsz = total_read; |
nuclear@0 | 81 } else { |
nuclear@0 | 82 total_read += rd; |
nuclear@0 | 83 } |
nuclear@0 | 84 } |
nuclear@0 | 85 |
nuclear@0 | 86 if(!total_read) { |
nuclear@0 | 87 buf->num_samples = 0; |
nuclear@0 | 88 pthread_mutex_unlock(&vflock); |
nuclear@0 | 89 return false; |
nuclear@0 | 90 } |
nuclear@0 | 91 |
nuclear@0 | 92 buf->num_samples = bufsz / vinfo->channels / 2; |
nuclear@0 | 93 pthread_mutex_unlock(&vflock); |
nuclear@0 | 94 return true; |
nuclear@0 | 95 } |