]> git.tdb.fi Git - libs/al.git/blob - source/jukebox.cpp
Drop copyright notices and Id tags from source files
[libs/al.git] / source / jukebox.cpp
1 #include <algorithm>
2 #include <cstdlib>
3 #include <msp/core/except.h>
4 #include "jukebox.h"
5 #include "sound.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace AL {
11
12 Jukebox::Jukebox():
13         streamer(source),
14         sound(0),
15         current_track(tracks.end()),
16         shuffle(false)
17 { }
18
19 Jukebox::~Jukebox()
20 {
21         streamer.stop();
22         delete sound;
23 }
24
25 void Jukebox::add_track(const string &trk)
26 {
27         bool was_empty = tracks.empty();
28         tracks.push_back(trk);
29         if(was_empty)
30         {
31                 current_track = tracks.begin();
32                 signal_track_changed.emit(*current_track);
33         }
34 }
35
36 void Jukebox::remove_track(const string &trk)
37 {
38         list<string>::iterator i = find(tracks.begin(), tracks.end(), trk);
39         if(i!=tracks.end())
40         {
41                 if(i==current_track)
42                         next();
43                 tracks.erase(i);
44                 if(tracks.empty())
45                         current_track = tracks.end();
46         }
47 }
48
49 void Jukebox::clear_tracks()
50 {
51         stop();
52         tracks.clear();
53         current_track = tracks.end();
54 }
55
56 const string &Jukebox::get_current_track() const
57 {
58         if(tracks.empty())
59                 throw InvalidState("No current track");
60         return *current_track;
61 }
62
63 void Jukebox::set_shuffle(bool s)
64 {
65         shuffle = s;
66 }
67
68 void Jukebox::play()
69 {
70         if(tracks.empty() || sound)
71                 return;
72
73         sound = new AL::Sound;
74         sound->open_file(*current_track);
75         streamer.play(*sound);
76 }
77
78 void Jukebox::next()
79 {
80         stop();
81         if(tracks.size()>1)
82         {
83                 if(shuffle)
84                 {
85                         while(1)
86                         {
87                                 list<string>::iterator i = tracks.begin();
88                                 advance(i, rand()%tracks.size());
89                                 if(i!=current_track)
90                                 {
91                                         current_track = i;
92                                         break;
93                                 }
94                         }
95                 }
96                 else
97                 {
98                         ++current_track;
99                         if(current_track==tracks.end())
100                                 current_track = tracks.begin();
101                 }
102                 signal_track_changed.emit(*current_track);
103         }
104         play();
105 }
106
107 void Jukebox::previous()
108 {
109         if(shuffle)
110                 return next();
111
112         stop();
113         if(tracks.size()>1)
114         {
115                 if(current_track==tracks.begin())
116                         current_track = tracks.end();
117                 --current_track;
118                 signal_track_changed.emit(*current_track);
119         }
120         play();
121 }
122
123 void Jukebox::stop()
124 {
125         streamer.stop();
126         delete sound;
127         sound = 0;
128 }
129
130 void Jukebox::tick()
131 {
132         streamer.tick();
133         if(sound && sound->eof())
134                 next();
135 }
136
137 } // namespace AL
138 } // namespace Msp