Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: apply google c++ coding standard for readability #20

Merged
merged 6 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Language
Language: Cpp

# Style
BasedOnStyle: Microsoft

# No indent for extern block and namespace
IndentExternBlock: NoIndent
NamespaceIndentation: None

# Header files order
IncludeIsMainRegex: ".*" # 1. main file
IncludeCategories:
- Regex: '^<c.*>'
Priority: 1
SortPriority: 0
- Regex: '^<.*>'
Priority: 2
SortPriority: 0
- Regex: '.*'
Priority: 3
SortPriority: 0

# Indentation
IndentWidth: 4

# Line length
ColumnLimit: 100

# Break before braces
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: true
AfterControlStatement: true
AfterFunction: true
AfterNamespace: true


KeepEmptyLinesAtTheStartOfBlocks: false
7 changes: 5 additions & 2 deletions .github/workflows/native-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ jobs:
submodules: recursive

- name: Install dependencies
run: sudo apt-get update && sudo apt-get install -y cmake build-essential libfftw3-dev libcurl4-openssl-dev ffmpeg jq yt-dlp
run: sudo apt-get update && sudo apt-get install -y cmake build-essential libfftw3-dev libcurl4-openssl-dev ffmpeg cpplint

- name: Check cpplint
run: cpplint --recursive lib include

- name: Configure CMake
run: cmake -B build -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
Expand All @@ -37,4 +40,4 @@ jobs:
run: sudo make install -C build

- name: Test CLI
run: vibra --recognize --file tests/sample.mp3 | jq .track.title -r | grep -q "Misty"
run: cd tests && ./test.sh 5
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -pedantic -O2 -fno-strict-aliasing")
include_directories(${CMAKE_SOURCE_DIR}/lib)

# build verbose output
# set(CMAKE_VERBOSE_MAKEFILE ON)
Expand Down
4 changes: 4 additions & 0 deletions CPPLINT.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set noparent
linelength=100
filter=-whitespace/newline,-whitespace/braces,-whitespace/parens,-whitespace/indent,-whitespace/comments,-legal/copyright
exclude_files=./cli/args
89 changes: 48 additions & 41 deletions cli/cli.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include <iostream>
#include <fstream>
#include "../cli/cli.h"
#include <curl/curl.h>
#include "cli.h"
#include <fstream>
#include <iostream>
#include <vector>
#include "args/args.hxx"

static std::string read_buffer;
static std::string read_buffer; // NOLINT

std::size_t writeCallback(void* contents, size_t size, size_t nmemb, void* userp)
std::size_t writeCallback(void *contents, size_t size, size_t nmemb, void *)
{
(void)userp; // suppress warning (unused parameter)
std::size_t realsize = size * nmemb;
read_buffer.append((char*)contents, realsize);
read_buffer.append(reinterpret_cast<char *>(contents), realsize);
return realsize;
}

