]> git.tdb.fi Git - libs/al.git/blob - source/streamer.cpp
99503b9b70a013e9a3266dda1e95ba7f56d76e68
[libs/al.git] / source / streamer.cpp
1 /* $Id$
2
3 This file is part of libmspal
4 Copyright © 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "buffer.h"
9 #include "sound.h"
10 #include "streamer.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace AL {
16
17 Streamer::Streamer(Source &s):
18         src(s),
19         snd(0)
20 { }
21
22 Streamer::~Streamer()
23 {
24         src.stop();
25         src.clear_buffers();
26         for(list<Buffer *>::iterator i=buffers.begin(); i!=buffers.end(); ++i)
27                 delete *i;
28 }
29
30 void Streamer::play(Sound &s)
31 {
32         snd=&s;
33         tick();
34         src.play();
35 }
36
37 void Streamer::stop()
38 {
39         snd=0;
40         src.stop();
41 }
42
43 void Streamer::tick()
44 {
45         if(unsigned n=src.get_buffers_processed())
46         {
47                 for(unsigned i=0; i<n; ++i)
48                 {
49                         src.unqueue_buffer(*buffers.front());
50                         delete buffers.front();
51                         buffers.erase(buffers.begin());
52                 }
53         }
54
55         if(!snd)
56                 return;
57
58         unsigned freq=snd->get_frequency();
59         unsigned chunk_size=freq&~0xF;
60         unsigned queued=src.get_buffers_queued();
61         for(unsigned i=queued; i<4; ++i)
62         {
63                 char data[chunk_size];
64                 unsigned pos=0;
65                 while(pos<chunk_size)
66                 {
67                         unsigned len=snd->read(data+pos, chunk_size-pos);
68                         if(len==0)
69                                 break;
70                         pos+=len;
71                 }
72
73                 if(pos)
74                 {
75                         Buffer *buf=new Buffer;
76                         buf->data(snd->get_format(), data, pos, freq);
77                         src.queue_buffer(*buf);
78                         buffers.push_back(buf);
79                 }
80         }
81
82         if(snd->eof())
83                 snd=0;
84 }
85
86 } // namespace AL
87 } // namespace Msp