johnyHV

Speech Time

Sep 2nd, 2011
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.52 KB | None | 0 0
  1. |---------------------------------------|
  2. |                   |
  3. |   Created johnyHV         |
  4. |   E-mail johnyhvv@gmail.com   |
  5. |                   |
  6. |   Licence: GNU/GPL        |
  7. |                   |
  8. |---------------------------------------|
  9.  
  10. --------------------------------------------------------------------------------
  11. ////    main.cpp
  12.  
  13. #include <iostream>                                                                     //Kniznica pre IO operacie
  14. #include <ctime>                                                                        //Kniznica pre citanie casu
  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.  
  27.     HideConsole();
  28.  
  29.  
  30.     std::string name_audio[24] = {"00.wav","01.wav","02.wav","03.wav","04.wav","05.wav","06.wav","07.wav","08.wav","09.wav","10.wav","11.wav",
  31.                                   "12.wav","13.wav","14.wav","15.wav","16.wav","17.wav","18.wav","19.wav","20.wav","21.wav","22.wav","23.wav"};
  32.     Speech R_Cas(&name_audio[0]);
  33.     bool Tone,Audio;
  34.     string config,home,cesta;
  35.     int Hlasitost,Moznost;
  36.  
  37.  
  38.     // Nastavenie ciest pre config
  39.  
  40.     //cesta = "/SpeechTime/Language/";                                                  // Linux cesta s ulozenym config file
  41.     //home = getenv("HOME");                                                            // Linux ziskava adresu home adresara
  42.  
  43.     cesta = "\\Program Files\\SpeechTime\\";                                            // Windows cesta s ulozenym config file
  44.     home = getenv("SYSTEMDRIVE");                                                       // Windows ziskanie particie na ktorej je nainstalovany system
  45.  
  46.  
  47.     // kontrola casu a samotne prehratie
  48.  
  49.     int Hodina;
  50.     for (;;)
  51.     {
  52.         // nacitanie config pre volume
  53.  
  54.         config = home + cesta + "volume";                                               // vytvorenie absolutnej cesty configu s nastavenou hlasitostou
  55.  
  56.         if (!(R_Cas.Read_Config(config.c_str(),Hlasitost)))                             // nacitanie configu nacitania hlasitosti
  57.             Hlasitost = 100;                                                            // Ak nebol nacitany config tak sa nastavi defaultne 100% hlasitost
  58.  
  59.  
  60.         // nacitanie config pre mod prehravania
  61.  
  62.         config = home + cesta + "config";                                               // Vytvorenie absolutnej cesty pre config stylu prehravania
  63.  
  64.         if (!(R_Cas.Read_Config(config.c_str(),Moznost)))                               // nacitanie configu nacita nastavenie
  65.             Moznost = 1;                                                                // ak nebol nacitany config tak sa nastavi default... prv pipne potom prehra aktualny cas
  66.  
  67.  
  68.         // nastavenie modu prehravania
  69.  
  70.         R_Cas.Set_play(Moznost,Tone,Audio);                                             // Nastavenie modu prehravania
  71.  
  72.         if (R_Cas.Control_time(Hodina))                                                 // kontrola casu (celej hodiny)
  73.         {
  74.             R_Cas.Play_ton(Tone,Hlasitost);                                             // prehravanie tonu
  75.             sf::Sleep(0.10);                                                            // casove oneskorenie medzi prehravanim
  76.             R_Cas.Play_time(Audio,Hodina,Hlasitost);                                    // prehravanie audio stop
  77.             sf::Sleep(50);                                                              // casove oneskorenie aby sa stale dookola neprehravalo... Moze sa stat ze za minutu stihne prehrat aj 2 krat
  78.         }
  79.     }
  80. }
  81.  
  82. std::string GetConsoleTitle()
  83. {
  84.   unsigned long length;
  85.   std::string result( 1000, '\0' );
  86.   length = GetConsoleTitle(const_cast<char *>(result.c_str()),1000);
  87.   result.resize( length );
  88.   return result;
  89. }
  90.  
  91. void HideConsole()
  92. {
  93.     HWND hConsole = FindWindow(STR_CONSOLE_WINDOW_CLASS,GetConsoleTitle().c_str());
  94.     ShowWindow( hConsole, SW_HIDE );
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. --------------------------------------------------------------------------------
  104. ////    Speech.cpp
  105.  
  106. #include "speech.h"
  107.  
  108. Speech::Speech(std::string *v_name_audio)
  109. {
  110.     name_audio = v_name_audio;
  111.  
  112.     //cesta = "/SpeechTime/Language/";                                                  // Linux,
  113.     //home = getenv("HOME");                                                            // Linux ziskava adresu home adresara
  114.     //tone = "tone.wav";                                                                // Linux
  115.  
  116.     home = getenv("SYSTEMDRIVE");                                                       // Windows ziskanie particie na ktorej je nainstalovany system
  117.     cesta = "\\Program Files\\SpeechTime\\Language\\";                                  // Windows
  118.     tone = "tone.wav";                                                                  // Windows
  119.  
  120. }
  121.  
  122. void Speech::Play_time(bool &vstup, int &cas, int &volume)
  123. {
  124.     if (vstup)                                                                          // Kontrola ci ma spustit prehratie alebo nie
  125.     {
  126.         plna_cesta = home + cesta + name_audio[cas];                                    // Vytvori sa absolutna cesta s umiestnenim danej stopy
  127.  
  128.         sf::SoundBuffer Buffer;
  129.         if (!Buffer.LoadFromFile(plna_cesta))                                           // Otvorenie audio stopy
  130.             std::cout <<"Sound file Not Found!"<<std::endl;
  131.         sf::Sound Sound(Buffer);
  132.  
  133.         Sound.SetVolume(volume);                                                        // Nastavenie hlasitosti
  134.         Sound.Play();                                                                   // Spusti prehratie
  135.  
  136.         while (Sound.GetStatus() == sf::Sound::Playing)                                 // Caka pokial sa stopa este prehrava
  137.         {
  138.             sf::Sleep(0.9f);
  139.         }
  140.     }
  141. }
  142.  
  143.  
  144. void Speech::Play_ton(bool &vstup, int &volume)
  145. {
  146.     if (vstup)                                                                          // Kontrola ci sa ma spustit prehratie alebo nie
  147.     {
  148.         plna_cesta = home + cesta + tone;                                               // Vytvara absolutnu cestu tonu
  149.  
  150.         sf::SoundBuffer Buffer;
  151.         if (!Buffer.LoadFromFile(plna_cesta))                                           // Otvorenie audio stopy
  152.             std::cout <<"Sound file Not Found!"<<std::endl;
  153.         sf::Sound Sound(Buffer);
  154.  
  155.         Sound.SetVolume(volume);                                                        // Nastavenie hlastitosti
  156.         Sound.Play();
  157.  
  158.         while (Sound.GetStatus() == sf::Sound::Playing)                                 // Caka pokial sa stopa este prehrava
  159.         {
  160.             sf::Sleep(0.1f);
  161.         }
  162.     }
  163. }
  164.  
  165.  
  166. bool Speech::Control_time(int &cas)
  167. {
  168.     sf::Sleep(1);
  169.     time (&rawtime);
  170.     timeinfo = localtime(&rawtime);
  171.  
  172.     for (int i = 0;i<=23;i++)
  173.     {
  174.         if ((timeinfo->tm_hour == i) && (timeinfo->tm_min == 00))                       // Kontroluje ci je cela hodina
  175.         {
  176.             cas = i;                                                                    // pokial ano, priradi hodnotu hodiny do premennej
  177.             return true;                                                                // a vrati true hodnotu pre dalsie spracovanie
  178.         }
  179.     }
  180.     return false;                                                                       // pokial nieje cela hodina vrati fasle hodnotu
  181. }
  182.  
  183. bool Speech::Read_Config(const char *meno, int &config)
  184. {
  185.     inFile.open(meno);                                                                  // otvorenie suboru
  186.     if (!(inFile.is_open()))                                                            // Kontrola existencie suboru a spravneho otvorenia
  187.     {
  188.         inFile.close();                                                                 // Uyatvori subor
  189.         return false;                                                                   // Ak sa subor neotvoril vrati false
  190.     }
  191.     else
  192.     {
  193.         inFile >> config;                                                               // Ak jestvuje nastavi sa moznost podla configu
  194.         inFile.close();                                                                 // Uzatvori subor
  195.         return true;                                                                    // Vrati navratovu hodnotu true
  196.     }
  197. }
  198.  
  199. void Speech::Set_play(int moznost, bool &tone, bool &audio)
  200. {
  201.     switch(moznost)
  202.     {
  203.         case 1:                                                                         // Prehrava sa aj Audio aj Ton
  204.         {
  205.             tone = true;
  206.             audio = true;
  207.         }break;
  208.         case 2:                                                                         // Prehrava sa len Audio
  209.         {
  210.             tone = false;
  211.             audio = true;
  212.         }break;
  213.         case 3:                                                                         // Prehrava sa len Ton
  214.         {
  215.             tone = true;
  216.             audio = false;
  217.         }break;
  218.         default:                                                                        // Ochrana ak by sa nahodou nieco pokazilo
  219.         {
  220.             tone = true;
  221.             audio = true;
  222.         }
  223.     }
  224. }
  225.  
  226.  
  227.  
  228.  
  229.  
  230. --------------------------------------------------------------------------------
  231. //// Speech.h
  232.  
  233. #ifndef _SPEECH_H_
  234. #define _SPEECH_H_
  235.  
  236. #include <SFML/Audio.hpp>                                                   // Kniznica pre prehravanie audio stop (sfml-dev.org) + pridat dalsi prepinac compilovania -lsfml-audio
  237. #include <SFML/System.hpp>                                                  // Kniznica pre metodu sleep
  238. #include <ctime>                                                            // Kniznica pre citanie casu
  239. #include <string>                                                           // Kniznica pre string
  240. #include <iostream>                                                         // Kniznica pre IO operacie
  241. #include <fstream>                                                          // Kniznica pre pracu s subormi
  242.  
  243. using namespace std;
  244.  
  245. class Speech
  246. {
  247.     private:
  248.  
  249.             std::string plna_cesta,cesta,tone,home;
  250.             std::string *name_audio;                                         // ukazovatel na string
  251.             time_t rawtime;
  252.             tm *timeinfo;                                                    // pointer struktury tm
  253.             ifstream inFile;                                                 // objekt pre pracu s suborom
  254.  
  255.  
  256.     public:
  257.  
  258.             Speech(std::string *v_name_audio);                              // Konstruktor koli cestam
  259.             void Play_time(bool &vstup, int &cas, int &volume);             // Prehrava audio nahravky casu
  260.             void Play_ton(bool &vstup, int &volume);                        // Prehrava tony
  261.             void Sleep(int &cas) { sf::Sleep(cas); };                       // Spanok aplikacie
  262.             bool Control_time(int &cas);                                    // Kontrola aktualneho casu
  263.             bool Read_Config(const char *meno, int &config);                // Otvaranie suborov a citanie config nastaveni
  264.             void Set_play(int moznost, bool &tone, bool & audio);           // Nastavi styl prehravania zvucky
  265.             ~Speech(){};                                                    // Destruktor
  266. };
  267.  
  268. #endif
Advertisement
Add Comment
Please, Sign In to add comment