Exemplo n.º 1
0
int main(int argc, char **argv)
{
	if (argc != 3)
	{
		printf("Usage: %s <language code> <duration in seconds>\n", argv[0]);
		exit(1);
	}
	
	struct sprec_wav_header *hdr;
	struct sprec_server_response *resp;
	char *flac_file_buf;
	int flac_file_len;
	char *text;
	double confidence;
	int err;
	
	/**
	 * Generate two temporary files to store the WAV and FLAC data
	 */
	char wavfile[L_tmpnam + 5];
	char flacfile[L_tmpnam + 6];
	char *tmp_filename_stub = tmpnam(NULL);
	sprintf(wavfile, "%s.wav", tmp_filename_stub);
	sprintf(flacfile, "%s.flac", tmp_filename_stub);
	
	/**
	 * Start recording a WAV: sample rate = 16000Hz, bit depth = 16bps, stereo
	 */
	hdr = sprec_wav_header_from_params(16000, 16, 2);
	if (!hdr)
	{
		fprintf(stderr, "Error allocating WAV header\n");
		exit(1);
	}
	err = sprec_record_wav(wavfile, hdr, 1000 * strtod(argv[2], NULL));
	if (err)
	{
		fprintf(stderr, "Error recording WAV file: %d\n", err);
	 	exit(1);
	}
	
	free(hdr);
	
	/**
	 * Make the raw PCM in the WAV file into a FLAC file
	 */
	err = sprec_flac_encode(wavfile, flacfile);
	if (err)
	{
		fprintf(stderr, "Error converting WAV file to FLAC: %d\n", err);
		exit(1);
	}
	
	/**
	 * Read the entire FLAC file...
	 */
	err = sprec_get_file_contents(flacfile, &flac_file_buf, &flac_file_len);
	if (err)
	{
		fprintf(stderr, "Error reading FLAC file: %d\n", err);
		exit(1);
	}
	
	/**
	 * ...and send it to Google
	 */
	resp = sprec_send_audio_data(flac_file_buf, flac_file_len, argv[1], 16000);
	if (!resp)
	{
		fprintf(stderr, "Error sending audio data\n");
		exit(1);
	}
	
	free(flac_file_buf);
	
	/**
	 * Get the JSON from the response object,
	 * then parse it to get the actual text
	 */
	text = sprec_get_text_from_json(resp->data);
	confidence = sprec_get_confidence_from_json(resp->data);
	sprec_free_response(resp);
	
	printf("%s\n", text ? text : "");
	printf("Confidence: %d%%\n", (int)(confidence * 100));
	free(text);
	
	/**
	 * Let's not fill up the entire /tmp folder
	 */
	remove(wavfile);
	remove(flacfile);
	
	return 0;
}
Exemplo n.º 2
0
struct sprec_result *sprec_recognize_sync(const char *lang, float dur_s)
{
	struct sprec_wav_header *hdr;
	struct sprec_server_response *resp;
	struct sprec_result *res;
	int err, len;
	char *text, *tmpstub, *buf;
	char wavfile[L_tmpnam + 5];
	char flacfile[L_tmpnam + 6];
	double confidence;
	
	tmpstub = tmpnam(NULL);
	sprintf(wavfile, "%s.wav", tmpstub);
	sprintf(flacfile, "%s.flac", tmpstub);

	/**
	 * sample rate = 16000Hz, bit depth = 16bps, stereo
	 */
	hdr = sprec_wav_header_from_params(16000, 16, 2);
	if (hdr == NULL)
	{
		return NULL;
	}

	err = sprec_record_wav(wavfile, hdr, 1000 * dur_s);
	if (err != 0)
	{
		free(hdr);
		return NULL;
	}


	/**
	 * Convert the WAV file to a FLAC file
	 */
	err = sprec_flac_encode(wavfile, flacfile);
	if (err != 0)
	{
		free(hdr);
		return NULL;
	}

	/**
	 * Read the entire FLAC file into memory...
	 */
	err = sprec_get_file_contents(flacfile, &buf, &len);
	if (err != 0)
	{
		free(hdr);
		return NULL;
	}

	/**
	 * ...and send it to Google
	 */
	resp = sprec_send_audio_data(buf, len, lang, hdr->sample_rate);
	free(buf);
	free(hdr);
	if (resp == NULL)
	{
		return NULL;
	}


	/**
	 * Get the JSON from the response object,
	 * then parse it to get the actual text and confidence
	 */
	text = sprec_get_text_from_json(resp->data);
	confidence = sprec_get_confidence_from_json(resp->data);
	sprec_free_response(resp);

	/**
	 * Remove the temporary files in order
	 * not fill the /tmp folder with garbage
	 */
	remove(wavfile);
	remove(flacfile);

	/**
	 * Compose the return value
	 */
	res = malloc(sizeof(*res));
	if (res == NULL)
	{
		free(text);
		return NULL;
	}
	
	res->text = text;
	res->confidence = confidence;
	return res;
}
Exemplo n.º 3
0
sprec_server_response *
sprec_send_audio_data(
	const void *data,
	size_t length,
	const char *apikey,
	const char *language,
	uint32_t sample_rate
)
{
	CURL *conn_hndl;
	struct curl_httppost *form, *lastptr;
	struct curl_slist *headers;
	sprec_server_response *resp;
	char url[0x100];
	char header[0x100];

	if (data == NULL) {
		return NULL;
	}

	/*
	 * Initialize the variables
	 * Put the language code to the URL query string
	 * If no language given, default to U. S. English
	 */
	snprintf(
		url,
		sizeof url,
		"https://www.google.com/speech-api/v2/recognize?output=json&key=%s&lang=%s",
		apikey,
		language ? language : "en-US"
	);

	resp = malloc(sizeof *resp);
	if (resp == NULL) {
		return NULL;
	}

	resp->data = NULL;
	resp->length = 0;

	conn_hndl = curl_easy_init();
	if (conn_hndl == NULL) {
		sprec_free_response(resp);
		return NULL;
	}

	form = NULL;
	lastptr = NULL;
	headers = NULL;
	snprintf(
		header,
		sizeof header,
		"Content-Type: audio/x-flac; rate=%" PRIu32,
		sample_rate
	);
	headers = curl_slist_append(headers, header);

	curl_formadd(
		&form,
		&lastptr,
		CURLFORM_COPYNAME,
		"myfile",
		CURLFORM_CONTENTSLENGTH,
		(long)length,
		CURLFORM_PTRCONTENTS,
		data,
		CURLFORM_END
	);

	/*
	 * Setup the cURL handle
	 */
	curl_easy_setopt(conn_hndl, CURLOPT_URL, url);
	curl_easy_setopt(conn_hndl, CURLOPT_HTTPHEADER, headers);
	curl_easy_setopt(conn_hndl, CURLOPT_HTTPPOST, form);
	curl_easy_setopt(conn_hndl, CURLOPT_WRITEFUNCTION, http_callback);
	curl_easy_setopt(conn_hndl, CURLOPT_WRITEDATA, resp);

	/*
	 * SSL certificates are not available on iOS, so we have to trust Google
	 * (0 means false)
	 */
	curl_easy_setopt(conn_hndl, CURLOPT_SSL_VERIFYPEER, 0);

	/*
	 * Initiate the HTTP(S) transfer
	 */
	curl_easy_perform(conn_hndl);

	/*
	 * Clean up
	 */
	curl_formfree(form);
	curl_slist_free_all(headers);
	curl_easy_cleanup(conn_hndl);

	/*
	 * NULL-terminate the JSON response string
	 */
	resp->data[resp->length] = '\0';

	return resp;
}