Sound system with metadesk, mono only for now

main
Cameron Murphy Reikes 2 years ago
parent de9d55d62c
commit 644161ab16

@ -1,3 +1,7 @@
@sound simple_talk:
{
filepath: "simple_text_chirp.wav",
}
@image merchant: @image merchant:
{ {
filepath: "copyrighted/merchant.png", filepath: "copyrighted/merchant.png",

Binary file not shown.

@ -12,4 +12,3 @@
#define BUFF_PUSH_FRONT(buff_ptr, value) { (buff_ptr)->cur_index++; BUFF_VALID(buff_ptr); for(int i = (buff_ptr)->cur_index - 1; i > 0; i--) { (buff_ptr)->data[i] = (buff_ptr)->data[i - 1]; }; (buff_ptr)->data[0] = value; } #define BUFF_PUSH_FRONT(buff_ptr, value) { (buff_ptr)->cur_index++; BUFF_VALID(buff_ptr); for(int i = (buff_ptr)->cur_index - 1; i > 0; i--) { (buff_ptr)->data[i] = (buff_ptr)->data[i - 1]; }; (buff_ptr)->data[0] = value; }
#define BUFF_REMOVE_FRONT(buff_ptr) {if((buff_ptr)->cur_index > 0) {for(int i = 0; i < (buff_ptr)->cur_index - 1; i++) { (buff_ptr)->data[i] = (buff_ptr)->data[i+1]; }; (buff_ptr)->cur_index--;}} #define BUFF_REMOVE_FRONT(buff_ptr) {if((buff_ptr)->cur_index > 0) {for(int i = 0; i < (buff_ptr)->cur_index - 1; i++) { (buff_ptr)->data[i] = (buff_ptr)->data[i+1]; }; (buff_ptr)->cur_index--;}}
#define BUFF_CLEAR(buff_ptr) {memset((buff_ptr), 0, sizeof(*(buff_ptr))); ((buff_ptr)->cur_index = 0);} #define BUFF_CLEAR(buff_ptr) {memset((buff_ptr), 0, sizeof(*(buff_ptr))); ((buff_ptr)->cur_index = 0);}

@ -252,6 +252,19 @@ int main(int argc, char **argv) {
MD_String8List level_decl_list = {0}; MD_String8List level_decl_list = {0};
MD_String8List tileset_decls = {0}; MD_String8List tileset_decls = {0};
for(MD_EachNode(node, parse.node->first_child)) { for(MD_EachNode(node, parse.node->first_child)) {
if(MD_S8Match(node->first_tag->string, MD_S8Lit("sound"), 0)) {
MD_String8 variable_name = MD_S8Fmt(cg_arena, "sound_%.*s", MD_S8VArg(node->string));
log("New sound variable %.*s\n", MD_S8VArg(variable_name));
MD_String8 filepath = ChildValue(node, MD_S8Lit("filepath"));
filepath = asset_file_path(filepath);
assert_cond(filepath.str != 0, MD_S8Fmt(cg_arena, "No filepath specified for sound '%.*s'", MD_S8VArg(node->string)));
FILE *asset_file = fopen(filepath.str, "r");
assert_cond(asset_file, MD_S8Fmt(cg_arena, "Could not open filepath %.*s for asset '%.*s'", MD_S8VArg(filepath), MD_S8VArg(node->string)));
fclose(asset_file);
MD_S8ListPush(cg_arena, &declarations_list, MD_S8Fmt(cg_arena, "AudioSample %.*s = {0};\n", MD_S8VArg(variable_name)));
MD_S8ListPush(cg_arena, &load_list, MD_S8Fmt(cg_arena, "%.*s = load_wav_audio(\"%.*s\");\n", MD_S8VArg(variable_name), MD_S8VArg(filepath)));
}
if(MD_S8Match(node->first_tag->string, MD_S8Lit("image"), 0)) { if(MD_S8Match(node->first_tag->string, MD_S8Lit("image"), 0)) {
MD_String8 variable_name = MD_S8Fmt(cg_arena, "image_%.*s", MD_S8VArg(node->string)); MD_String8 variable_name = MD_S8Fmt(cg_arena, "image_%.*s", MD_S8VArg(node->string));
log("New image variable %.*s\n", MD_S8VArg(variable_name)); log("New image variable %.*s\n", MD_S8VArg(variable_name));

517
main.c

@ -13,12 +13,16 @@
#include "sokol_app.h" #include "sokol_app.h"
#include "sokol_gfx.h" #include "sokol_gfx.h"
#include "sokol_time.h" #include "sokol_time.h"
#include "sokol_audio.h"
#include "sokol_log.h"
#include "sokol_glue.h" #include "sokol_glue.h"
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" #include "stb_image.h"
#define STB_TRUETYPE_IMPLEMENTATION #define STB_TRUETYPE_IMPLEMENTATION
#include "stb_truetype.h" #include "stb_truetype.h"
#include "HandmadeMath.h" #include "HandmadeMath.h"
#define DR_WAV_IMPLEMENTATION
#include "dr_wav.h"
#pragma warning(disable : 4996) // fopen is safe. I don't care about fopen_s #pragma warning(disable : 4996) // fopen is safe. I don't care about fopen_s
@ -280,231 +284,61 @@ void make_space_and_append(Dialog *d, DialogElement elem)
BUFF_APPEND(d, elem); BUFF_APPEND(d, elem);
} }
void say_characters(Entity *npc, int num_characters) typedef struct AudioSample
{
Sentence *sentence_to_append_to = &npc->player_dialog.data[npc->player_dialog.cur_index-1].s;
for(int i = 0; i < num_characters; i++)
{
if(!BUFF_EMPTY(&npc->player_dialog) && !BUFF_EMPTY(&npc->sentence_to_say))
{
char new_character = npc->sentence_to_say.data[0];
bool found_matching_star = false;
BUFF(char, MAX_SENTENCE_LENGTH) match_buffer = {0};
if(new_character == '*')
{
for(int ii = sentence_to_append_to->cur_index-1; ii >= 0; ii--)
{
if(sentence_to_append_to->data[ii] == '*')
{
found_matching_star = true;
break;
}
BUFF_PUSH_FRONT(&match_buffer, sentence_to_append_to->data[ii]);
}
}
if(found_matching_star)
{
#if 0 // actions
if(strcmp(match_buffer.data, "fights player") == 0 && npc->npc_kind == OLD_MAN)
{
npc->aggressive = true;
}
if(strcmp(match_buffer.data, "sells grounding boots") == 0 && npc->npc_kind == MERCHANT)
{
player->boots_modifier -= 1;
}
if(strcmp(match_buffer.data, "sells swiftness boots") == 0 && npc->npc_kind == MERCHANT)
{
player->boots_modifier += 1;
}
if(strcmp(match_buffer.data, "moves") == 0 && npc->npc_kind == DEATH)
{
npc->going_to_target = true;
npc->target_goto = AddV2(npc->pos, V2(0.0, -TILE_SIZE*1.5f));
}
#endif
}
BUFF_APPEND(sentence_to_append_to, new_character);
BUFF_REMOVE_FRONT(&npc->sentence_to_say);
}
}
}
bool npc_is_knight_sprite(Entity *it)
{
return it->is_npc && ( it->npc_kind == NPC_Max || it->npc_kind == NPC_Hunter || it->npc_kind == NPC_John);
}
void add_new_npc_sentence(Entity *npc, char *sentence)
{ {
size_t sentence_len = strlen(sentence); float *pcm_data; // allocated by loader, must be freed
assert(sentence_len < MAX_SENTENCE_LENGTH); uint64_t pcm_data_length;
Sentence new_sentence = {0}; } AudioSample;
bool inside_star = false;
for(int i = 0; i < sentence_len; i++)
{
if(sentence[i] == '"') break;
if(sentence[i] == '\n') continue;
BUFF_APPEND(&new_sentence, sentence[i]);
}
DialogElement empty_elem = { .author = NPC };
say_characters(npc, npc->sentence_to_say.cur_index);
make_space_and_append(&npc->player_dialog, empty_elem);
npc->sentence_to_say = new_sentence;
}
void begin_text_input(); // called when player engages in dialog, must say something and fill text_input_buffer typedef struct AudioPlayer
// a callback, when 'text backend' has finished making text. End dialog
void end_text_input(char *what_player_said)
{
player->state = CHARACTER_IDLE;
#ifdef WEB // hacky
_sapp_emsc_register_eventhandlers();
#endif
size_t player_said_len = strlen(what_player_said);
int actual_len = 0;
for(int i = 0; i < player_said_len; i++) if(what_player_said[i] != '\n') actual_len++;
if(actual_len == 0)
{
// this just means cancel the dialog
}
else
{
Sentence what_player_said_sentence = {0};
assert(player_said_len < ARRLEN(what_player_said_sentence.data));
for(int i = 0; i < player_said_len; i++)
{
char c = what_player_said[i];
if(c == '\n') break;
BUFF_APPEND(&what_player_said_sentence, c);
}
// order is player message, item status message in training data. So has to be same here
Dialog *to_append = &player->talking_to->player_dialog;
Entity *talking = player->talking_to;
make_space_and_append(to_append, (DialogElement){.s = what_player_said_sentence, .author = PLAYER});
if(talking->last_seen_holding != player->holding_item)
{
if(talking->last_seen_holding)
{
Sentence discard = from_str(item_discard_message_table[talking->last_seen_holding->item_kind]);
BUFF_APPEND(&discard, '\n');
make_space_and_append(to_append, (DialogElement){.author = SYSTEM, .s = discard});
assert(talking->last_seen_holding->is_item);
talking->last_seen_holding = 0;
}
if(player->holding_item)
{ {
assert(player->holding_item->is_item); AudioSample *sample; // if not 0, exists
Sentence possess = from_str(item_possess_message_table[player->holding_item->item_kind]); double volume; // ZII, 1.0 + this again
BUFF_APPEND(&possess, '\n'); double pitch; // zero initialized, the pitch used to play is 1.0 + this
make_space_and_append(to_append, (DialogElement){.author = SYSTEM, .s = possess}); double cursor_time; // in seconds, current audio sample is cursor_time * sample_rate
} } AudioPlayer;
talking->last_seen_holding = player->holding_item;
}
// the npc response will be appended here, or at least be async queued to be appended here AudioPlayer playing_audio[128] = {0};
BUFF(char, 4000) prompt_buff = {0};
BUFF(char *, 100) to_join = {0};
#define SAMPLE_RATE 44100
assert(talking->npc_kind >= 0); AudioSample load_wav_audio(const char *path)
assert(talking->npc_kind < ARRLEN(prompt_table));
assert(talking->npc_kind < ARRLEN(general_prompt_table));
assert(talking->npc_kind < ARRLEN(name_table));
// general prompt
BUFF_APPEND(&to_join, general_prompt_table[talking->npc_kind]);
BUFF_APPEND(&to_join, "\n");
// item prompt
if(player->holding_item)
{ {
BUFF_APPEND(&to_join, item_prompt_table[player->holding_item->item_kind]); unsigned int channels;
BUFF_APPEND(&to_join, "\n"); unsigned int sampleRate;
} AudioSample to_return = {0};
to_return.pcm_data = drwav_open_file_and_read_pcm_frames_f32(path, &channels, &sampleRate, &to_return.pcm_data_length, 0);
// characters prompt assert(channels == 1);
BUFF_APPEND(&to_join, prompt_table[talking->npc_kind]); assert(sampleRate == SAMPLE_RATE);
BUFF_APPEND(&to_join, "\n"); return to_return;
char *character_prompt = name_table[talking->npc_kind];
// all the dialog
int i = 0;
BUFF_ITER(DialogElement, &player->talking_to->player_dialog)
{
//bool is_player =
if(it->author == PLAYER)
{
BUFF_APPEND(&to_join, "Player: \"");
}
else if(it->author == NPC)
{
BUFF_APPEND(&to_join, character_prompt);
BUFF_APPEND(&to_join, ": \"");
}
else if(it->author == SYSTEM)
{
}
else
{
assert(false);
}
BUFF_APPEND(&to_join, it->s.data);
if(it->author == PLAYER || it->author == NPC)
BUFF_APPEND(&to_join, "\"\n");
i++;
} }
BUFF_APPEND(&to_join, character_prompt); uint64_t cursor_pcm(AudioPlayer *p)
BUFF_APPEND(&to_join, ": \"");
// concatenate into prompt_buff
BUFF_ITER(char *, &to_join)
{ {
size_t cur_len = strlen(*it); return (uint64_t)(p->cursor_time * SAMPLE_RATE);
for(int i = 0; i < cur_len; i++)
{
BUFF_APPEND(&prompt_buff, (*it)[i]);
} }
} float float_rand( float min, float max )
const char * prompt = prompt_buff.data;
#ifdef DEVTOOLS
Log("Prompt: `%s`\n", prompt);
#endif
#ifdef WEB
// fire off generation request, save id
int req_id = EM_ASM_INT({
return make_generation_request(UTF8ToString($1), UTF8ToString($0));
}, SERVER_URL, prompt);
player->talking_to->gen_request_id = req_id;
#endif
#ifdef DESKTOP
if(player->talking_to->npc_kind == NPC_Death)
{ {
add_new_npc_sentence(player->talking_to, "test *moves* I am death, destroyer of games. Come join me in the afterlife, or continue onwards *moves*"); float scale = rand() / (float) RAND_MAX; /* [0, 1.0] */
//add_new_npc_sentence(player->talking_to, "test"); return min + scale * ( max - min ); /* [min, max] */
} }
if(player->talking_to->npc_kind == NPC_Hunter) void play_audio(AudioSample *sample)
{ {
add_new_npc_sentence(player->talking_to, "I am hunter"); AudioPlayer *to_use = 0;
} for(int i = 0; i < ARRLEN(playing_audio); i++)
if(player->talking_to->npc_kind == NPC_Max)
{ {
add_new_npc_sentence(player->talking_to, "I am max"); if(playing_audio[i].sample == 0)
}
if(player->talking_to->npc_kind == NPC_John)
{ {
add_new_npc_sentence(player->talking_to, "I am john *gives WhiteSquare*"); to_use = &playing_audio[i];
break;
} }
#endif
} }
assert(to_use);
*to_use = (AudioPlayer){0};
to_use->sample = sample;
to_use->volume = -0.5f;
to_use->pitch = float_rand(0.9f, 1.1f);
} }
// keydown needs to be referenced when begin text input, // keydown needs to be referenced when begin text input,
// on web it disables event handling so the button up event isn't received // on web it disables event handling so the button up event isn't received
bool keydown[SAPP_KEYCODE_MENU] = {0}; bool keydown[SAPP_KEYCODE_MENU] = {0};
@ -756,6 +590,7 @@ TileInstance get_tile(Level *l, TileCoord t)
return get_tile_layer(l, 0, t); return get_tile_layer(l, 0, t);
} }
sg_image load_image(const char *path) sg_image load_image(const char *path)
{ {
sg_image to_return = {0}; sg_image to_return = {0};
@ -791,6 +626,234 @@ sg_image load_image(const char *path)
#include "quad-sapp.glsl.h" #include "quad-sapp.glsl.h"
#include "assets.gen.c" #include "assets.gen.c"
void say_characters(Entity *npc, int num_characters)
{
play_audio(&sound_simple_talk);
Sentence *sentence_to_append_to = &npc->player_dialog.data[npc->player_dialog.cur_index-1].s;
for(int i = 0; i < num_characters; i++)
{
if(!BUFF_EMPTY(&npc->player_dialog) && !BUFF_EMPTY(&npc->sentence_to_say))
{
char new_character = npc->sentence_to_say.data[0];
bool found_matching_star = false;
BUFF(char, MAX_SENTENCE_LENGTH) match_buffer = {0};
if(new_character == '*')
{
for(int ii = sentence_to_append_to->cur_index-1; ii >= 0; ii--)
{
if(sentence_to_append_to->data[ii] == '*')
{
found_matching_star = true;
break;
}
BUFF_PUSH_FRONT(&match_buffer, sentence_to_append_to->data[ii]);
}
}
if(found_matching_star)
{
#if 0 // actions
if(strcmp(match_buffer.data, "fights player") == 0 && npc->npc_kind == OLD_MAN)
{
npc->aggressive = true;
}
if(strcmp(match_buffer.data, "sells grounding boots") == 0 && npc->npc_kind == MERCHANT)
{
player->boots_modifier -= 1;
}
if(strcmp(match_buffer.data, "sells swiftness boots") == 0 && npc->npc_kind == MERCHANT)
{
player->boots_modifier += 1;
}
if(strcmp(match_buffer.data, "moves") == 0 && npc->npc_kind == DEATH)
{
npc->going_to_target = true;
npc->target_goto = AddV2(npc->pos, V2(0.0, -TILE_SIZE*1.5f));
}
#endif
}
BUFF_APPEND(sentence_to_append_to, new_character);
BUFF_REMOVE_FRONT(&npc->sentence_to_say);
}
}
}
bool npc_is_knight_sprite(Entity *it)
{
return it->is_npc && ( it->npc_kind == NPC_Max || it->npc_kind == NPC_Hunter || it->npc_kind == NPC_John);
}
void add_new_npc_sentence(Entity *npc, char *sentence)
{
size_t sentence_len = strlen(sentence);
assert(sentence_len < MAX_SENTENCE_LENGTH);
Sentence new_sentence = {0};
bool inside_star = false;
for(int i = 0; i < sentence_len; i++)
{
if(sentence[i] == '"') break;
if(sentence[i] == '\n') continue;
BUFF_APPEND(&new_sentence, sentence[i]);
}
DialogElement empty_elem = { .author = NPC };
say_characters(npc, npc->sentence_to_say.cur_index);
make_space_and_append(&npc->player_dialog, empty_elem);
npc->sentence_to_say = new_sentence;
}
void begin_text_input(); // called when player engages in dialog, must say something and fill text_input_buffer
// a callback, when 'text backend' has finished making text. End dialog
void end_text_input(char *what_player_said)
{
player->state = CHARACTER_IDLE;
#ifdef WEB // hacky
_sapp_emsc_register_eventhandlers();
#endif
size_t player_said_len = strlen(what_player_said);
int actual_len = 0;
for(int i = 0; i < player_said_len; i++) if(what_player_said[i] != '\n') actual_len++;
if(actual_len == 0)
{
// this just means cancel the dialog
}
else
{
Sentence what_player_said_sentence = {0};
assert(player_said_len < ARRLEN(what_player_said_sentence.data));
for(int i = 0; i < player_said_len; i++)
{
char c = what_player_said[i];
if(c == '\n') break;
BUFF_APPEND(&what_player_said_sentence, c);
}
// order is player message, item status message in training data. So has to be same here
Dialog *to_append = &player->talking_to->player_dialog;
Entity *talking = player->talking_to;
make_space_and_append(to_append, (DialogElement){.s = what_player_said_sentence, .author = PLAYER});
if(talking->last_seen_holding != player->holding_item)
{
if(talking->last_seen_holding)
{
Sentence discard = from_str(item_discard_message_table[talking->last_seen_holding->item_kind]);
BUFF_APPEND(&discard, '\n');
make_space_and_append(to_append, (DialogElement){.author = SYSTEM, .s = discard});
assert(talking->last_seen_holding->is_item);
talking->last_seen_holding = 0;
}
if(player->holding_item)
{
assert(player->holding_item->is_item);
Sentence possess = from_str(item_possess_message_table[player->holding_item->item_kind]);
BUFF_APPEND(&possess, '\n');
make_space_and_append(to_append, (DialogElement){.author = SYSTEM, .s = possess});
}
talking->last_seen_holding = player->holding_item;
}
// the npc response will be appended here, or at least be async queued to be appended here
BUFF(char, 4000) prompt_buff = {0};
BUFF(char *, 100) to_join = {0};
assert(talking->npc_kind >= 0);
assert(talking->npc_kind < ARRLEN(prompt_table));
assert(talking->npc_kind < ARRLEN(general_prompt_table));
assert(talking->npc_kind < ARRLEN(name_table));
// general prompt
BUFF_APPEND(&to_join, general_prompt_table[talking->npc_kind]);
BUFF_APPEND(&to_join, "\n");
// item prompt
if(player->holding_item)
{
BUFF_APPEND(&to_join, item_prompt_table[player->holding_item->item_kind]);
BUFF_APPEND(&to_join, "\n");
}
// characters prompt
BUFF_APPEND(&to_join, prompt_table[talking->npc_kind]);
BUFF_APPEND(&to_join, "\n");
char *character_prompt = name_table[talking->npc_kind];
// all the dialog
int i = 0;
BUFF_ITER(DialogElement, &player->talking_to->player_dialog)
{
//bool is_player =
if(it->author == PLAYER)
{
BUFF_APPEND(&to_join, "Player: \"");
}
else if(it->author == NPC)
{
BUFF_APPEND(&to_join, character_prompt);
BUFF_APPEND(&to_join, ": \"");
}
else if(it->author == SYSTEM)
{
}
else
{
assert(false);
}
BUFF_APPEND(&to_join, it->s.data);
if(it->author == PLAYER || it->author == NPC)
BUFF_APPEND(&to_join, "\"\n");
i++;
}
BUFF_APPEND(&to_join, character_prompt);
BUFF_APPEND(&to_join, ": \"");
// concatenate into prompt_buff
BUFF_ITER(char *, &to_join)
{
size_t cur_len = strlen(*it);
for(int i = 0; i < cur_len; i++)
{
BUFF_APPEND(&prompt_buff, (*it)[i]);
}
}
const char * prompt = prompt_buff.data;
#ifdef DEVTOOLS
Log("Prompt: `%s`\n", prompt);
#endif
#ifdef WEB
// fire off generation request, save id
int req_id = EM_ASM_INT({
return make_generation_request(UTF8ToString($1), UTF8ToString($0));
}, SERVER_URL, prompt);
player->talking_to->gen_request_id = req_id;
#endif
#ifdef DESKTOP
if(player->talking_to->npc_kind == NPC_Death)
{
add_new_npc_sentence(player->talking_to, "test *moves* I am death, destroyer of games. Come join me in the afterlife, or continue onwards *moves*");
//add_new_npc_sentence(player->talking_to, "test");
}
if(player->talking_to->npc_kind == NPC_Hunter)
{
add_new_npc_sentence(player->talking_to, "I am hunter");
}
if(player->talking_to->npc_kind == NPC_Max)
{
add_new_npc_sentence(player->talking_to, "I am max");
}
if(player->talking_to->npc_kind == NPC_John)
{
add_new_npc_sentence(player->talking_to, "I am john *gives WhiteSquare*");
}
#endif
}
}
AnimatedSprite knight_idle = AnimatedSprite knight_idle =
{ {
.img = &image_knight_idle, .img = &image_knight_idle,
@ -974,6 +1037,34 @@ void reset_level()
item->pos = AddV2(player->pos, V2(0.0, 30.0)); item->pos = AddV2(player->pos, V2(0.0, 30.0));
} }
void audio_stream_callback(float *buffer, int num_frames, int num_channels)
{
assert(num_channels == 1);
const int num_samples = num_frames * num_channels;
double time_to_play = (double)num_frames / (double)SAMPLE_RATE;
double time_per_sample = 1.0 / (double)SAMPLE_RATE;
for(int i = 0; i < num_samples; i++)
{
float output_frame = 0.0f;
for(int audio_i = 0; audio_i < ARRLEN(playing_audio); audio_i++)
{
AudioPlayer *it = &playing_audio[audio_i];
if(it->sample != 0)
{
if(cursor_pcm(it) >= it->sample->pcm_data_length)
{
it->sample = 0;
}
else
{
output_frame += it->sample->pcm_data[cursor_pcm(it)]*(float)(it->volume + 1.0);
it->cursor_time += time_per_sample*(it->pitch + 1.0);
}
}
}
buffer[i] = output_frame;
}
}
void init(void) void init(void)
{ {
@ -983,6 +1074,10 @@ void init(void)
.context = sapp_sgcontext(), .context = sapp_sgcontext(),
}); });
stm_setup(); stm_setup();
saudio_setup(&(saudio_desc){
.stream_cb = audio_stream_callback,
.logger.func = slog_func,
});
scratch = make_arena(1024 * 10); scratch = make_arena(1024 * 10);
@ -1713,6 +1808,16 @@ AABB draw_text(TextParams t)
{ {
col = t.colors[i]; col = t.colors[i];
} }
if(false) // drop shadow, don't really like it
if(t.world_space)
{
Quad shadow_quad = to_draw;
for(int i = 0; i < 4; i++)
{
shadow_quad.points[i] = AddV2(shadow_quad.points[i], V2(0.0, -1.0));
}
draw_quad((DrawParams){t.world_space, shadow_quad, image_font, font_atlas_region, (Color){0.0f,0.0f,0.0f,0.4f}, t.clip_to, .y_coord_sorting = 1.0f, .queue_for_translucent = true});
}
draw_quad((DrawParams){t.world_space, to_draw, image_font, font_atlas_region, col, t.clip_to, .y_coord_sorting = 1.0f, .queue_for_translucent = true}); draw_quad((DrawParams){t.world_space, to_draw, image_font, font_atlas_region, col, t.clip_to, .y_coord_sorting = 1.0f, .queue_for_translucent = true});
} }
} }

