Skip to content

Commit 3a7bd44

Browse files
authored
add c codestyle (#836)
1 parent 24509e8 commit 3a7bd44

File tree

35 files changed

+805
-699
lines changed

35 files changed

+805
-699
lines changed

.clang-format

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Generated from CLion C/C++ Code Style settings
2+
BasedOnStyle: LLVM
3+
Cpp11BracedListStyle: true
4+
AccessModifierOffset: -4
5+
AlignConsecutiveMacros: true
6+
AlignTrailingComments: false
7+
AlignAfterOpenBracket: Align
8+
AllowAllArgumentsOnNextLine: false
9+
AllowAllConstructorInitializersOnNextLine: false
10+
AllowAllParametersOfDeclarationOnNextLine: false
11+
AlignArrayOfStructures: Left
12+
AllowShortBlocksOnASingleLine: true
13+
AllowShortCaseLabelsOnASingleLine: false
14+
AllowShortFunctionsOnASingleLine: None
15+
AllowShortIfStatementsOnASingleLine: Never
16+
AllowShortLambdasOnASingleLine: None
17+
AllowShortLoopsOnASingleLine: false
18+
AlwaysBreakAfterReturnType: None
19+
AlwaysBreakTemplateDeclarations: Yes
20+
BreakBeforeBraces: Custom
21+
BinPackArguments: false
22+
BinPackParameters: false
23+
BraceWrapping:
24+
AfterCaseLabel: false
25+
AfterClass: false
26+
AfterExternBlock: false
27+
AfterControlStatement: Never
28+
AfterEnum: false
29+
AfterFunction: false
30+
AfterNamespace: false
31+
AfterUnion: false
32+
BeforeCatch: false
33+
BeforeElse: false
34+
IndentBraces: false
35+
SplitEmptyFunction: false
36+
SplitEmptyRecord: true
37+
# BraceBreakingStyle: Attach
38+
BreakBeforeBinaryOperators: None
39+
BreakBeforeTernaryOperators: true
40+
BreakConstructorInitializers: BeforeColon
41+
BreakInheritanceList: BeforeColon
42+
ColumnLimit: 0
43+
CommentPragmas: '^[^ ]'
44+
CompactNamespaces: false
45+
ContinuationIndentWidth: 8
46+
IncludeBlocks: Preserve
47+
IncludeCategories:
48+
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
49+
Priority: 2
50+
SortPriority: 0
51+
CaseSensitive: false
52+
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
53+
Priority: 3
54+
SortPriority: 0
55+
CaseSensitive: false
56+
- Regex: '.*'
57+
Priority: 1
58+
SortPriority: 0
59+
CaseSensitive: false
60+
IndentCaseLabels: true
61+
IndentPPDirectives: None
62+
IndentWidth: 4
63+
InsertTrailingCommas: Wrapped
64+
KeepEmptyLinesAtTheStartOfBlocks: true
65+
MaxEmptyLinesToKeep: 2
66+
NamespaceIndentation: All
67+
ObjCSpaceAfterProperty: false
68+
ObjCSpaceBeforeProtocolList: true
69+
PointerAlignment: Right
70+
ReflowComments: false
71+
SpaceAfterCStyleCast: true
72+
SpaceAfterLogicalNot: false
73+
SpaceAfterTemplateKeyword: false
74+
SpaceBeforeAssignmentOperators: true
75+
SpaceBeforeCpp11BracedList: false
76+
SpaceBeforeCtorInitializerColon: true
77+
SpaceBeforeInheritanceColon: true
78+
SpaceBeforeParens: ControlStatements
79+
SpaceBeforeRangeBasedForLoopColon: true
80+
SpaceInEmptyParentheses: false
81+
SpacesBeforeTrailingComments: 1
82+
SpacesInAngles: false
83+
SpacesInCStyleCastParentheses: false
84+
SpacesInContainerLiterals: false
85+
SpacesInParentheses: false
86+
SpacesInSquareBrackets: false
87+
TabWidth: 4
88+
UseTab: Never

.github/workflows/c-codestyle.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: C Codestyle
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches: [ master ]
7+
paths:
8+
- '**/*.c'
9+
- '.github/workflows/c-codestyle.yml'
10+
pull_request:
11+
branches: [ master ]
12+
paths:
13+
- '**/*.c'
14+
- '.github/workflows/c-codestyle.yml'
15+
16+
jobs:
17+
check-c-codestyle:
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- uses: actions/checkout@v2
22+
23+
- name: Set up Python 3.10
24+
uses: actions/setup-python@v2
25+
with:
26+
python-version: '3.10'
27+
28+
- name: Install dependencies
29+
run: sudo apt install clang-format
30+
31+
- name: Check c codestyle
32+
run: python3 resources/lint/c/formatter.py -c -v

demo/c/porcupine_demo_file.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ static void *open_dl(const char *dl_path) {
4141
return dlopen(dl_path, RTLD_NOW);
4242

4343
#endif
44-
4544
}
4645

4746
static void *load_symbol(void *handle, const char *symbol) {
@@ -55,7 +54,6 @@ static void *load_symbol(void *handle, const char *symbol) {
5554
return dlsym(handle, symbol);
5655

5756
#endif
58-
5957
}
6058

6159
static void close_dl(void *handle) {
@@ -69,7 +67,6 @@ static void close_dl(void *handle) {
6967
dlclose(handle);
7068

7169
#endif
72-
7370
}
7471

7572
static void print_dl_error(const char *message) {
@@ -83,16 +80,15 @@ static void print_dl_error(const char *message) {
8380
fprintf(stderr, "%s with '%s'.\n", message, dlerror());
8481

8582
#endif
86-
8783
}
8884

8985
static struct option long_options[] = {
90-
{"library_path", required_argument, NULL, 'l'},
91-
{"model_path", required_argument, NULL, 'm'},
92-
{"keyword_path", required_argument, NULL, 'k'},
93-
{"sensitivity", required_argument, NULL, 't'},
94-
{"access_key", required_argument, NULL, 'a'},
95-
{"wav_path", required_argument, NULL, 'w'},
86+
{"library_path", required_argument, NULL, 'l'},
87+
{"model_path", required_argument, NULL, 'm'},
88+
{"keyword_path", required_argument, NULL, 'k'},
89+
{"sensitivity", required_argument, NULL, 't'},
90+
{"access_key", required_argument, NULL, 'a'},
91+
{"wav_path", required_argument, NULL, 'w'},
9692
};
9793

9894
void print_usage(const char *program_name) {
@@ -156,8 +152,8 @@ int picovoice_main(int argc, char *argv[]) {
156152
exit(1);
157153
}
158154

159-
pv_status_t (*pv_porcupine_init_func)(const char *, const char *, int32_t, const char *const *, const float *, pv_porcupine_t **)
160-
= load_symbol(porcupine_library, "pv_porcupine_init");
155+
pv_status_t (*pv_porcupine_init_func)(const char *, const char *, int32_t, const char *const *, const float *, pv_porcupine_t **) =
156+
load_symbol(porcupine_library, "pv_porcupine_init");
161157
if (!pv_porcupine_init_func) {
162158
print_dl_error("failed to load 'pv_porcupine_init'");
163159
exit(1);
@@ -169,8 +165,7 @@ int picovoice_main(int argc, char *argv[]) {
169165
exit(1);
170166
}
171167

172-
pv_status_t (*pv_porcupine_process_func)(pv_porcupine_t *, const int16_t *, int32_t *) =
173-
load_symbol(porcupine_library, "pv_porcupine_process");
168+
pv_status_t (*pv_porcupine_process_func)(pv_porcupine_t *, const int16_t *, int32_t *) = load_symbol(porcupine_library, "pv_porcupine_process");
174169
if (!pv_porcupine_process_func) {
175170
print_dl_error("failed to load 'pv_porcupine_process'");
176171
exit(1);
@@ -243,8 +238,7 @@ int picovoice_main(int argc, char *argv[]) {
243238
struct timeval after;
244239
gettimeofday(&after, NULL);
245240

246-
total_cpu_time_usec +=
247-
(double) (after.tv_sec - before.tv_sec) * 1e6 + (double) (after.tv_usec - before.tv_usec);
241+
total_cpu_time_usec += (double) (after.tv_sec - before.tv_sec) * 1e6 + (double) (after.tv_usec - before.tv_usec);
248242
total_processed_time_usec += (pv_porcupine_frame_length_func() * 1e6) / pv_sample_rate_func();
249243

250244
if (keyword_index != -1) {
@@ -269,14 +263,14 @@ int main(int argc, char *argv[]) {
269263
#if defined(_WIN32) || defined(_WIN64)
270264

271265
#define UTF8_COMPOSITION_FLAG (0)
272-
#define NULL_TERMINATED (-1)
266+
#define NULL_TERMINATED (-1)
273267

274268
LPWSTR *wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
275269
if (wargv == NULL) {
276270
fprintf(stderr, "CommandLineToArgvW failed\n");
277271
exit(1);
278272
}
279-
273+
280274
char *utf8_argv[argc];
281275

282276
for (int i = 0; i < argc; ++i) {

demo/c/porcupine_demo_mic.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ static void *open_dl(const char *dl_path) {
4141
return dlopen(dl_path, RTLD_NOW);
4242

4343
#endif
44-
4544
}
4645

4746
static void *load_symbol(void *handle, const char *symbol) {
@@ -55,7 +54,6 @@ static void *load_symbol(void *handle, const char *symbol) {
5554
return dlsym(handle, symbol);
5655

5756
#endif
58-
5957
}
6058

6159
static void close_dl(void *handle) {
@@ -69,7 +67,6 @@ static void close_dl(void *handle) {
6967
dlclose(handle);
7068

7169
#endif
72-
7370
}
7471

7572
static void print_dl_error(const char *message) {
@@ -83,7 +80,6 @@ static void print_dl_error(const char *message) {
8380
fprintf(stderr, "%s with '%s'.\n", message, dlerror());
8481

8582
#endif
86-
8783
}
8884

8985
static struct option long_options[] = {
@@ -97,8 +93,12 @@ static struct option long_options[] = {
9793
};
9894

9995
static void print_usage(const char *program_name) {
100-
fprintf(stderr, "Usage : %s -l LIBRARY_PATH -m MODEL_PATH -k KEYWORD_PATH -t SENSITIVITY -a ACCESS_KEY -d AUDIO_DEVICE_INDEX\n"
101-
" %s [-s, --show_audio_devices]\n", program_name, program_name);
96+
fprintf(stderr,
97+
"Usage : %s -l LIBRARY_PATH -m MODEL_PATH -k KEYWORD_PATH -t SENSITIVITY -a ACCESS_KEY -d "
98+
"AUDIO_DEVICE_INDEX\n"
99+
" %s [-s, --show_audio_devices]\n",
100+
program_name,
101+
program_name);
102102
}
103103

104104
void interrupt_handler(int _) {
@@ -186,8 +186,8 @@ int picovoice_main(int argc, char *argv[]) {
186186
exit(1);
187187
}
188188

189-
pv_status_t (*pv_porcupine_init_func)(const char *, const char *, int32_t, const char *const *, const float *, pv_porcupine_t **)
190-
= load_symbol(porcupine_library, "pv_porcupine_init");
189+
pv_status_t (*pv_porcupine_init_func)(const char *, const char *, int32_t, const char *const *, const float *, pv_porcupine_t **) =
190+
load_symbol(porcupine_library, "pv_porcupine_init");
191191
if (!pv_porcupine_init_func) {
192192
print_dl_error("failed to load 'pv_porcupine_init'");
193193
exit(1);
@@ -199,8 +199,7 @@ int picovoice_main(int argc, char *argv[]) {
199199
exit(1);
200200
}
201201

202-
pv_status_t (*pv_porcupine_process_func)(pv_porcupine_t *, const int16_t *, int32_t *)
203-
= load_symbol(porcupine_library, "pv_porcupine_process");
202+
pv_status_t (*pv_porcupine_process_func)(pv_porcupine_t *, const int16_t *, int32_t *) = load_symbol(porcupine_library, "pv_porcupine_process");
204203
if (!pv_porcupine_process_func) {
205204
print_dl_error("failed to load 'pv_porcupine_process'");
206205
exit(1);
@@ -291,14 +290,14 @@ int main(int argc, char *argv[]) {
291290
#if defined(_WIN32) || defined(_WIN64)
292291

293292
#define UTF8_COMPOSITION_FLAG (0)
294-
#define NULL_TERMINATED (-1)
293+
#define NULL_TERMINATED (-1)
295294

296295
LPWSTR *wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
297296
if (wargv == NULL) {
298297
fprintf(stderr, "CommandLineToArgvW failed\n");
299298
exit(1);
300299
}
301-
300+
302301
char *utf8_argv[argc];
303302

304303
for (int i = 0; i < argc; ++i) {

demo/mcu/stm32f407/stm32f407g-disc1/Src/main.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#define MEMORY_BUFFER_SIZE (20 * 1024)
2525

26-
static const char* ACCESS_KEY = ... //AccessKey string obtained from Picovoice Console (https://picovoice.ai/console/)
26+
static const char *ACCESS_KEY = "${ACCESS_KEY}"; //AccessKey string obtained from Picovoice Console (https://picovoice.ai/console/)
2727

2828
static int8_t memory_buffer[MEMORY_BUFFER_SIZE] __attribute__((aligned(16)));
2929

@@ -37,14 +37,14 @@ static void wake_word_callback(void) {
3737
}
3838

3939
static void error_handler(void) {
40-
while(true);
40+
while (true) {}
4141
}
4242

4343
int main(void) {
4444

4545
pv_status_t status = pv_board_init();
4646
if (status != PV_STATUS_SUCCESS) {
47-
error_handler();
47+
error_handler();
4848
}
4949

5050
const uint8_t *board_uuid = pv_get_uuid();
@@ -56,8 +56,8 @@ int main(void) {
5656

5757
status = pv_audio_rec_init();
5858
if (status != PV_STATUS_SUCCESS) {
59-
printf("Audio init failed with '%s'", pv_status_to_string(status));
60-
error_handler();
59+
printf("Audio init failed with '%s'", pv_status_to_string(status));
60+
error_handler();
6161
}
6262

6363
status = pv_audio_rec_start();
@@ -68,15 +68,7 @@ int main(void) {
6868

6969
pv_porcupine_t *handle = NULL;
7070

71-
status = pv_porcupine_init(
72-
ACCESS_KEY,
73-
MEMORY_BUFFER_SIZE,
74-
memory_buffer,
75-
1,
76-
&KEYWORD_MODEL_SIZES,
77-
&KEYWORD_MODELS,
78-
&SENSITIVITY,
79-
&handle);
71+
status = pv_porcupine_init(ACCESS_KEY, MEMORY_BUFFER_SIZE, memory_buffer, 1, &KEYWORD_MODEL_SIZES, &KEYWORD_MODELS, &SENSITIVITY, &handle);
8072

8173
if (status != PV_STATUS_SUCCESS) {
8274
printf("Porcupine init failed with '%s'", pv_status_to_string(status));
@@ -85,10 +77,10 @@ int main(void) {
8577

8678
uint32_t frame_number = 0;
8779
while (true) {
88-
const int16_t *buffer = pv_audio_rec_get_new_buffer();
89-
if (buffer) {
90-
int32_t keyword_index;
91-
const pv_status_t status = pv_porcupine_process(handle, buffer, &keyword_index);
80+
const int16_t *buffer = pv_audio_rec_get_new_buffer();
81+
if (buffer) {
82+
int32_t keyword_index;
83+
const pv_status_t status = pv_porcupine_process(handle, buffer, &keyword_index);
9284
if (status != PV_STATUS_SUCCESS) {
9385
printf("Porcupine process failed with '%s'", pv_status_to_string(status));
9486
error_handler();
@@ -102,10 +94,8 @@ int main(void) {
10294
frame_number = 0;
10395
}
10496
}
105-
10697
}
10798
pv_board_deinit();
10899
pv_audio_rec_deinit();
109100
pv_porcupine_delete(handle);
110101
}
111-

0 commit comments

Comments
 (0)