Compare commits

...

14 Commits

Author SHA1 Message Date
Paul Dino Jones b3067e520d Fix latency on pulseaudio backend 2023-11-14 15:31:44 +00:00
Paul Dino Jones b4f459a539 Add basic info for pulseaudio backend into readme 2023-11-13 07:50:10 +00:00
Paul Dino Jones b4ca18c351 Updated config for new pulseaudio backend 2023-11-13 07:46:04 +00:00
Paul Dino Jones 67d0423997 Fix possible causes of leaks and segfaults 2023-11-12 13:27:17 -05:00
Paul Dino Jones 784f71b831 Adding pulseaudio backend 2023-11-11 18:19:39 +00:00
Paul Dino Jones be7d13efbc Improve game detection in gameloop 2023-10-12 05:26:55 +00:00
Paul Dino Jones 208833b052 Update to latest simapi to fix compilation issue 2023-10-11 15:57:55 +00:00
Paul Dino Jones edf8116872 Updated to latest simapi for ACC Support and more RFactor2 support 2023-10-07 19:38:03 +00:00
Paul Dino Jones 744ff916a8 Updating to latest simapi for RFactor2 fixes. 2023-10-01 15:03:19 +00:00
Paul Dino Jones 4d403c6955 Updating to latest simapi to support RFactor2 detection. 2023-09-29 21:26:03 +00:00
Paul Jones 7a75c9999d
Merge pull request #1 from condaatje/patch-1
add workaround for microsoft ssh clone requirements
2023-09-29 16:36:28 +00:00
condaatje 9860139158
Update README.md
remove workaround in favour of default success
2023-09-29 12:14:08 -04:00
condaatje fe22aef0c3
Update .gitmodules
ssh -> https gitmodule
2023-09-29 12:12:47 -04:00
condaatje 2fc81f24fe
add workaround for microsoft ssh clone requirements 2023-09-21 10:06:48 -04:00
19 changed files with 497 additions and 113 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,4 @@
[submodule "src/monocoque/simulatorapi/simapi"] [submodule "src/monocoque/simulatorapi/simapi"]
path = src/monocoque/simulatorapi/simapi path = src/monocoque/simulatorapi/simapi
url = git@github.com:spacefreak18/simapi url = https://github.com/spacefreak18/simapi
# url = git@github.com:spacefreak18/simapi # ssh is better but Microsoft requires an account

View File

