コード例 #1
0
static char *expand_vars(char *xml_str) {
    char *var, *val;
    char *rp = xml_str; /* read pointer */
    char *ep, *wp, *buff; /* end pointer, write pointer, write buffer */

    if (!(strstr(xml_str, "$${"))) {
        return xml_str;
    }

    switch_zmalloc(buff, strlen(xml_str) * 2);
    wp = buff;
    ep = buff + (strlen(xml_str) * 2) - 1;

    while (*rp && wp < ep) {
        if (*rp == '$' && *(rp + 1) == '$' && *(rp + 2) == '{') {
            char *e = switch_find_end_paren(rp + 2, '{', '}');

            if (e) {
                rp += 3;
                var = rp;
                *e++ = '\0';
                rp = e;

                if ((val = switch_core_get_variable_dup(var))) {
                    char *p;
                    for (p = val; p && *p && wp <= ep; p++) {
                        *wp++ = *p;
                    }
                    switch_safe_free(val);
                }
                continue;
            }
        }

        *wp++ = *rp++;
    }

    *wp++ = '\0';

    switch_safe_free(xml_str);
    return buff;
}
コード例 #2
0
ファイル: switch_core_speech.c プロジェクト: bodji/freeswitch
SWITCH_DECLARE(switch_status_t) switch_core_speech_feed_tts(switch_speech_handle_t *sh, char *text, switch_speech_flag_t *flags)
{
    switch_status_t status = SWITCH_STATUS_SUCCESS;
    char *param_string = NULL;
    char *data = NULL;
    char *ltext = NULL;

    switch_assert(sh != NULL);

    if (zstr(text)) {
        status = SWITCH_STATUS_FALSE;
        goto done;
    }

    /* Set TTS parameters from params in the text string
     * Params are defined as follows {name1=val1,name2=val2,name3=val3}text to speak
     */
    ltext = strdup(text);
    data = ltext;

    /* strip leading spaces */
    while (data && *data == ' ') {
        data++;
    }
    if (zstr(data)) {
        status = SWITCH_STATUS_FALSE;
        goto done;
    }

    /* extract params */
    if (*data == '{') {
        param_string = data + 1;
        data = switch_find_end_paren(data, '{', '}');
        if (zstr(data)) {
            status = SWITCH_STATUS_FALSE;
            goto done;
        } else {
            *data = '\0';
            data++;
        }
    }

    /* set TTS params */
    if (!zstr(param_string)) {
        char *param[256] = { 0 };
        int i;
        int argc = switch_separate_string(param_string, ',', param, (sizeof(param) / sizeof(param[0])));
        for (i = 0; i < argc && param[i]; ++i) {
            char *param_pair[2] = { 0 };
            if (switch_separate_string(param[i], '=', param_pair, (sizeof(param_pair) / sizeof(param_pair[0]))) == 2) {
                switch_core_speech_text_param_tts(sh, param_pair[0], param_pair[1]);
            }
        }
    }

    status = sh->speech_interface->speech_feed_tts(sh, data, flags);

done:

    switch_safe_free(ltext);
    return status;
}
コード例 #3
0
SWITCH_DECLARE(switch_status_t) switch_core_perform_file_open(const char *file, const char *func, int line,
															  switch_file_handle_t *fh,
															  const char *file_path,
															  uint32_t channels, uint32_t rate, unsigned int flags, switch_memory_pool_t *pool)
{
	char *ext;
	switch_status_t status = SWITCH_STATUS_FALSE;
	char stream_name[128] = "";
	char *rhs = NULL;
	const char *spool_path = NULL;
	int is_stream = 0;
	char *fp = NULL;
	int to = 0;

	if (switch_test_flag(fh, SWITCH_FILE_OPEN)) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Handle already open\n");
		return SWITCH_STATUS_FALSE;
	}

	fh->samples_in = 0;

	if (!fh->samplerate) {
		if (!(fh->samplerate = rate)) {
			fh->samplerate = 8000;
		}
	}

	if (zstr(file_path)) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Filename\n");
		return SWITCH_STATUS_FALSE;
	}

	fh->flags = flags;

	if (pool) {
		fh->memory_pool = pool;
	} else {
		if ((status = switch_core_new_memory_pool(&fh->memory_pool)) != SWITCH_STATUS_SUCCESS) {
			UNPROTECT_INTERFACE(fh->file_interface);
			return status;
		}
		switch_set_flag(fh, SWITCH_FILE_FLAG_FREE_POOL);
	}

	if (*file_path == '{') {
		char *timeout;
		char *new_fp;
		fp = switch_core_strdup(fh->memory_pool, file_path);

		if (switch_event_create_brackets(fp, '{', '}', ',', &fh->params, &new_fp, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
			if ((timeout = switch_event_get_header(fh->params, "timeout"))) {
				if ((to = atoi(timeout)) < 1) {
					to = 0;
				}
			}
		} else {
			new_fp = fp;
		}

		file_path = new_fp;
	}

	if (switch_directory_exists(file_path, fh->memory_pool) == SWITCH_STATUS_SUCCESS) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "File [%s] is a directory not a file.\n", file_path);
		status = SWITCH_STATUS_GENERR;
		goto fail;
	}

	if ((rhs = strstr(file_path, SWITCH_URL_SEPARATOR))) {
		switch_copy_string(stream_name, file_path, (rhs + 1) - file_path);
		ext = stream_name;
		file_path = rhs + 3;
		fh->file_path = switch_core_strdup(fh->memory_pool, file_path);
		is_stream = 1;
	} else {
		if ((flags & SWITCH_FILE_FLAG_WRITE)) {

			char *p, *e;

			fh->file_path = switch_core_strdup(fh->memory_pool, file_path);
			p = fh->file_path;

			if (*p == '[' && *(p + 1) == *SWITCH_PATH_SEPARATOR) {
				e = switch_find_end_paren(p, '[', ']');

				if (e) {
					*e = '\0';
					spool_path = p + 1;
					fh->file_path = e + 1;
				}
			}

			if (!spool_path) {
				spool_path = switch_core_get_variable_pdup(SWITCH_AUDIO_SPOOL_PATH_VARIABLE, fh->memory_pool);
			}

			file_path = fh->file_path;
		}

		if ((ext = strrchr(file_path, '.')) == 0) {
			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unknown file Format [%s]\n", file_path);
			switch_goto_status(SWITCH_STATUS_FALSE, fail);
		}
		ext++;
		fh->file_path = switch_core_strdup(fh->memory_pool, file_path);
	}



	if ((fh->file_interface = switch_loadable_module_get_file_interface(ext)) == 0) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid file format [%s] for [%s]!\n", ext, file_path);
		switch_goto_status(SWITCH_STATUS_GENERR, fail);
	}

	fh->file = file;
	fh->func = func;
	fh->line = line;


	if (spool_path) {
		char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
		switch_uuid_t uuid;
		switch_uuid_get(&uuid);
		switch_uuid_format(uuid_str, &uuid);

		fh->spool_path = switch_core_sprintf(fh->memory_pool, "%s%s%s.%s", spool_path, SWITCH_PATH_SEPARATOR, uuid_str, ext);
	} else {
		fh->spool_path = NULL;
	}

	if (rhs) {
		fh->handler = switch_core_strdup(fh->memory_pool, rhs);
	} else {
		fh->handler = NULL;
	}

	if (channels) {
		fh->channels = channels;
	} else {
		fh->channels = 1;
	}

	file_path = fh->spool_path ? fh->spool_path : fh->file_path;

	if ((status = fh->file_interface->file_open(fh, file_path)) != SWITCH_STATUS_SUCCESS) {
		if (fh->spool_path) {
			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Spool dir is set.  Make sure [%s] is also a valid path\n", fh->spool_path);
		}
		UNPROTECT_INTERFACE(fh->file_interface);
		switch_goto_status(status, fail);
	}

	fh->real_channels = fh->channels;

	if (channels) {
		fh->channels = channels;
	}

	if ((flags & SWITCH_FILE_FLAG_WRITE) && !is_stream && (status = switch_file_exists(file_path, fh->memory_pool)) != SWITCH_STATUS_SUCCESS) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "File [%s] not created!\n", file_path);
		fh->file_interface->file_close(fh);
		UNPROTECT_INTERFACE(fh->file_interface);
		switch_goto_status(status, fail);
	}

	if (to) {
		fh->max_samples = (fh->samplerate / 1000) * to;
	}


	if ((flags & SWITCH_FILE_FLAG_READ)) {
		fh->native_rate = fh->samplerate;
	} else {
		fh->native_rate = rate;
	}

	if (fh->samplerate && rate && fh->samplerate != rate) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "File %s sample rate %d doesn't match requested rate %d\n", file_path, fh->samplerate, rate);
		if ((flags & SWITCH_FILE_FLAG_READ)) {
			fh->samplerate = rate;
		}
	}

	if (fh->pre_buffer_datalen) {
		//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Prebuffering %d bytes\n", (int)fh->pre_buffer_datalen);
		switch_buffer_create_dynamic(&fh->pre_buffer, fh->pre_buffer_datalen * fh->channels, fh->pre_buffer_datalen * fh->channels, 0);
		fh->pre_buffer_data = switch_core_alloc(fh->memory_pool, fh->pre_buffer_datalen * fh->channels);
	}


	if (fh->real_channels != fh->channels && (flags & SWITCH_FILE_FLAG_READ) && !(fh->flags & SWITCH_FILE_NOMUX)) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "File has %d channels, muxing to %d channel%s will occur.\n", fh->real_channels, fh->channels, fh->channels == 1 ? "" : "s");
	}

	switch_set_flag(fh, SWITCH_FILE_OPEN);
	return status;

  fail:

	switch_clear_flag(fh, SWITCH_FILE_OPEN);

	if (fh->params) {
		switch_event_destroy(&fh->params);
	}

	fh->samples_in = 0;
	fh->max_samples = 0;
	
	if (switch_test_flag(fh, SWITCH_FILE_FLAG_FREE_POOL)) {
		switch_core_destroy_memory_pool(&fh->memory_pool);
	}

	return status;
}
コード例 #4
0
SWITCH_DECLARE(switch_status_t) switch_core_asr_load_grammar(switch_asr_handle_t *ah, const char *grammar, const char *name)
{
	switch_status_t status = SWITCH_STATUS_SUCCESS;
	char *param_string = NULL;
	char *data = NULL;
	char *lgrammar = NULL;

	switch_assert(ah != NULL);

	if (zstr(grammar)) {
		status = SWITCH_STATUS_FALSE;
		goto done;
	}

	/* Set ASR parameters from params in the grammar string
	 * Params are defined as follows {name1=val1,name2=val2,name3=val3}grammar text
	 */
	lgrammar = strdup(grammar);
	data = lgrammar;

	/* strip leading spaces */
	while (data && *data == ' ') {
		data++;
	}
	if (zstr(data)) {
		status = SWITCH_STATUS_FALSE;
		goto done;
	}

	/* extract params */
	if (*data == '{') {
		param_string = data + 1;
		data = switch_find_end_paren(data, '{', '}');
		if (zstr(data)) {
			status = SWITCH_STATUS_FALSE;
			goto done;
		} else {
			*data = '\0';
			data++;
		}
	}

	/* set ASR params */
	if (!zstr(param_string)) {
		char *param[256] = { 0 };
		int i;
		int argc = switch_separate_string(param_string, ',', param, (sizeof(param) / sizeof(param[0])));
		for (i = 0; i < argc && param[i]; ++i) {
			char *param_pair[2] = { 0 };
			if (switch_separate_string(param[i], '=', param_pair, (sizeof(param_pair) / sizeof(param_pair[0]))) == 2) {
				switch_core_asr_text_param(ah, param_pair[0], param_pair[1]);
			}
		}
	}

	status = ah->asr_interface->asr_load_grammar(ah, data, name);

  done:

	switch_safe_free(lgrammar);
	return status;
}