Coding Samples

General discussions

Moderator: senjer

Post Reply
User avatar
Sgt_DeBones
Posts: 645
Joined: 05 Apr 2019, 15:23
Location: Los Angeles, California
Contact:

Coding Samples

Post by Sgt_DeBones »

Code: Select all

local soundeffectsutil = require "soundeffectsutil"

function data()
return {
	tracks = {
		{ name = "vehicle/es44ac/6fullminutesofgevo12idle.wav", refDist = 25.0 },
		{ name = "vehicle/es44ac/gevo_733_edit.wav", refDist = 25.0 },
		{ name = "vehicle/es44ac/gevo12_notchsomething.wav", refDist = 25.0 },
		{ name = "vehicle/es44ac/gevo12_notch8.wav", refDist = 25.0 },
           { name = "vehicle/es44ac/x_AC60screamlow.wav", refDist = 25.0 },
		{ name = "vehicle/es44ac/PWR_TractionACHi.wav", refDist = 25.0 },
		{ name = "vehicle/es44ac/ebell.wav", refDist = 25.0 },
		{ name = "vehicle/train/wheels_ringing1.wav", refDist = 25.0 },
		{ name = "vehicle/train_diesel/_brakes.wav", refDist = 25.0 }
	},
	events =  {
		clacks = {
			names = {
				"vehicle/clack/modern/part_1.wav",
				"vehicle/clack/modern/part_2.wav",
				"vehicle/clack/modern/part_3.wav",
				"vehicle/clack/modern/part_4.wav",
				"vehicle/clack/modern/part_5.wav",
				"vehicle/clack/modern/part_6.wav",
				"vehicle/clack/modern/part_7.wav",
				"vehicle/clack/modern/part_8.wav",
				"vehicle/clack/modern/part_9.wav",
				"vehicle/clack/modern/part_10.wav"
			},
			refDist = 15.0
		},
		horn = { names = { "vehicle/es44ac/k5hl(Nathan_K5HL_Family_Horn_Completion)2.wav" }, refDist = 50.0 },
		closeDoors = {
			names = {
				"vehicle/es44ac/k5hl(Nathan_K5HL_Family_Horn_Completion)2.wav",
				"vehicle/es44ac/k5hl(Nathan_K5HL_Family_Horn_Completion)2b.wav"
			},
			refDist = 50.0
		},
	},

	updateFn = function (input)
		local axleRefWeight = 10.0
		
		return {
			tracks = {
				{ 
					gain = soundeffectsutil.sampleCurve({ { .0, 1.0 }, { .1, 0.8 }, { .25, .0 }, { 1.0, 0.0 } }, input.power01),
					pitch = soundeffectsutil.sampleCurve({ { .0, 1.0 }, { .25, 1.3 }, { 1.0, 1.3 } }, input.power01)
				},
				{ 
					gain = soundeffectsutil.sampleCurve({ { .0, .0 }, { .25, 1.0 }, { .5, 0.0 }, { 1.0, 0.0 } }, input.power01),
					pitch = soundeffectsutil.sampleCurve({ { .0, .85 }, { .25, 1.0 }, { .5, 1.19 }, { 1.0, 1.19 } }, input.power01)
				},
				{ 
					gain = soundeffectsutil.sampleCurve({ { .0, .0 }, { .2, 0.0 }, { .5, 1.0 }, { .65, 1.0 }, { .9, 0.0 }, { 1.0, 0.0 } }, input.power01),
					pitch = soundeffectsutil.sampleCurve({ { .0, .8 }, { .25, 0.8 }, { .8, 1.07 }, { 1.0, 1.07 } }, input.power01)
				},
				{ 
					gain = soundeffectsutil.sampleCurve({ { .0, .0 }, { .6, 0.0 }, { 0.85, 1.0 },  { 1.0, 1.0 } }, input.power01),
					pitch = soundeffectsutil.sampleCurve({ { .0, .9 }, { .6, 0.93 }, { 1.0, 1.0 } }, input.power01)
				},
				{ 
					gain = soundeffectsutil.sampleCurve({ { .0, .0 }, { .09, .6 }, { .21, .6 }, { .36, .4 }, { .45, .0 }, { 1.0, .0 } }, input.speed01),
					pitch = soundeffectsutil.sampleCurve({ { .0, 0.82 }, { .25, 1.0 }, { .45, 1.25 }, { 1.0, 1.25  } }, input.speed01)
				},
				{ 
					gain = soundeffectsutil.sampleCurve({ { .0, .0 }, { .26, 0.0 }, { .5, .4 }, { .45, .4 }, { .75, 0.4 }, { 1.0, 0.4 } }, input.speed01),
					pitch = soundeffectsutil.sampleCurve({ { .0, 0.54 }, { .29, 0.54 }, { 0.45, 0.76 }, { 1.0, 1.0 } }, input.speed01)
				},
				{ 
					gain = soundeffectsutil.sampleCurve({ { 0.0, 0.0 }, { 0.001, 1.0 }, { 0.26, 1.0 }, { 0.2666, 0.0 }, { 1.0, 0.0 } }, input.speed01),
					pitch = 1.0
				},
				soundeffectsutil.squeal(input.speed, input.sideForce, input.maxSideForce),
				soundeffectsutil.brake(input.speed, input.brakeDecel, 0.5)
			},
			events = {
				clacks = soundeffectsutil.clacks(input.speed, input.weight, input.numAxles, axleRefWeight, input.gameSpeedUp),
				horn = { gain = 1.0, pitch = 1.0 },
				closeDoors = { gain = 1.0, pitch = 1.0 }
			}
		}
	end
}
end
Here's an example of how to implement doppler effects for sound. This was ripped from a mod in TpF2 for General Electric's Evolution Series locomotives here in the US. To understand, you must have the proper sound files so the simulation can read the corresponding files to function, as seen at the opening strings. Then, you can set the pitch and volume as seen in the second half of the code. This basically manipulates how sounds work, making it actually sound like a train is passing by or you're passing a crossing with bells. The extra sound files listed here at the top are additional sounds to the unit to make them sound as real as they are.