@ -33,16 +33,24 @@ FIND_LIBRARY(LIBUSB_LIBRARY NAMES usb-1.0
set(HIDAPI_WITH_LIBUSB TRUE) # surely will be used only on Linux set(HIDAPI_WITH_LIBUSB TRUE) # surely will be used only on Linux
set(BUILD_SHARED_LIBS TRUE) # HIDAPI as static library on all platforms set(BUILD_SHARED_LIBS TRUE) # HIDAPI as static library on all platforms
add_executable(monocoque src/monocoque/monocoque.c)
if(USE_PULSEAUDIO)
message("Using pulseaudio backend...")
add_compile_definitions(USE_PULSEAUDIO=true)
target_link_libraries(monocoque m ${LIBUSB_LIBRARY} hidapi-libusb pulse serialport xml2 argtable2 config gameloop helper devices slog simulatorapi)
else()
message("Using portaudio backend...")
target_link_libraries(monocoque m ${LIBUSB_LIBRARY} hidapi-libusb portaudio serialport xml2 argtable2 config gameloop helper devices slog simulatorapi)
endif()
target_include_directories(monocoque PUBLIC config ${LIBUSB_INCLUDE_DIR} ${LIBXML_INCLUDE_DIR})
add_subdirectory(src/monocoque/gameloop) add_subdirectory(src/monocoque/gameloop)
add_subdirectory(src/monocoque/simulatorapi) add_subdirectory(src/monocoque/simulatorapi)
add_subdirectory(src/monocoque/helper) add_subdirectory(src/monocoque/helper)
add_subdirectory(src/monocoque/devices) add_subdirectory(src/monocoque/devices)
add_subdirectory(src/monocoque/slog) add_subdirectory(src/monocoque/slog)
add_executable(monocoque src/monocoque/monocoque.c)
target_include_directories(monocoque PUBLIC config ${LIBUSB_INCLUDE_DIR} ${LIBXML_INCLUDE_DIR})
target_link_libraries(monocoque m ${LIBUSB_LIBRARY} hidapi-libusb portaudio serialport xml2 argtable2 config gameloop helper devices slog simulatorapi)
add_executable(listusb tests/testlibusb.c) add_executable(listusb tests/testlibusb.c)
target_include_directories(listusb PUBLIC ${LIBUSB_INCLUDE_DIR}) target_include_directories(listusb PUBLIC ${LIBUSB_INCLUDE_DIR})
target_link_libraries(listusb ${LIBUSB_LIBRARY} portaudio) target_link_libraries(listusb ${LIBUSB_LIBRARY} portaudio)

View File

@ -16,12 +16,14 @@ Cross Platform device manager for driving and flight simulators, for use with co
- Includes utility to configure revburner tachometer - Includes utility to configure revburner tachometer
- Can send data to any serial device. So far only tested with arduino. Includes sample arduino sketch for sim lights. - Can send data to any serial device. So far only tested with arduino. Includes sample arduino sketch for sim lights.
- The support for haptic bass shakers is limited and needs the most work. So far the engine rev is a simple sine wave, which I find convincing. The gear shift event works but not convincing enough for me. - The support for haptic bass shakers is limited and needs the most work. So far the engine rev is a simple sine wave, which I find convincing. The gear shift event works but not convincing enough for me.
- Choice of Portaudio or Pulseaudio backend.
## Dependencies ## Dependencies
- libserialport - arduino serial devices - libserialport - arduino serial devices
- hidapi - usb hid devices - hidapi - usb hid devices
- libusb - used by hidapi - libusb - used by hidapi
- portaudio - sound devices (haptic bass shakers) - portaudio - sound devices (haptic bass shakers)
- pulseaudio - sound devices (haptic bass shakers)
- libenet - UDP support (not yet implemented) - libenet - UDP support (not yet implemented)
- libxml2 - libxml2
- argtable2 - argtable2
@ -36,12 +38,19 @@ This code depends on the shared memory data headers in the simapi [repo](https:/
git submodule sync --recursive git submodule sync --recursive
git submodule update --init --recursive git submodule update --init --recursive
``` ```
Then to compile simply: Then to compile simply:
``` ```
mkdir build; cd build mkdir build; cd build
cmake .. cmake ..
make make
``` ```
to use the pulseaudio backend use this cmake command
```
cmake -DUSE_PULSEAUDIO=YES ..
```
## Testing ## Testing
### Setting up Your Arduino Device ### Setting up Your Arduino Device

View File

@ -6,11 +6,18 @@ devices = ( { device = "USB";
granularity = 4; granularity = 4;
config = "/home/paul/.config/monocoque/revburner1.xml"; }, config = "/home/paul/.config/monocoque/revburner1.xml"; },
{ device = "Sound"; { device = "Sound";
type = "Engine" type = "Engine";
devid = "98FD:83AC"; }, devid = "alsa_output.usb-Generic_USB2.0_Device_20170726905959-00.analog-stereo";
pan = 1;
volume = 90;
frequency = 27; },
{ device = "Sound"; { device = "Sound";
type = "Gear" type = "Gear"
devid = "98FD:83AC"; }, devid = "alsa_output.usb-Generic_USB2.0_Device_20170726905959-00.analog-stereo";
pan = 1;
duration = .25;
volume = 100;
frequency = 27; },
{ device = "Serial"; { device = "Serial";
type = "ShiftLights"; type = "ShiftLights";
config = "None"; config = "None";

View File

@ -9,10 +9,13 @@ set(devices_source_files
serialdevice.c serialdevice.c
tachdevice.h tachdevice.h
tachdevice.c tachdevice.c
sound.h
sound.c
usb/revburner.h usb/revburner.h
usb/revburner.c usb/revburner.c
sound/usb_generic_shaker.h sound/usb_generic_shaker.h
sound/usb_generic_shaker.c sound/usb_generic_shaker.c
sound/usb_generic_shaker_pulse.c
serial/arduino.h serial/arduino.h
serial/arduino.c serial/arduino.c
) )

View File

@ -9,6 +9,7 @@
#include "../helper/confighelper.h" #include "../helper/confighelper.h"
#include "../simulatorapi/simapi/simapi/simdata.h" #include "../simulatorapi/simapi/simapi/simdata.h"
typedef struct SimDevice SimDevice; typedef struct SimDevice SimDevice;
struct SimDevice struct SimDevice
@ -83,9 +84,13 @@ typedef struct
int id; int id;
SoundType type; SoundType type;
VibrationEffectType effecttype; VibrationEffectType effecttype;
PATestData sounddata; SoundData sounddata;
#ifdef USE_PULSEAUDIO
pa_stream *stream;
#else
PaStreamParameters outputParameters; PaStreamParameters outputParameters;
PaStream* stream; PaStream* stream;
#endif
} }
SoundDevice; SoundDevice;

View File

@ -0,0 +1,79 @@
#include "sound.h"
#include "../slog/slog.h"
#ifdef USE_PULSEAUDIO
pa_threaded_mainloop* mainloop;
pa_context* context;
void context_state_cb(pa_context* context, void* mainloop) {
pa_threaded_mainloop_signal(mainloop, 0);
}
int setupsound()
{
pa_mainloop_api *mainloop_api;
slogi("connecting pulseaudio...");
// Get a mainloop and its context
mainloop = pa_threaded_mainloop_new();
assert(mainloop);
mainloop_api = pa_threaded_mainloop_get_api(mainloop);
context = pa_context_new(mainloop_api, "Monocoque");
assert(context);
pa_context_set_state_callback(context, &context_state_cb, mainloop);
// Lock the mainloop so that it does not run and crash before the context is ready
pa_threaded_mainloop_lock(mainloop);
// Start the mainloop
assert(pa_threaded_mainloop_start(mainloop) == 0);
assert(pa_context_connect(context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) == 0);
// Wait for the context to be ready
for(;;) {
pa_context_state_t context_state = pa_context_get_state(context);
assert(PA_CONTEXT_IS_GOOD(context_state));
if (context_state == PA_CONTEXT_READY) break;
pa_threaded_mainloop_wait(mainloop);
}
slogi("successfully connected pulseaudio...");
return 1;
}
int freesound()
{
if (context)
pa_context_unref(context);
if (mainloop) {
pa_signal_done();
pa_threaded_mainloop_free(mainloop);
}
}
#else
int setupsound()
{
slogi("connecting portaudio...");
}
int freesound()
{
}
#endif

View File

@ -0,0 +1,13 @@
#ifndef _SOUND_H
#define _SOUND_H
#include <pulse/pulseaudio.h>
extern pa_threaded_mainloop* mainloop;
extern pa_context* context;
int setupsound();
int freesound();
#endif

View File

@ -1,3 +1,5 @@
#ifndef USE_PULSEAUDIO
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
@ -10,6 +12,9 @@
#define SAMPLE_RATE (48000) #define SAMPLE_RATE (48000)
#define DURATION 4.0
#define AMPLITUDE .5
#ifndef M_PI #ifndef M_PI
#define M_PI (3.14159265) #define M_PI (3.14159265)
#endif #endif
@ -22,39 +27,27 @@ int patestCallbackEngineRPM(const void* inputBuffer,
PaStreamCallbackFlags statusFlags, PaStreamCallbackFlags statusFlags,
void* userData) void* userData)
{ {
PATestData* data = (PATestData*)userData; SoundData* data = (SoundData*)userData;
float* out = (float*)outputBuffer; float* out = (float*)outputBuffer;
memset(out, 0, framesPerBuffer * 2 * sizeof(float)); memset(out, 0, framesPerBuffer * 2 * sizeof(float));
unsigned int i; unsigned int i;
unsigned int n; unsigned int n;
n = data->n; //n = data->n;
(void) inputBuffer; /* Prevent unused argument warning. */ (void) inputBuffer; /* Prevent unused argument warning. */
for( i=0; i<framesPerBuffer; i++,n++ ) for( i=0; i<framesPerBuffer; i++,n++ )
{ {
float v = 0; static double t = 0.0;
v = data->amp * sin (2 * M_PI * ((float) n) / (float) SAMPLE_RATE); double sample = AMPLITUDE * 32767.0 * sin(2.0 * M_PI * data->curr_frequency * t);
if (n>=data->table_size) t += 1.0 / SAMPLE_RATE;
{ if (t >= DURATION) {
n=0; t = 0.0;
}
*out++ = sample;
*out++ = sample;
} }
if (data->gear_sound_data > 0)
{
*out++ = 0;
*out++ = 0;
}
else
{
*out++ = v;
*out++ = v;
}
}
data->gear_sound_data = 0;
data->n=n;
return 0; return 0;
} }
@ -66,39 +59,33 @@ int patestCallbackGearShift(const void* inputBuffer,
PaStreamCallbackFlags statusFlags, PaStreamCallbackFlags statusFlags,
void* userData) void* userData)
{ {
PATestData* data = (PATestData*)userData; SoundData* data = (SoundData*)userData;
float* out = (float*)outputBuffer; float* out = (float*)outputBuffer;
memset(out, 0, framesPerBuffer * 2 * sizeof(float)); memset(out, 0, framesPerBuffer * 2 * sizeof(float));
unsigned int i; unsigned int i;
unsigned int n; unsigned int n;
n = data->n; //n = data->n;
(void) inputBuffer; /* Prevent unused argument warning. */ (void) inputBuffer; /* Prevent unused argument warning. */
for( i=0; i<framesPerBuffer; i++,n++ ) for( i=0; i<framesPerBuffer; i++,n++ )
{ {
float v = 0; static double t = 0.0;
v = data->amp * sin (2 * M_PI * ((float) n) / (float) SAMPLE_RATE); double sample = 0;
if (data->frequency>0)
if (n>=data->table_size)
{ {
n=0; sample = AMPLITUDE * 32767.0 * sin(2.0 * M_PI * data->curr_frequency * data->curr_duration);
} }
if(data->gear_sound_data > 0)
{
*out++ = v;
*out++ = v;
}
else
{
*out++ = 0;
*out++ = 0;
}
}
data->gear_sound_data = 0;
data->n=n; data->curr_duration += 1.0 / SAMPLE_RATE;
return 0; if (data->curr_duration >= data->duration) {
data->curr_duration = 0.0;
data->curr_frequency = 0;
}
*out++ = sample;
*out++ = sample;
}
} }
int usb_generic_shaker_free(SoundDevice* sounddevice) int usb_generic_shaker_free(SoundDevice* sounddevice)
@ -169,3 +156,5 @@ error:
Pa_Terminate(); Pa_Terminate();
return err; return err;
} }
#endif

