Skip to content

Instantly share code, notes, and snippets.

@roxlu
Created January 30, 2019 14:27
Show Gist options
  • Save roxlu/3be5bcdfda9ea9dc4ad7a82c64e31dff to your computer and use it in GitHub Desktop.
Save roxlu/3be5bcdfda9ea9dc4ad7a82c64e31dff to your computer and use it in GitHub Desktop.
Crashes with:
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <vector>
#include <poly/mini_al.h>
#include <poly/Log.h>
using namespace poly;
/* -------------------------------------------- */
static mal_uint32 audio_callback(mal_device* dev, mal_uint32 frameCount, void* dest);
/* -------------------------------------------- */
std::ifstream ifs;
std::vector<char> audio_buffer;
size_t bytes_read = 0;
/* -------------------------------------------- */
int main(int argc, char* argv[]) {
poly_log_init(1024, argc, argv);
poly_log_add_sink_stdout();
SX_VERBOSE("MINI AL TEST V1");
mal_decoder dec;
mal_result result;
mal_device device;
mal_device_config config;
config = mal_device_config_init_playback(mal_format_s16, 2, 44100, audio_callback);
result = mal_device_init(nullptr, mal_device_type_playback, nullptr, &config, nullptr, &device);
if (MAL_SUCCESS != result) {
SX_ERROR("Failed to init the device. (exit)");
exit(EXIT_FAILURE);
}
std::ifstream ifs;
ifs.open("./data/test/audio/s16le-both-channels.raw", std::ios::in | std::ios::binary);
if (false == ifs.is_open()) {
SX_ERROR("Failed to open the raw PCM data. (exiting).");
exit(EXIT_FAILURE);
}
audio_buffer.assign(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>());
ifs.close();
if (MAL_SUCCESS != mal_device_start(&device)) {
SX_ERROR("Failed to start the device (exit).");
exit(EXIT_FAILURE);
}
printf("Press enter to quit.");
getchar();
mal_device_uninit(&device);
return 0;
}
/* -------------------------------------------- */
static mal_uint32 audio_callback(mal_device* dev, mal_uint32 frameCount, void* dest) {
size_t to_read = std::min<size_t>(frameCount * sizeof(uint16_t) * 2, audio_buffer.size());
memcpy(dest, (&audio_buffer[0]) + bytes_read, to_read);
bytes_read += to_read;
return to_read;
}
/* -------------------------------------------- */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment