johnyHV

johnyHV / Speech Time

Sep 20th, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.54 KB | None | 0 0
  1. //----------------------------------------------//
  2. //  Speech Time v 2.1           //
  3. //                      //
  4. //  [email protected]          //
  5. //                      //
  6. //  Licence: GNU/GPL            //
  7. //                      //
  8. //----------------------------------------------//
  9.  
  10.  
  11. // main.cpp
  12.  
  13.  
  14. #include <iostream>                                                                         //Kniznica pre IO operacie
  15. #include <string>                                                                           //Kniznica pre pracu s string retazcami
  16. #include <windows.h>                                                                      //Kniznica pre skrytie windows okna
  17. #include "Speech.h"
  18.  
  19. using namespace std;
  20.  
  21. const char STR_CONSOLE_WINDOW_CLASS[] = "ConsoleWindowClass";
  22. void HideConsole();
  23.  
  24. int main()
  25. {
  26.     HideConsole();
  27.  
  28.     bool hod,pol_hod,mod[4];
  29.     string config,home,cesta,p_cesta,a_cesta,jazyk;
  30.     int Hlasitost,Hodina,Ton;
  31.  
  32.     // Nastavenie ciest pre config
  33.  
  34.     //cesta = "/SpeechTime/";                                                                                         // Linux cesta s ulozenym config file
  35.     //home = getenv("HOME");                                                                                          // Linux home adresar
  36.  
  37.     cesta = "\\Program Files\\SpeechTime\\";                                                                      // Windows cesta s ulozenym config file
  38.     home = getenv("SYSTEMDRIVE");                                                                                 // Windows home jednotka
  39.  
  40.     p_cesta = home + cesta;
  41.  
  42.     string name_audio[32] = {"0.wav","1.wav","2.wav","3.wav","4.wav","5.wav","6.wav","7.wav","8.wav","9.wav","10.wav","11.wav",
  43.                              "12.wav","13.wav","14.wav","15.wav","16.wav","17.wav","18.wav","19.wav","20.wav","21.wav","22.wav",
  44.                              "23.wav","24.wav","25.wav","26.wav","27.wav","28.wav","29.wav","30.wav","31.wav"};
  45.  
  46.     Speech R_Cas(&name_audio[0]);
  47.  
  48.     // oznamenie datumu
  49.     int *datum = new int;
  50.     config = p_cesta + "date";                                                                                      // vytvorenie absolutnej cesty pre config hlasitosti
  51.     if (!(R_Cas.Read_Config(*datum, config.c_str())))                                                                // Nacitanie configu ci sa ma alebo nema oznamit datum
  52.         *datum = 1;
  53.  
  54.     if (*datum == 1)
  55.     {
  56.         sf::Sleep(60);                                                                                             // Casove oneskorenie pre nacitanie OS a pod
  57.  
  58.         // nacitanie config pre volume
  59.         config = p_cesta + "volume";                                                                                // vytvorenie absolutnej cesty pre config hlasitosti
  60.         if (!(R_Cas.Read_Config(Hlasitost, config.c_str())))                                                        // Nacitanie hlasitosti
  61.             Hlasitost = 100;
  62.  
  63.         // nacitanie config pre jazyk
  64.         config = p_cesta + "language";                                                                              // Vytvorenie absolutnej cesty pre config jazyka
  65.         if (!(R_Cas.Language(jazyk, config.c_str())))                                                               // nacitanie jazyka
  66.             jazyk = "EN";
  67.  
  68.         string cesta_den,cesta_mesiac,cesta_den_tyzdni;
  69.         int den,mesiac,dent;
  70.  
  71.         R_Cas.Read_Date(dent,den,mesiac);
  72.  
  73.         //cesta_den_tyzdni = p_cesta + "Audio/" + jazyk + "/" + "Date/dayt/" + name_audio[dent];                      // Linux
  74.         cesta_den_tyzdni = p_cesta + "Audio\\" + jazyk + "\\Date\\dayt\\" + name_audio[dent];             // Windows
  75.         //cesta_den = p_cesta + "Audio/" + jazyk + "/" + "Date/day/" + name_audio[den];                               // Linux
  76.         cesta_den =  p_cesta + "Audio\\" + jazyk + "\\Date\\day\\" + name_audio[den];                     // Windows
  77.         //cesta_mesiac = p_cesta + "Audio/" + jazyk + "/" + "Date/moon/" + name_audio[mesiac];                        // Linux
  78.         cesta_mesiac = p_cesta + "Audio\\" + jazyk + "\\Date\\moon\\" + name_audio[mesiac];                   // Windows
  79.  
  80.         R_Cas.Play_date(Hlasitost,cesta_den_tyzdni,cesta_den,cesta_mesiac);
  81.     }
  82.  
  83.     delete datum;
  84.  
  85.     // kontrola casu a samotne prehratie
  86.     for (;;)
  87.     {
  88.         sf::Sleep(0.1);
  89.  
  90.         // nacitanie config pre volume
  91.         config = p_cesta + "volume";                                                        // vytvorenie absolutnej cesty pre config hlasitosti
  92.         if (!(R_Cas.Read_Config(Hlasitost, config.c_str())))                                // Nacitanie hlasitosti
  93.             Hlasitost = 100;
  94.  
  95.         // nacitanie config pre mod prehravania
  96.         config = p_cesta + "config";                                                        // Vytvorenie absolutnej cesty pre config modu prehravania
  97.         if (!(R_Cas.Read_set_play(mod, config.c_str())))                                    // nacitanie modu prehravania
  98.             mod = {1,0,1,0};
  99.  
  100.         // nacitanie tonu prehravania
  101.         config = p_cesta + "ton";                                                           // Vytvorenie absolutnej cesty pre ton
  102.         if (!(R_Cas.Read_Config(Ton, config.c_str())))                                      // Precitanie nastavenia tonu
  103.             Ton = 1;
  104.  
  105.         // nacitanie config pre jazyk
  106.         config = p_cesta + "language";                                                      // Vytvorenie absolutnej cesty pre config jazyka
  107.         if (!(R_Cas.Language(jazyk, config.c_str())))                                       // nacitanie jazyka
  108.             jazyk = "EN";
  109.  
  110.         if (R_Cas.Control_time(hod,pol_hod,Hodina))                                         // kontrola casu (celej hodiny a pol hodiny )
  111.         {
  112.  
  113.             //a_cesta = p_cesta + "Audio/Tone/";                                              // Linux
  114.             a_cesta = p_cesta + "Audio\\Tone\\";                                        // Windows
  115.  
  116.             R_Cas.Play_ton(hod,pol_hod,mod,Hlasitost,Ton,a_cesta);                          // prehravanie tonu
  117.             sf::Sleep(0.40);                                                                // casove oneskorenie medzi prehravanim
  118.  
  119.             //a_cesta = p_cesta + "Audio/" + jazyk + "/" + "Time/";                           // Linux
  120.             a_cesta = p_cesta + "Audio\\" + jazyk + "\\Time\\";                    // Windows
  121.  
  122.             R_Cas.Play_time(hod,pol_hod,mod,Hlasitost,Hodina,jazyk,a_cesta);                // prehravanie audio stop
  123.             sf::Sleep(60);                                                                  // casove oneskorenie aby sa stale dookola neprehravalo... Moze sa stat ze za minutu stihne prehrat aj 2 krat
  124.         }
  125.     }
  126. }
  127.  
  128.  
  129. std::string GetConsoleTitle()
  130. {
  131.   unsigned long length;
  132.   std::string result( 1000, '\0' );
  133.   length = GetConsoleTitle(const_cast<char *>(result.c_str()),1000);
  134.   result.resize( length );
  135.   return result;
  136. }
  137.  
  138. void HideConsole()
  139. {
  140.     HWND hConsole = FindWindow(STR_CONSOLE_WINDOW_CLASS,GetConsoleTitle().c_str());
  141.     ShowWindow( hConsole, SW_HIDE );
  142. }
  143.  
  144.  
  145. // Speech.h
  146.  
  147.  
  148. #ifndef _SPEECH_H_
  149. #define _SPEECH_H_
  150.  
  151. #include <SFML/Audio.hpp>                                                   // Kniznica pre prehravanie audio stop (sfml-dev.org) + pridat dalsi prepinac compilovania -lsfml-audio -lsfml-system
  152. #include <SFML/System.hpp>                                                  // Kniznica pre metodu sleep
  153. #include <ctime>                                                            // Kniznica pre citanie casu
  154. #include <string>                                                           // Kniznica pre string
  155. #include <iostream>                                                         // Kniznica pre IO operacie
  156. #include <fstream>                                                          // Kniznica pre pracu s subormi
  157.  
  158. using namespace std;
  159.  
  160. class Speech
  161. {
  162.     private:
  163.  
  164.             string plna_cesta,pln_cesta;
  165.             string *name_audio;
  166.             string *tone;
  167.             time_t rawtime;
  168.             tm *timeinfo;
  169.             ifstream inFile;
  170.  
  171.  
  172.     public:
  173.  
  174.             Speech(string *v_name_audio);                                                                               // Konstruktor koli cestam
  175.             void Play_time(bool &hod, bool &pol_h, bool (&mod)[4], int &volume, int &cas, string &jazyk, string &cesta);// Prehrava audio nahravky casu
  176.             void Play_ton(bool &hod, bool &p_hod, bool (&mod)[4], int &volume, int ton, string cesta);                  // Prehrava tony
  177.             void Play_date(int &volume, string &cesta_d_t, string &cesta_d, string &cesta_m);                           // Prehra aktualny datum                                                       // Prehrava datum a pod
  178.             void Read_Date(int &dent, int &den, int &mesiac);                                                           // zapise cisla dna v tyzdni,mesiaca, dna v mesiaci
  179.             bool Control_time(bool &hod, bool &p_hod, int &cas);                                                        // Kontrola aktualneho casu
  180.             bool Read_Config(int &config, const char *meno);                                                            // Otvaranie suborov a citanie config nastaveni
  181.             bool Read_set_play(bool (&mod)[4], const char *cesta);                                                      // Nastavi styl prehravania zvucky
  182.             bool Language(string &jazyk, const char *cesta);                                                            // Nacitanie jazyka
  183.             ~Speech(){};                                                                                                // Destruktor
  184. };
  185.  
  186. #endif
  187.  
  188.  
  189. // Speech.cpp
  190.  
  191.  
  192.  
  193. #include "Speech.h"
  194.  
  195. Speech::Speech(string *v_name_audio)
  196. {
  197.     name_audio = v_name_audio;
  198. }
  199.  
  200. void Speech::Play_time(bool &hod, bool &pol_h, bool (&mod)[4], int &volume, int &cas, string &jazyk, string &cesta)
  201. {
  202.         if (pol_h && mod[1])
  203.         {
  204.             plna_cesta = cesta + "30m.wav";
  205.             sf::Music pol_hodina;
  206.             pol_hodina.OpenFromFile(plna_cesta);
  207.  
  208.             pln_cesta = cesta + name_audio[cas];
  209.             sf::Music hodina;
  210.             hodina.OpenFromFile(pln_cesta);
  211.  
  212.             pol_hodina.SetVolume(volume);
  213.             hodina.SetVolume(volume);
  214.  
  215.             if ((jazyk == "EN") || (jazyk == "DE"))
  216.             {
  217.                 pol_hodina.Play();
  218.                 while (pol_hodina.GetStatus() == sf::Music::Playing)
  219.                     ;
  220.  
  221.                 hodina.Play();
  222.                 while (hodina.GetStatus() == sf::Music::Playing)
  223.                     sf::Sleep(0.3);
  224.             }
  225.             else
  226.             {
  227.                 hodina.Play();
  228.                 while (hodina.GetStatus() == sf::Music::Playing)
  229.                     ;
  230.  
  231.                 pol_hodina.Play();
  232.                 while (pol_hodina.GetStatus() == sf::Music::Playing)
  233.                     sf::Sleep(0.3);
  234.             }
  235.  
  236.         }
  237.         else if (hod && mod[0])
  238.         {
  239.             plna_cesta = cesta + name_audio[cas];
  240.             pln_cesta = cesta + "hour.wav";
  241.  
  242.             sf::Music cas;
  243.             cas.OpenFromFile(plna_cesta);
  244.  
  245.             cas.SetVolume(volume);
  246.  
  247.             if ((jazyk == "EN")||(jazyk == "DE"))
  248.             {
  249.                 sf::Music hodina;
  250.                 hodina.OpenFromFile(pln_cesta);
  251.                 hodina.SetVolume(volume);
  252.  
  253.                 cas.Play();
  254.                 while (cas.GetStatus() == sf::Music::Playing)
  255.                     ;
  256.  
  257.                 hodina.Play();
  258.                 while (hodina.GetStatus() == sf::Music::Playing)
  259.                     sf::Sleep(0.1f);
  260.             }
  261.             else
  262.             {
  263.                 cas.Play();
  264.                 while (cas.GetStatus() == sf::Music::Playing)
  265.                     sf::Sleep(0.3);
  266.             }
  267.         }
  268.     }
  269.  
  270.  
  271. void Speech::Play_ton(bool &hod, bool &p_hod, bool (&mod)[4], int &volume, int ton, string cesta)
  272. {
  273.     if (hod && mod[2] || p_hod && mod[3])                                                            // Oznamuje hodinu, pol hodinu
  274.     {
  275.         plna_cesta = cesta + name_audio[ton];
  276.  
  277.         sf::SoundBuffer Buffer;
  278.         if (!Buffer.LoadFromFile(plna_cesta))
  279.             std::cout <<"Sound file Not Found!"<<std::endl;
  280.         sf::Sound Sound(Buffer);
  281.  
  282.         Sound.SetVolume(volume);                                                                     // Nastavenie hlastitosti
  283.         Sound.Play();
  284.  
  285.         while (Sound.GetStatus() == sf::Sound::Playing)                                              // Caka pokial sa stopa este prehrava
  286.         {
  287.             sf::Sleep(0.1f);
  288.         }
  289.     }
  290. }
  291.  
  292.  
  293. void Speech::Play_date(int &volume,string &cesta_d_t, string &cesta_d, string &cesta_m)
  294. {
  295.     sf::Music tyzden;
  296.     tyzden.OpenFromFile(cesta_d_t);
  297.  
  298.     sf::Music den;
  299.     den.OpenFromFile(cesta_d);
  300.  
  301.     sf::Music mesiac;
  302.     mesiac.OpenFromFile(cesta_m);
  303.  
  304.     tyzden.SetVolume(volume);
  305.     den.SetVolume(volume);
  306.     mesiac.SetVolume(volume);
  307.  
  308.     // Prehra den v tyzdni
  309.     tyzden.Play();
  310.     while (tyzden.GetStatus() == sf::Music::Playing)
  311.         sf::Sleep(0.2f);
  312.  
  313.     // Prehra den v mesiaci
  314.     den.Play();
  315.     while (den.GetStatus() == sf::Music::Playing)
  316.         sf::Sleep(0.1);
  317.  
  318.     // Prehra mesiac
  319.     mesiac.Play();
  320.     while (mesiac.GetStatus() == sf::Music::Playing)
  321.         sf::Sleep(0.3f);
  322. }
  323.  
  324. void Speech::Read_Date(int &dent, int &den, int &mesiac)
  325. {
  326.     time (&rawtime);
  327.     timeinfo = localtime(&rawtime);
  328.  
  329.     dent = timeinfo->tm_wday;
  330.     den = timeinfo->tm_mday;
  331.     mesiac = timeinfo->tm_mon;
  332. }
  333.  
  334. bool Speech::Control_time(bool &hod, bool &p_hod, int &cas)                                          // Kontrola casu
  335. {
  336.     sf::Sleep(1);
  337.     time (&rawtime);
  338.     timeinfo = localtime(&rawtime);
  339.  
  340.     for (int i = 0;i<=23;i++)
  341.     {
  342.         if ((timeinfo->tm_hour == i) && (timeinfo->tm_min == 30))                                   // Kontroluje pol hodinu
  343.         {
  344.             cas = i;
  345.             hod = false;
  346.             p_hod = true;
  347.             return true;
  348.         }
  349.  
  350.         if ((timeinfo->tm_hour == i) && (timeinfo->tm_min == 00))                                    // Kontroluje celu hodinu
  351.         {
  352.             cas = i;
  353.             hod = true;
  354.             p_hod = false;
  355.             return true;
  356.         }
  357.     }
  358.     return false;
  359. }
  360.  
  361. bool Speech::Read_Config(int &config, const char *cesta)
  362. {
  363.     inFile.open(cesta);
  364.     if (!(inFile.is_open()))                                                                        // Kontrola existencie suboru a spravneho otvorenia
  365.     {
  366.         inFile.close();
  367.         return false;
  368.     }
  369.     else
  370.     {
  371.         inFile >> config;                                                                           // Ak jestvuje nastavi sa moznost podla configu
  372.         inFile.close();
  373.         return true;
  374.     }
  375. }
  376.  
  377. bool Speech::Read_set_play(bool (&mod)[4], const char *cesta)                                       // Nacitanie modu prehravania
  378. {
  379.     inFile.open(cesta);
  380.     if (!(inFile.is_open()))                                                                        // Kontrola existencie suboru a spravneho otvorenia
  381.     {
  382.         inFile.close();
  383.         return false;
  384.     }
  385.     else
  386.     {
  387.         char temp;
  388.         for (int i=0;i<4;i++)
  389.         {
  390.             inFile.get(temp);                                                                       // ziskanie jedneho znaku z config file
  391.             mod[i] = int (temp) - 48;                                                               // prekonvertuje char na 1 alebo 0 a nasledne priradi do bool pola
  392.         }
  393.         inFile.close();
  394.         return true;
  395.     }
  396. }
  397.  
  398. bool Speech::Language(string &jazyk, const char *cesta)                                             // Nacitanie jazyka
  399. {
  400.     string config;
  401.     inFile.open(cesta);
  402.  
  403.     if (!(inFile.is_open()))                                                                        // Kontrola existencie suboru a spravneho otvorenia
  404.     {
  405.         inFile.close();
  406.         cout <<"Subor neotvoreny!"<<endl;
  407.         return false;
  408.     }
  409.     else
  410.     {
  411.         inFile >> config;
  412.         if ( config == "EN" || config == "DE" || config == "CZ" )                                   // Kontrola spravneho nastavenia jazyka
  413.         {
  414.             inFile.close();
  415.             jazyk = config;
  416.             return true;
  417.         }
  418.         else                                                                                        // Pri nespravnom nastaveni sa nastavi default
  419.         {
  420.             inFile.close();
  421.             return false;
  422.         }
  423.     }
  424. }
Advertisement
Add Comment
Please, Sign In to add comment