View File

@ -1,10 +1,13 @@
#ifndef _USB_GENERIC_SHAKER_H #ifndef _USB_GENERIC_SHAKER_H
#define _USB_GENERIC_SHAKER_H #define _USB_GENERIC_SHAKER_H
//#include "../sounddevice.h"
#include "../simdevice.h" #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);
#else
int usb_generic_shaker_init(SoundDevice* sounddevice); int usb_generic_shaker_init(SoundDevice* sounddevice);
#endif
int usb_generic_shaker_free(SoundDevice* sounddevice); int usb_generic_shaker_free(SoundDevice* sounddevice);
#endif #endif

View File

@ -0,0 +1,163 @@
#ifdef USE_PULSEAUDIO
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include "usb_generic_shaker.h"
#include "../sounddevice.h"
#define FORMAT PA_SAMPLE_S16LE
#define SAMPLE_RATE (48000)
#define AMPLITUDE .5
#define DURATION 4.0
#ifndef M_PI
#define M_PI (3.14159265)
#endif
void gear_sound_stream(pa_stream *s, size_t length, void *userdata) {
SoundData* data = (SoundData*)userdata;
size_t num_samples = length / sizeof(int16_t);
int16_t buffer[num_samples];
for (size_t i = 0; i < num_samples; i++) {
static double t = 0.0;
double sample = 0;
if (data->frequency>0)
{
sample = AMPLITUDE * 32767.0 * sin(2.0 * M_PI * data->curr_frequency * data->curr_duration);
}
buffer[i] = (int16_t)sample;
data->curr_duration += 1.0 / SAMPLE_RATE;
if (data->curr_duration >= data->duration) {
data->curr_duration = 0.0;
data->curr_frequency = 0;
}
}
pa_stream_write(s, buffer, length, NULL, 0LL, PA_SEEK_RELATIVE);
}
void engine_sound_stream(pa_stream *s, size_t length, void *userdata) {
SoundData* data = (SoundData*)userdata;
size_t num_samples = length / sizeof(int16_t);
int16_t buffer[num_samples];
for (size_t i = 0; i < num_samples; i++) {
static double t = 0.0;
double sample = AMPLITUDE * 32767.0 * sin(2.0 * M_PI * data->curr_frequency * t);
buffer[i] = (int16_t)sample;
t += 1.0 / SAMPLE_RATE;
if (t >= DURATION) {
t = 0.0;
}
}
pa_stream_write(s, buffer, length, NULL, 0LL, PA_SEEK_RELATIVE);
}
void stream_success_cb(pa_stream *stream, int success, void *userdata) {
return;
}
void stream_state_cb(pa_stream *s, void *mainloop) {
pa_threaded_mainloop_signal(mainloop, 0);
}
int usb_generic_shaker_free(SoundDevice* sounddevice)
{
int err = 0;
//err = Pa_CloseStream( sounddevice->stream );
//if( err != paNoError )
//{
// err = Pa_Terminate();
//}
if (sounddevice->stream)
{
pa_stream_unref(sounddevice->stream);
pa_xfree(sounddevice->stream);
}
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)
{
pa_stream *stream;
// Create a playback stream
pa_sample_spec sample_specifications;
sample_specifications.format = FORMAT;
sample_specifications.rate = SAMPLE_RATE;
sample_specifications.channels = 2;
pa_channel_map channel_map;
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);
pa_stream_set_state_callback(stream, stream_state_cb, mainloop);
if (sounddevice->effecttype == SOUNDEFFECT_GEARSHIFT)
{
pa_stream_set_write_callback(stream, gear_sound_stream, &sounddevice->sounddata);
}
else
{
pa_stream_set_write_callback(stream, engine_sound_stream, &sounddevice->sounddata);
}
// recommended settings, i.e. server uses sensible values
pa_buffer_attr buffer_attr;
buffer_attr.maxlength = (uint32_t) 32767;
buffer_attr.tlength = (uint32_t) -1;
buffer_attr.prebuf = (uint32_t) -1;
buffer_attr.minreq = (uint32_t) -1;
pa_cvolume cv;
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);
assert(pa_stream_connect_playback(stream, devname, &buffer_attr, stream_flags, &cv, NULL) == 0);
// Wait for the stream to be ready
for(;;) {
pa_stream_state_t stream_state = pa_stream_get_state(stream);
assert(PA_STREAM_IS_GOOD(stream_state));
if (stream_state == PA_STREAM_READY) break;
pa_threaded_mainloop_wait(mainloop);
}
//pa_threaded_mainloop_unlock(mainloop);
// Uncork the stream so it will start playing
pa_stream_cork(stream, 0, stream_success_cb, mainloop);
sounddevice->stream = stream;
return 0;
}
#endif

