Skip to content

Commit

Permalink
Merge pull request #59 from roocell/raspizerow_options
Browse files Browse the repository at this point in the history
raspizerow main to accept args and CMake
  • Loading branch information
adamcohenhillel committed Feb 19, 2024
2 parents 9c5f573 + b68103a commit 562eb19
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 3 deletions.
38 changes: 38 additions & 0 deletions devices/raspizerow/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.10)
project(adeus_raspizerow)

# Include FetchContent module
include(FetchContent)

# Fetch cxxopts
FetchContent_Declare(
cxxopts
GIT_REPOSITORY https://github.com/jarro2783/cxxopts.git
GIT_TAG v3.2.0
)
FetchContent_MakeAvailable(cxxopts)

# Add your source files here
set(SOURCES
main.cpp
)
# Add executable target
add_executable(main ${SOURCES})

# Set C++ standard
set_target_properties(main PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
)

# Link against required libraries
find_package(ALSA REQUIRED)
find_package(CURL REQUIRED)
find_package(Threads REQUIRED)

target_link_libraries(main PRIVATE
${ALSA_LIBRARIES}
${CURL_LIBRARIES}
Threads::Threads
cxxopts
)
13 changes: 12 additions & 1 deletion devices/raspizerow/compile.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
#!/bin/bash
sudo g++ -std=c++17 main.cpp -g -o main -lasound -lcurl -lpthread

# Create a build directory
mkdir -p build
cd build

# Generate build files using CMake
cmake ..

# Build the project using make
make

cp -f main ..
2 changes: 2 additions & 0 deletions devices/raspizerow/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
cd ~
sudo apt-get -y update
sudo apt-get -y upgrade
sudo apt-get -y install cmake
sudo apt-get install libasound2-dev
sudo apt-get install libcurl4-openssl-dev
sudo apt install -y python3-pip
sudo pip3 install --upgrade adafruit-python-shell
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/i2smic.py
sudo python3 i2smic.py
chmod +x compile.sh

read -p "Enter your Supabase URL: " supabase_url
read -p "Enter your Auth Token: " auth_token
Expand Down
83 changes: 81 additions & 2 deletions devices/raspizerow/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@
#include <iostream>
#include <alsa/asoundlib.h>
#include <curl/curl.h>
#include <fstream>
#include "cxxopts.hpp"
#include <chrono>
#include <filesystem> // C++17 feature

#define DO_NOT_APPLY_GAIN 1.0

// Assuming 4 bytes per sample for S32_LE format and mono audio
int bytesPerSample = 4;
short channels = 1;
unsigned int sampleRate = 48000;
unsigned int sampleRate = 44100;
int durationInSeconds = 60; // Duration you want to accumulate before sending
int targetBytes = sampleRate * durationInSeconds * bytesPerSample * channels;
int rc;
float audio_gain = DO_NOT_APPLY_GAIN;
bool save_to_local_file = false;

template <typename T>
class SafeQueue
Expand Down Expand Up @@ -142,8 +150,35 @@ void createWavHeader(std::vector<char> &header, int bitsPerSample, int dataSize
header.insert(header.end(), dataSizeBytes, dataSizeBytes + 4);
}

void saveWavToFile(const std::vector<char> &buffer) {
// Generate a timestamp for the filename
auto now = std::chrono::system_clock::now();
auto timestamp = std::chrono::system_clock::to_time_t(now);

// Convert the timestamp to a string
std::string timestampStr = std::to_string(timestamp);

// Create the directory "data" if it doesn't exist
std::filesystem::create_directory("data");

// Construct the filename with the timestamp
std::string filename = "data/" + timestampStr + "_audio.wav";

// Write the buffer to the file
std::ofstream outfile(filename, std::ios::binary);
if (outfile.is_open()) {
outfile.write(buffer.data(), buffer.size());
outfile.close();
}
}

void sendWavBuffer(const std::vector<char> &buffer)
{
if (save_to_local_file)
{
saveWavToFile(buffer);
}

CURL *curl;
CURLcode res;
const char *supabaseUrlEnv = getenv("SUPABASE_URL");
Expand Down Expand Up @@ -194,6 +229,21 @@ void handleAudioBuffer()
dataChunk.insert(dataChunk.end(), buffer.begin(), buffer.end());
}

if (audio_gain != DO_NOT_APPLY_GAIN)
{
// Apply volume increase by scaling the audio samples
for (size_t i = 0; i < dataChunk.size(); i += 2) // Assuming 16-bit (2-byte) audio samples
{
// Convert the two bytes to a short (16-bit sample)
short sample = static_cast<short>((dataChunk[i + 1] << 8) | dataChunk[i]);
// Scale the sample by the volume factor
sample = static_cast<short>(std::min(std::max(-32768, static_cast<int>(audio_gain * sample)), 32767));
// Split the short back into two bytes
dataChunk[i] = sample & 0xFF;
dataChunk[i + 1] = (sample >> 8) & 0xFF;
}
}

// Process and send the accumulated data
if (!dataChunk.empty())
{
Expand All @@ -214,8 +264,37 @@ void handleAudioBuffer()
}
}

int main()
void process_args(int argc, char* argv[]) {
cxxopts::Options options("main", " - command line options");

options.add_options()
("h,help", "Print help")
("s,save", "Save audio to local file")
("g,gain", "Microphone gain (increase volume of audio)", cxxopts::value<float>())
;

auto result = options.parse(argc, argv);

if (result.count("help")) {
std::cout << options.help() << std::endl;
exit;
}

if (result.count("save")) {
std::cout << "Saving audio to local file" << std::endl;
save_to_local_file = true;
}

if (result.count("gain")) {
float gain = result["gain"].as<float>();
std::cout << "Microphone gain: " << gain << std::endl;
audio_gain = gain;
}
}

int main(int argc, char* argv[])
{
process_args(argc, argv);
snd_pcm_t *capture_handle;
snd_pcm_format_t format = SND_PCM_FORMAT_S32_LE;

Expand Down

0 comments on commit 562eb19

Please sign in to comment.