Skip to content

Instantly share code, notes, and snippets.

@professionalmoment
Created January 18, 2023 15:05
Show Gist options
  • Save professionalmoment/a502621c9243b196cd08987e811ece62 to your computer and use it in GitHub Desktop.
Save professionalmoment/a502621c9243b196cd08987e811ece62 to your computer and use it in GitHub Desktop.
test a virtual surround setup with miniaudio
#include <math.h>
#include <unistd.h>
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
#include <string.h>
#include <stdio.h>
// name of the device to find
// this is a virtual device which combines 2 stereo devices to get surround
// created with pactl load-module module-null-sink sink_name=combinedDevice object.linger=1 media.class=Audio/Sink channel_map=FL,FR,RL,RR
// then linking monitor of each channel to an output with qpwgraph
#define DEVICE_NAME "combinedDevice Audio/Sink sink"
int main(int argc, char** argv) {
ma_result result;
ma_engine engine;
ma_sound sound;
ma_engine_config engineConfig;
ma_context context;
ma_device_info* deviceInfo;
ma_device_config deviceConfig;
ma_uint32 deviceCount;
ma_device device;
ma_context_config contextConfig = ma_context_config_init();
contextConfig.pulse.pApplicationName = "C AUDIO BACKEND";
ma_backend backends[] = { ma_backend_pulseaudio};
result = ma_context_init(backends, 2, &contextConfig, &context);
if (result != MA_SUCCESS) {
printf("failed to get context\n");
return -1;
}
result = ma_context_get_devices(&context, &deviceInfo, &deviceCount,NULL, NULL);
if (result != MA_SUCCESS) {
printf("Failed to enumerate playback devices.\n");
ma_context_uninit(&context);
return -1;
}
engineConfig = ma_engine_config_init();
printf("DEVICES FOR FUTURE REFERENCE\n");
for (int i = 0; i < deviceCount; i++) {
printf("%d: %s\n", i, deviceInfo[i].name);
}
for (int i = 0; i < deviceCount; i++) {
if (strcmp(deviceInfo[i].name, DEVICE_NAME) == 0) {
printf("using %d: %s\n", i, deviceInfo[i].name);
//deviceConfig.playback.pDeviceID = &deviceInfo[i].id;
engineConfig.pPlaybackDeviceID = &deviceInfo[i].id;
break;
}
}
//deviceConfig.playback.channelMixMode = ma_channel_mix_mode_rectangular;
//deviceConfig.playback.channels = 4;
//ma_channel channelMap[] = {MA_CHANNEL_FRONT_LEFT, MA_CHANNEL_FRONT_RIGHT, MA_CHANNEL_BACK_LEFT, MA_CHANNEL_BACK_RIGHT};
//deviceConfig.playback.pChannelMap = channelMap;
//result = ma_device_init(&context, &deviceConfig, &device);
// engineConfig.channels = 4;
// engineConfig.pPlaybackDeviceID = 0;
// engineConfig.listenerCount = 1;
// engineConfig.sampleRate = 48000;
// engineConfig.pDevice = &device;
// ma_channel channelMap[] = {MA_CHANNEL_FRONT_LEFT, MA_CHANNEL_FRONT_RIGHT, MA_CHANNEL_BACK_LEFT, MA_CHANNEL_BACK_RIGHT};
result = ma_engine_init(&engineConfig, &engine);
if (result != MA_SUCCESS) {
printf("Failed to init audio engine\n");
return -1;
}
// play test sound close to each speaker
result = ma_sound_init_from_file(&engine, "FL.flac", 0, NULL, NULL, &sound);
ma_sound_set_position(&sound, -1.0, 0.0, -1.0);
result = ma_sound_start(&sound);
sleep(2);
ma_sound_uninit(&sound);
result = ma_sound_init_from_file(&engine, "FR.flac", 0, NULL, NULL, &sound);
ma_sound_set_position(&sound, 1.0, 0.0, -1.0);
result = ma_sound_start(&sound);
sleep(2);
ma_sound_uninit(&sound);
result = ma_sound_init_from_file(&engine, "RL.flac", 0, NULL, NULL, &sound);
ma_sound_set_position(&sound, -1.0, 0.0, 1.0);
result = ma_sound_start(&sound);
sleep(2);
ma_sound_uninit(&sound);
result = ma_sound_init_from_file(&engine, "RR.flac", 0, NULL, NULL, &sound);
ma_sound_set_position(&sound, 1.0, 0.0, 1.0);
result = ma_sound_start(&sound);
sleep(2);
ma_sound_uninit(&sound);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment