Skip to content

Commit

Permalink
Refactor build script for Linux and MacOS in CI workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
royshil committed Sep 24, 2024
1 parent 6130544 commit 0c96957
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 83 deletions.
2 changes: 1 addition & 1 deletion cmake/BuildSDL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ExternalProject_Add(
-DSDL_STATIC=ON
-DSDL_SHARED=OFF
BUILD_BYPRODUCTS
"${SDL_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}SDL2${CMAKE_STATIC_LIBRARY_SUFFIX}"
"${SDL_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}SDL2-static${CMAKE_STATIC_LIBRARY_SUFFIX}"
"${SDL_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}SDL2main${CMAKE_STATIC_LIBRARY_SUFFIX}"
)

Expand Down
34 changes: 18 additions & 16 deletions examples/audio_capture.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
#pragma once

#define SDL_MAIN_HANDLED
#include <SDL.h>

#include <atomic>
#include <cstdint>
#include <vector>
#include <mutex>

class AudioCapture {
public:
AudioCapture(int buffer_duration_ms);
~AudioCapture();
AudioCapture(int buffer_duration_ms);
~AudioCapture();

bool initialize(int device_index, int sample_rate);
bool startCapture();
bool stopCapture();
bool resetBuffer();
bool initialize(int device_index, int sample_rate);
bool startCapture();
bool stopCapture();
bool resetBuffer();

void processAudio(uint8_t* stream, int length);
void getAudioData(int duration_ms, std::vector<float>& output);
void processAudio(uint8_t *stream, int length);
void getAudioData(int duration_ms, std::vector<float> &output);

private:
SDL_AudioDeviceID device_id = 0;
int buffer_duration_ms = 0;
int sample_rate = 0;
std::atomic_bool is_capturing;
std::mutex buffer_mutex;
std::vector<float> audio_buffer;
size_t write_position = 0;
size_t buffer_size = 0;
SDL_AudioDeviceID device_id = 0;
int buffer_duration_ms = 0;
int sample_rate = 0;
std::atomic_bool is_capturing;
std::mutex buffer_mutex;
std::vector<float> audio_buffer;
size_t write_position = 0;
size_t buffer_size = 0;
};

bool handleSDLEvents();
112 changes: 61 additions & 51 deletions examples/realtime_transcription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,65 @@

#include <iostream>