8350
thirdparty/dr_wav.h vendored

File diff suppressed because it is too large Load Diff

@ -132,18 +132,30 @@
a good balance between low-latency and glitch-free playback a good balance between low-latency and glitch-free playback
on all audio backends. on all audio backends.
You should always provide a logging callback to be aware of any
warnings and errors. The easiest way is to use sokol_log.h for this:
#include "sokol_log.h"
// ...
saudio_setup(&(saudio_desc){
.logger = {
.func = slog_func,
}
});
If you want to use the callback-model, you need to provide a stream If you want to use the callback-model, you need to provide a stream
callback function either in saudio_desc.stream_cb or saudio_desc.stream_userdata_cb, callback function either in saudio_desc.stream_cb or saudio_desc.stream_userdata_cb,
otherwise keep both function pointers zero-initialized. otherwise keep both function pointers zero-initialized.
Use push model and default playback parameters: Use push model and default playback parameters:
saudio_setup(&(saudio_desc){0}); saudio_setup(&(saudio_desc){ .logger.func = slog_func });
Use stream callback model and default playback parameters: Use stream callback model and default playback parameters:
saudio_setup(&(saudio_desc){ saudio_setup(&(saudio_desc){
.stream_cb = my_stream_callback .stream_cb = my_stream_callback
.logger.func = slog_func,
}); });
The standard stream callback doesn't have a user data argument, if you want The standard stream callback doesn't have a user data argument, if you want
@ -152,6 +164,7 @@
saudio_setup(&(saudio_desc){ saudio_setup(&(saudio_desc){
.stream_userdata_cb = my_stream_callback, .stream_userdata_cb = my_stream_callback,
.user_data = &my_data .user_data = &my_data
.logger.func = slog_func,
}); });
The following playback parameters can be provided through the The following playback parameters can be provided through the
@ -400,27 +413,43 @@
was called, so you don't need to worry about thread-safety. was called, so you don't need to worry about thread-safety.
LOG FUNCTION OVERRIDE ERROR REPORTING AND LOGGING
===================== ===========================
You can override the log function at initialization time like this: To get any logging information at all you need to provide a logging callback in the setup call
the easiest way is to use sokol_log.h:
void my_log(const char* message, void* user_data) { #include "sokol_log.h"
printf("saudio says: \s\n", message);
} saudio_setup(&(saudio_desc){ .logger.func = slog_func });
To override logging with your own callback, first write a logging function like this:
void my_log(const char* tag, // e.g. 'saudio'
uint32_t log_level, // 0=panic, 1=error, 2=warn, 3=info
uint32_t log_item_id, // SAUDIO_LOGITEM_*
const char* message_or_null, // a message string, may be nullptr in release mode
uint32_t line_nr, // line number in sokol_audio.h
const char* filename_or_null, // source filename, may be nullptr in release mode
void* user_data)
{
... ...
}
...and then setup sokol-audio like this:
saudio_setup(&(saudio_desc){ saudio_setup(&(saudio_desc){
// ...
.logger = { .logger = {
.log_cb = my_log, .func = my_log,
.user_data = ..., .user_data = my_user_data,
} }
}); });
...
If no overrides are provided, puts will be used on most platforms. The provided logging function must be reentrant (e.g. be callable from
On Android, __android_log_write will be used instead. different threads).
If you don't want to provide your own custom logger it is highly recommended to use
the standard logger in sokol_log.h instead, otherwise you won't see any warnings or
errors.
LICENSE LICENSE
======= =======
@ -470,6 +499,77 @@
extern "C" { extern "C" {
#endif #endif
/*
saudio_log_item
Log items are defined via X-Macros, and expanded to an
enum 'saudio_log_item', and in debug mode only,
corresponding strings.
Used as parameter in the logging callback.
*/
#define _SAUDIO_LOG_ITEMS \
_SAUDIO_LOGITEM_XMACRO(OK, "Ok") \
_SAUDIO_LOGITEM_XMACRO(MALLOC_FAILED, "memory allocation failed") \
_SAUDIO_LOGITEM_XMACRO(ALSA_SND_PCM_OPEN_FAILED, "snd_pcm_open() failed") \
_SAUDIO_LOGITEM_XMACRO(ALSA_FLOAT_SAMPLES_NOT_SUPPORTED, "floating point sample format not supported") \
_SAUDIO_LOGITEM_XMACRO(ALSA_REQUESTED_BUFFER_SIZE_NOT_SUPPORTED, "requested buffer size not supported") \
_SAUDIO_LOGITEM_XMACRO(ALSA_REQUESTED_CHANNEL_COUNT_NOT_SUPPORTED, "requested channel count not supported") \
_SAUDIO_LOGITEM_XMACRO(ALSA_SND_PCM_HW_PARAMS_SET_RATE_NEAR_FAILED, "snd_pcm_hw_params_set_rate_near() failed") \
_SAUDIO_LOGITEM_XMACRO(ALSA_SND_PCM_HW_PARAMS_FAILED, "snd_pcm_hw_params() failed") \
_SAUDIO_LOGITEM_XMACRO(ALSA_PTHREAD_CREATE_FAILED, "pthread_create() failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_CREATE_EVENT_FAILED, "CreateEvent() failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_CREATE_DEVICE_ENUMERATOR_FAILED, "CoCreateInstance() for IMMDeviceEnumerator failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_GET_DEFAULT_AUDIO_ENDPOINT_FAILED, "IMMDeviceEnumerator.GetDefaultAudioEndpoint() failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_DEVICE_ACTIVATE_FAILED, "IMMDevice.Activate() failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_AUDIO_CLIENT_INITIALIZE_FAILED, "IAudioClient.Initialize() failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_AUDIO_CLIENT_GET_BUFFER_SIZE_FAILED, "IAudioClient.GetBufferSize() failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_AUDIO_CLIENT_GET_SERVICE_FAILED, "IAudioClient.GetService() failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_AUDIO_CLIENT_SET_EVENT_HANDLE_FAILED, "IAudioClient.SetEventHandle() failed") \
_SAUDIO_LOGITEM_XMACRO(WASAPI_CREATE_THREAD_FAILED, "CreateThread() failed") \
_SAUDIO_LOGITEM_XMACRO(AAUDIO_STREAMBUILDER_OPEN_STREAM_FAILED, "AAudioStreamBuilder_openStream() failed") \
_SAUDIO_LOGITEM_XMACRO(AAUDIO_PTHREAD_CREATE_FAILED, "pthread_create() failed after AAUDIO_ERROR_DISCONNECTED") \
_SAUDIO_LOGITEM_XMACRO(AAUDIO_RESTARTING_STREAM_AFTER_ERROR, "restarting AAudio stream after error") \
_SAUDIO_LOGITEM_XMACRO(USING_AAUDIO_BACKEND, "using AAudio backend") \
_SAUDIO_LOGITEM_XMACRO(AAUDIO_CREATE_STREAMBUILDER_FAILED, "AAudio_createStreamBuilder() failed") \
_SAUDIO_LOGITEM_XMACRO(USING_SLES_BACKEND, "using OpenSLES backend") \
_SAUDIO_LOGITEM_XMACRO(SLES_CREATE_ENGINE_FAILED, "slCreateEngine() failed") \
_SAUDIO_LOGITEM_XMACRO(SLES_ENGINE_GET_ENGINE_INTERFACE_FAILED, "GetInterface() for SL_IID_ENGINE failed") \
_SAUDIO_LOGITEM_XMACRO(SLES_CREATE_OUTPUT_MIX_FAILED, "CreateOutputMix() failed") \
_SAUDIO_LOGITEM_XMACRO(SLES_MIXER_GET_VOLUME_INTERFACE_FAILED, "GetInterface() for SL_IID_VOLUME failed") \
_SAUDIO_LOGITEM_XMACRO(SLES_ENGINE_CREATE_AUDIO_PLAYER_FAILED, "CreateAudioPlayer() failed") \
_SAUDIO_LOGITEM_XMACRO(SLES_PLAYER_GET_PLAY_INTERFACE_FAILED, "GetInterface() for SL_IID_PLAY failed") \
_SAUDIO_LOGITEM_XMACRO(SLES_PLAYER_GET_VOLUME_INTERFACE_FAILED, "GetInterface() for SL_IID_VOLUME failed") \
_SAUDIO_LOGITEM_XMACRO(SLES_PLAYER_GET_BUFFERQUEUE_INTERFACE_FAILED, "GetInterface() for SL_IID_ANDROIDSIMPLEBUFFERQUEUE failed") \
_SAUDIO_LOGITEM_XMACRO(COREAUDIO_NEW_OUTPUT_FAILED, "AudioQueueNewOutput() failed") \
_SAUDIO_LOGITEM_XMACRO(COREAUDIO_ALLOCATE_BUFFER_FAILED, "AudioQueueAllocateBuffer() failed") \
_SAUDIO_LOGITEM_XMACRO(COREAUDIO_START_FAILED, "AudioQueueStart() failed") \
_SAUDIO_LOGITEM_XMACRO(BACKEND_BUFFER_SIZE_ISNT_MULTIPLE_OF_PACKET_SIZE, "backend buffer size isn't multiple of packet size") \
#define _SAUDIO_LOGITEM_XMACRO(item,msg) SAUDIO_LOGITEM_##item,
typedef enum saudio_log_item {
_SAUDIO_LOG_ITEMS
} saudio_log_item;
#undef _SAUDIO_LOGITEM_XMACRO
/*
saudio_logger
Used in saudio_desc to provide a custom logging and error reporting
callback to sokol-audio.
*/
typedef struct saudio_logger {
void (*func)(
const char* tag, // always "saudio"
uint32_t log_level, // 0=panic, 1=error, 2=warning, 3=info
uint32_t log_item_id, // SAUDIO_LOGITEM_*
const char* message_or_null, // a message string, may be nullptr in release mode
uint32_t line_nr, // line number in sokol_audio.h
const char* filename_or_null, // source filename, may be nullptr in release mode
void* user_data);
void* user_data;
} saudio_logger;
/* /*
saudio_allocator saudio_allocator
@ -484,17 +584,6 @@ typedef struct saudio_allocator {
void* user_data; void* user_data;
} saudio_allocator; } saudio_allocator;
/*
saudio_logger
Used in saudio_desc to provide custom log callbacks to sokol_audio.h.
Default behavior is SOKOL_LOG(message).
*/
typedef struct saudio_logger {
void (*log_cb)(const char* message, void* user_data);
void* user_data;
} saudio_logger;
typedef struct saudio_desc { typedef struct saudio_desc {
int sample_rate; // requested sample rate int sample_rate; // requested sample rate
int num_channels; // number of channels, default: 1 (mono) int num_channels; // number of channels, default: 1 (mono)
@ -505,7 +594,7 @@ typedef struct saudio_desc {
void (*stream_userdata_cb)(float* buffer, int num_frames, int num_channels, void* user_data); //... and with user data void (*stream_userdata_cb)(float* buffer, int num_frames, int num_channels, void* user_data); //... and with user data
void* user_data; // optional user data argument for stream_userdata_cb void* user_data; // optional user data argument for stream_userdata_cb
saudio_allocator allocator; // optional allocation override functions saudio_allocator allocator; // optional allocation override functions
saudio_logger logger; // optional log override functions saudio_logger logger; // optional logging function (default: NO LOGGING!)
} saudio_desc; } saudio_desc;
/* setup sokol-audio */ /* setup sokol-audio */
@ -540,12 +629,11 @@ inline void saudio_setup(const saudio_desc& desc) { return saudio_setup(&desc);
#endif #endif
#endif // SOKOL_AUDIO_INCLUDED #endif // SOKOL_AUDIO_INCLUDED
// ██╗███╗ ███╗██████╗ ██╗ ███████╗███╗ ███╗███████╗███╗ ██╗████████╗ █████╗ ████████╗██╗ ██████╗ ███╗ ██╗ // ██ ███ ███ ██████ ██ ███████ ███ ███ ███████ ███ ██ ████████ █████ ████████ ██ ██████ ███ ██
// ██║████╗ ████║██╔══██╗██║ ██╔════╝████╗ ████║██╔════╝████╗ ██║╚══██╔══╝██╔══██╗╚══██╔══╝██║██╔═══██╗████╗ ██║ // ██ ████ ████ ██ ██ ██ ██ ████ ████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
// ██║██╔████╔██║██████╔╝██║ █████╗ ██╔████╔██║█████╗ ██╔██╗ ██║ ██║ ███████║ ██║ ██║██║ ██║██╔██╗ ██║ // ██ ██ ████ ██ ██████ ██ █████ ██ ████ ██ █████ ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██
// ██║██║╚██╔╝██║██╔═══╝ ██║ ██╔══╝ ██║╚██╔╝██║██╔══╝ ██║╚██╗██║ ██║ ██╔══██║ ██║ ██║██║ ██║██║╚██╗██║ // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██║██║ ╚═╝ ██║██║ ███████╗███████╗██║ ╚═╝ ██║███████╗██║ ╚████║ ██║ ██║ ██║ ██║ ██║╚██████╔╝██║ ╚████║ // ██ ██ ██ ██ ███████ ███████ ██ ██ ███████ ██ ████ ██ ██ ██ ██ ██ ██████ ██ ████
// ╚═╝╚═╝ ╚═╝╚═╝ ╚══════╝╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝
// //
// >>implementation // >>implementation
#ifdef SOKOL_AUDIO_IMPL #ifdef SOKOL_AUDIO_IMPL
@ -572,21 +660,6 @@ inline void saudio_setup(const saudio_desc& desc) { return saudio_setup(&desc);
#define SOKOL_ASSERT(c) assert(c) #define SOKOL_ASSERT(c) assert(c)
#endif #endif
#if !defined(SOKOL_DEBUG)
#define SAUDIO_LOG(s)
#else
#define SAUDIO_LOG(s) _saudio_log(s)
#ifndef SOKOL_LOG
#if defined(__ANDROID__)
#include <android/log.h>
#define SOKOL_LOG(s) __android_log_write(ANDROID_LOG_INFO, "SOKOL_AUDIO", s)
#else
#include <stdio.h>
#define SOKOL_LOG(s) puts(s)
#endif
#endif
#endif
#ifndef _SOKOL_PRIVATE #ifndef _SOKOL_PRIVATE
#if defined(__GNUC__) || defined(__clang__) #if defined(__GNUC__) || defined(__clang__)
#define _SOKOL_PRIVATE __attribute__((unused)) static #define _SOKOL_PRIVATE __attribute__((unused)) static
@ -611,14 +684,12 @@ inline void saudio_setup(const saudio_desc& desc) { return saudio_setup(&desc);
#define _SAUDIO_MACOS (1) #define _SAUDIO_MACOS (1)
#endif #endif
#elif defined(__EMSCRIPTEN__) #elif defined(__EMSCRIPTEN__)
#define _SAUDIO_EMSCRIPTEN #define _SAUDIO_EMSCRIPTEN (1)
#elif defined(_WIN32) #elif defined(_WIN32)
#define _SAUDIO_WINDOWS (1) #define _SAUDIO_WINDOWS (1)
#include <winapifamily.h> #include <winapifamily.h>
#if (defined(WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) #if (defined(WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP))
#define _SAUDIO_UWP (1) #error "sokol_audio.h no longer supports UWP"
#else
#define _SAUDIO_WIN32 (1)
#endif #endif
#elif defined(__ANDROID__) #elif defined(__ANDROID__)
#define _SAUDIO_ANDROID (1) #define _SAUDIO_ANDROID (1)
@ -644,12 +715,8 @@ inline void saudio_setup(const saudio_desc& desc) { return saudio_setup(&desc);
#endif #endif
#include <windows.h> #include <windows.h>
#include <synchapi.h> #include <synchapi.h>
#if defined(_SAUDIO_UWP)
#pragma comment (lib, "WindowsApp")
#else
#pragma comment (lib, "kernel32") #pragma comment (lib, "kernel32")
#pragma comment (lib, "ole32") #pragma comment (lib, "ole32")
#endif
#ifndef CINTERFACE #ifndef CINTERFACE
#define CINTERFACE #define CINTERFACE
#endif #endif
@ -713,6 +780,7 @@ inline void saudio_setup(const saudio_desc& desc) { return saudio_setup(&desc);
#include "aaudio/AAudio.h" #include "aaudio/AAudio.h"
#endif #endif
#elif defined(_SAUDIO_LINUX) #elif defined(_SAUDIO_LINUX)
#include <alloca.h>
#define _SAUDIO_PTHREADS (1) #define _SAUDIO_PTHREADS (1)
#include <pthread.h> #include <pthread.h>
#define ALSA_PCM_NEW_HW_PARAMS_API #define ALSA_PCM_NEW_HW_PARAMS_API
@ -734,12 +802,11 @@ inline void saudio_setup(const saudio_desc& desc) { return saudio_setup(&desc);
#define SAUDIO_RING_MAX_SLOTS (1024) #define SAUDIO_RING_MAX_SLOTS (1024)
#endif #endif
// ███████╗████████╗██████╗ ██╗ ██╗ ██████╗████████╗███████╗ // ███████ ████████ ██████ ██ ██ ██████ ████████ ███████
// ██╔════╝╚══██╔══╝██╔══██╗██║ ██║██╔════╝╚══██╔══╝██╔════╝ // ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████╗ ██║ ██████╔╝██║ ██║██║ ██║ ███████╗ // ███████ ██ ██████ ██ ██ ██ ██ ███████
// ╚════██║ ██║ ██╔══██╗██║ ██║██║ ██║ ╚════██║ // ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████║ ██║ ██║ ██║╚██████╔╝╚██████╗ ██║ ███████║ // ███████ ██ ██ ██ ██████ ██████ ██ ███████
// ╚══════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
// //
// >>structs // >>structs
#if defined(_SAUDIO_PTHREADS) #if defined(_SAUDIO_PTHREADS)
@ -939,15 +1006,8 @@ typedef struct {
} _saudio_wasapi_thread_data_t; } _saudio_wasapi_thread_data_t;
typedef struct { typedef struct {
#if defined(_SAUDIO_UWP)
LPOLESTR interface_activation_audio_interface_uid_string;
IActivateAudioInterfaceAsyncOperation* interface_activation_operation;
BOOL interface_activation_success;
HANDLE interface_activation_mutex;
#else
IMMDeviceEnumerator* device_enumerator; IMMDeviceEnumerator* device_enumerator;
IMMDevice* device; IMMDevice* device;
#endif
IAudioClient* audio_client; IAudioClient* audio_client;
IAudioRenderClient* render_client; IAudioRenderClient* render_client;
_saudio_wasapi_thread_data_t thread; _saudio_wasapi_thread_data_t thread;
@ -1032,12 +1092,50 @@ _SOKOL_PRIVATE void _saudio_stream_callback(float* buffer, int num_frames, int n
} }
} }
// ███╗ ███╗███████╗███╗ ███╗ ██████╗ ██████╗ ██╗ ██╗ // ██ ██████ ██████ ██████ ██ ███ ██ ██████
// ████╗ ████║██╔════╝████╗ ████║██╔═══██╗██╔══██╗╚██╗ ██╔╝ // ██ ██ ██ ██ ██ ██ ████ ██ ██
// ██╔████╔██║█████╗ ██╔████╔██║██║ ██║██████╔╝ ╚████╔╝ // ██ ██ ██ ██ ███ ██ ███ ██ ██ ██ ██ ██ ███
// ██║╚██╔╝██║██╔══╝ ██║╚██╔╝██║██║ ██║██╔══██╗ ╚██╔╝ // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██║ ╚═╝ ██║███████╗██║ ╚═╝ ██║╚██████╔╝██║ ██║ ██║ // ███████ ██████ ██████ ██████ ██ ██ ████ ██████
// ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ //
// >>logging
#if defined(SOKOL_DEBUG)
#define _SAUDIO_LOGITEM_XMACRO(item,msg) #item ": " msg,
static const char* _saudio_log_messages[] = {
_SAUDIO_LOG_ITEMS
};
#undef _SAUDIO_LOGITEM_XMACRO
#endif // SOKOL_DEBUG
#define _SAUDIO_PANIC(code) _saudio_log(SAUDIO_LOGITEM_ ##code, 0, __LINE__)
#define _SAUDIO_ERROR(code) _saudio_log(SAUDIO_LOGITEM_ ##code, 1, __LINE__)
#define _SAUDIO_WARN(code) _saudio_log(SAUDIO_LOGITEM_ ##code, 2, __LINE__)
#define _SAUDIO_INFO(code) _saudio_log(SAUDIO_LOGITEM_ ##code, 3, __LINE__)
static void _saudio_log(saudio_log_item log_item, uint32_t log_level, uint32_t line_nr) {
if (_saudio.desc.logger.func) {
#if defined(SOKOL_DEBUG)
const char* filename = __FILE__;
const char* message = _saudio_log_messages[log_item];
#else
const char* filename = 0;
const char* message = 0;
#endif
_saudio.desc.logger.func("saudio", log_level, log_item, message, line_nr, filename, _saudio.desc.logger.user_data);
}
else {
// for log level PANIC it would be 'undefined behaviour' to continue
if (log_level == 0) {
abort();
}
}
}
// ███ ███ ███████ ███ ███ ██████ ██████ ██ ██
// ████ ████ ██ ████ ████ ██ ██ ██ ██ ██ ██
// ██ ████ ██ █████ ██ ████ ██ ██ ██ ██████ ████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ███████ ██ ██ ██████ ██ ██ ██
// //
// >>memory // >>memory
_SOKOL_PRIVATE void _saudio_clear(void* ptr, size_t size) { _SOKOL_PRIVATE void _saudio_clear(void* ptr, size_t size) {
@ -1054,7 +1152,9 @@ _SOKOL_PRIVATE void* _saudio_malloc(size_t size) {
else { else {
ptr = malloc(size); ptr = malloc(size);
} }
SOKOL_ASSERT(ptr); if (0 == ptr) {
_SAUDIO_PANIC(MALLOC_FAILED);
}
return ptr; return ptr;
} }
@ -1073,23 +1173,11 @@ _SOKOL_PRIVATE void _saudio_free(void* ptr) {
} }
} }
#if defined(SOKOL_DEBUG) // ███ ███ ██ ██ ████████ ███████ ██ ██
_SOKOL_PRIVATE void _saudio_log(const char* msg) { // ████ ████ ██ ██ ██ ██ ██ ██
SOKOL_ASSERT(msg); // ██ ████ ██ ██ ██ ██ █████ ███
if (_saudio.desc.logger.log_cb) { // ██ ██ ██ ██ ██ ██ ██ ██ ██
_saudio.desc.logger.log_cb(msg, _saudio.desc.logger.user_data); // ██ ██ ██████ ██ ███████ ██ ██
} else {
SOKOL_LOG(msg);
}
}
#endif
// ███╗ ███╗██╗ ██╗████████╗███████╗██╗ ██╗
// ████╗ ████║██║ ██║╚══██╔══╝██╔════╝╚██╗██╔╝
// ██╔████╔██║██║ ██║ ██║ █████╗ ╚███╔╝
// ██║╚██╔╝██║██║ ██║ ██║ ██╔══╝ ██╔██╗
// ██║ ╚═╝ ██║╚██████╔╝ ██║ ███████╗██╔╝ ██╗
// ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═╝
// //
// >>mutex // >>mutex
#if defined(_SAUDIO_NOTHREADS) #if defined(_SAUDIO_NOTHREADS)
@ -1140,12 +1228,11 @@ _SOKOL_PRIVATE void _saudio_mutex_unlock(_saudio_mutex_t* m) {
#error "sokol_audio.h: unknown platform!" #error "sokol_audio.h: unknown platform!"
#endif #endif
// ██████╗ ██╗███╗ ██╗ ██████╗ ██████╗ ██╗ ██╗███████╗███████╗███████╗██████╗ // ██████ ██ ███ ██ ██████ ██████ ██ ██ ███████ ███████ ███████ ██████
// ██╔══██╗██║████╗ ██║██╔════╝ ██╔══██╗██║ ██║██╔════╝██╔════╝██╔════╝██╔══██╗ // ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████╔╝██║██╔██╗ ██║██║ ███╗██████╔╝██║ ██║█████╗ █████╗ █████╗ ██████╔╝ // ██████ ██ ██ ██ ██ ██ ███ ██████ ██ ██ █████ █████ █████ ██████
// ██╔══██╗██║██║╚██╗██║██║ ██║██╔══██╗██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██╔══██╗ // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██║ ██║██║██║ ╚████║╚██████╔╝██████╔╝╚██████╔╝██║ ██║ ███████╗██║ ██║ // ██ ██ ██ ██ ████ ██████ ██████ ██████ ██ ██ ███████ ██ ██
// ╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝
// //
// >>ringbuffer // >>ringbuffer
_SOKOL_PRIVATE int _saudio_ring_idx(_saudio_ring_t* ring, int i) { _SOKOL_PRIVATE int _saudio_ring_idx(_saudio_ring_t* ring, int i) {
@ -1193,12 +1280,11 @@ _SOKOL_PRIVATE int _saudio_ring_dequeue(_saudio_ring_t* ring) {
return val; return val;
} }
// ███████╗██╗███████╗ ██████╗ // ███████ ██ ███████ ██████
// ██╔════╝██║██╔════╝██╔═══██╗ // ██ ██ ██ ██ ██
// █████╗ ██║█████╗ ██║ ██║ // █████ ██ █████ ██ ██
// ██╔══╝ ██║██╔══╝ ██║ ██║ // ██ ██ ██ ██ ██
// ██║ ██║██║ ╚██████╔╝ // ██ ██ ██ ██████
// ╚═╝ ╚═╝╚═╝ ╚═════╝
// //
// >>fifo // >>fifo
_SOKOL_PRIVATE void _saudio_fifo_init_mutex(_saudio_fifo_t* fifo) { _SOKOL_PRIVATE void _saudio_fifo_init_mutex(_saudio_fifo_t* fifo) {
@ -1330,12 +1416,11 @@ _SOKOL_PRIVATE int _saudio_fifo_read(_saudio_fifo_t* fifo, uint8_t* ptr, int num
return num_bytes_copied; return num_bytes_copied;
} }
// ██████╗ ██╗ ██╗███╗ ███╗███╗ ███╗██╗ ██╗ // ██████ ██ ██ ███ ███ ███ ███ ██ ██
// ██╔══██╗██║ ██║████╗ ████║████╗ ████║╚██╗ ██╔╝ // ██ ██ ██ ██ ████ ████ ████ ████ ██ ██
// ██║ ██║██║ ██║██╔████╔██║██╔████╔██║ ╚████╔╝ // ██ ██ ██ ██ ██ ████ ██ ██ ████ ██ ████
// ██║ ██║██║ ██║██║╚██╔╝██║██║╚██╔╝██║ ╚██╔╝ // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████╔╝╚██████╔╝██║ ╚═╝ ██║██║ ╚═╝ ██║ ██║ // ██████ ██████ ██ ██ ██ ██ ██
// ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝
// //
// >>dummy // >>dummy
#if defined(SOKOL_DUMMY_BACKEND) #if defined(SOKOL_DUMMY_BACKEND)
@ -1345,12 +1430,11 @@ _SOKOL_PRIVATE bool _saudio_dummy_backend_init(void) {
}; };
_SOKOL_PRIVATE void _saudio_dummy_backend_shutdown(void) { }; _SOKOL_PRIVATE void _saudio_dummy_backend_shutdown(void) { };
// █████╗ ██╗ ███████╗ █████╗ // █████ ██ ███████ █████
// ██╔══██╗██║ ██╔════╝██╔══██╗ // ██ ██ ██ ██ ██ ██
// ███████║██║ ███████╗███████║ // ███████ ██ ███████ ███████
// ██╔══██║██║ ╚════██║██╔══██║ // ██ ██ ██ ██ ██ ██
// ██║ ██║███████╗███████║██║ ██║ // ██ ██ ███████ ███████ ██ ██
// ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝ ╚═╝
// //
// >>alsa // >>alsa
#elif defined(_SAUDIO_LINUX) #elif defined(_SAUDIO_LINUX)
@ -1385,7 +1469,7 @@ _SOKOL_PRIVATE bool _saudio_alsa_backend_init(void) {
int dir; uint32_t rate; int dir; uint32_t rate;
int rc = snd_pcm_open(&_saudio.backend.device, "default", SND_PCM_STREAM_PLAYBACK, 0); int rc = snd_pcm_open(&_saudio.backend.device, "default", SND_PCM_STREAM_PLAYBACK, 0);
if (rc < 0) { if (rc < 0) {
SAUDIO_LOG("sokol_audio.h: snd_pcm_open() failed"); _SAUDIO_ERROR(ALSA_SND_PCM_OPEN_FAILED);
return false; return false;
} }
@ -1398,26 +1482,26 @@ _SOKOL_PRIVATE bool _saudio_alsa_backend_init(void) {
snd_pcm_hw_params_any(_saudio.backend.device, params); snd_pcm_hw_params_any(_saudio.backend.device, params);
snd_pcm_hw_params_set_access(_saudio.backend.device, params, SND_PCM_ACCESS_RW_INTERLEAVED); snd_pcm_hw_params_set_access(_saudio.backend.device, params, SND_PCM_ACCESS_RW_INTERLEAVED);
if (0 > snd_pcm_hw_params_set_format(_saudio.backend.device, params, SND_PCM_FORMAT_FLOAT_LE)) { if (0 > snd_pcm_hw_params_set_format(_saudio.backend.device, params, SND_PCM_FORMAT_FLOAT_LE)) {
SAUDIO_LOG("sokol_audio.h: float samples not supported"); _SAUDIO_ERROR(ALSA_FLOAT_SAMPLES_NOT_SUPPORTED);
goto error; goto error;
} }
if (0 > snd_pcm_hw_params_set_buffer_size(_saudio.backend.device, params, (snd_pcm_uframes_t)_saudio.buffer_frames)) { if (0 > snd_pcm_hw_params_set_buffer_size(_saudio.backend.device, params, (snd_pcm_uframes_t)_saudio.buffer_frames)) {
SAUDIO_LOG("sokol_audio.h: requested buffer size not supported"); _SAUDIO_ERROR(ALSA_REQUESTED_BUFFER_SIZE_NOT_SUPPORTED);
goto error; goto error;
} }
if (0 > snd_pcm_hw_params_set_channels(_saudio.backend.device, params, (uint32_t)_saudio.num_channels)) { if (0 > snd_pcm_hw_params_set_channels(_saudio.backend.device, params, (uint32_t)_saudio.num_channels)) {
SAUDIO_LOG("sokol_audio.h: requested channel count not supported"); _SAUDIO_ERROR(ALSA_REQUESTED_CHANNEL_COUNT_NOT_SUPPORTED);
goto error; goto error;
} }
/* let ALSA pick a nearby sampling rate */ /* let ALSA pick a nearby sampling rate */
rate = (uint32_t) _saudio.sample_rate; rate = (uint32_t) _saudio.sample_rate;
dir = 0; dir = 0;
if (0 > snd_pcm_hw_params_set_rate_near(_saudio.backend.device, params, &rate, &dir)) { if (0 > snd_pcm_hw_params_set_rate_near(_saudio.backend.device, params, &rate, &dir)) {
SAUDIO_LOG("sokol_audio.h: snd_pcm_hw_params_set_rate_near() failed"); _SAUDIO_ERROR(ALSA_SND_PCM_HW_PARAMS_SET_RATE_NEAR_FAILED);
goto error; goto error;
} }
if (0 > snd_pcm_hw_params(_saudio.backend.device, params)) { if (0 > snd_pcm_hw_params(_saudio.backend.device, params)) {
SAUDIO_LOG("sokol_audio.h: snd_pcm_hw_params() failed"); _SAUDIO_ERROR(ALSA_SND_PCM_HW_PARAMS_FAILED);
goto error; goto error;
} }
@ -1432,7 +1516,7 @@ _SOKOL_PRIVATE bool _saudio_alsa_backend_init(void) {
/* create the buffer-streaming start thread */ /* create the buffer-streaming start thread */
if (0 != pthread_create(&_saudio.backend.thread, 0, _saudio_alsa_cb, 0)) { if (0 != pthread_create(&_saudio.backend.thread, 0, _saudio_alsa_cb, 0)) {
SAUDIO_LOG("sokol_audio.h: pthread_create() failed"); _SAUDIO_ERROR(ALSA_PTHREAD_CREATE_FAILED);
goto error; goto error;
} }
@ -1454,58 +1538,15 @@ _SOKOL_PRIVATE void _saudio_alsa_backend_shutdown(void) {
_saudio_free(_saudio.backend.buffer); _saudio_free(_saudio.backend.buffer);
}; };
// ██╗ ██╗ █████╗ ███████╗ █████╗ ██████╗ ██╗ // ██ ██ █████ ███████ █████ ██████ ██
// ██║ ██║██╔══██╗██╔════╝██╔══██╗██╔══██╗██║ // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██║ █╗ ██║███████║███████╗███████║██████╔╝██║ // ██ █ ██ ███████ ███████ ███████ ██████ ██
// ██║███╗██║██╔══██║╚════██║██╔══██║██╔═══╝ ██║ // ██ ███ ██ ██ ██ ██ ██ ██ ██ ██
// ╚███╔███╔╝██║ ██║███████║██║ ██║██║ ██║ // ███ ███ ██ ██ ███████ ██ ██ ██ ██
// ╚══╝╚══╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝
// //
// >>wasapi // >>wasapi
#elif defined(_SAUDIO_WINDOWS) #elif defined(_SAUDIO_WINDOWS)
#if defined(_SAUDIO_UWP)
/* Minimal implementation of an IActivateAudioInterfaceCompletionHandler COM object in plain C.
Meant to be a static singleton (always one reference when add/remove reference)
and implements IUnknown and IActivateAudioInterfaceCompletionHandler when queryinterface'd
Do not know why but IActivateAudioInterfaceCompletionHandler's GUID is not the one system queries for,
so I'm advertising the one actually requested.
*/
_SOKOL_PRIVATE HRESULT STDMETHODCALLTYPE _saudio_interface_completion_handler_queryinterface(IActivateAudioInterfaceCompletionHandler* instance, REFIID riid, void** ppvObject) {
if (!ppvObject) {
return E_POINTER;
}
if (IsEqualIID(riid, _SOKOL_AUDIO_WIN32COM_ID(_saudio_IID_IActivateAudioInterface_Completion_Handler)) || IsEqualIID(riid, _SOKOL_AUDIO_WIN32COM_ID(IID_IUnknown)))
{
*ppvObject = (void*)instance;
return S_OK;
}
*ppvObject = NULL;
return E_NOINTERFACE;
}
_SOKOL_PRIVATE ULONG STDMETHODCALLTYPE _saudio_interface_completion_handler_addref_release(IActivateAudioInterfaceCompletionHandler* instance) {
_SOKOL_UNUSED(instance);
return 1;
}
_SOKOL_PRIVATE HRESULT STDMETHODCALLTYPE _saudio_backend_activate_audio_interface_cb(IActivateAudioInterfaceCompletionHandler* instance, IActivateAudioInterfaceAsyncOperation* activateOperation) {
_SOKOL_UNUSED(instance);
WaitForSingleObject(_saudio.backend.interface_activation_mutex, INFINITE);
_saudio.backend.interface_activation_success = TRUE;
HRESULT activation_result;
if (FAILED(activateOperation->lpVtbl->GetActivateResult(activateOperation, &activation_result, (IUnknown**)(&_saudio.backend.audio_client))) || FAILED(activation_result)) {
_saudio.backend.interface_activation_success = FALSE;
}
ReleaseMutex(_saudio.backend.interface_activation_mutex);
return S_OK;
}
#endif // _SAUDIO_UWP
/* fill intermediate buffer with new data and reset buffer_pos */ /* fill intermediate buffer with new data and reset buffer_pos */
_SOKOL_PRIVATE void _saudio_wasapi_fill_buffer(void) { _SOKOL_PRIVATE void _saudio_wasapi_fill_buffer(void) {
if (_saudio_has_callback()) { if (_saudio_has_callback()) {
@ -1593,16 +1634,6 @@ _SOKOL_PRIVATE void _saudio_wasapi_release(void) {
IAudioClient_Release(_saudio.backend.audio_client); IAudioClient_Release(_saudio.backend.audio_client);
_saudio.backend.audio_client = 0; _saudio.backend.audio_client = 0;
} }
#if defined(_SAUDIO_UWP)
if (_saudio.backend.interface_activation_audio_interface_uid_string) {
CoTaskMemFree(_saudio.backend.interface_activation_audio_interface_uid_string);
_saudio.backend.interface_activation_audio_interface_uid_string = 0;
}
if (_saudio.backend.interface_activation_operation) {
IActivateAudioInterfaceAsyncOperation_Release(_saudio.backend.interface_activation_operation);
_saudio.backend.interface_activation_operation = 0;
}
#else
if (_saudio.backend.device) { if (_saudio.backend.device) {
IMMDevice_Release(_saudio.backend.device); IMMDevice_Release(_saudio.backend.device);
_saudio.backend.device = 0; _saudio.backend.device = 0;
@ -1611,7 +1642,6 @@ _SOKOL_PRIVATE void _saudio_wasapi_release(void) {
IMMDeviceEnumerator_Release(_saudio.backend.device_enumerator); IMMDeviceEnumerator_Release(_saudio.backend.device_enumerator);
_saudio.backend.device_enumerator = 0; _saudio.backend.device_enumerator = 0;
} }
#endif
if (0 != _saudio.backend.thread.buffer_end_event) { if (0 != _saudio.backend.thread.buffer_end_event) {
CloseHandle(_saudio.backend.thread.buffer_end_event); CloseHandle(_saudio.backend.thread.buffer_end_event);
_saudio.backend.thread.buffer_end_event = 0; _saudio.backend.thread.buffer_end_event = 0;
@ -1620,70 +1650,30 @@ _SOKOL_PRIVATE void _saudio_wasapi_release(void) {
_SOKOL_PRIVATE bool _saudio_wasapi_backend_init(void) { _SOKOL_PRIVATE bool _saudio_wasapi_backend_init(void) {
REFERENCE_TIME dur; REFERENCE_TIME dur;
/* UWP Threads are CoInitialized by default with a different threading model, and this call fails
See https://github.com/Microsoft/cppwinrt/issues/6#issuecomment-253930637 */
#if defined(_SAUDIO_WIN32)
/* CoInitializeEx could have been called elsewhere already, in which /* CoInitializeEx could have been called elsewhere already, in which
case the function returns with S_FALSE (thus it does not make much case the function returns with S_FALSE (thus it does not make much
sense to check the result) sense to check the result)
*/ */
HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED); HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
_SOKOL_UNUSED(hr); _SOKOL_UNUSED(hr);
#endif
_saudio.backend.thread.buffer_end_event = CreateEvent(0, FALSE, FALSE, 0); _saudio.backend.thread.buffer_end_event = CreateEvent(0, FALSE, FALSE, 0);
if (0 == _saudio.backend.thread.buffer_end_event) { if (0 == _saudio.backend.thread.buffer_end_event) {
SAUDIO_LOG("sokol_audio wasapi: failed to create buffer_end_event"); _SAUDIO_ERROR(WASAPI_CREATE_EVENT_FAILED);
goto error;
}
#if defined(_SAUDIO_UWP)
_saudio.backend.interface_activation_mutex = CreateMutexA(NULL, FALSE, "interface_activation_mutex");
if (_saudio.backend.interface_activation_mutex == NULL) {
SAUDIO_LOG("sokol_audio wasapi: failed to create interface activation mutex");
goto error;
}
if (FAILED(StringFromIID(_SOKOL_AUDIO_WIN32COM_ID(_saudio_IID_Devinterface_Audio_Render), &_saudio.backend.interface_activation_audio_interface_uid_string))) {
SAUDIO_LOG("sokol_audio wasapi: failed to get default audio device ID string");
goto error;
}
/* static instance of the fake COM object */
static IActivateAudioInterfaceCompletionHandlerVtbl completion_handler_interface_vtable = {
_saudio_interface_completion_handler_queryinterface,
_saudio_interface_completion_handler_addref_release,
_saudio_interface_completion_handler_addref_release,
_saudio_backend_activate_audio_interface_cb
};
static IActivateAudioInterfaceCompletionHandler completion_handler_interface = { &completion_handler_interface_vtable };
if (FAILED(ActivateAudioInterfaceAsync(_saudio.backend.interface_activation_audio_interface_uid_string, _SOKOL_AUDIO_WIN32COM_ID(_saudio_IID_IAudioClient), NULL, &completion_handler_interface, &_saudio.backend.interface_activation_operation))) {
SAUDIO_LOG("sokol_audio wasapi: failed to get default audio device ID string");
goto error;
}
while (!(_saudio.backend.audio_client)) {
if (WaitForSingleObject(_saudio.backend.interface_activation_mutex, 10) != WAIT_TIMEOUT) {
ReleaseMutex(_saudio.backend.interface_activation_mutex);
}
}
if (!(_saudio.backend.interface_activation_success)) {
SAUDIO_LOG("sokol_audio wasapi: interface activation failed. Unable to get audio client");
goto error; goto error;
} }
#else
if (FAILED(CoCreateInstance(_SOKOL_AUDIO_WIN32COM_ID(_saudio_CLSID_IMMDeviceEnumerator), if (FAILED(CoCreateInstance(_SOKOL_AUDIO_WIN32COM_ID(_saudio_CLSID_IMMDeviceEnumerator),
0, CLSCTX_ALL, 0, CLSCTX_ALL,
_SOKOL_AUDIO_WIN32COM_ID(_saudio_IID_IMMDeviceEnumerator), _SOKOL_AUDIO_WIN32COM_ID(_saudio_IID_IMMDeviceEnumerator),
(void**)&_saudio.backend.device_enumerator))) (void**)&_saudio.backend.device_enumerator)))
{ {
SAUDIO_LOG("sokol_audio wasapi: failed to create device enumerator"); _SAUDIO_ERROR(WASAPI_CREATE_DEVICE_ENUMERATOR_FAILED);
goto error; goto error;
} }
if (FAILED(IMMDeviceEnumerator_GetDefaultAudioEndpoint(_saudio.backend.device_enumerator, if (FAILED(IMMDeviceEnumerator_GetDefaultAudioEndpoint(_saudio.backend.device_enumerator,
eRender, eConsole, eRender, eConsole,
&_saudio.backend.device))) &_saudio.backend.device)))
{ {
SAUDIO_LOG("sokol_audio wasapi: GetDefaultAudioEndPoint failed"); _SAUDIO_ERROR(WASAPI_GET_DEFAULT_AUDIO_ENDPOINT_FAILED);
goto error; goto error;
} }
if (FAILED(IMMDevice_Activate(_saudio.backend.device, if (FAILED(IMMDevice_Activate(_saudio.backend.device,
@ -1691,10 +1681,9 @@ _SOKOL_PRIVATE bool _saudio_wasapi_backend_init(void) {
CLSCTX_ALL, 0, CLSCTX_ALL, 0,
(void**)&_saudio.backend.audio_client))) (void**)&_saudio.backend.audio_client)))
{ {
SAUDIO_LOG("sokol_audio wasapi: device activate failed"); _SAUDIO_ERROR(WASAPI_DEVICE_ACTIVATE_FAILED);
goto error; goto error;
} }
#endif
WAVEFORMATEXTENSIBLE fmtex; WAVEFORMATEXTENSIBLE fmtex;
_saudio_clear(&fmtex, sizeof(fmtex)); _saudio_clear(&fmtex, sizeof(fmtex));
@ -1720,22 +1709,22 @@ _SOKOL_PRIVATE bool _saudio_wasapi_backend_init(void) {
AUDCLNT_STREAMFLAGS_EVENTCALLBACK|AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM|AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY, AUDCLNT_STREAMFLAGS_EVENTCALLBACK|AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM|AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY,
dur, 0, (WAVEFORMATEX*)&fmtex, 0))) dur, 0, (WAVEFORMATEX*)&fmtex, 0)))
{ {
SAUDIO_LOG("sokol_audio wasapi: audio client initialize failed"); _SAUDIO_ERROR(WASAPI_AUDIO_CLIENT_INITIALIZE_FAILED);
goto error; goto error;
} }
if (FAILED(IAudioClient_GetBufferSize(_saudio.backend.audio_client, &_saudio.backend.thread.dst_buffer_frames))) { if (FAILED(IAudioClient_GetBufferSize(_saudio.backend.audio_client, &_saudio.backend.thread.dst_buffer_frames))) {
SAUDIO_LOG("sokol_audio wasapi: audio client get buffer size failed"); _SAUDIO_ERROR(WASAPI_AUDIO_CLIENT_GET_BUFFER_SIZE_FAILED);
goto error; goto error;
} }
if (FAILED(IAudioClient_GetService(_saudio.backend.audio_client, if (FAILED(IAudioClient_GetService(_saudio.backend.audio_client,
_SOKOL_AUDIO_WIN32COM_ID(_saudio_IID_IAudioRenderClient), _SOKOL_AUDIO_WIN32COM_ID(_saudio_IID_IAudioRenderClient),
(void**)&_saudio.backend.render_client))) (void**)&_saudio.backend.render_client)))
{ {
SAUDIO_LOG("sokol_audio wasapi: audio client GetService failed"); _SAUDIO_ERROR(WASAPI_AUDIO_CLIENT_GET_SERVICE_FAILED);
goto error; goto error;
} }
if (FAILED(IAudioClient_SetEventHandle(_saudio.backend.audio_client, _saudio.backend.thread.buffer_end_event))) { if (FAILED(IAudioClient_SetEventHandle(_saudio.backend.audio_client, _saudio.backend.thread.buffer_end_event))) {
SAUDIO_LOG("sokol_audio wasapi: audio client SetEventHandle failed"); _SAUDIO_ERROR(WASAPI_AUDIO_CLIENT_SET_EVENT_HANDLE_FAILED);
goto error; goto error;
} }
_saudio.bytes_per_frame = _saudio.num_channels * (int)sizeof(float); _saudio.bytes_per_frame = _saudio.num_channels * (int)sizeof(float);
@ -1748,7 +1737,7 @@ _SOKOL_PRIVATE bool _saudio_wasapi_backend_init(void) {
/* create streaming thread */ /* create streaming thread */
_saudio.backend.thread.thread_handle = CreateThread(NULL, 0, _saudio_wasapi_thread_fn, 0, 0, 0); _saudio.backend.thread.thread_handle = CreateThread(NULL, 0, _saudio_wasapi_thread_fn, 0, 0, 0);
if (0 == _saudio.backend.thread.thread_handle) { if (0 == _saudio.backend.thread.thread_handle) {
SAUDIO_LOG("sokol_audio wasapi: CreateThread failed"); _SAUDIO_ERROR(WASAPI_CREATE_THREAD_FAILED);
goto error; goto error;
} }
return true; return true;
@ -1769,18 +1758,14 @@ _SOKOL_PRIVATE void _saudio_wasapi_backend_shutdown(void) {
IAudioClient_Stop(_saudio.backend.audio_client); IAudioClient_Stop(_saudio.backend.audio_client);
} }
_saudio_wasapi_release(); _saudio_wasapi_release();
#if defined(_SAUDIO_WIN32)
CoUninitialize(); CoUninitialize();
#endif
} }
// ██╗ ██╗███████╗██████╗ █████╗ ██╗ ██╗██████╗ ██╗ ██████╗ // ██ ██ ███████ ██████ █████ ██ ██ ██████ ██ ██████
// ██║ ██║██╔════╝██╔══██╗██╔══██╗██║ ██║██╔══██╗██║██╔═══██╗ // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██║ █╗ ██║█████╗ ██████╔╝███████║██║ ██║██║ ██║██║██║ ██║ // ██ █ ██ █████ ██████ ███████ ██ ██ ██ ██ ██ ██ ██
// ██║███╗██║██╔══╝ ██╔══██╗██╔══██║██║ ██║██║ ██║██║██║ ██║ // ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ╚███╔███╔╝███████╗██████╔╝██║ ██║╚██████╔╝██████╔╝██║╚██████╔╝ // ███ ███ ███████ ██████ ██ ██ ██████ ██████ ██ ██████
// ╚══╝╚══╝ ╚══════╝╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═════╝
// //
// >>webaudio // >>webaudio
#elif defined(_SAUDIO_EMSCRIPTEN) #elif defined(_SAUDIO_EMSCRIPTEN)
@ -1932,12 +1917,11 @@ _SOKOL_PRIVATE void _saudio_webaudio_backend_shutdown(void) {
} }
} }
// █████╗ █████╗ ██╗ ██╗██████╗ ██╗ ██████╗ // █████ █████ ██ ██ ██████ ██ ██████
// ██╔══██╗██╔══██╗██║ ██║██╔══██╗██║██╔═══██╗ // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ███████║███████║██║ ██║██║ ██║██║██║ ██║ // ███████ ███████ ██ ██ ██ ██ ██ ██ ██
// ██╔══██║██╔══██║██║ ██║██║ ██║██║██║ ██║ // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██║ ██║██║ ██║╚██████╔╝██████╔╝██║╚██████╔╝ // ██ ██ ██ ██ ██████ ██████ ██ ██████
// ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═════╝
// //
// >>aaudio // >>aaudio
#elif defined(SAUDIO_ANDROID_AAUDIO) #elif defined(SAUDIO_ANDROID_AAUDIO)
@ -1961,7 +1945,7 @@ _SOKOL_PRIVATE aaudio_data_callback_result_t _saudio_aaudio_data_callback(AAudio
_SOKOL_PRIVATE bool _saudio_aaudio_start_stream(void) { _SOKOL_PRIVATE bool _saudio_aaudio_start_stream(void) {
if (AAudioStreamBuilder_openStream(_saudio.backend.builder, &_saudio.backend.stream) != AAUDIO_OK) { if (AAudioStreamBuilder_openStream(_saudio.backend.builder, &_saudio.backend.stream) != AAUDIO_OK) {
SAUDIO_LOG("sokol_audio.h aaudio: AAudioStreamBuilder_openStream failed"); _SAUDIO_ERROR(AAUDIO_STREAMBUILDER_OPEN_STREAM_FAILED);
return false; return false;
} }
AAudioStream_requestStart(_saudio.backend.stream); AAudioStream_requestStart(_saudio.backend.stream);
@ -1978,7 +1962,7 @@ _SOKOL_PRIVATE void _saudio_aaudio_stop_stream(void) {
_SOKOL_PRIVATE void* _saudio_aaudio_restart_stream_thread_fn(void* param) { _SOKOL_PRIVATE void* _saudio_aaudio_restart_stream_thread_fn(void* param) {
_SOKOL_UNUSED(param); _SOKOL_UNUSED(param);
SAUDIO_LOG("sokol_audio.h aaudio: restarting stream after error"); _SAUDIO_WARN(AAUDIO_RESTARTING_STREAM_AFTER_ERROR);
pthread_mutex_lock(&_saudio.backend.mutex); pthread_mutex_lock(&_saudio.backend.mutex);
_saudio_aaudio_stop_stream(); _saudio_aaudio_stop_stream();
_saudio_aaudio_start_stream(); _saudio_aaudio_start_stream();
@ -1991,7 +1975,7 @@ _SOKOL_PRIVATE void _saudio_aaudio_error_callback(AAudioStream* stream, void* us
_SOKOL_UNUSED(user_data); _SOKOL_UNUSED(user_data);
if (error == AAUDIO_ERROR_DISCONNECTED) { if (error == AAUDIO_ERROR_DISCONNECTED) {
if (0 != pthread_create(&_saudio.backend.thread, 0, _saudio_aaudio_restart_stream_thread_fn, 0)) { if (0 != pthread_create(&_saudio.backend.thread, 0, _saudio_aaudio_restart_stream_thread_fn, 0)) {
SAUDIO_LOG("sokol_audio.h aaudio: pthread_create failed in _saudio_audio_error_callback"); _SAUDIO_ERROR(AAUDIO_PTHREAD_CREATE_FAILED);
} }
} }
} }
@ -2008,7 +1992,7 @@ _SOKOL_PRIVATE void _saudio_aaudio_backend_shutdown(void) {
} }
_SOKOL_PRIVATE bool _saudio_aaudio_backend_init(void) { _SOKOL_PRIVATE bool _saudio_aaudio_backend_init(void) {
SAUDIO_LOG("sokol_audio.h: using AAudio backend"); _SAUDIO_INFO(USING_AAUDIO_BACKEND);
_saudio.bytes_per_frame = _saudio.num_channels * (int)sizeof(float); _saudio.bytes_per_frame = _saudio.num_channels * (int)sizeof(float);
@ -2017,7 +2001,7 @@ _SOKOL_PRIVATE bool _saudio_aaudio_backend_init(void) {
pthread_mutex_init(&_saudio.backend.mutex, &attr); pthread_mutex_init(&_saudio.backend.mutex, &attr);
if (AAudio_createStreamBuilder(&_saudio.backend.builder) != AAUDIO_OK) { if (AAudio_createStreamBuilder(&_saudio.backend.builder) != AAUDIO_OK) {
SAUDIO_LOG("sokol_audio.h aaudio: AAudio_createStreamBuilder() failed!"); _SAUDIO_ERROR(AAUDIO_CREATE_STREAMBUILDER_FAILED);
_saudio_aaudio_backend_shutdown(); _saudio_aaudio_backend_shutdown();
return false; return false;
} }
@ -2038,12 +2022,11 @@ _SOKOL_PRIVATE bool _saudio_aaudio_backend_init(void) {
return true; return true;
} }
// ██████╗ ██████╗ ███████╗███╗ ██╗███████╗██╗ ███████╗███████╗ // ██████ ██████ ███████ ███ ██ ███████ ██ ███████ ███████
// ██╔═══██╗██╔══██╗██╔════╝████╗ ██║██╔════╝██║ ██╔════╝██╔════╝ // ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██
// ██║ ██║██████╔╝█████╗ ██╔██╗ ██║███████╗██║ █████╗ ███████╗ // ██ ██ ██████ █████ ██ ██ ██ ███████ ██ █████ ███████
// ██║ ██║██╔═══╝ ██╔══╝ ██║╚██╗██║╚════██║██║ ██╔══╝ ╚════██║ // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ╚██████╔╝██║ ███████╗██║ ╚████║███████║███████╗███████╗███████║ // ██████ ██ ███████ ██ ████ ███████ ███████ ███████ ███████
// ╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝╚══════╝╚══════╝╚══════╝╚══════╝
// //
// >>opensles // >>opensles
// >>sles // >>sles
@ -2107,9 +2090,8 @@ _SOKOL_PRIVATE void _saudio_sles_fill_buffer(void) {
} }
_SOKOL_PRIVATE void SLAPIENTRY _saudio_sles_play_cb(SLPlayItf player, void *context, SLuint32 event) { _SOKOL_PRIVATE void SLAPIENTRY _saudio_sles_play_cb(SLPlayItf player, void *context, SLuint32 event) {
(void)(context); _SOKOL_UNUSED(context);
(void)(player); _SOKOL_UNUSED(player);
if (event & SL_PLAYEVENT_HEADATEND) { if (event & SL_PLAYEVENT_HEADATEND) {
_saudio_sles_semaphore_post(&_saudio.backend.buffer_sem, 1); _saudio_sles_semaphore_post(&_saudio.backend.buffer_sem, 1);
} }
@ -2163,7 +2145,7 @@ _SOKOL_PRIVATE void _saudio_sles_backend_shutdown(void) {
} }
_SOKOL_PRIVATE bool _saudio_sles_backend_init(void) { _SOKOL_PRIVATE bool _saudio_sles_backend_init(void) {
SAUDIO_LOG("sokol_audio.h: using OpenSLES backend"); _SAUDIO_INFO(USING_SLES_BACKEND);
_saudio.bytes_per_frame = (int)sizeof(float) * _saudio.num_channels; _saudio.bytes_per_frame = (int)sizeof(float) * _saudio.num_channels;
@ -2180,14 +2162,14 @@ _SOKOL_PRIVATE bool _saudio_sles_backend_init(void) {
/* Create engine */ /* Create engine */
const SLEngineOption opts[] = { { SL_ENGINEOPTION_THREADSAFE, SL_BOOLEAN_TRUE } }; const SLEngineOption opts[] = { { SL_ENGINEOPTION_THREADSAFE, SL_BOOLEAN_TRUE } };
if (slCreateEngine(&_saudio.backend.engine_obj, 1, opts, 0, NULL, NULL ) != SL_RESULT_SUCCESS) { if (slCreateEngine(&_saudio.backend.engine_obj, 1, opts, 0, NULL, NULL ) != SL_RESULT_SUCCESS) {
SAUDIO_LOG("sokol_audio opensles: slCreateEngine failed"); _SAUDIO_ERROR(SLES_CREATE_ENGINE_FAILED);
_saudio_sles_backend_shutdown(); _saudio_sles_backend_shutdown();
return false; return false;
} }
(*_saudio.backend.engine_obj)->Realize(_saudio.backend.engine_obj, SL_BOOLEAN_FALSE); (*_saudio.backend.engine_obj)->Realize(_saudio.backend.engine_obj, SL_BOOLEAN_FALSE);
if ((*_saudio.backend.engine_obj)->GetInterface(_saudio.backend.engine_obj, SL_IID_ENGINE, &_saudio.backend.engine) != SL_RESULT_SUCCESS) { if ((*_saudio.backend.engine_obj)->GetInterface(_saudio.backend.engine_obj, SL_IID_ENGINE, &_saudio.backend.engine) != SL_RESULT_SUCCESS) {
SAUDIO_LOG("sokol_audio opensles: GetInterface->Engine failed"); _SAUDIO_ERROR(SLES_ENGINE_GET_ENGINE_INTERFACE_FAILED);
_saudio_sles_backend_shutdown(); _saudio_sles_backend_shutdown();
return false; return false;
} }
@ -2197,16 +2179,15 @@ _SOKOL_PRIVATE bool _saudio_sles_backend_init(void) {
const SLInterfaceID ids[] = { SL_IID_VOLUME }; const SLInterfaceID ids[] = { SL_IID_VOLUME };
const SLboolean req[] = { SL_BOOLEAN_FALSE }; const SLboolean req[] = { SL_BOOLEAN_FALSE };
if( (*_saudio.backend.engine)->CreateOutputMix(_saudio.backend.engine, &_saudio.backend.output_mix_obj, 1, ids, req) != SL_RESULT_SUCCESS) if ((*_saudio.backend.engine)->CreateOutputMix(_saudio.backend.engine, &_saudio.backend.output_mix_obj, 1, ids, req) != SL_RESULT_SUCCESS) {
{ _SAUDIO_ERROR(SLES_CREATE_OUTPUT_MIX_FAILED);
SAUDIO_LOG("sokol_audio opensles: CreateOutputMix failed");
_saudio_sles_backend_shutdown(); _saudio_sles_backend_shutdown();
return false; return false;
} }
(*_saudio.backend.output_mix_obj)->Realize(_saudio.backend.output_mix_obj, SL_BOOLEAN_FALSE); (*_saudio.backend.output_mix_obj)->Realize(_saudio.backend.output_mix_obj, SL_BOOLEAN_FALSE);
if ((*_saudio.backend.output_mix_obj)->GetInterface(_saudio.backend.output_mix_obj, SL_IID_VOLUME, &_saudio.backend.output_mix_vol) != SL_RESULT_SUCCESS) { if ((*_saudio.backend.output_mix_obj)->GetInterface(_saudio.backend.output_mix_obj, SL_IID_VOLUME, &_saudio.backend.output_mix_vol) != SL_RESULT_SUCCESS) {
SAUDIO_LOG("sokol_audio opensles: GetInterface->OutputMixVol failed"); _SAUDIO_WARN(SLES_MIXER_GET_VOLUME_INTERFACE_FAILED);
} }
} }
@ -2247,22 +2228,22 @@ _SOKOL_PRIVATE bool _saudio_sles_backend_init(void) {
if ((*_saudio.backend.engine)->CreateAudioPlayer(_saudio.backend.engine, &_saudio.backend.player_obj, &src, &_saudio.backend.dst_data_sink, sizeof(ids) / sizeof(ids[0]), ids, req) != SL_RESULT_SUCCESS) if ((*_saudio.backend.engine)->CreateAudioPlayer(_saudio.backend.engine, &_saudio.backend.player_obj, &src, &_saudio.backend.dst_data_sink, sizeof(ids) / sizeof(ids[0]), ids, req) != SL_RESULT_SUCCESS)
{ {
SAUDIO_LOG("sokol_audio opensles: CreateAudioPlayer failed"); _SAUDIO_ERROR(SLES_ENGINE_CREATE_AUDIO_PLAYER_FAILED);
_saudio_sles_backend_shutdown(); _saudio_sles_backend_shutdown();
return false; return false;
} }
(*_saudio.backend.player_obj)->Realize(_saudio.backend.player_obj, SL_BOOLEAN_FALSE); (*_saudio.backend.player_obj)->Realize(_saudio.backend.player_obj, SL_BOOLEAN_FALSE);
if ((*_saudio.backend.player_obj)->GetInterface(_saudio.backend.player_obj, SL_IID_PLAY, &_saudio.backend.player) != SL_RESULT_SUCCESS) { if ((*_saudio.backend.player_obj)->GetInterface(_saudio.backend.player_obj, SL_IID_PLAY, &_saudio.backend.player) != SL_RESULT_SUCCESS) {
SAUDIO_LOG("sokol_audio opensles: GetInterface->Play failed"); _SAUDIO_ERROR(SLES_PLAYER_GET_PLAY_INTERFACE_FAILED);
_saudio_sles_backend_shutdown(); _saudio_sles_backend_shutdown();
return false; return false;
} }
if ((*_saudio.backend.player_obj)->GetInterface(_saudio.backend.player_obj, SL_IID_VOLUME, &_saudio.backend.player_vol) != SL_RESULT_SUCCESS) { if ((*_saudio.backend.player_obj)->GetInterface(_saudio.backend.player_obj, SL_IID_VOLUME, &_saudio.backend.player_vol) != SL_RESULT_SUCCESS) {
SAUDIO_LOG("sokol_audio opensles: GetInterface->PlayerVol failed"); _SAUDIO_ERROR(SLES_PLAYER_GET_VOLUME_INTERFACE_FAILED);
} }
if ((*_saudio.backend.player_obj)->GetInterface(_saudio.backend.player_obj, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &_saudio.backend.player_buffer_queue) != SL_RESULT_SUCCESS) { if ((*_saudio.backend.player_obj)->GetInterface(_saudio.backend.player_obj, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &_saudio.backend.player_buffer_queue) != SL_RESULT_SUCCESS) {
SAUDIO_LOG("sokol_audio opensles: GetInterface->BufferQ failed"); _SAUDIO_ERROR(SLES_PLAYER_GET_BUFFERQUEUE_INTERFACE_FAILED);
_saudio_sles_backend_shutdown(); _saudio_sles_backend_shutdown();
return false; return false;
} }
@ -2288,12 +2269,11 @@ _SOKOL_PRIVATE bool _saudio_sles_backend_init(void) {
return true; return true;
} }
// ██████╗ ██████╗ ██████╗ ███████╗ █████╗ ██╗ ██╗██████╗ ██╗ ██████╗ // ██████ ██████ ██████ ███████ █████ ██ ██ ██████ ██ ██████
// ██╔════╝██╔═══██╗██╔══██╗██╔════╝██╔══██╗██║ ██║██╔══██╗██║██╔═══██╗ // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██║ ██║ ██║██████╔╝█████╗ ███████║██║ ██║██║ ██║██║██║ ██║ // ██ ██ ██ ██████ █████ ███████ ██ ██ ██ ██ ██ ██ ██
// ██║ ██║ ██║██╔══██╗██╔══╝ ██╔══██║██║ ██║██║ ██║██║██║ ██║ // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ╚██████╗╚██████╔╝██║ ██║███████╗██║ ██║╚██████╔╝██████╔╝██║╚██████╔╝ // ██████ ██████ ██ ██ ███████ ██ ██ ██████ ██████ ██ ██████
// ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═════╝
// //
// >>coreaudio // >>coreaudio
#elif defined(_SAUDIO_APPLE) #elif defined(_SAUDIO_APPLE)
@ -2418,7 +2398,7 @@ _SOKOL_PRIVATE bool _saudio_coreaudio_backend_init(void) {
fmt.mBitsPerChannel = 32; fmt.mBitsPerChannel = 32;
_saudio_OSStatus res = AudioQueueNewOutput(&fmt, _saudio_coreaudio_callback, 0, NULL, NULL, 0, &_saudio.backend.ca_audio_queue); _saudio_OSStatus res = AudioQueueNewOutput(&fmt, _saudio_coreaudio_callback, 0, NULL, NULL, 0, &_saudio.backend.ca_audio_queue);
if (0 != res) { if (0 != res) {
SAUDIO_LOG("sokol_audio.h: AudioQueueNewOutput() failed!\n"); _SAUDIO_ERROR(COREAUDIO_NEW_OUTPUT_FAILED);
return false; return false;
} }
SOKOL_ASSERT(_saudio.backend.ca_audio_queue); SOKOL_ASSERT(_saudio.backend.ca_audio_queue);
@ -2429,7 +2409,7 @@ _SOKOL_PRIVATE bool _saudio_coreaudio_backend_init(void) {
const uint32_t buf_byte_size = (uint32_t)_saudio.buffer_frames * fmt.mBytesPerFrame; const uint32_t buf_byte_size = (uint32_t)_saudio.buffer_frames * fmt.mBytesPerFrame;
res = AudioQueueAllocateBuffer(_saudio.backend.ca_audio_queue, buf_byte_size, &buf); res = AudioQueueAllocateBuffer(_saudio.backend.ca_audio_queue, buf_byte_size, &buf);
if (0 != res) { if (0 != res) {
SAUDIO_LOG("sokol_audio.h: AudioQueueAllocateBuffer() failed!\n"); _SAUDIO_ERROR(COREAUDIO_ALLOCATE_BUFFER_FAILED);
_saudio_coreaudio_backend_shutdown(); _saudio_coreaudio_backend_shutdown();
return false; return false;
} }
@ -2444,7 +2424,7 @@ _SOKOL_PRIVATE bool _saudio_coreaudio_backend_init(void) {
/* ...and start playback */ /* ...and start playback */
res = AudioQueueStart(_saudio.backend.ca_audio_queue, NULL); res = AudioQueueStart(_saudio.backend.ca_audio_queue, NULL);
if (0 != res) { if (0 != res) {
SAUDIO_LOG("sokol_audio.h: AudioQueueStart() failed!\n"); _SAUDIO_ERROR(COREAUDIO_START_FAILED);
_saudio_coreaudio_backend_shutdown(); _saudio_coreaudio_backend_shutdown();
return false; return false;
} }
@ -2495,12 +2475,11 @@ void _saudio_backend_shutdown(void) {
#endif #endif
} }
// ██████╗ ██╗ ██╗██████╗ ██╗ ██╗ ██████╗ // ██████ ██ ██ ██████ ██ ██ ██████
// ██╔══██╗██║ ██║██╔══██╗██║ ██║██╔════╝ // ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████╔╝██║ ██║██████╔╝██║ ██║██║ // ██████ ██ ██ ██████ ██ ██ ██
// ██╔═══╝ ██║ ██║██╔══██╗██║ ██║██║ // ██ ██ ██ ██ ██ ██ ██ ██
// ██║ ╚██████╔╝██████╔╝███████╗██║╚██████╗ // ██ ██████ ██████ ███████ ██ ██████
// ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═════╝
// //
// >>public // >>public
SOKOL_API_IMPL void saudio_setup(const saudio_desc* desc) { SOKOL_API_IMPL void saudio_setup(const saudio_desc* desc) {
@ -2524,7 +2503,7 @@ SOKOL_API_IMPL void saudio_setup(const saudio_desc* desc) {
the requested packet size the requested packet size
*/ */
if (0 != (_saudio.buffer_frames % _saudio.packet_frames)) { if (0 != (_saudio.buffer_frames % _saudio.packet_frames)) {
SAUDIO_LOG("sokol_audio.h: actual backend buffer size isn't multiple of requested packet size"); _SAUDIO_ERROR(BACKEND_BUFFER_SIZE_ISNT_MULTIPLE_OF_PACKET_SIZE);
_saudio_backend_shutdown(); _saudio_backend_shutdown();
return; return;
} }

@ -0,0 +1,343 @@
#if defined(SOKOL_IMPL) && !defined(SOKOL_LOG_IMPL)
#define SOKOL_LOG_IMPL
#endif
#ifndef SOKOL_LOG_INCLUDED
/*
sokol_log.h -- common logging callback for sokol headers
Project URL: https://github.com/floooh/sokol
Example code: https://github.com/floooh/sokol-samples
Do this:
#define SOKOL_IMPL or
#define SOKOL_LOG_IMPL
before you include this file in *one* C or C++ file to create the
implementation.
Optionally provide the following defines when building the implementation:
SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
SOKOL_UNREACHABLE() - a guard macro for unreachable code (default: assert(false))
SOKOL_LOG_API_DECL - public function declaration prefix (default: extern)
SOKOL_API_DECL - same as SOKOL_GFX_API_DECL
SOKOL_API_IMPL - public function implementation prefix (default: -)
Optionally define the following for verbose output:
SOKOL_DEBUG - by default this is defined if _DEBUG is defined
OVERVIEW
========
sokol_log.h provides a default logging callback for other sokol headers.
To use the default log callback, just include sokol_log.h and provide
a function pointer to the 'slog_func' function when setting up the
sokol library:
For instance with sokol_audio.h:
#include "sokol_log.h"
...
saudio_setup(&(saudio_desc){ .logger.func = slog_func });
Logging output goes to stderr and/or a platform specific logging subsystem
(which means that in some scenarios you might see logging messages duplicated):
- Windows: stderr + OutputDebugStringA()
- macOS/iOS/Linux: stderr + syslog()
- Emscripten: console.info()/warn()/error()
- Android: __android_log_write()
On Windows with sokol_app.h also note the runtime config items to make
stdout/stderr output visible on the console for WinMain() applications
via sapp_desc.win32_console_attach or sapp_desc.win32_console_create,
however when running in a debugger on Windows, the logging output should
show up on the debug output UI panel.
In debug mode, a log message might look like this:
[sspine][error][id:12] /Users/floh/projects/sokol/util/sokol_spine.h:3472:0:
SKELETON_DESC_NO_ATLAS: no atlas object provided in sspine_skeleton_desc.atlas
The source path and line number is formatted like compiler errors, in some IDEs (like VSCode)
such error messages are clickable.
In release mode, logging is less verbose as to not bloat the executable with string data, but you still get
enough information to identify the type and location of an error:
[sspine][error][id:12][line:3472]
RULES FOR WRITING YOUR OWN LOGGING FUNCTION
===========================================
- must be re-entrant because it might be called from different threads
- must treat **all** provided string pointers as optional (can be null)
- don't store the string pointers, copy the string data instead
- must not return for log level panic
LICENSE
=======
zlib/libpng license
Copyright (c) 2023 Andre Weissflog
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#define SOKOL_LOG_INCLUDED (1)
#include <stdint.h>
#if defined(SOKOL_API_DECL) && !defined(SOKOL_LOG_API_DECL)
#define SOKOL_LOG_API_DECL SOKOL_API_DECL
#endif
#ifndef SOKOL_LOG_API_DECL
#if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_LOG_IMPL)
#define SOKOL_LOG_API_DECL __declspec(dllexport)
#elif defined(_WIN32) && defined(SOKOL_DLL)
#define SOKOL_LOG_API_DECL __declspec(dllimport)
#else
#define SOKOL_LOG_API_DECL extern
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*
Plug this function into the 'logger.func' struct item when initializating any of the sokol
headers. For instance for sokol_audio.h it would loom like this:
saudio_setup(&(saudio_desc){
.logger = {
.func = slog_func
}
});
*/
SOKOL_LOG_API_DECL void slog_func(const char* tag, uint32_t log_level, uint32_t log_item, const char* message, uint32_t line_nr, const char* filename, void* user_data);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // SOKOL_LOG_INCLUDED
// ██ ███ ███ ██████ ██ ███████ ███ ███ ███████ ███ ██ ████████ █████ ████████ ██ ██████ ███ ██
// ██ ████ ████ ██ ██ ██ ██ ████ ████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
// ██ ██ ████ ██ ██████ ██ █████ ██ ████ ██ █████ ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ██ ██ ███████ ███████ ██ ██ ███████ ██ ████ ██ ██ ██ ██ ██ ██████ ██ ████
//
// >>implementation
#ifdef SOKOL_LOG_IMPL
#define SOKOL_LOG_IMPL_INCLUDED (1)
#ifndef SOKOL_API_IMPL
#define SOKOL_API_IMPL
#endif
#ifndef SOKOL_DEBUG
#ifndef NDEBUG
#define SOKOL_DEBUG
#endif
#endif
#ifndef SOKOL_ASSERT
#include <assert.h>
#define SOKOL_ASSERT(c) assert(c)
#endif
#ifndef _SOKOL_PRIVATE
#if defined(__GNUC__) || defined(__clang__)
#define _SOKOL_PRIVATE __attribute__((unused)) static
#else
#define _SOKOL_PRIVATE static
#endif
#endif
#ifndef _SOKOL_UNUSED
#define _SOKOL_UNUSED(x) (void)(x)
#endif
// platform detection
#if defined(__APPLE__)
#define _SLOG_APPLE (1)
#elif defined(__EMSCRIPTEN__)
#define _SLOG_EMSCRIPTEN (1)
#elif defined(_WIN32)
#define _SLOG_WINDOWS (1)
#elif defined(__ANDROID__)
#define _SLOG_ANDROID (1)
#elif defined(__linux__) || defined(__unix__)
#define _SLOG_LINUX (1)
#else
#error "sokol_log.h: unknown platform"
#endif
#include <stdlib.h> // abort
#include <stdio.h> // fputs
#include <stddef.h> // size_t
#if defined(_SLOG_EMSCRIPTEN)
#include <emscripten/emscripten.h>
#elif defined(_SLOG_WINDOWS)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#elif defined(_SLOG_ANDROID)
#include <android/log.h>
#elif defined(_SLOG_LINUX) || defined(_SLOG_APPLE)
#include <syslog.h>
#endif
// size of line buffer (on stack!) in bytes including terminating zero
#define _SLOG_LINE_LENGTH (512)
_SOKOL_PRIVATE char* _slog_append(const char* str, char* dst, char* end) {
if (str) {
char c;
while (((c = *str++) != 0) && (dst < (end - 1))) {
*dst++ = c;
}
}
*dst = 0;
return dst;
}
_SOKOL_PRIVATE char* _slog_itoa(uint32_t x, char* buf, size_t buf_size) {
const size_t max_digits_and_null = 11;
if (buf_size < max_digits_and_null) {
return 0;
}
char* p = buf + max_digits_and_null;
*--p = 0;
do {
*--p = '0' + (x % 10);
x /= 10;
} while (x != 0);
return p;
}
#if defined(_SLOG_EMSCRIPTEN)
EM_JS(void, slog_js_log, (uint32_t level, const char* c_str), {
const str = UTF8ToString(c_str);
switch (level) {
case 0: console.error(str); break;
case 1: console.error(str); break;
case 2: console.warn(str); break;
default: console.info(str); break;
}
});
#endif
SOKOL_API_IMPL void slog_func(const char* tag, uint32_t log_level, uint32_t log_item, const char* message, uint32_t line_nr, const char* filename, void* user_data) {
_SOKOL_UNUSED(user_data);
const char* log_level_str;
switch (log_level) {
case 0: log_level_str = "panic"; break;
case 1: log_level_str = "error"; break;
case 2: log_level_str = "warning"; break;
default: log_level_str = "info"; break;
}
// build log output line
char line_buf[_SLOG_LINE_LENGTH];
char* str = line_buf;
char* end = line_buf + sizeof(line_buf);
char num_buf[32];
if (tag) {
str = _slog_append("[", str, end);
str = _slog_append(tag, str, end);
str = _slog_append("]", str, end);
}
str = _slog_append("[", str, end);
str = _slog_append(log_level_str, str, end);
str = _slog_append("]", str, end);
str = _slog_append("[id:", str, end);
str = _slog_append(_slog_itoa(log_item, num_buf, sizeof(num_buf)), str, end);
str = _slog_append("]", str, end);
// if a filename is provided, build a clickable log message that's compatible with compiler error messages
if (filename) {
str = _slog_append(" ", str, end);
#if defined(_MSC_VER)
// MSVC compiler error format
str = _slog_append(filename, str, end);
str = _slog_append("(", str, end);
str = _slog_append(_slog_itoa(line_nr, num_buf, sizeof(num_buf)), str, end);
str = _slog_append("): ", str, end);
#else
// gcc/clang compiler error format
str = _slog_append(filename, str, end);
str = _slog_append(":", str, end);
str = _slog_append(_slog_itoa(line_nr, num_buf, sizeof(num_buf)), str, end);
str = _slog_append(":0: ", str, end);
#endif
}
else {
str = _slog_append("[line:", str, end);
str = _slog_append(_slog_itoa(line_nr, num_buf, sizeof(num_buf)), str, end);
str = _slog_append("] ", str, end);
}
if (message) {
str = _slog_append("\n\t", str, end);
str = _slog_append(message, str, end);
}
str = _slog_append("\n\n", str, end);
if (0 == log_level) {
str = _slog_append("ABORTING because of [panic]\n", str, end);
(void)str;
}
// print to stderr?
#if defined(_SLOG_LINUX) || defined(_SLOG_WINDOWS) || defined(_SLOG_APPLE)
fputs(line_buf, stderr);
#endif
// platform specific logging calls
#if defined(_SLOG_WINDOWS)
OutputDebugStringA(line_buf);
#elif defined(_SLOG_ANDROID)
int prio;
switch (log_level) {
case 0: prio = ANDROID_LOG_FATAL; break;
case 1: prio = ANDROID_LOG_ERROR; break;
case 2: prio = ANDROID_LOG_WARN; break;
default: prio = ANDROID_LOG_INFO; break;
}
__android_log_write(prio, "SOKOL", line_buf);
#elif defined(_SLOG_EMSCRIPTEN)
slog_js_log(log_level, line_buf);
#elif defined(_SLOG_LINUX) || defined(_SLOG_APPLE)
int prio;
switch (log_level) {
case 0: prio = LOG_CRIT; break;
case 1: prio = LOG_ERR; break;
case 2: prio = LOG_WARNING; break;
default: prio = LOG_INFO; break;
}
syslog(prio, "%s", line_buf);
#endif
if (0 == log_level) {
abort();
}
}
#endif // SOKOL_LOG_IMPL

@ -60,6 +60,8 @@
@npc "Hello, Max. Be careful with the form of your swing, you could get hurt fighting the monsters", @npc "Hello, Max. Be careful with the form of your swing, you could get hurt fighting the monsters",
@player "Can I have the white square?", @player "Can I have the white square?",
@npc "*gives WhiteSquare*", @npc "*gives WhiteSquare*",
@player "Give me the white square",
@npc "I don't have it anymore bozo",
} }
} }
@ -81,6 +83,23 @@
}, },
} }
@training
{
with: Hunter,
data:
{
@player "Join me and fight Death",
@npc "Nonsense! Watch your tongue, or I'll gut you like a fish.",
@player "Sorry! He doesn't seem like a good guy.",
@npc "I trust him a lot more than you, whoever you are.",
@player "Do you trust me now?",
@item_possess WhiteSquare,
@npc "Certainly a strange artifact, you're holding, but it's no death incarnate",
@player "Fine.",
@npc "Certainly.",
},
}
@training @training
{ {
with: John, with: John,
@ -108,10 +127,10 @@
@player "It's ok I got rid of it calm down", @player "It's ok I got rid of it calm down",
@item_discard WhiteSquare, @item_discard WhiteSquare,
@npc "Thanks", @npc "Thanks",
@player "What's up with you?"< @player "What's up with you?",
@npc "I'm going on a crusade. I do not wish to die", @npc "I'm going on a crusade. I do not wish to die",
@player "Too bad", @player "Too bad",
@item_possess WhiteSquare, @item_possess WhiteSquare,
@npc " @npc "Get that THING AWAY FROM ME",
}, },
} }

Loading…
Cancel
Save