int CLI::Run(int argc, char** argv)
int CLI::Run(int argc, char **argv)
{
args::ArgumentParser parser("");
parser.SetArgumentSeparations(false, false, true, true);
Expand All @@ -28,23 +28,25 @@ int CLI::Run(int argc, char** argv)
parser.helpParams.showValueName = false;
parser.helpParams.optionsString = "Options:";
parser.helpParams.proglineOptions = "{COMMAND} [OPTIONS]";

args::Group actions(parser, "Commands:", args::Group::Validators::Xor);
args::Flag fingerprint_only(actions, "fingerprint", "Generate a fingerprint", {'F', "fingerprint"});
args::Flag fingerprint_only(actions, "fingerprint", "Generate a fingerprint",
{'F', "fingerprint"});
args::Flag recognize(actions, "recognize", "Recognize a song", {'R', "recognize"});
args::HelpFlag help(actions, "help", "Display this help menu", {'h', "help"});

args::Group sources(parser, "Sources:", args::Group::Validators::Xor);

args::Group file_sources(sources, "File sources:", args::Group::Validators::Xor);
args::ValueFlag<std::string> music_file(file_sources, "file", "FFmpeg required for non-wav files", {'f', "file"});

args::ValueFlag<std::string> music_file(file_sources, "file",
"FFmpeg required for non-wav files", {'f', "file"});

args::Group raw_sources(sources, "Raw PCM sources:", args::Group::Validators::All);
args::ValueFlag<int> chunk_seconds(raw_sources, "seconds", "Chunk seconds", {'s', "seconds"});
args::ValueFlag<int> sample_rate(raw_sources, "rate", "Sample rate", {'r', "rate"});
args::ValueFlag<int> channels(raw_sources, "channels", "Channels", {'c', "channels"});
args::ValueFlag<int> bits_per_sample(raw_sources, "bits", "Bits per sample", {'b', "bits"});

args::Group source_type(raw_sources, "Source type:", args::Group::Validators::AtMostOne);
args::Flag signed_pcm(source_type, "signed", "Signed PCM (default)", {'S', "signed"});
args::Flag float_pcm(source_type, "float", "Float PCM", {'D', "float"});
Expand All @@ -53,12 +55,12 @@ int CLI::Run(int argc, char** argv)
{
parser.ParseCLI(argc, argv);
}
catch (const args::Help&)
catch (const args::Help &)
{
std::cout << parser;
return 0;
}
catch (const std::runtime_error& e)
catch (const std::runtime_error &e)
{
std::cerr << std::endl;
std::cerr << e.what() << std::endl;
Expand All @@ -67,7 +69,7 @@ int CLI::Run(int argc, char** argv)
return 1;
}

Fingerprint* fingerprint = nullptr;
Fingerprint *fingerprint = nullptr;
if (music_file)
{
std::string file = args::get(music_file);
Expand All @@ -76,20 +78,16 @@ int CLI::Run(int argc, char** argv)
else if (chunk_seconds && sample_rate && channels && bits_per_sample)
{
bool is_signed = signed_pcm || !float_pcm;
fingerprint = getFingerprintFromStdin(
args::get(chunk_seconds),
args::get(sample_rate),
args::get(channels),
args::get(bits_per_sample),
is_signed
);
fingerprint =
getFingerprintFromStdin(args::get(chunk_seconds), args::get(sample_rate),
args::get(channels), args::get(bits_per_sample), is_signed);
}
else
{
std::cerr << "Invalid arguments" << std::endl;
return 1;
return 1;
}

if (fingerprint_only)
{
std::cout << fingerprint->uri << std::endl;
Expand All @@ -101,7 +99,7 @@ int CLI::Run(int argc, char** argv)
return 0;
}

Fingerprint* CLI::getFingerprintFromMusicFile(const std::string& music_file)
Fingerprint *CLI::getFingerprintFromMusicFile(const std::string &music_file)
{
if (std::ifstream(music_file).good() == false)
{
Expand All @@ -111,31 +109,33 @@ Fingerprint* CLI::getFingerprintFromMusicFile(const std::string& music_file)
return vibra_get_fingerprint_from_music_file(music_file.c_str());
}

Fingerprint* CLI::getFingerprintFromStdin(int chunk_seconds, int sample_rate,
int channels, int bits_per_sample, bool is_signed)
Fingerprint *CLI::getFingerprintFromStdin(int chunk_seconds, int sample_rate, int channels,
int bits_per_sample, bool is_signed)
{
std::size_t bytes = chunk_seconds * sample_rate * channels * (bits_per_sample / 8);
std::vector<char> buffer(bytes);
std::cin.read(buffer.data(), bytes);
if (is_signed)
{
return vibra_get_fingerprint_from_signed_pcm(buffer.data(), bytes, sample_rate, bits_per_sample, channels);
return vibra_get_fingerprint_from_signed_pcm(buffer.data(), bytes, sample_rate,
bits_per_sample, channels);
}
return vibra_get_fingerprint_from_float_pcm(buffer.data(), bytes, sample_rate, bits_per_sample, channels);
return vibra_get_fingerprint_from_float_pcm(buffer.data(), bytes, sample_rate, bits_per_sample,
channels);
}

std::string CLI::getMetadataFromShazam(const Fingerprint* fingerprint)
std::string CLI::getMetadataFromShazam(const Fingerprint *fingerprint)
{
auto content = vibra_get_shazam_request_json(fingerprint);
auto user_agent = vibra_get_shazam_random_user_agent();
std::string url = vibra_get_shazam_host();

CURL* curl = curl_easy_init();
CURL *curl = curl_easy_init();
read_buffer.clear();

if (curl)
if (curl)
{
struct curl_slist* headers = nullptr;
struct curl_slist *headers = nullptr;
headers = curl_slist_append(headers, "Accept-Encoding: gzip, deflate, br");
headers = curl_slist_append(headers, "Accept: */*");
headers = curl_slist_append(headers, "Connection: keep-alive");
Expand All @@ -148,24 +148,31 @@ std::string CLI::getMetadataFromShazam(const Fingerprint* fingerprint)
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &read_buffer);

curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip, deflate, br");
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK)
if (res != CURLE_OK)
{
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}

long http_code = 0;
long http_code = 0; // NOLINT
/*
Jayden:

I don't know why, if type of http_code is std::int32_t,
it will cause a double free corrupt error.
*/

curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
if (http_code != 200)
if (http_code != 200)
{
std::cerr << "HTTP code: " << http_code << std::endl;
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
return read_buffer;
}
}
23 changes: 11 additions & 12 deletions cli/cli.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#ifndef __CLI_H__
#define __CLI_H__
#ifndef CLI_CLI_H_
#define CLI_CLI_H_

#include <string>
#include "../include/vibra.h"

class CLI
class CLI
{
public:
int Run(int argc, char** argv);

private:
Fingerprint* getFingerprintFromMusicFile(const std::string& music_file);
Fingerprint* getFingerprintFromStdin(int chunk_seconds, int sample_rate,
int channels, int bits_per_sample, bool is_signed);
std::string getMetadataFromShazam(const Fingerprint* fingerprint);
public:
int Run(int argc, char **argv);

private:
Fingerprint *getFingerprintFromMusicFile(const std::string &music_file);
Fingerprint *getFingerprintFromStdin(int chunk_seconds, int sample_rate, int channels,
int bits_per_sample, bool is_signed);
std::string getMetadataFromShazam(const Fingerprint *fingerprint);
};

#endif // __CLI_H__
#endif // CLI_CLI_H_
6 changes: 3 additions & 3 deletions cli/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "cli.h"
#include "./cli.h"

int main(int argc, char** argv)
int main(int argc, char **argv)
{
CLI cli;
return cli.Run(argc, argv);
}
}
Loading
Loading