int main() {
// Initialize the library
locaal::Transcription tt;

// Set the transcription parameters (language, model, etc.)
tt.setTranscriptionParams("en-US");

tt.setModelDownloadCallbacks([](const std::string &model_name, const std::string &model_path) {
std::cout << "Model downloaded: " << model_name << " at " << model_path << std::endl;
}, [](const std::string &model_name, const std::string &model_path) {
std::cout << "Model download failed: " << model_name << " at " << model_path << std::endl;
}, [](const std::string &model_name, const std::string &model_path) {
std::cout << "Model download progress: " << model_name << " at " << model_path << std::endl;
});

// Set the callbacks for the transcription
tt.setTranscriptionCallback([](const locaal::TranscriptionResult &result) {
// Print the transcription result
std::cout << "Transcription: " << result.text << std::endl;
});

// Start real-time transcription background thread
tt.startTranscription();

// Start capturing audio from the microphone
AudioCapture audio_capture(1000);
if (!audio_capture.initialize(0, 16000)) {
std::cerr << "Failed to initialize audio capture" << std::endl;
return 1;
}
if (!audio_capture.startCapture()) {
std::cerr << "Failed to start audio capture" << std::endl;
return 1;
}

// Main loop
while (true) {
// Handle SDL events
if (!handleSDLEvents()) {
break;
}

// Get audio data from the audio capture
std::vector<float> audio_data;
audio_capture.getAudioData(1000, audio_data);

// Process the audio data for transcription
tt.processAudio(audio_data);
}

return 0;
int main()
{
SDL_SetMainReady();

// Initialize the library
locaal::Transcription tt;

// Set the transcription parameters (language, model, etc.)
tt.setTranscriptionParams("en-US");

tt.setModelDownloadCallbacks(
[](const std::string &model_name, const std::string &model_path) {
std::cout << "Model downloaded: " << model_name << " at " << model_path
<< std::endl;
},
[](const std::string &model_name, const std::string &model_path) {
std::cout << "Model download failed: " << model_name << " at " << model_path
<< std::endl;
},
[](const std::string &model_name, const std::string &model_path) {
std::cout << "Model download progress: " << model_name << " at "
<< model_path << std::endl;
});

// Set the callbacks for the transcription
tt.setTranscriptionCallback([](const locaal::TranscriptionResult &result) {
// Print the transcription result
std::cout << "Transcription:" << (result.is_partial ? " (partial) " : " ")
<< result.text << std::endl;
});

// Start real-time transcription background thread
tt.startTranscription();

// Start capturing audio from the microphone
AudioCapture audio_capture(1000);
if (!audio_capture.initialize(0, 16000)) {
std::cerr << "Failed to initialize audio capture" << std::endl;
return 1;
}
if (!audio_capture.startCapture()) {
std::cerr << "Failed to start audio capture" << std::endl;
return 1;
}

// Main loop
while (true) {
// Handle SDL events
if (!handleSDLEvents()) {
break;
}

// Get audio data from the audio capture
std::vector<float> audio_data;
audio_capture.getAudioData(1000, audio_data);

// Process the audio data for transcription
tt.processAudio(audio_data);
}

return 0;
}
7 changes: 4 additions & 3 deletions src/modules/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ add_library(Core
src/model-downloader.cpp
src/model-infos.cpp
src/model-find-utils.cpp
src/logger.cpp
)

target_link_libraries(Core PUBLIC sago_platform_folders_lib libcurl)
target_include_directories(Core
PRIVATE
${SAGO_INSTALL_DIR}/include
${SAGO_INSTALL_DIR}/include
)

target_include_directories(Core
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)

set_target_properties(Core PROPERTIES
Expand Down
2 changes: 1 addition & 1 deletion src/modules/core/include/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Logger {

static void setLogCallback(LogCallback callback);
static void setLogLevel(Level level);
static void Logger::log(Level level, const std::string &format, ...);
static void Logger::log(Level level, const std::string format, ...);
// set log level

private:
Expand Down
2 changes: 1 addition & 1 deletion src/modules/core/src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void Logger::setLogLevel(Logger::Level level)
s_logLevel = level;
}

void Logger::log(Level level, const std::string &format, ...)
void Logger::log(Level level, const std::string format, ...)
{
if (level < s_logLevel) {
return;
Expand Down
16 changes: 9 additions & 7 deletions src/modules/core/src/model-downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <sago/platform_folders.h>

#include <filesystem>
#include <fstream>

#include <curl/curl.h>

Expand Down Expand Up @@ -67,10 +68,11 @@ std::string find_model_ext_file(const ModelInfo &model_info, const std::string &
return find_ext_file_in_folder(model_local_folder_path, ext);
}

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
size_t write_data(void *ptr, size_t size, size_t nmemb, void *userdata)
{
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
std::ofstream *fp = static_cast<std::ofstream *>(userdata);
fp->write((const char *)ptr, size * nmemb);
return size * nmemb;
}

std::string get_filename_from_url(const std::string &url)
Expand Down Expand Up @@ -123,8 +125,8 @@ void download_model(const ModelInfo &model_info, download_finished_callback_t fi
continue;
}

FILE *fp = fopen(model_file_save_path.c_str(), "wb");
if (fp == nullptr) {
std::ofstream fp(model_file_save_path, std::ios::binary);
if (!fp.is_open()) {
Logger::log(Logger::Level::ERROR_LOG,
"Failed to open model file for writing %s.",
model_file_save_path.c_str());
Expand All @@ -134,7 +136,7 @@ void download_model(const ModelInfo &model_info, download_finished_callback_t fi
}
curl_easy_setopt(curl, CURLOPT_URL, model_download_file.url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fp);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION,
[progress_callback](void *clientp, curl_off_t dltotal,
Expand All @@ -158,7 +160,7 @@ void download_model(const ModelInfo &model_info, download_finished_callback_t fi
error_callback(DownloadError::DOWNLOAD_ERROR_NETWORK,
"Failed to download model file.");
}
fclose(fp);
fp.close();
}
curl_easy_cleanup(curl);
finished_callback(DownloadStatus::DOWNLOAD_STATUS_OK, model_local_config_path);
Expand Down
4 changes: 1 addition & 3 deletions src/modules/transcription/include/whisper-processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ struct DetectionResultWithText {
std::vector<whisper_token_data> tokens;
std::string language;
};
whats it

void
whisper_loop(void *data);
void whisper_loop(void *data);
struct whisper_context *init_whisper_context(const std::string &model_path,
struct transcription_context *gf);
void run_inference_and_callbacks(transcription_context *gf, uint64_t start_offset_ms,
Expand Down

0 comments on commit 0c96957

Please sign in to comment.