Question

I’m trying to use the C CoreFoundation interface to the Speech Synthesis Manager. How do you register a speech callback (such as kSpeechSpeechDoneCallBack or kSpeechTextDoneCallBack)? I know how to use the old deprecated SetSpeechInfo function; how do you do it with the new SetSpeechProperty? When I try to use it, it causes “Segmentation fault: 11” instead of calling the function I registered.

According to Speech-Channel Properties, I think you’re supposed to pass in a long CFNumberRef whose value is the desired function pointer.

Here’s a simple example where the main thread registers a callback and waits for the speech to finish. But instead of calling the callback, the dispatch thread gives error “EXC_BAD_ACCESS (code=13, address=0x0)”. Using the deprecated function (--old), the callback is called with no error.

// clang -g -framework ApplicationServices main.c && ./a.out
#include <stdio.h>
#include <pthread.h>
#include <ApplicationServices/ApplicationServices.h>

void checkResult(OSErr error, const char *description) {
    if (error == 0)
        return;
    fprintf(stderr, "Error: %d during %s. exiting.", error, description);
    exit(error);
}

struct SpeechState {
    pthread_mutex_t mutex;
    pthread_cond_t speakingDone;
    int stillSpeakingCount;
};

void speechDone(SpeechChannel chan, SRefCon refCon) {
    printf("Speech done!\n");
    struct SpeechState *speechState = (struct SpeechState*)refCon;

    pthread_mutex_lock(&speechState->mutex);
    speechState->stillSpeakingCount--;
    pthread_cond_broadcast(&speechState->speakingDone);
    pthread_mutex_unlock(&speechState->mutex);
}

int main(int argc, const char * argv[])
{
    bool old = false;
    for (int i = 1; i < argc; i++) {
        if (0 == strcmp(argv[i], "--old"))
            old = true;
    }
    SpeechChannel chan;
    checkResult(NewSpeechChannel((VoiceSpec*)NULL, &chan), "NewSpeechChannel");

    struct SpeechState speechState;
    pthread_mutex_init(&speechState.mutex, NULL);
    pthread_cond_init(&speechState.speakingDone, NULL);

    if (! old) {
        // The new way seems to crash.
        CFNumberRef doneCallbackNumber = CFNumberCreate(NULL, kCFNumberLongType, speechDone);
        CFNumberRef refConNumber = CFNumberCreate(NULL, kCFNumberLongType, &speechState);
        printf("Registering speechDone callback for address %p\n", speechDone);
        checkResult(SetSpeechProperty(chan, kSpeechSpeechDoneCallBack, doneCallbackNumber), "SetSpeechProperty(sdcb)");
        checkResult(SetSpeechProperty(chan, kSpeechRefConProperty, refConNumber), "SetSpeechProperty(refc)");
        CFRelease(doneCallbackNumber);
        CFRelease(refConNumber);
    } else {
        // The deprecated way to do it works.
        checkResult(SetSpeechInfo(chan, soSpeechDoneCallBack, &speechDone), "SetSpeechInfo(sdcb)");
        checkResult(SetSpeechInfo(chan, soRefCon, &speechState), "SetSpeechInfo(refc)");
    }

    printf("Speaking...\n");
    CFStringRef string = CFStringCreateWithCString(NULL, "Most people recognize me by my voice!", kCFStringEncodingUTF8);
    checkResult(SpeakCFString(chan, string, NULL), "SpeakCFString");
    CFRelease(string);

    pthread_mutex_lock(&speechState.mutex);
    speechState.stillSpeakingCount++;
    while (speechState.stillSpeakingCount > 0) {
        pthread_cond_wait(&speechState.speakingDone, &speechState.mutex);
    }
    pthread_mutex_unlock(&speechState.mutex);

    printf("Done!\n");
    return 0;
}
Was it helpful?

Solution

After re-reading the documentation I realized that I called CFNumberCreate wrong; I gave it the value of the number, whereas it actually takes a pointer to the number. So in this case I needed to pass in a pointer to the function pointer and a pointer to the struct pointer:

void (*speechDonePtr)(SpeechChannel, SRefCon) = speechDone;
struct SpeechState *speechStatePtr = &speechState;
CFNumberRef doneCallbackNumber = CFNumberCreate(NULL, kCFNumberLongType, &speechDonePtr);
CFNumberRef refConNumber = CFNumberCreate(NULL, kCFNumberLongType, &speechStatePtr);

What a silly mistake!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top