Skip to content

Commit 2fb489f

Browse files
committed
Add an assert macro that stops the callback
1 parent 06b8c03 commit 2fb489f

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

rtmixer_build.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
include_dirs=['src', 'portaudio/include', 'portaudio/src/common'],
5353
sources=['portaudio/src/common/pa_ringbuffer.c'],
5454
#extra_compile_args=['-Wconversion'],
55+
#undef_macros=['NDEBUG'],
5556
)
5657

5758
if __name__ == '__main__':

src/rtmixer.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* See ../rtmixer_build.py */
22

3-
#include <assert.h> // for assert()
43
#include <math.h> // for llround()
54
#include <stdbool.h> // for bool, true, false
65

@@ -9,9 +8,19 @@
98

109
#include "rtmixer.h"
1110

11+
#ifdef NDEBUG
12+
#define CALLBACK_ASSERT(expr) ((void)(0))
13+
#else
14+
#define CALLBACK_ASSERT(expr) \
15+
do { if (!(expr)) { \
16+
printf("Failed assertion in audio callback: \"" #expr "\" (" __FILE__ \
17+
":%i)\n", __LINE__); \
18+
return paAbort; \
19+
}} while (false)
20+
#endif
21+
1222
void remove_action(struct action** addr, struct state* state)
1323
{
14-
assert(addr && *addr && state);
1524
struct action* action = *addr;
1625
*addr = action->next; // Current action is removed from list
1726
action->next = NULL;
@@ -29,6 +38,7 @@ int callback(const void* input, void* output, frame_t frameCount
2938
, void* userData)
3039
{
3140
struct state* state = userData;
41+
CALLBACK_ASSERT(state);
3242

3343
memset(output, 0, sizeof(float) * state->output_channels * frameCount);
3444

@@ -49,7 +59,7 @@ int callback(const void* input, void* output, frame_t frameCount
4959

5060
if (action->type == CANCEL)
5161
{
52-
assert(action->action);
62+
CALLBACK_ASSERT(action->action);
5363

5464
struct action** actionaddr = &(state->actions);
5565
while (*actionaddr)
@@ -148,7 +158,7 @@ int callback(const void* input, void* output, frame_t frameCount
148158
}
149159
if (frames + offset > frameCount)
150160
{
151-
assert(frameCount > offset);
161+
CALLBACK_ASSERT(frameCount > offset);
152162
frames = frameCount - offset;
153163
}
154164

0 commit comments

Comments
 (0)