View File

@ -5,6 +5,7 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include "sound.h"
#include "simdevice.h" #include "simdevice.h"
#include "sounddevice.h" #include "sounddevice.h"
#include "sound/usb_generic_shaker.h" #include "sound/usb_generic_shaker.h"
@ -12,13 +13,18 @@
#include "../helper/parameters.h" #include "../helper/parameters.h"
#include "../slog/slog.h" #include "../slog/slog.h"
int gear_sound_set(SoundDevice* sounddevice, SimData* simdata) int gear_sound_set(SoundDevice* sounddevice, SimData* simdata)
{ {
if (sounddevice->sounddata.last_gear != simdata->gear && simdata->gear != 0) if (sounddevice->sounddata.last_gear != simdata->gear && simdata->gear > 1)
{ {
sounddevice->sounddata.gear_sound_data = 3.14; //sounddevice->sounddata.gear_sound_data = 3.14;
sounddevice->sounddata.curr_frequency = sounddevice->sounddata.frequency;
sounddevice->sounddata.curr_duration = 0;
} }
sounddevice->sounddata.last_gear = simdata->gear; sounddevice->sounddata.last_gear = simdata->gear;
slogt("set gear frequency to %i", sounddevice->sounddata.frequency);
} }
// we could make a vtable for these different effects too // we could make a vtable for these different effects too
@ -26,9 +32,10 @@ int sounddev_engine_update(SimDevice* this, SimData* simdata)
{ {
SoundDevice* sounddevice = (void *) this->derived; SoundDevice* sounddevice = (void *) this->derived;
gear_sound_set(sounddevice, simdata); sounddevice->sounddata.curr_frequency = simdata->rpms/60;
//sounddevice->sounddata.table_size = 48000/(sounddevice->sounddata.frequency);
sounddevice->sounddata.table_size = 44100/(simdata->rpms/60); slogt("set engine frequency to %i", sounddevice->sounddata.frequency);
} }
int sounddev_gearshift_update(SimDevice* this, SimData* simdata) int sounddev_gearshift_update(SimDevice* this, SimData* simdata)
@ -49,28 +56,51 @@ int sounddev_free(SimDevice* this)
return 0; return 0;
} }
int sounddev_init(SoundDevice* sounddevice) int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int frequency, int pan, double duration)
{ {
slogi("initializing standalone sound device..."); slogi("initializing standalone sound device...");
sounddevice->sounddata.pitch = 1;
sounddevice->sounddata.pitch = 261.626;
sounddevice->sounddata.amp = 32;
sounddevice->sounddata.left_phase = sounddevice->sounddata.right_phase = 0;
sounddevice->sounddata.table_size = 44100/(100/60);
sounddevice->sounddata.last_gear = 0;
slogi("volume is: %i", volume);
slogi("frequency is: %i", frequency);
slogi("pan is: %i", pan);
slogi("duration is: %f", duration);
sounddevice->sounddata.frequency = frequency;
//sounddevice->sounddata.pitch = 1;
//sounddevice->sounddata.pitch = 261.626;
//sounddevice->sounddata.amp = 32;
//sounddevice->sounddata.left_phase = sounddevice->sounddata.right_phase = 0;
//sounddevice->sounddata.table_size = 48000/(100/60);
sounddevice->sounddata.curr_frequency = 100/60;
const char* streamname= "Engine";
switch (sounddevice->effecttype) { switch (sounddevice->effecttype) {
case (SOUNDEFFECT_GEARSHIFT): case (SOUNDEFFECT_GEARSHIFT):
sounddevice->sounddata.pitch = 500; sounddevice->sounddata.last_gear = 0;
sounddevice->sounddata.amp = 128; //sounddevice->sounddata.pitch = 500;
sounddevice->sounddata.left_phase = sounddevice->sounddata.right_phase = 0; //sounddevice->sounddata.amp = 128;
sounddevice->sounddata.table_size = 44100/(1); //sounddevice->sounddata.left_phase = sounddevice->sounddata.right_phase = 0;
//sounddevice->sounddata.table_size = 48000/(1);
sounddevice->sounddata.duration = duration;
sounddevice->sounddata.curr_duration = duration;
streamname = "Gear";
break; break;
} }
#ifdef USE_PULSEAUDIO
//pa_threaded_mainloop* mainloop;
//pa_context* context;
usb_generic_shaker_init(sounddevice, mainloop, context, devname, volume, pan, streamname);
#else
usb_generic_shaker_init(sounddevice); usb_generic_shaker_init(sounddevice);
#endif
} }
static const vtable engine_sound_simdevice_vtable = { &sounddev_engine_update, &sounddev_free }; static const vtable engine_sound_simdevice_vtable = { &sounddev_engine_update, &sounddev_free };
@ -98,7 +128,9 @@ SoundDevice* new_sound_device(DeviceSettings* ds) {
break; break;
} }
int error = sounddev_init(this); 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);
if (error != 0) if (error != 0)
{ {
free(this); free(this);

View File

@ -1,7 +1,11 @@
#ifndef _SOUNDDEVICE_H #ifndef _SOUNDDEVICE_H
#define _SOUNDDEVICE_H #define _SOUNDDEVICE_H
#ifdef USE_PULSEAUDIO
#include <pulse/pulseaudio.h>
#else
#include "portaudio.h" #include "portaudio.h"
#endif
typedef enum typedef enum
{ {
@ -20,16 +24,13 @@ VibrationEffectType;
#define MAX_TABLE_SIZE (6000) #define MAX_TABLE_SIZE (6000)
typedef struct typedef struct
{ {
float sine[MAX_TABLE_SIZE];
float pitch;
int last_gear; int last_gear;
int left_phase; int volume;
int right_phase; int frequency;
int n; double duration;
int table_size; int curr_frequency;
int amp; double curr_duration;
int gear_sound_data;
} }
PATestData; SoundData;
#endif #endif

View File

@ -183,21 +183,14 @@ int clilooper(SimDevice* devices, int numdevices, Parameters* p, SimData* simdat
} }
sleep(3); sleep(3);
struct termios newsettings, canonicalmode;
tcgetattr(0, &canonicalmode);
newsettings = canonicalmode;
newsettings.c_lflag &= (~ICANON & ~ECHO);
newsettings.c_cc[VMIN] = 1;
newsettings.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &newsettings);
char ch;
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
double update_rate = DEFAULT_UPDATE_RATE; double update_rate = DEFAULT_UPDATE_RATE;
char ch;
int t=0; int t=0;
int s=0; int s=0;
int go = true; bool go = true;
while (go == true) while (go == true && simdata->simstatus > 1)
{ {
simdatamap(simdata, simmap, p->sim); simdatamap(simdata, simmap, p->sim);
showstats(simdata); showstats(simdata);
@ -210,8 +203,8 @@ int clilooper(SimDevice* devices, int numdevices, Parameters* p, SimData* simdat
for (int x = 0; x < numdevices; x++) for (int x = 0; x < numdevices; x++)
{ {
devices[x].update(&devices[x], simdata); devices[x].update(&devices[x], simdata);
} }
if( poll(&mypoll, 1, 1000.0/update_rate) ) if( poll(&mypoll, 1, 1000.0/update_rate) )
{ {
scanf("%c", &ch); scanf("%c", &ch);
@ -221,9 +214,6 @@ int clilooper(SimDevice* devices, int numdevices, Parameters* p, SimData* simdat
} }
} }
} }
fprintf(stdout, "\n");
fflush(stdout);
tcsetattr(0, TCSANOW, &canonicalmode);
simdata->velocity = 0; simdata->velocity = 0;
simdata->rpms = 100; simdata->rpms = 100;
@ -232,6 +222,7 @@ int clilooper(SimDevice* devices, int numdevices, Parameters* p, SimData* simdat
devices[x].update(&devices[x], simdata); devices[x].update(&devices[x], simdata);
} }
fprintf(stdout, "\n");
return 0; return 0;
} }
@ -329,7 +320,7 @@ int tester(SimDevice* devices, int numdevices)
sleep(3); sleep(3);
fprintf(stdout, "Shifting into first gear\n"); fprintf(stdout, "Shifting into first gear\n");
simdata->gear = 1; simdata->gear = 2;
for (int x = 0; x < numdevices; x++) for (int x = 0; x < numdevices; x++)
{ {
devices[x].update(&devices[x], simdata); devices[x].update(&devices[x], simdata);
@ -349,7 +340,7 @@ int tester(SimDevice* devices, int numdevices)
sleep(3); sleep(3);
fprintf(stdout, "Shifting into second gear\n"); fprintf(stdout, "Shifting into second gear\n");
simdata->gear = 2; simdata->gear = 3;
for (int x = 0; x < numdevices; x++) for (int x = 0; x < numdevices; x++)
{ {
devices[x].update(&devices[x], simdata); devices[x].update(&devices[x], simdata);
@ -369,7 +360,7 @@ int tester(SimDevice* devices, int numdevices)
sleep(3); sleep(3);
fprintf(stdout, "Shifting into third gear\n"); fprintf(stdout, "Shifting into third gear\n");
simdata->gear = 3; simdata->gear = 4;
for (int x = 0; x < numdevices; x++) for (int x = 0; x < numdevices; x++)
{ {
devices[x].update(&devices[x], simdata); devices[x].update(&devices[x], simdata);
@ -397,7 +388,7 @@ int tester(SimDevice* devices, int numdevices)
sleep(3); sleep(3);
fprintf(stdout, "Shifting into fourth gear\n"); fprintf(stdout, "Shifting into fourth gear\n");
simdata->gear = 4; simdata->gear = 5;
for (int x = 0; x < numdevices; x++) for (int x = 0; x < numdevices; x++)
{ {
devices[x].update(&devices[x], simdata); devices[x].update(&devices[x], simdata);

View File

@ -11,7 +11,9 @@
#include "confighelper.h" #include "confighelper.h"
#include "../slog/slog.h" #include "../slog/slog.h"
#include "parameters.h"
#include <pulse/pulseaudio.h>
int strcicmp(char const *a, char const *b) int strcicmp(char const *a, char const *b)
{ {
@ -84,6 +86,11 @@ int strtodevsubtype(const char* device_subtype, DeviceSettings* ds, int simdev)
ds->dev_subtype = SIMDEVTYPE_GEARSOUND; ds->dev_subtype = SIMDEVTYPE_GEARSOUND;
break; break;
} }
if (strcicmp(device_subtype, "ABS") == 0)
{
ds->dev_subtype = SIMDEVTYPE_ABSBRAKES;
break;
}
default: default:
ds->is_valid = false; ds->is_valid = false;
slogw("%s does not appear to be a valid device sub type, but attempting to continue with other devices", device_subtype); slogw("%s does not appear to be a valid device sub type, but attempting to continue with other devices", device_subtype);
@ -228,7 +235,7 @@ int loadconfig(const char* config_file, DeviceSettings* ds)
int devsetup(const char* device_type, const char* device_subtype, const char* config_file, MonocoqueSettings* ms, DeviceSettings* ds, config_setting_t* device_settings) int devsetup(const char* device_type, const char* device_subtype, const char* config_file, MonocoqueSettings* ms, DeviceSettings* ds, config_setting_t* device_settings)
{ {
int error = MONOCOQUE_ERROR_NONE; int error = MONOCOQUE_ERROR_NONE;
slogi("Called device setup with %s %s %s", device_type, device_subtype, config_file); //slogt("Called device setup with %s %s %s", device_type, device_subtype, config_file);
ds->dev_type = SIMDEV_UNKNOWN; ds->dev_type = SIMDEV_UNKNOWN;
error = strtodev(device_type, device_subtype, ds); error = strtodev(device_type, device_subtype, ds);
@ -246,6 +253,41 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
return error; return error;
} }
if (ds->dev_type == SIMDEV_SOUND)
{
slogi("reading configured sound device settings");
ds->sounddevsettings.frequency = -1;
ds->sounddevsettings.volume = -1;
ds->sounddevsettings.lowbound_frequency = -1;
ds->sounddevsettings.upperbound_frequency = -1;
ds->sounddevsettings.pan = 0;
ds->sounddevsettings.duration = 2.0;
if (ds->dev_subtype == SIMDEVTYPE_GEARSOUND)
{
ds->sounddevsettings.duration = .125;
}
if (device_settings != NULL)
{
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_float(device_settings, "duration", &ds->sounddevsettings.duration);
const char* temp;
int found = 0;
found = config_setting_lookup_string(device_settings, "devid", &temp);
if (found == 0)
{
ds->sounddevsettings.dev = NULL;
}
else
{
ds->sounddevsettings.dev = strdup(temp);
}
}
}
if (ds->dev_subtype == SIMDEVTYPE_TACHOMETER) if (ds->dev_subtype == SIMDEVTYPE_TACHOMETER)
{ {
if (device_settings != NULL) if (device_settings != NULL)
@ -264,6 +306,8 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
ds->tachsettings.use_pulses = false; ds->tachsettings.use_pulses = false;
} }
} }
if (ds->dev_subtype == SIMDEVTYPE_SIMWIND || ds->dev_subtype == SIMDEVTYPE_SHIFTLIGHTS) if (ds->dev_subtype == SIMDEVTYPE_SIMWIND || ds->dev_subtype == SIMDEVTYPE_SHIFTLIGHTS)
{ {
if (device_settings != NULL) if (device_settings != NULL)
@ -286,7 +330,14 @@ int settingsfree(DeviceSettings ds)
{ {
free(ds.serialdevsettings.portdev); free(ds.serialdevsettings.portdev);
} }
}
}
if (ds.dev_type == SIMDEV_SOUND)
{
if (ds.sounddevsettings.dev != NULL)
{
free(ds.sounddevsettings.dev);
}
}
return 0; return 0;
} }

View File

@ -24,7 +24,8 @@ typedef enum
SIMDEVTYPE_SHIFTLIGHTS = 2, SIMDEVTYPE_SHIFTLIGHTS = 2,
SIMDEVTYPE_SIMWIND = 3, SIMDEVTYPE_SIMWIND = 3,
SIMDEVTYPE_ENGINESOUND = 4, SIMDEVTYPE_ENGINESOUND = 4,
SIMDEVTYPE_GEARSOUND = 5 SIMDEVTYPE_GEARSOUND = 5,
SIMDEVTYPE_ABSBRAKES = 6
} }
DeviceSubType; DeviceSubType;
@ -74,6 +75,18 @@ typedef struct
} }
SerialDeviceSettings; SerialDeviceSettings;
typedef struct
{
int frequency;
int volume;
int lowbound_frequency;
int upperbound_frequency;
int pan;
double duration;
char* dev;
}
SoundDeviceSettings;
typedef struct typedef struct
{ {
bool is_valid; bool is_valid;
@ -81,6 +94,7 @@ typedef struct
DeviceSubType dev_subtype; DeviceSubType dev_subtype;
TachometerSettings tachsettings; TachometerSettings tachsettings;
SerialDeviceSettings serialdevsettings; SerialDeviceSettings serialdevsettings;
SoundDeviceSettings sounddevsettings;
} }
DeviceSettings; DeviceSettings;

