Skip to content

Commit

Permalink
Refactor and implement internal and console functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ChemiCalChems committed May 6, 2017
1 parent a78cc47 commit 2d5704c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 46 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ add_library(morse SHARED ${SOURCE_FILES})

add_executable(test src/test.cpp)

target_link_libraries(test ${OPENAL_LIBRARY})

target_link_libraries(morse ${OPENAL_LIBRARY})
target_link_libraries(test morse)
24 changes: 19 additions & 5 deletions src/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,35 @@
namespace Morse {
void console(std::string command) {
//TODO: say, parse, print
std::regex sayregex ("\\s*say\\s+(\\w+)");

while(true) {
std::regex playregex ("^\\s*play\\s+([\\w\\s]+)\\s*$");
std::regex parseregex ("^\\s*parse\\s+([/.\\s\\-]+)\\s*$");
std::regex printregex ("^\\s*print\\s+([\\w\\s]+)\\s*$");

while (true) {
std::cout << ">>> ";
std::string input;
std::getline(std::cin, input);

std::smatch m;
if(std::regex_match(input, m, sayregex)) {
auto buf = eventsToBuffer(stringToEvents(m[1]));
if (std::regex_match(input, m, playregex)) {
auto buf = eventsToBuffer(textToEvents(m[1]));

ALuint src;
alGenSources(1, &src);
alSourcei(src, AL_BUFFER, *buf);
alSourcePlay(src);
}
if (std::regex_match(input, m, parseregex)) {
std::cout << eventsToText(morseToEvents(m[1])) << std::endl;
}
if (std::regex_match(input, m, printregex)) {
std::cout << eventsToMorse(textToEvents(m[1])) << std::endl;
}
if (std::regex_match(input, m, std::regex("^\\s*exit\\s*"))) {
std::cout << "closing console..." << std::endl;
return;
}
}

}
}
66 changes: 43 additions & 23 deletions src/morse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ namespace Morse {
{'0', {Event::dah, Event::symbolSpace, Event::dah, Event::symbolSpace, Event::dah, Event::symbolSpace, Event::dah, Event::symbolSpace, Event::dah}}
};

std::vector<Event> stringToEvents(std::string str) {
std::vector<Event> textToEvents(std::string str) {
std::vector<Event> result;
for (char c : str) {
result.push_back(Event::letterSpace);
for (int i = 0; i<str.size(); i++) {
char c = str.at(i);

if(!result.empty()) {
if (str.at(i-1) != ' ' && c != ' ') result.push_back(Event::letterSpace);
}
result.insert(result.end(), morseCode.at(c).begin(), morseCode.at(c).end());
}

Expand Down Expand Up @@ -110,11 +114,11 @@ namespace Morse {
ALuint* buffer = new ALuint;
alGenBuffers(1, buffer);

alBufferData(*buffer, AL_FORMAT_MONO16, &samples[0], 2*samples.size(), sampleRate);
alBufferData(*buffer, AL_FORMAT_MONO16, &samples[0], sizeof(short)*samples.size(), sampleRate);
return buffer;
}

std::string eventsToMorseString (std::vector<Event> events) {
std::string eventsToMorse(std::vector<Event> events) {
std::string result;

for (auto event : events) {
Expand All @@ -129,33 +133,49 @@ namespace Morse {
result += " ";
break;
case Event::wordSpace:
result += " / ";
result += "/";
break;
}
}

return result;
}
}

int main() {
Morse::initialize();
std::string eventsToText(std::vector<Event> events) {
std::string result;

while (true) {
std::string input;
std::getline(std::cin, input);
std::for_each(input.begin(), input.end(), [](char& c){c = std::tolower(c);});

auto output = Morse::stringToEvents(input);
auto buf = Morse::eventsToBuffer(output);

std::cout << Morse::eventsToMorseString(output);
std::vector<Event> currentLetter;
for (auto event : events) {
if (event == Event::letterSpace || event == Event::wordSpace) {
std::for_each(morseCode.begin(), morseCode.end(), [event, currentLetter, &result](std::pair<char, std::vector<Event>> p) {
if (p.second == currentLetter) result += p.first;
});
if (event == Event::wordSpace) {result += " ";}
currentLetter.clear();
} else {
currentLetter.push_back(event);
}
}

ALuint src;
alGenSources(1, &src);
alSourcei(src, AL_BUFFER, *buf);
alSourcePlay(src);
std::for_each(morseCode.begin(), morseCode.end(), [currentLetter, &result](std::pair<char, std::vector<Event>> p) {
if (p.second == currentLetter) result += p.first;
});

return result;
}

Morse::terminate();
std::vector<Event> morseToEvents(std::string morse) {
std::vector<Event> result;

for (char c : morse) {
switch (c) {
case '.': if (!result.empty() && (result.back() == Event::dih || result.back() == Event::dah)) result.push_back(Event::symbolSpace); result.push_back(Event::dih); break;
case '-': if (!result.empty() && (result.back() == Event::dih || result.back() == Event::dah)) result.push_back(Event::symbolSpace); result.push_back(Event::dah); break;
case ' ': result.push_back(Event::letterSpace); break;
case '/': result.push_back(Event::wordSpace); break;
}
}

return result;
}
}
11 changes: 8 additions & 3 deletions src/morse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@

namespace Morse {
enum class Event {dih, dah, symbolSpace, letterSpace, wordSpace};

extern std::map<char, std::vector<Event>> morseCode;

std::vector<Event> stringToEvents(std::string str);
void initialize();
void terminate();

std::vector<Event> textToEvents(std::string str);
std::vector<Event> morseToEvents(std::string str);

std::string eventsToMorse(std::vector<Event>);
std::string eventsToText(std::vector<Event>);

ALuint* eventsToBuffer(std::vector<Event>);
std::string eventsToMorseString(std::vector<Event>);
std::vector<Event> bufferToEvents(ALuint* buffer);
}

#endif
15 changes: 2 additions & 13 deletions src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,9 @@
#include "console.hpp"

int main() {

Morse::initialize();

while (true) {
Morse::console();
std::string input;
std::getline(std::cin, input);
std::for_each(input.begin(), input.end(), [](char& c){c = std::tolower(c);});

auto output = Morse::stringToEvents(input);
auto buf = Morse::eventsToBuffer(output);

std::cout << Morse::eventsToMorseString(output);
}

Morse::console();

Morse::terminate();
}

0 comments on commit 2d5704c

Please sign in to comment.