/** Swift engine callback */ static swift_result_t mrcp_swift_write_audio(swift_event *event, swift_event_t type, void *udata) { void *buf; int len; mrcp_swift_channel_t *synth_channel = udata; swift_event_t rv = SWIFT_SUCCESS; if(type & SWIFT_EVENT_END) { apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Swift Engine: Write End-of-Speech Event"); mpf_buffer_event_write(synth_channel->audio_buffer,MEDIA_FRAME_TYPE_EVENT); return rv; } rv = swift_event_get_audio(event, &buf, &len); if(!SWIFT_FAILED(rv)) { #if 0 /* Get the event times */ float time_start, time_len; swift_event_get_times(event, &time_start, &time_len); apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Swift Engine: Write Audio [%d | %0.4f | %0.4f]",len, time_start, time_len); #endif mpf_buffer_audio_write(synth_channel->audio_buffer,buf,len); } return rv; }
/* This callback caches the audio in the buffer */ static swift_result_t write_audio(swift_event * event, swift_event_t type, void *udata) { cepstral_t *cepstral; swift_event_t rv = SWIFT_SUCCESS; void *buf = NULL; int len = 0, i = 0; cepstral = udata; assert(cepstral != NULL); if (!cepstral->port || cepstral->done || cepstral->done_gen) { return SWIFT_UNKNOWN_ERROR; } /* Only proceed when we have success */ if (!SWIFT_FAILED((rv = swift_event_get_audio(event, &buf, &len)))) { while (!cepstral->done) { switch_mutex_lock(cepstral->audio_lock); if (switch_buffer_write(cepstral->audio_buffer, buf, len) > 0) { switch_mutex_unlock(cepstral->audio_lock); break; } switch_mutex_unlock(cepstral->audio_lock); if (!cepstral->done) { for (i = 0; i < 10; i++) { switch_yield(10000); if (cepstral->done) { break; } } } } } else { cepstral->done = 1; } if (cepstral->done) { rv = SWIFT_UNKNOWN_ERROR; } return rv; }
static swift_result_t swift_cb(swift_event *event, swift_event_t type, void *udata) { void *buf; int len, spacefree; unsigned long sleepfor; swift_event_t rv = SWIFT_SUCCESS; struct stuff *ps = udata; if (type == SWIFT_EVENT_AUDIO) { rv = swift_event_get_audio(event, &buf, &len); if (!SWIFT_FAILED(rv) && len > 0) { ast_log(LOG_DEBUG, "audio callback\n"); ASTOBJ_WRLOCK(ps); /* Sleep while waiting for some queue space to become available */ while (len + ps->qc > cfg_buffer_size && !ps->immediate_exit) { /* Each byte is 125us of time, so assume queue space will become available at that rate and guess when we'll have enough space available. + another (125 usec/sample * framesize samples) (1 frame) for fudge */ sleepfor = ((unsigned long)(len - (cfg_buffer_size - ps->qc)) * 125UL) + (125UL * (unsigned long)framesize); /* ast_log(LOG_DEBUG, "generator: %d bytes to write but only %d space avail, sleeping %ldus\n", len, cfg_buffer_size - ps->qc, sleepfor); */ ASTOBJ_UNLOCK(ps); usleep(sleepfor); ASTOBJ_WRLOCK(ps); } if (ps->immediate_exit) { ASTOBJ_UNLOCK(ps); return SWIFT_SUCCESS; } spacefree = cfg_buffer_size - ((uintptr_t) ps->pq_w - (uintptr_t)ps->q); if (len > spacefree) { ast_log(LOG_DEBUG, "audio fancy write; %d bytes but only %d avail to end %d totalavail\n", len, spacefree, cfg_buffer_size - ps->qc); /* write #1 to end of mem */ memcpy(ps->pq_w, buf, spacefree); ps->pq_w = ps->q; ps->qc += spacefree; /* write #2 and beg of mem */ memcpy(ps->pq_w, buf + spacefree, len - spacefree); ps->pq_w += len - spacefree; ps->qc += len - spacefree; } else { ast_log(LOG_DEBUG, "audio easy write, %d avail to end %d totalavail\n", spacefree, cfg_buffer_size - ps->qc); memcpy(ps->pq_w, buf, len); ps->pq_w += len; ps->qc += len; } ASTOBJ_UNLOCK(ps); } else { ast_log(LOG_DEBUG, "got audio callback but get_audio call failed\n"); } } else if (type == SWIFT_EVENT_END) { ast_log(LOG_DEBUG, "got END callback; done generating audio\n"); ASTOBJ_WRLOCK(ps); ps->generating_done = 1; ASTOBJ_UNLOCK(ps); } else { ast_log(LOG_DEBUG, "UNKNOWN callback\n"); } return rv; }