View File

@ -104,6 +104,7 @@ ConfigError getParameters(int argc, char** argv, Parameters* p)
else if (nerrors3==0) else if (nerrors3==0)
{ {
p->program_action = A_TEST; p->program_action = A_TEST;
p->verbosity_count = arg_verbosity3->count;
exitcode = E_SUCCESS_AND_DO; exitcode = E_SUCCESS_AND_DO;
} }
else else

View File

@ -7,12 +7,14 @@
#include "gameloop/gameloop.h" #include "gameloop/gameloop.h"
#include "gameloop/tachconfig.h" #include "gameloop/tachconfig.h"
#include "devices/simdevice.h" #include "devices/simdevice.h"
#include "devices/sound.h"
#include "helper/parameters.h" #include "helper/parameters.h"
#include "helper/dirhelper.h" #include "helper/dirhelper.h"
#include "helper/confighelper.h" #include "helper/confighelper.h"
#include "simulatorapi/simapi/simapi/simdata.h" #include "simulatorapi/simapi/simapi/simdata.h"
#include "slog/slog.h" #include "slog/slog.h"
int create_dir(char* dir) int create_dir(char* dir)
{ {
struct stat st = {0}; struct stat st = {0};
@ -149,9 +151,9 @@ int main(int argc, char** argv)
DeviceSettings ds[configureddevices]; DeviceSettings ds[configureddevices];
slogi("found %i devices in configuration", configureddevices); slogi("found %i devices in configuration", configureddevices);
int i = 0; int i = 0;
error = MONOCOQUE_ERROR_NONE;
while (i<configureddevices) while (i<configureddevices)
{ {
error = MONOCOQUE_ERROR_NONE;
DeviceSettings settings; DeviceSettings settings;
config_setting_t* config_device = config_setting_get_elem(config_devices, i); config_setting_t* config_device = config_setting_get_elem(config_devices, i);
@ -162,6 +164,9 @@ int main(int argc, char** argv)
config_setting_lookup_string(config_device, "type", &device_subtype); config_setting_lookup_string(config_device, "type", &device_subtype);
config_setting_lookup_string(config_device, "config", &device_config_file); config_setting_lookup_string(config_device, "config", &device_config_file);
//slogt("device type: %s", device_type);
//slogt("device sub type: %s", device_subtype);
//slogt("device config file: %s", device_config_file);
if (error == MONOCOQUE_ERROR_NONE) if (error == MONOCOQUE_ERROR_NONE)
{ {
error = devsetup(device_type, device_subtype, device_config_file, ms, &settings, config_device); error = devsetup(device_type, device_subtype, device_config_file, ms, &settings, config_device);
@ -179,18 +184,22 @@ int main(int argc, char** argv)
i = 0; i = 0;
int j = 0; int j = 0;
error = MONOCOQUE_ERROR_NONE; error = MONOCOQUE_ERROR_NONE;
setupsound();
SimDevice* devices = malloc(numdevices * sizeof(SimDevice)); SimDevice* devices = malloc(numdevices * sizeof(SimDevice));
int initdevices = devinit(devices, configureddevices, ds); int initdevices = devinit(devices, configureddevices, ds);
if (p->program_action == A_PLAY) if (p->program_action == A_PLAY)
{ {
//slogi("running monocoque in gameloop mode.."); slogi("running monocoque in gameloop mode..");
//error = strtogame(p->sim_string, ms); //error = strtogame(p->sim_string, ms);
//if (error != MONOCOQUE_ERROR_NONE) //if (error != MONOCOQUE_ERROR_NONE)
//{ //{
// goto cleanup_final; // goto cleanup_final;
//} //}
#ifdef USE_PULSEAUDIO
pa_threaded_mainloop_unlock(mainloop);
#endif
error = looper(devices, initdevices, p); error = looper(devices, initdevices, p);
if (error == MONOCOQUE_ERROR_NONE) if (error == MONOCOQUE_ERROR_NONE)
@ -205,6 +214,11 @@ int main(int argc, char** argv)
else else
{ {
slogi("running monocoque in test mode..."); slogi("running monocoque in test mode...");
#ifdef USE_PULSEAUDIO
pa_threaded_mainloop_unlock(mainloop);
#endif
error = tester(devices, initdevices); error = tester(devices, initdevices);
if (error == MONOCOQUE_ERROR_NONE) if (error == MONOCOQUE_ERROR_NONE)
{ {

@ -1 +1 @@
Subproject commit ee41c498ee24c410bc551cf09ea68a9fbde0cee7 Subproject commit c77e96339caf1eb79479d89ce342161ad83ca70e