It would be best for those who live in Netherlands to record sounds of the R-Net metro systems and upload them here so they could be implemented into the rolling stocks with this style of functionality.
WW3 is at our doorstep. Gear up just in case. PROTECT THE METROS!
mrLU
Posts: 2
Joined: 23 Aug 2022, 10:31

Re: Coding Samples

Post by mrLU »

I would suggest that a full sound overhaul would perhaps be best for the long term, with such a diverse set of rolling stock now in the sim the looping SG2/SG3 sounds seem a bit out of place. I would be glad to record some sounds for all the London stock.
User avatar
Sgt_DeBones
Posts: 645
Joined: 05 Apr 2019, 15:23
Location: Los Angeles, California
Contact:

Re: Coding Samples

Post by Sgt_DeBones »

Which is exactly what I was eluding to. This method would allow multiple different sounds of a single unit while making them feel more lively and realistic.

You'll have one file simulating the sound of traction motors while another simulates the sound of the engines running. AC traction motors on metros usual goes through different phases of generating energy while moving.

Example, let's say we have the Kinki Sharyo DART SLRV in the simulator. There would be multiple sound files and depending on the speed, the sound transfers from one sound to the other by fainting one out for the other. So at low speed, it'll have the multi-pulse windup, then switches to a sound loop file for coasting at the appropriate speeds (coasting at lower speeds would drag out the initial start file instead of looping).
https://youtu.be/E7LzvPDTBJA


(silly fun off topic fact, the SLRV on the DART network uses the same Westinghouse AEG motors that the LA Metro Siemens P2000 uses; same with the Bombardier T1 and NYC R110A)
WW3 is at our doorstep. Gear up just in case. PROTECT THE METROS!
Post Reply