I was wondering what would be the best way to implement a morse code generator. The way it would work is the user types in a word or phrase and then that NSString would be passed into my method as an argument for processing. The way I want to process is to loop through each character in the string and then play the correct sequence of tones as I go. I've already implemented the mechanism in a separate class.
I've already tried so many different methods but they all seem to introduce some sort of problem. Things i've tried so far are: creating an NS Timer, using delaying method calls (performSelector:withObject:afterDelay).
Below is an example of the method i'm using to delay, but the problem is that if the user types in say "aaaa" only the first "a" will play because the entire loop will have been completed before the morse sequence is even done generating. It would be more ideal if the loop could wait for the switch case to finish before continuing.
Anyways this is the way that i thought it should be implemented but it doesn't seem to be working, so maybe there is a better way of implementing the morse code generator or i'm just missing a step so if anyone can please help me figure this out i'd really appreciate it.
- (NSString *)convertTextToMorse:(NSString *)phrase {
for (int i = 0; i < [phrase length]; i++) {
unichar ch;
ch = [phrase characterAtIndex:i];
NSLog(@"Processing charachter %c",ch);
//Dot = 100ms
//Dash = 300ms
//Pause = 200ms
switch (ch) {
case 'a':
//Morse "a"
[self playDotSound];
[self performSelector:@selector(playDashSound) withObject:nil afterDelay:0.2];
break;
case 'b':
//Morse "b"
break;
default:
break;
}
}
return phrase;
}