Example #1
0
        /**
        // \fn public bool CreatePlaylist(string name, string webradioName, int webradioId, AudioType type, out Playlist newPlaylist)
        //
        // \brief Creates a playlist.
        //
        // \author Simon Menetrey
        // \date 26.05.2014
        //
        // \param name              The name.
        // \param webradioName      Name of the webradio.
        // \param webradioId        Identifier for the webradio.
        // \param type              The type.
        // \param [out] newPlaylist The new playlist.
        //
        // \return true if it succeeds, false if it fails.
        **/
        public bool CreatePlaylist(string name, string webradioName, int webradioId, AudioType type, out Playlist newPlaylist)
        {
            Webradio selectedWebradio = this.Webradios[webradioId];
            int id = Bdd.ERROR;
            newPlaylist = null;
            if (this.IsValidFilename(name))
            {
                string filename = DEFAULT_WEBRADIOS_FOLDER + webradioName + "/" + DEFAULT_PLAYLISTS_FOLDER + name + ".lst";
                if (type == AudioType.Music)
                    newPlaylist = new PlaylistMusic(name, filename);
                else
                    newPlaylist = new PlaylistAd(name, filename);
                id = this.Bdd.CreatePlaylist(newPlaylist, webradioId);
                if (id == Bdd.ERROR)
                    return false;
                else
                {
                    try
                    {
                        newPlaylist.Id = id;
                        newPlaylist.GenerateConfigFile();
                        selectedWebradio.Playlists.Add(newPlaylist);
                        this.UpdateObservers(webradioId);
                        return true;
                    }
                    catch
                    {
                        return false;
                    }

                }
            }
            else
            {
                return false;
            }
        }
Example #2
0
        /**
        // \fn public bool GeneratePlaylist(string name, TimeSpan duration, AudioType type, string gender, int webradioId, string webradioName)
        //
        // \brief Generates a playlist.
        //
        // \author Simon Menetrey
        // \date 26.05.2014
        //
        // \param name         The name.
        // \param duration     The duration.
        // \param type         The type.
        // \param gender       The gender.
        // \param webradioId   Identifier for the webradio.
        // \param webradioName Name of the webradio.
        //
        // \return true if it succeeds, false if it fails.
        **/
        public bool GeneratePlaylist(string name, TimeSpan duration, AudioType type, string gender, int webradioId, string webradioName)
        {
            Playlist newPlaylist;
            string filename = DEFAULT_WEBRADIOS_FOLDER + webradioName + "/" + DEFAULT_PLAYLISTS_FOLDER + name + ".lst";
            if (type == AudioType.Music)
                newPlaylist = new PlaylistMusic(name, filename);
            else
                newPlaylist = new PlaylistAd(name, filename);
            TimeSpan tmpDuration = new TimeSpan();
            Random random = new Random();
            AudioFile audioFile;
            int retries = 0;
            List<int> audioFilesId = new List<int>();
            while (tmpDuration <= duration)
            {
                audioFile = this.Library[random.Next(0, this.Library.Count - 1)];
                if (audioFile.Type == type)
                {
                    if (retries > MAX_TRY_GENERATE)
                        break;
                    if ((tmpDuration + audioFile.Duration) > duration)
                    {
                        retries++;
                        continue;
                    }
                    else
                        retries = 0;

                    if ((audioFile.Type == AudioType.Ad) || (audioFile.Type == AudioType.Music && audioFile.Gender == gender))
                    {
                        tmpDuration += audioFile.Duration;
                        newPlaylist.AudioFileList.Add(audioFile.Filename);
                        audioFilesId.Add(audioFile.Id);
                    }
                }
            }

            //Impossible to create a playlist
            if (newPlaylist.AudioFileList.Count == 0)
                return false;

            //Add to database
            int idPlaylist = this.Bdd.AddGeneratedPlaylist(newPlaylist, audioFilesId, webradioId);
            if (idPlaylist == Bdd.ERROR)
                return false;
            newPlaylist.Id = idPlaylist;
            //Add to model
            this.Webradios[webradioId].Playlists.Add(newPlaylist);
            newPlaylist.GenerateConfigFile();
            this.UpdateObservers(webradioId);
            return true;
        }
