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