random

:-)
git clone read: git://git.margiolis.net/random.git
Log | Files | Refs | LICENSE

morse.cpp (2483B)


      1 #include <iostream>
      2 #include <map>
      3 #include <string>
      4 
      5 #include <SDL2/SDL.h>
      6 #include <SDL2/SDL_mixer.h>
      7 
      8 static void
      9 play(Mix_Chunk *sound)
     10 {
     11 	Mix_PlayChannel(-1, sound, 0);	  
     12 	while (Mix_Playing(-1) != 0)
     13 		SDL_Delay(200);
     14 }
     15 
     16 int
     17 main(int argc, char *argv[])
     18 {
     19 	Mix_Chunk *dot	= nullptr;
     20 	Mix_Chunk *dash = nullptr;
     21 	std::string in, out;
     22 	std::map<char, std::string>::const_iterator it;
     23 	std::map<char, std::string> morsetable = {
     24 		{'a', ".-"     }, {'A', ".-"	 }, 
     25 		{'b', "-..."   }, {'B', "-..."	 },
     26 		{'c', "-.-."   }, {'C', "-.-."	 },
     27 		{'d', "-.."    }, {'D', "-.."	 },
     28 		{'e', "."      }, {'E', "."	 },   
     29 		{'f', "..-."   }, {'F', "..-."	 },
     30 		{'g', "--."    }, {'G', "--."	 },
     31 		{'h', "...."   }, {'H', "...."	 },
     32 		{'i', ".."     }, {'I', ".."	 }, 
     33 		{'j', ".---"   }, {'J', ".---"	 },
     34 		{'k', "-.-"    }, {'K', "-.-"	 },
     35 		{'l', ".-.."   }, {'L', ".-.."	 },
     36 		{'m', "--"     }, {'M', "--"	 }, 
     37 		{'n', "-."     }, {'N', "-."	 }, 
     38 		{'o', "---"    }, {'O', "---"	 },
     39 		{'p', ".--."   }, {'P', ".--."	 },
     40 		{'q', "--.-"   }, {'Q', "--.-"	 },
     41 		{'r', ".-."    }, {'R', ".-."	 },
     42 		{'s', "..."    }, {'S', "..."	 },
     43 		{'t', "-"      }, {'T', "-"	 },   
     44 		{'u', "..-"    }, {'U', "..-"	 },
     45 		{'v', "...-"   }, {'V', "...-"	 },
     46 		{'w', ".--"    }, {'W', ".--"	 },
     47 		{'x', "-..-"   }, {'X', "-..-"	 },
     48 		{'y', "-.--"   }, {'Y', "-.--"	 },
     49 		{'z', "--.."   }, {'Z', "--.."	 },
     50 		{'&', ".-..."  }, {'\'', ".----."},
     51 		{'@', ".--.-." }, {':', "---..." },  
     52 		{'(', "-.--."  }, {')', "-.--.-" },
     53 		{',', "--..--" }, {'=', "-...-"  },
     54 		{'!', "-.-.--" }, {'.', ".-.-.-" },
     55 		{'-', "-....-" }, {'+', ".-.-."  },
     56 		{'\"', ".-..-."}, {'\?', "..--.."},
     57 		{'/', "-..-."  }, {' ', " / "	 }
     58 	};
     59 
     60 	if (SDL_Init(SDL_INIT_AUDIO) < 0)
     61 		return (1);
     62 	Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 1, 4096);
     63 	dot  = Mix_LoadWAV("dot.wav");
     64 	dash = Mix_LoadWAV("dash.wav");
     65 
     66 	std::cout << "Press CTRL+C to exit" << std::endl;
     67 	for (;;) {
     68 		std::cout << "> ";
     69 		std::getline(std::cin, in);
     70 		for (const char& c : in) {
     71 			it = morsetable.find(c);
     72 			if (morsetable.find(c) != morsetable.end())
     73 				out += it->second;
     74 			else
     75 				std::cerr << "Character '" << c <<
     76 				    "' not found." << std::endl;
     77 			out += " ";
     78 		}
     79 		if (!out.empty()) {
     80 			std::cout << out << std::endl;
     81 			for (const char& c : out) {
     82 				if (c == '.')
     83 					play(dot);
     84 				else if (c == '-')
     85 					play(dash);
     86 			}
     87 			out.clear();
     88 		}
     89 	}
     90 
     91 	Mix_FreeChunk(dot);
     92 	Mix_FreeChunk(dash);
     93 	Mix_CloseAudio();
     94 	SDL_Quit();
     95 
     96 	return (0);
     97 }