views:

42

answers:

3

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;

}
A: 

As you figure out what sounds to play, add them to an array. Play through the array and save your position in the array as you go along. If you get stuck parsing, pause, and continue right where you left off.

Alternatively, figure out how long each letter takes and use a variable for the playback delay.

Moshe
+1  A: 

Another option might be to run the entire thing on a separate thread:

- (NSString *) convertTextToMorse:(NSString *)phrase {
    [self performSelectorInBackground:@selector(morseInternal:) withObject:phrase];
    return phrase;
}

- (NSString *) morseInternal:(NSString *)phrase {
    NSAutoreleasePool *pool = [NSAutoreleasePool new];
    for (int i = 0; i < [phrase length]; i++) {
        if (stopMorse) {
            break;
        }
        unichar ch = [phrase characterAtIndex:i];
        NSLog(@"Processing charachter %c",ch);
        switch (ch) {
            case 'a':
                [self playDotSound];
                [self pause];
                [self playDashSound];
            break;
            case 'b':
                //Morse "b"
                break;
            default:
                break;
        }
    }
    [pool release];
}

- (void) pause {
    [NSThread sleepForTimeInterval:0.2];
}

Notice I added a stopMorse boolean instance variable that you can set to true if you want to stop the morse code generation in the middle of the string.

Ed Marty
WOW!!!! All I can say is that you are my HERO lol. It works perfectly, thanks alot
cgossain
A: 

NSTimers and run loop delays produce very poor Morse Code, and get worse at higher WPM. It's better to create a DSP audio synthesis engine good enough for millisecond precise drum loops (probably requiring use of the RemoteIO Audio Unit), and using that to mix down timed beeps instead of percussion instrument samples.

hotpaw2