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