Sie sind auf Seite 1von 5

/* Author: Rakesh Singh Purpose: Annuciate current date & time Inputs : .

wav files im c:\sound Outputs: Time annuciated in my voice Version: 1.0 Date : August 18, 2013 */ // note 1 //Refrences: //http://www.tutorialspoint.com/cplusplus/cpp_date_time.htm // note 2 /* http://www.cplusplus.com/forum/general/8510/ Use this API Function in windows.h PlaySound("C:\\SOUNDS\\BELLS.WAV", NULL, SND_ASYNC);

Source: http://www.dreamincode.net/forums/showtopic18352.htm This may not play large .wav files. (more than about 1MB) Edit: More on the PlaySound function http://msdn.microsoft.com/en-us/library/ms712879.aspx */ // note 3 /*PlaySound function With the help of my colleage, I have solved the the problem when compiling with the PlaySound function. Here is the solution: 1. Include the following header files in this order: #include "windows.h" #include "mmsystem.h" 2. follow the following steps to add winmm.lib to the linker (assuming Visual S tudio 2010): a. Right click the project name in the Solution Explorer and select "Property". b. On the left pane of the Property window, select "Linker" and then "Input" c. On the right pane, type winmm.lib in the "Additional Dependencies" row. d. Click "Apply" and then "OK". You are now ready to compile and play .wav files. */ #include #include #include #include #include #include <iostream> <ctime> "windows.h" "mmsystem.h" <string> <sstream>

using namespace std; /* struct tm { int tm_sec; // seconds of minutes from 0 to 61 int tm_min; // minutes of hour from 0 to 59

int int int int int int int } */

tm_hour; tm_mday; tm_mon; tm_year; tm_wday; tm_yday; tm_isdst;

// // // // // // //

hours of day from 0 to 24 day of month from 1 to 31 month of year from 0 to 11 year since 1900 days since sunday days since January 1st hours of daylight savings time

int main( ) { // declare variables string date; //ostringstream convert; string hour; string minute; string second; //tm *ltm; time_t now; // multithreads can clobber the buffer, so keep a separate buffer struct tm tm_buffer = {(0,0,0,0,0,0,0,0,0)}; tm *ltm; ltm = &tm_buffer; //localtime_s(&timeinfo, &rawtime); // infinite loop for( ; ; ) { date = ""; hour = ""; minute = ""; second = ""; // current date/time based on current system now = time(0); //cout << "Number of sec since January 1,1970:" << now << endl; //ltm = localtime(&now); localtime_s(ltm, &now); // print various components of tm structure. /* Diagnostics cout << "Year: "<< 1900 + ltm->tm_year << endl; cout << "Month: "<< 1 + ltm->tm_mon<< endl; cout << "Day: "<< ltm->tm_mday << endl; //cout << "Time: "<< 1 + ltm->tm_hour << ":"; cout << "Time: "<< ltm->tm_hour << ":"; cout << 1 + ltm->tm_min << ":"; cout << 1 + ltm->tm_sec << endl; */ PlaySound("c:\\sound\\SET_DATE_TO.WAV", NULL, SND_FILENAME); // annunciate month switch (1 + ltm->tm_mon) { case 1 : // January

PlaySound("c:\\sound\\JAN.WAV", NULL, SND_FILENAME); break; case 2 : // February PlaySound("c:\\sound\\FEB.WAV", NULL, SND_FILENAME); break; case 3 : // March PlaySound("c:\\sound\\MAR.WAV", NULL, SND_FILENAME); break; case 4 : // April PlaySound("c:\\sound\\APR.WAV", NULL, SND_FILENAME); break; case 5 : // May PlaySound("c:\\sound\\MAY.WAV", NULL, SND_FILENAME); break; case 6 : // June PlaySound("c:\\sound\\JUN.WAV", NULL, SND_FILENAME); break; case 7 : // July PlaySound("c:\\sound\\JUL.WAV", NULL, SND_FILENAME); break; case 8 : // August PlaySound("c:\\sound\\AUG.WAV", NULL, SND_FILENAME); break; case 9 : // September PlaySound("c:\\sound\\SEP.WAV", NULL, SND_FILENAME); break; case 10 : // October PlaySound("c:\\sound\\OCT.WAV", NULL, SND_FILENAME); break; case 11 : // November PlaySound("c:\\sound\\NOV.WAV", NULL, SND_FILENAME); break; case 12 : // December PlaySound("c:\\sound\\DEC.WAV", NULL, SND_FILENAME); break; default :

// drop out break; } // annunciate date //date = "c:\\sound\\" + to_string(1 + ltm->tm_mday) + ".WAV"; date = "c:\\sound\\"; // convert number to string ostringstream convert; convert << ltm->tm_mday; date = date + convert.str() + ".WAV"; //cout << date << endl; PlaySound(date.c_str(), NULL, SND_FILENAME); // annunciate year if (1900 + ltm->tm_year == 2013) PlaySound("c:\\sound\\2013.WAV", NULL, SND_FILENAME); // set current time PlaySound("c:\\sound\\SET_TIME_TO.WAV", NULL, SND_FILENAME); // annunciate hour // current date/time based on current system now = time(0); localtime_s(ltm, &now); hour = "c:\\sound\\"; ostringstream convert1; if (ltm->tm_hour > 12) convert1 << ltm->tm_hour - 12; else convert1 << ltm->tm_hour; hour = hour + convert1.str() + ".WAV"; //cout <<"hour =" << hour << endl; PlaySound(hour.c_str(), NULL, SND_FILENAME); // annunciate minute // current date/time based on current system now = time(0); localtime_s(ltm, &now); minute = "c:\\sound\\"; ostringstream convert2; convert2 << ltm->tm_min; minute = minute + convert2.str() + ".WAV"; if (ltm->tm_min > 0 && ltm->tm_min < 10) PlaySound("c:\\sound\\0.WAV", NULL, SND_FILENAME); PlaySound(minute.c_str(), NULL, SND_FILENAME); // annunciate second // current date/time based on current system now = time(0); localtime_s(ltm, &now); PlaySound("c:\\sound\\AND.WAV", NULL, SND_FILENAME); second = "c:\\sound\\"; ostringstream convert3; convert3 << ltm->tm_sec; second = second + convert3.str() + ".WAV"; PlaySound(second.c_str(), NULL, SND_FILENAME);

PlaySound("c:\\sound\\SECONDS.WAV", NULL, SND_FILENAME); if (ltm->tm_hour > 12) PlaySound("c:\\sound\\PM.WAV", NULL, SND_FILENAME); else PlaySound("c:\\sound\\AM.WAV", NULL, SND_FILENAME); } // end infinite loop return 0; }

Das könnte Ihnen auch gefallen