From 6d592815c0befc47239beaf48abf73751a5007c5 Mon Sep 17 00:00:00 2001 From: milek7 Date: Fri, 18 Aug 2017 22:40:23 +0200 Subject: [PATCH] sound fixed --- DynObj.cpp | 2 +- Train.cpp | 105 +++++++++++++++++++++++++++++++++++------------------ sound.cpp | 8 +++- sound.h | 1 + stdafx.h | 3 +- 5 files changed, 79 insertions(+), 40 deletions(-) diff --git a/DynObj.cpp b/DynObj.cpp index 48301636..8c4b9409 100644 --- a/DynObj.cpp +++ b/DynObj.cpp @@ -4711,7 +4711,7 @@ void TDynamicObject::LoadMMediaFile(std::string BaseDir, std::string TypeName, for( int i = 0; i < iAxles; i++ ) { parser.getTokens( 1, false ); parser >> dWheelsPosition[ i ]; - parser.getTokens(); + parser.getTokens(1, false); parser >> token; if( token != "end" ) { rsStukot[i] = sound_man->create_sound(token); diff --git a/Train.cpp b/Train.cpp index b6c79106..f53eec75 100644 --- a/Train.cpp +++ b/Train.cpp @@ -6528,101 +6528,134 @@ bool TTrain::LoadMMediaFile(std::string const &asFileName) // hamowanie zwykle: rsBrake = sound_man->create_sound(parser.getToken()); parser.getTokens(4, false); - parser >> rsBrake->gain_mul >> rsBrake->gain_off >> rsBrake->pitch_mul >> rsBrake->pitch_off; - rsBrake->gain_mul /= (1 + mvOccupied->MaxBrakeForce * 1000); - rsBrake->pitch_mul /= (1 + mvOccupied->Vmax); + if (rsBrake) + { + parser >> rsBrake->gain_mul >> rsBrake->gain_off >> rsBrake->pitch_mul >> rsBrake->pitch_off; + rsBrake->gain_mul /= (1 + mvOccupied->MaxBrakeForce * 1000); + rsBrake->pitch_mul /= (1 + mvOccupied->Vmax); + } } else if (token == "slipperysound:") { // sanie: rsSlippery = sound_man->create_sound(parser.getToken()); parser.getTokens(2, false); - parser >> rsSlippery->gain_mul >> rsSlippery->gain_off; - rsSlippery->pitch_mul = 0.0; - rsSlippery->pitch_off = 1.0; - rsSlippery->gain_mul /= (1 + mvOccupied->Vmax); + if (rsSlippery) + { + parser >> rsSlippery->gain_mul >> rsSlippery->gain_off; + rsSlippery->pitch_mul = 0.0; + rsSlippery->pitch_off = 1.0; + rsSlippery->gain_mul /= (1 + mvOccupied->Vmax); + } } else if (token == "airsound:") { // syk: rsHiss = sound_man->create_sound(parser.getToken()); parser.getTokens(2, false); - parser >> rsHiss->gain_mul >> rsHiss->gain_off; - rsHiss->pitch_mul = 0.0; - rsHiss->pitch_off = 1.0; + if (rsHiss) + { + parser >> rsHiss->gain_mul >> rsHiss->gain_off; + rsHiss->pitch_mul = 0.0; + rsHiss->pitch_off = 1.0; + } } else if (token == "airsound2:") { // syk: rsHissU = sound_man->create_sound(parser.getToken()); parser.getTokens(2, false); - parser >> rsHissU->gain_mul >> rsHissU->gain_off; - rsHissU->pitch_mul = 0.0; - rsHissU->pitch_off = 1.0; + if (rsHissU) + { + parser >> rsHissU->gain_mul >> rsHissU->gain_off; + rsHissU->pitch_mul = 0.0; + rsHissU->pitch_off = 1.0; + } } else if (token == "airsound3:") { // syk: rsHissE = sound_man->create_sound(parser.getToken()); parser.getTokens(2, false); - parser >> rsHissE->gain_mul >> rsHissE->gain_off; - rsHissE->pitch_mul = 0.0; - rsHissE->pitch_off = 1.0; + if (rsHissE) + { + parser >> rsHissE->gain_mul >> rsHissE->gain_off; + rsHissE->pitch_mul = 0.0; + rsHissE->pitch_off = 1.0; + } } else if (token == "airsound4:") { // syk: rsHissX = sound_man->create_sound(parser.getToken()); parser.getTokens(2, false); - parser >> rsHissX->gain_mul >> rsHissX->gain_off; - rsHissX->pitch_mul = 0.0; - rsHissX->pitch_off = 1.0; + if (rsHissX) + { + parser >> rsHissX->gain_mul >> rsHissX->gain_off; + rsHissX->pitch_mul = 0.0; + rsHissX->pitch_off = 1.0; + } } else if (token == "airsound5:") { // syk: rsHissT = sound_man->create_sound(parser.getToken()); parser.getTokens(2, false); - parser >> rsHissT->gain_mul >> rsHissT->gain_off; - rsHissT->pitch_mul = 0.0; - rsHissT->pitch_off = 1.0; + if (rsHissT) + { + parser >> rsHissT->gain_mul >> rsHissT->gain_off; + rsHissT->pitch_mul = 0.0; + rsHissT->pitch_off = 1.0; + } } else if (token == "fadesound:") { // syk: rsFadeSound = sound_man->create_sound(parser.getToken()); - rsFadeSound->gain_mul = 1.0; - rsFadeSound->gain_off = 1.0; - rsFadeSound->pitch_mul = 1.0; - rsFadeSound->pitch_off = 1.0; + if (rsFadeSound) + { + rsFadeSound->gain_mul = 1.0; + rsFadeSound->gain_off = 1.0; + rsFadeSound->pitch_mul = 1.0; + rsFadeSound->pitch_off = 1.0; + } } else if (token == "localbrakesound:") { // syk: rsSBHiss = sound_man->create_sound(parser.getToken()); parser.getTokens(2, false); - parser >> rsSBHiss->gain_mul >> rsSBHiss->gain_off; - rsSBHiss->pitch_mul = 0.0; - rsSBHiss->pitch_off = 1.0; + if (rsSBHiss) + { + parser >> rsSBHiss->gain_mul >> rsSBHiss->gain_off; + rsSBHiss->pitch_mul = 0.0; + rsSBHiss->pitch_off = 1.0; + } } else if (token == "runningnoise:") { // szum podczas jazdy: rsRunningNoise = sound_man->create_sound(parser.getToken()); parser.getTokens(4, false); - parser >> rsRunningNoise->gain_mul >> rsRunningNoise->gain_off >> rsRunningNoise->pitch_mul >> - rsRunningNoise->pitch_off; - rsRunningNoise->gain_mul /= (1 + mvOccupied->Vmax); - rsRunningNoise->pitch_mul /= (1 + mvOccupied->Vmax); + if (rsRunningNoise) + { + parser >> rsRunningNoise->gain_mul >> rsRunningNoise->gain_off >> rsRunningNoise->pitch_mul >> + rsRunningNoise->pitch_off; + rsRunningNoise->gain_mul /= (1 + mvOccupied->Vmax); + rsRunningNoise->pitch_mul /= (1 + mvOccupied->Vmax); + } } else if (token == "engageslippery:") { // tarcie tarcz sprzegla: rsEngageSlippery = sound_man->create_sound(parser.getToken()); parser.getTokens(4, false); - parser >> rsEngageSlippery->gain_mul >> rsEngageSlippery->gain_off >> rsEngageSlippery->pitch_mul >> - rsEngageSlippery->pitch_off; - rsEngageSlippery->pitch_mul /= (1 + mvOccupied->nmax); + if (rsEngageSlippery) + { + parser >> rsEngageSlippery->gain_mul >> rsEngageSlippery->gain_off >> rsEngageSlippery->pitch_mul >> + rsEngageSlippery->pitch_off; + rsEngageSlippery->pitch_mul /= (1 + mvOccupied->nmax); + } } else if (token == "mechspring:") { diff --git a/sound.cpp b/sound.cpp index e0d07a5d..79ab5cea 100644 --- a/sound.cpp +++ b/sound.cpp @@ -65,7 +65,7 @@ std::string sound_manager::find_file(std::string name) return name; name.erase(name.rfind('.')); - std::vector exts { ".wav", ".flac", ".ogg" }; + std::vector exts { ".wav", ".WAV", ".flac", ".ogg" }; for (auto const &ext : exts) if (FileExists(name + ext)) return name + ext; @@ -243,6 +243,7 @@ sound::sound() gain_mul = 1.0f; pitch_off = 0.0f; pitch_mul = 1.0f; + dt_sum = 0.0f; } simple_sound::simple_sound(sound_buffer *buf) : sound::sound() @@ -317,12 +318,15 @@ void simple_sound::stop() void sound::update(float dt) { + if (spatial) + dt_sum += dt; if (spatial && pos_dirty) { - glm::vec3 velocity = (pos - last_pos) / dt; // m/s + glm::vec3 velocity = (pos - last_pos) / dt_sum; // m/s alSourcefv(id, AL_VELOCITY, glm::value_ptr(velocity)); last_pos = pos; pos_dirty = false; + dt_sum = 0.0f; } } diff --git a/sound.h b/sound.h index b63ee250..132cff88 100644 --- a/sound.h +++ b/sound.h @@ -41,6 +41,7 @@ class sound { bool pos_dirty; glm::vec3 last_pos; + float dt_sum; protected: float max_dist; diff --git a/stdafx.h b/stdafx.h index 46d2d40b..2d45a0fd 100644 --- a/stdafx.h +++ b/stdafx.h @@ -77,7 +77,7 @@ #include "GL/wglew.h" #endif #define GLFW_INCLUDE_GLU -//m7todo: jest tu bo nie chciało mi się wpychać do wszystkich plików + #include #include #include @@ -86,6 +86,7 @@ #include #include #include +#include #include "openglmatrixstack.h" #define STRINGIZE_DETAIL(x) #x