transcribe raw audio

This commit is contained in:
talksik
2025-07-26 19:30:03 -07:00
parent ed3850abd6
commit 966050a7b4
+13 -7
View File
@@ -33,6 +33,7 @@ size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userdata) {
// Returns string that caller must free
char* find_transcript(char* json_response)
{
printf("Parsing response: %s \n", json_response);
char* start = strstr(json_response, "\"transcript\":\"");
if (!start) return NULL;
@@ -61,6 +62,8 @@ size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp) {
response->size += total_size;
response->data[response->size] = '\0'; // Null terminate
printf("read an extra %d bytes of data\n", (int)total_size);
return total_size;
}
@@ -88,9 +91,8 @@ char* transcribe(FILE* audio_file) {
if (curl) {
// Set headers for Deepgram API
headers = curl_slist_append(headers, "Authorization: Token YOUR_DEEPGRAM_API_KEY");
headers = curl_slist_append(headers, "Content-Type: audio/*");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.deepgram.com/v1/listen");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.deepgram.com/v1/listen?encoding=linear16&sample_rate=48000&channels=1");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
@@ -108,6 +110,8 @@ char* transcribe(FILE* audio_file) {
return NULL;
}
printf("done performing curl\n");
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
@@ -214,9 +218,8 @@ int main() {
perror("Error opening file");
return 1;
}
printf("======READING FRAMES for 5 seconds========\n");
while (snd_pcm_readi(pcm_handle, (void *)buf, period_size) > 0 && periods_read < periods_to_read) {
printf("======READING FRAMES========\n");
// Because each frame is 2 bytes (format/bit rate), cast to int16
// int16_t *samples = (int16_t *)buf;
// for (int i = 0; i < period_size; i++) {
@@ -224,10 +227,9 @@ int main() {
// }
fwrite(buf, sizeof(int16_t), period_size, file);
printf("\n");
periods_read++;
}
printf("done capturing audio\n");
fclose(file);
@@ -238,10 +240,14 @@ int main() {
if (transcript) {
printf("Transcript: %s\n", transcript);
free(transcript);
} else {
fprintf(stderr, "unable to get transcript");
}
fclose(file);
} else {
fprintf(stderr, "unable to open file for reading\n");
}
free((void *)buf);
snd_pcm_close(pcm_handle);
snd_pcm_hw_params_free(params);