Skip to content

Commit

Permalink
fix CMake, add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
swiftraccoon committed Oct 9, 2023
1 parent 2aab05f commit 420f702
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@
*.app

.vscode/
build/
build/
20230927_181109North_Carolina_VIPER_Rutherford_T-Control__TO_41003_FROM_1610051.mp3
11 changes: 5 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)

find_package(CURL REQUIRED)

# For the main executable
add_executable(sdrTrunkTranscriber main.cpp curlHelper.cpp)
target_link_libraries(sdrTrunkTranscriber CURL::libcurl)

# Add GoogleTest
# For tests
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# Create test executable
add_executable(runTests test.cpp)
target_link_libraries(runTests ${GTEST_LIBRARIES} pthread)

target_link_libraries(sdrTrunkTranscriber CURL::libcurl)
add_executable(runTests test.cpp curlHelper.cpp) # Include curlHelper.cpp here
target_link_libraries(runTests ${GTEST_LIBRARIES} pthread CURL::libcurl) # Include CURL::libcurl here
7 changes: 6 additions & 1 deletion curlHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
#include <stdexcept>

const std::string API_URL = "https://api.openai.com/v1/audio/transcriptions";
extern const char* ENV_OPENAI_API_KEY;
const char* ENV_OPENAI_API_KEY = std::getenv("OPENAI_API_KEY");

// Callback function to write the CURL response to a string
size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string *)userp)->append((char *)contents, size * nmemb);
return size * nmemb;
}

// Setup CURL headers
void setupCurlHeaders(CURL *curl, struct curl_slist *&headers)
{
headers = curl_slist_append(headers, ("Authorization: Bearer " + std::string(ENV_OPENAI_API_KEY)).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
}

// Setup CURL post fields
void setupCurlPostFields(CURL *curl, curl_mime *&mime, const std::string &file_path)
{
curl_mimepart *part;
Expand Down Expand Up @@ -48,6 +51,7 @@ void setupCurlPostFields(CURL *curl, curl_mime *&mime, const std::string &file_p
curl_mime_data(part, "en", CURL_ZERO_TERMINATED);
}

// Make a CURL request and return the response
std::string makeCurlRequest(CURL *curl, curl_mime *mime)
{
std::string response;
Expand All @@ -62,6 +66,7 @@ std::string makeCurlRequest(CURL *curl, curl_mime *mime)
return response;
}

// Transcribe audio using CURL
std::string curl_transcribe_audio(const std::string &file_path)
{
CURL *curl = curl_easy_init();
Expand Down
9 changes: 9 additions & 0 deletions curlHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@
#include <string>
#include <curl/curl.h>

// Callback function to write the CURL response to a string
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp);

// Setup CURL headers
void setupCurlHeaders(CURL* curl, struct curl_slist*& headers);

// Setup CURL post fields
void setupCurlPostFields(CURL* curl, curl_mime*& mime, const std::string& file_path);

// Make a CURL request and return the response
std::string makeCurlRequest(CURL* curl, curl_mime* mime);

// Transcribe audio using CURL
std::string curl_transcribe_audio(const std::string& file_path);
29 changes: 17 additions & 12 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@
#include <stdexcept>
#include "curlHelper.h"

int main(int argc, char *argv[])
{
int main(int argc, char *argv[]) {
const char* ENV_OPENAI_API_KEY = std::getenv("OPENAI_API_KEY");

if (argc == 2 && std::string(argv[1]) == "--help") {
std::cout << "Usage: ./sdrTrunkTranscriber <file_path>" << std::endl;
return 0;
}

if (ENV_OPENAI_API_KEY == nullptr) {
throw std::runtime_error("OPENAI_API_KEY environment variable not set");
std::cerr << "OPENAI_API_KEY environment variable not set" << std::endl;
return 1;
}
if (argc < 2)
{
std::cout << "Usage: ./program <file_path>" << std::endl;

if (argc < 2) {
std::cout << "Usage: ./sdrTrunkTranscriber <file_path>" << std::endl;
return 1;
}

std::string file_path = argv[1];
try
{
try {
std::cout << curl_transcribe_audio(file_path) << std::endl;
}
catch (const std::exception &e)
{
} catch (const std::runtime_error &e) {
std::cerr << "Runtime Error: " << e.what() << std::endl;
} catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}
}

0 comments on commit 420f702

Please sign in to comment.