Add ability to use sound cards with more than 2 channels.

This commit is contained in:
Paul Dino Jones 2024-07-20 16:31:17 -04:00
parent dd20fe3eb4
commit 30a3c3a73f
5 changed files with 18 additions and 14 deletions

View File

@ -4,7 +4,7 @@
#include "../simdevice.h"
#ifdef USE_PULSEAUDIO
int usb_generic_shaker_init(SoundDevice* sounddevice, pa_threaded_mainloop* mainloop, pa_context* context, const char* devname, int volume, int pan, const char* streamname);
int usb_generic_shaker_init(SoundDevice* sounddevice, pa_threaded_mainloop* mainloop, pa_context* context, const char* devname, int volume, int pan, int channels, const char* streamname);
#else
int usb_generic_shaker_init(SoundDevice* sounddevice);
#endif

View File

@ -92,7 +92,7 @@ int usb_generic_shaker_free(SoundDevice* sounddevice)
return err;
}
int usb_generic_shaker_init(SoundDevice* sounddevice, pa_threaded_mainloop* mainloop, pa_context* context, const char* devname, int volume, int pan, const char* streamname)
int usb_generic_shaker_init(SoundDevice* sounddevice, pa_threaded_mainloop* mainloop, pa_context* context, const char* devname, int volume, int pan, int channels, const char* streamname)
{
pa_stream *stream;
@ -100,12 +100,13 @@ int usb_generic_shaker_init(SoundDevice* sounddevice, pa_threaded_mainloop* main
pa_sample_spec sample_specifications;
sample_specifications.format = FORMAT;
sample_specifications.rate = SAMPLE_RATE;
sample_specifications.channels = 2;
sample_specifications.channels = channels;
pa_channel_map channel_map;
pa_channel_map_init_stereo(&channel_map);
pa_channel_map_parse(&channel_map, "front-left,front-right");
pa_channel_map_init_auto(&channel_map, channels, PA_CHANNEL_MAP_DEFAULT);
//pa_channel_map_init_stereo(&channel_map);
//pa_channel_map_parse(&channel_map, "front-left,front-right");
stream = pa_stream_new(context, streamname, &sample_specifications, &channel_map);
@ -128,19 +129,18 @@ int usb_generic_shaker_init(SoundDevice* sounddevice, pa_threaded_mainloop* main
buffer_attr.prebuf = (uint32_t) -1;
buffer_attr.minreq = (uint32_t) -1;
pa_cvolume cv;
pa_cvolume* cv;
pa_cvolume_mute(cv, channels);
uint16_t pvolume = ceil(((double) volume/100.0d)*65535);
// Settings copied as per the chromium browser source
pa_stream_flags_t stream_flags;
stream_flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_NOT_MONOTONIC | PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_ADJUST_LATENCY | PA_STREAM_START_CORKED;
// Connect stream to the default audio output sink
pa_cvolume_set(&cv, sample_specifications.channels, pvolume);
pa_cvolume_set_balance(&cv, &channel_map, pan);
cv->values[pan] = pvolume;
assert(pa_stream_connect_playback(stream, devname, &buffer_attr, stream_flags, &cv, NULL) == 0);
assert(pa_stream_connect_playback(stream, devname, &buffer_attr, stream_flags, cv, NULL) == 0);
// Wait for the stream to be ready
for(;;) {

View File

@ -109,7 +109,7 @@ int sounddev_free(SimDevice* this)
return 0;
}
int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int frequency, int pan, double duration, double threshold)
int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int frequency, int pan, int channels, double duration, double threshold)
{
slogi("initializing standalone sound device...");
@ -117,6 +117,7 @@ int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int
slogi("volume is: %i", volume);
slogi("frequency is: %i", frequency);
slogi("pan is: %i", pan);
slogi("channels is: %i", channels);
slogi("duration is: %f", duration);
@ -165,7 +166,7 @@ int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int
//pa_threaded_mainloop* mainloop;
//pa_context* context;
usb_generic_shaker_init(sounddevice, mainloop, context, devname, volume, pan, streamname);
usb_generic_shaker_init(sounddevice, mainloop, context, devname, volume, pan, channels, streamname);
#else
usb_generic_shaker_init(sounddevice);
#endif
@ -219,7 +220,7 @@ SoundDevice* new_sound_device(DeviceSettings* ds, MonocoqueSettings* ms) {
slogt("Attempting to use device %s", ds->sounddevsettings.dev);
int error = sounddev_init(this, ds->sounddevsettings.dev, ds->sounddevsettings.volume, ds->sounddevsettings.frequency, ds->sounddevsettings.pan, ds->sounddevsettings.duration, ds->threshold);
int error = sounddev_init(this, ds->sounddevsettings.dev, ds->sounddevsettings.volume, ds->sounddevsettings.frequency, ds->sounddevsettings.pan, ds->sounddevsettings.channels, ds->sounddevsettings.duration, ds->threshold);
if (error != 0)
{
free(this);

View File

@ -376,6 +376,7 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
ds->sounddevsettings.lowbound_frequency = -1;
ds->sounddevsettings.upperbound_frequency = -1;
ds->sounddevsettings.pan = 0;
ds->sounddevsettings.channels = 1;
ds->sounddevsettings.duration = 2.0;
if (ds->effect_type == EFFECT_GEARSHIFT)
{
@ -387,6 +388,7 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
config_setting_lookup_int(device_settings, "volume", &ds->sounddevsettings.volume);
config_setting_lookup_int(device_settings, "frequency", &ds->sounddevsettings.frequency);
config_setting_lookup_int(device_settings, "pan", &ds->sounddevsettings.pan);
config_setting_lookup_int(device_settings, "channels", &ds->sounddevsettings.channels);
config_setting_lookup_float(device_settings, "duration", &ds->sounddevsettings.duration);
const char* temp;

View File

@ -129,6 +129,7 @@ typedef struct
int lowbound_frequency;
int upperbound_frequency;
int pan;
int channels;
double duration;
char* dev;
}