Example #3
0
        /**
        // \fn public Dictionary<int,Webradio> LoadWebradios()
        //
        // \brief Loads the webradios from db. All webradio's elements are loaded too.
        //
        // \author Simon Menetrey
        // \date 26.05.2014
        //
        // \return The webradios list.
        **/
        public Dictionary<int, Webradio> LoadWebradios()
        {
            Dictionary<int, Webradio> webradios = new Dictionary<int, Webradio>();
            SQLiteDataReader reader = this.Controls.ExecuteDataReader("SELECT * FROM twebradio");
            while(reader.Read())
            {
                webradios.Add( int.Parse(reader["id"].ToString()), new Webradio(reader["name"].ToString(), int.Parse(reader["id"].ToString())));
            }
            reader.Close();

            foreach(KeyValuePair<int,Webradio> pair in webradios)
            {
                Webradio wr = pair.Value;
                //Server
                int id = wr.Id;
                reader = this.Controls.ExecuteDataReader("SELECT * FROM tserver WHERE webradioid = " + id.ToString());
                reader.Read();
                wr.Server = new WebradioServer(int.Parse(reader["port"].ToString()),
                    reader["logfilename"].ToString(),
                    reader["configfilename"].ToString(),
                    reader["password"].ToString(),
                    reader["adminpassword"].ToString(),
                    int.Parse(reader["maxlistener"].ToString()));
                reader.Close();
                //----

                //Calendar
                reader = this.Controls.ExecuteDataReader("SELECT id, filename FROM tcalendar WHERE webradioid = " + id.ToString());
                reader.Read();
                wr.Calendar = new WebradioCalendar(int.Parse(reader["id"].ToString()), reader["filename"].ToString());
                reader.Close();
                reader = this.Controls.ExecuteDataReader("SELECT ce.id AS EventId, ce.playlistid, ce.starttime, ce.duration, ce.name AS EventName, ce.repeat, ce.priority, ce.shuffle, ce.loopatend, p.name  AS PlaylistName, p.filename AS PlaylistFilename, at.name AS AudiotypeName FROM tcalendarevent ce, tplaylist p, taudiotype at WHERE ce.calendarid = " + wr.Calendar.Id + " AND ce.playlistid = p.id AND p.typeid = at.id");
                while(reader.Read())
                {
                    string[] time = reader["starttime"].ToString().Split(':');
                    TimeSpan start = new TimeSpan(int.Parse(time[0]),int.Parse(time[1]),int.Parse(time[2]));
                    time = reader["duration"].ToString().Split(':');
                    TimeSpan duration = new TimeSpan(int.Parse(time[0]),int.Parse(time[1]),int.Parse(time[2]));
                    Playlist playlist;
                    if (reader["AudiotypeName"].ToString() == AudioType.Music.ToString())
                        playlist = new PlaylistMusic(reader["PlaylistName"].ToString(), reader["PlaylistFilename"].ToString());
                    else
                        playlist = new PlaylistAd(reader["PlaylistName"].ToString(), reader["PlaylistFilename"].ToString());
                    wr.Calendar.Events.Add(new CalendarEvent(int.Parse(reader["EventId"].ToString()), reader["EventName"].ToString(),
                        start,
                        duration,
                        int.Parse(reader["repeat"].ToString()),
                        int.Parse(reader["priority"].ToString()),
                        Convert.ToBoolean(reader["shuffle"].ToString()),
                        Convert.ToBoolean(reader["loopatend"].ToString()),
                        playlist));
                }
                reader.Close();
                //---

                //Playlists
                reader = this.Controls.ExecuteDataReader("SELECT p.id, p.name AS PlaylistName, p.filename, t.name AS AudioType FROM tplaylist p, taudiotype t WHERE p.typeid = t.id AND webradioid = " + id.ToString());
                while(reader.Read())
                {
                    Playlist p = null;
                    if(reader["AudioType"].ToString() == AudioType.Ad.ToString())
                        p = new PlaylistAd(int.Parse(reader["id"].ToString()), reader["PlaylistName"].ToString(), reader["filename"].ToString());
                    else if(reader["AudioType"].ToString() == AudioType.Music.ToString())
                        p = new PlaylistMusic(int.Parse(reader["id"].ToString()), reader["PlaylistName"].ToString(), reader["filename"].ToString());
                    wr.Playlists.Add(p);
                }
                reader.Close();
                foreach(Playlist playlist in wr.Playlists)
                {
                    reader = this.Controls.ExecuteDataReader("SELECT m.filename FROM tmusic m, tplaylist_has_music pm WHERE pm.playlistid = " + playlist.Id + " AND m.id = pm.musicid");
                    while(reader.Read())
                    {
                        playlist.AudioFileList.Add(reader["filename"].ToString());
                    }
                }
                reader.Close();
                //Transcoders
                reader = this.Controls.ExecuteDataReader("SELECT c.filename AS CalendarFilename, tr.id, tr.name AS TransName, tr.bitrate, tr.samplerate, tr.url, tr.ip, tr.port, tr.adminport, tr.password, tr.configfilename, tr.logfilename, st.name AS StreamName FROM tcalendar c, ttranscoder tr, tstreamtype st WHERE tr.webradioid = " + id.ToString() + " AND tr.streamtypeid = st.id AND c.webradioid = " + id.ToString());
                while(reader.Read())
                {
                    WebradioTranscoder trans = null;
                    if(reader["StreamName"].ToString() == StreamType.MP3.ToString())
                        trans = new TranscoderMp3(int.Parse(reader["id"].ToString()),
                            reader["TransName"].ToString(),
                            int.Parse(reader["bitrate"].ToString()),
                            int.Parse(reader["samplerate"].ToString()),
                            IPAddress.Parse(reader["ip"].ToString()),
                            int.Parse(reader["port"].ToString()),
                            int.Parse(reader["adminport"].ToString()),
                            reader["url"].ToString(),
                            reader["password"].ToString(),
                            reader["configfilename"].ToString(),
                            reader["logfilename"].ToString());
                    else
                        trans = new TranscoderAacPlus(int.Parse(reader["id"].ToString()),
                            reader["TransName"].ToString(),
                            int.Parse(reader["bitrate"].ToString()),
                            int.Parse(reader["samplerate"].ToString()),
                            IPAddress.Parse(reader["ip"].ToString()),
                            int.Parse(reader["port"].ToString()),
                            int.Parse(reader["adminport"].ToString()),
                            reader["url"].ToString(),
                            reader["password"].ToString(),
                            reader["configfilename"].ToString(),
                            reader["logfilename"].ToString());
                    trans.CalendarFile = reader["CalendarFilename"].ToString();
                    wr.Transcoders.Add(trans);
                }
                reader.Close();
                //---

            }

            return webradios;
        }