Skip to content

Commit 65f1404

Browse files
committed
clang format
1 parent d349727 commit 65f1404

File tree

5 files changed

+347
-252
lines changed

5 files changed

+347
-252
lines changed

.clang-format

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
BasedOnStyle: LLVM
3+
IndentWidth: 4
4+
---
5+
Language: Cpp
6+
BasedOnStyle: LLVM
7+
IndentWidth: 4
8+
AlignAfterOpenBracket: Align
9+
BreakBeforeBraces: Custom
10+
BraceWrapping:
11+
AfterCaseLabel: true
12+
AfterClass: true
13+
AfterControlStatement: Always
14+
AfterEnum: true
15+
AfterFunction: true
16+
AfterNamespace: true
17+
AfterObjCDeclaration: true
18+
AfterStruct: true
19+
AfterUnion: true
20+
AfterExternBlock: true
21+
BeforeCatch: true
22+
BeforeElse: true
23+
BeforeLambdaBody: false
24+
BeforeWhile: false
25+
IndentBraces: false
26+
SplitEmptyFunction: true
27+
SplitEmptyRecord: true
28+
SplitEmptyNamespace: true
29+
ColumnLimit: 100
30+
SortIncludes: false
31+
---

src-cli/crepe.cpp

+70-46
Original file line numberDiff line numberDiff line change
@@ -6,124 +6,148 @@
66

77
#define MINIAUDIO_IMPLEMENTATION
88
#include "miniaudio.h"
9-
#include "../src/crepe.hpp"
9+
#include "crepe.hpp"
1010

1111

