]> git.tdb.fi Git - libs/al.git/blob - source/jukebox.cpp
b753e6b9eb58bf391a13d2e5b0ef42735b8c9045
[libs/al.git] / source / jukebox.cpp
1 #include <algorithm>
2 #include <cstdlib>
3 #include <stdexcept>
4 #include "jukebox.h"
5 #include "playlist.h"
6 #include "sounddecoder.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace AL {
12
13 Jukebox::Jukebox():
14         streamer(source),
15         in(0),
16         decoder(0),
17         playlist(0),
18         current(0)
19 { }
20
21 Jukebox::~Jukebox()
22 {
23         streamer.stop();
24         delete decoder;
25 }
26
27 void Jukebox::set_playlist(const Playlist *p)
28 {
29         bool was_playing = decoder;
30         stop();
31         playlist = p;
32         if(playlist && was_playing)
33                 play();
34 }
35
36 void Jukebox::play()
37 {
38         if(!playlist || playlist->empty() || decoder)
39                 return;
40
41         in = playlist->open(current);
42         decoder = SoundDecoder::open_io(*in);
43         streamer.play(*decoder);
44 }
45
46 void Jukebox::next()
47 {
48         if(!playlist || playlist->empty())
49                 return;
50
51         stop();
52         current = playlist->advance(current, 1);
53         signal_track_changed.emit(current);
54         play();
55 }
56
57 void Jukebox::previous()
58 {
59         if(!playlist || playlist->empty())
60                 return;
61
62         stop();
63         current = playlist->advance(current, -1);
64         signal_track_changed.emit(current);
65         play();
66 }
67
68 void Jukebox::stop()
69 {
70         streamer.stop();
71         delete decoder;
72         decoder = 0;
73         delete in;
74         in = 0;
75 }
76
77 void Jukebox::tick()
78 {
79         streamer.tick();
80         if(decoder && decoder->eof())
81                 next();
82 }
83
84 } // namespace AL
85 } // namespace Msp