12-
class AudioProcessor {
12+
class AudioProcessor
13+
{
1314
private:
1415
moodycamel::ReaderWriterQueue<float> queue;
1516
std::atomic<bool> finished{false};
1617
size_t frame_size;
17-
18+
1819
public:
19-
explicit AudioProcessor(const size_t size) : queue(size * 10), frame_size(size) {}
20-
21-
void push(const float* data, const size_t count) {
22-
for (size_t i = 0; i < count; i++) {
20+
explicit AudioProcessor(const size_t size) : queue(size * 10), frame_size(size)
21+
{
22+
}
23+
24+
void push(const float *data, const size_t count)
25+
{
26+
for (size_t i = 0; i < count; i++)
27+
{
2328
queue.enqueue(data[i]);
2429
}
2530
}
26-
27-
bool get_frame(std::vector<float>& frame) {
28-
if (finished && queue.size_approx() < frame_size) {
31+
32+
bool get_frame(std::vector<float> &frame)
33+
{
34+
if (finished && queue.size_approx() < frame_size)
35+
{
2936
return false;
3037
}
31-
38+
3239
//until we have enough data
33-
while (queue.size_approx() < frame_size && !finished) {
40+
while (queue.size_approx() < frame_size && !finished)
41+
{
3442
std::this_thread::sleep_for(std::chrono::milliseconds(1));
3543
}
36-
37-
if (queue.size_approx() < frame_size) {
44+
45+
if (queue.size_approx() < frame_size)
46+
{
3847
return false;
3948
}
40-
49+
4150
frame.resize(frame_size);
42-
for (size_t i = 0; i < frame_size; i++) {
51+
for (size_t i = 0; i < frame_size; i++)
52+
{
4353
float value;
44-
if (bool success = queue.try_dequeue(value); !success) return false;
54+
if (bool success = queue.try_dequeue(value); !success)
55+
return false;
4556
frame[i] = value;
4657
}
4758
return true;
4859
}
49-
50-
void set_finished() {
60+
61+
void set_finished()
62+
{
5163
finished = true;
5264
}
5365
};
5466

5567
//miniaudio callbacl
56-
void data_callback(ma_device* device, void* output, const void* input, ma_uint32 frame_count) {
57-
auto* processor = static_cast<AudioProcessor*>(device->pUserData);
58-
const auto* in_samples = static_cast<const float*>(input);
59-
68+
void data_callback(ma_device *device, void *output, const void *input, ma_uint32 frame_count)
69+
{
70+
auto *processor = static_cast<AudioProcessor *>(device->pUserData);
71+
const auto *in_samples = static_cast<const float *>(input);
72+
6073
//push
6174
processor->push(in_samples, frame_count * device->capture.channels);
62-
75+
6376
// clear if it's required
64-
if (output != nullptr) {
77+
if (output != nullptr)
78+
{
6579
memset(output, 0, frame_count * device->playback.channels * sizeof(float));
6680
}
6781
}
6882

69-
int main() {
70-
try {
83+
int main()
84+
{
85+
try
86+
{
7187
//init miniaudio
7288
ma_device_config config = ma_device_config_init(ma_device_type_capture);
7389
config.capture.format = ma_format_f32;
74-
config.capture.channels = 1; // Mono for simplicity
90+
config.capture.channels = 1; // Mono for simplicity
7591
config.sampleRate = crepe::constants::SAMPLE_RATE;
7692
config.dataCallback = data_callback;
7793

7894
AudioProcessor processor(crepe::constants::FRAME_LENGTH);
7995
config.pUserData = &processor;
80-
96+
8197
ma_device device;
82-
if (ma_device_init(nullptr, &config, &device) != MA_SUCCESS) {
98+
if (ma_device_init(nullptr, &config, &device) != MA_SUCCESS)
99+
{
83100
std::cerr << "Error: Failed to initialize audio device" << std::endl;
84101
}
85-
86-
if (ma_device_start(&device) != MA_SUCCESS) {
102+
103+
if (ma_device_start(&device) != MA_SUCCESS)
104+
{
87105
ma_device_uninit(&device);
88106
std::cerr << "Error: Failed to start audio device" << std::endl;
89107
}
90108

91-
92109
std::cout << "Recording. Press Enter to stop." << std::endl;
93-
110+
94111
//analysis thread
95112
std::atomic<bool> running{true};
96113
std::thread analysis_thread([&]() {
97114
std::vector<float> frame;
98-
while (running) {
99-
if (processor.get_frame(frame)) {
115+
while (running)
116+
{
117+
if (processor.get_frame(frame))
118+
{
100119
// Run inference on the frame
101120
// Display the detected pitch
102-
if (crepe::PredictionResults results = crepe::run_inference(frame, crepe::constants::SAMPLE_RATE); results.num_frames > 0) {
103-
std::cout << "Pitch: " << results.pitches(0) << " Hz, Confidence: "
104-
<< results.confidences(0) << " \r" << std::flush;
121+
if (crepe::PredictionResults results = crepe::run_inference(
122+
frame, crepe::constants::SAMPLE_RATE); results.num_frames > 0)
123+
{
124+
std::cout << "Pitch: " << results.pitches(0) << " Hz, Confidence: "
125+
<< results.confidences(0) << " \r" << std::flush;
105126
}
106127
}
107128
}
108129
});
109-
130+
110131
// Wait for user to press Enter
111132
std::cin.get();
112133

113134
running = false;
114135
processor.set_finished();
115-
116-
if (analysis_thread.joinable()) {
136+
137+
if (analysis_thread.joinable())
138+
{
117139
analysis_thread.join();
118140
}
119-
141+
120142
ma_device_uninit(&device);
121143
std::cout << "\nRecording stopped." << std::endl;
122-
123-
} catch (const std::exception& e) {
144+
145+
}
146+
catch (const std::exception &e)
147+
{
124148
std::cerr << "Error: " << e.what() << std::endl;
125149
return 1;
126150
}
127-
151+
128152
return 0;
129153
}

src-test/crepe.cpp

+37-18
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,73 @@
88
#include "../deps/miniaudio/miniaudio.h"
99
#include <iostream>
1010

11-
std::vector<float> load_wav_file(const std::string &filename, int *out_sample_rate, std::string *error_msg) {
11+
std::vector<float> load_wav_file(const std::string &filename, int *out_sample_rate,
12+
std::string *error_msg)
13+
{
1214
ma_decoder decoder;
1315

14-
if (const ma_result result = ma_decoder_init_file(filename.c_str(), nullptr, &decoder); result != MA_SUCCESS) {
15-
if (error_msg) *error_msg = "Failed to initialize decoder for file: " + filename;
16+
if (const ma_result result = ma_decoder_init_file(filename.c_str(), nullptr, &decoder);
17+
result != MA_SUCCESS)
18+
{
19+
if (error_msg)
20+
*error_msg = "Failed to initialize decoder for file: " + filename;
1621
return {};
1722
}
1823

19-
if (out_sample_rate) {
24+
if (out_sample_rate)
25+
{
2026
*out_sample_rate = static_cast<int>(decoder.outputSampleRate);
2127
std::cout << "Debug: Detected sample rate: " << *out_sample_rate << "Hz" << std::endl;
2228
}
2329

2430
ma_uint64 frame_count;
2531
ma_decoder_get_length_in_pcm_frames(&decoder, &frame_count);
2632

27-
28-
if (out_sample_rate) {
33+
if (out_sample_rate)
34+
{
2935
*out_sample_rate = decoder.outputSampleRate;
3036
}
3137

3238
const bool needs_conversion = decoder.outputChannels > 1;
3339

3440
std::vector<float> audio_data(frame_count);
3541

36-
if (needs_conversion) {
42+
if (needs_conversion)
43+
{
3744
std::vector<float> multi_channel_data(frame_count * decoder.outputChannels);
3845
const ma_uint64 frames_read =
39-
ma_decoder_read_pcm_frames(&decoder, multi_channel_data.data(), frame_count, nullptr);
46+
ma_decoder_read_pcm_frames(&decoder, multi_channel_data.data(), frame_count, nullptr);
4047

41-
for (ma_uint64 i = 0; i < frames_read; i++) {
48+
for (ma_uint64 i = 0; i < frames_read; i++)
49+
{
4250
float sum = 0.0f;
43-
for (ma_uint32 c = 0; c < decoder.outputChannels; c++) {
51+
for (ma_uint32 c = 0; c < decoder.outputChannels; c++)
52+
{
4453
sum += multi_channel_data[i * decoder.outputChannels + c];
4554
}
4655
audio_data[i] = sum / decoder.outputChannels;
4756
}
48-
} else {
57+
}
58+
else
59+
{
4960
ma_decoder_read_pcm_frames(&decoder, audio_data.data(), frame_count, nullptr);
5061
}
5162

5263
ma_decoder_uninit(&decoder);
5364
return audio_data;
5465
}
5566

56-
int main() {
57-
try {
67+
int main()
68+
{
69+
try
70+
{
5871
// enable this to debug frequency mapping
5972
//crepe::analyze_frequency_bins();
6073

6174
const std::string wav_file_path = "sweep.wav";
6275
std::ifstream file_check(wav_file_path);
63-
if (!file_check) {
76+
if (!file_check)
77+
{
6478
std::cerr << "Error: Cannot open file at path: " << wav_file_path << std::endl;
6579
return 1;
6680
}
@@ -80,18 +94,23 @@ int main() {
8094
std::cout << "Mean confidence: " << analytics.mean_confidence << std::endl;
8195

8296
std::cout << "Sample frequencies (Hz): [";
83-
for (int i = 0; i < std::min(5, results.num_frames); i++) {
97+
for (int i = 0; i < std::min(5, results.num_frames); i++)
98+
{
8499
std::cout << results.pitches(i);
85-
if (i < std::min(4, results.num_frames - 1)) std::cout << " ";
100+
if (i < std::min(4, results.num_frames - 1))
101+
std::cout << " ";
86102
}
87103
std::cout << "]" << std::endl;
88104

89105
std::cout << "Min frequency: " << analytics.min_frequency << std::endl;
90106
std::cout << "Max frequency: " << analytics.max_frequency << std::endl;
91-
std::cout << "Correlation between time and frequency: " << analytics.time_pitch_correlation << std::endl;
107+
std::cout << "Correlation between time and frequency: " << analytics.time_pitch_correlation
108+
<< std::endl;
92109
std::cout << "Should be close to 1.0 for frequency sweep" << std::endl;
93110

94-
} catch (const std::exception& e) {
111+
}
112+
catch (const std::exception &e)
113+
{
95114
std::cerr << "Error: " << e.what() << std::endl;
96115
return 1;
97116
}

0 commit comments

Comments
 (0)