c172p-team / c172p

A high detailed version of the Cessna 172P aircraft for FlightGear
GNU General Public License v2.0
79 stars 44 forks source link

Thunder sound #443

Closed gilbertohasnofb closed 8 years ago

gilbertohasnofb commented 9 years ago

According to: http://wiki.flightgear.org/FlightGear_Newsletter_August_2015#Atmospheric_Light_Scattering

Since the simulation writes the position and range of the lightning strike relative to the current eye position into the /environment/lightning node of the Property Tree (lightning-pos-x, lightning-pos-y, and lightning-range), aircraft modellers can use this information to trigger a thunder sound after a delay.

So it would be cool if we implement some thunder sounds in our plane. I can come up with the sound and maybe @onox could give me a hand implementing it.

(Both could be supported if we can spawn sounds at absolute positions).

gilbertohasnofb commented 8 years ago

Did lightning-range work?

Nope, it's fixed at the value 3000. I think that's used for something else.

Btw, ehh.. where's the code that calls thunder()

I don't know where to put it!

Also, a mistake. Where I had:

var delay_seconds = lightning_distance * 340.29;

I should have:

var delay_seconds = lightning_distance / 340.29; Also, I found a problem with the formula

onox commented 8 years ago

You should add a call to setlistener() somewhere on line 234 I think (before the reset_system() call)

Listen for changes to /environment/lightning/lightning-pos-x.

gilbertohasnofb commented 8 years ago

onox, this is a bit beyond me, could you give me a hand on how to program this setlistener() code?

onox commented 8 years ago

Look at the use of it on line 228. First parameter is the property you want to listen for. Second parameter is a function that is called when the property changes. So in your case it would be something like:

setlistener("/environment/lightning/lightning-pos-x", thunder);

Make sure you remove that 'timeout' parameter in thunder() function. (Since it's useless)

gilbertohasnofb commented 8 years ago

Almost there. Now I get an error:

Nasal runtime error: undefined symbol: pow Nasal runtime error: undefined symbol: sqrt

but according to the wiki, these are mathematical functions of Nasal. I tried both:

var lightning_distance = sqrt(pow(lightning_pos_X,2) + pow(lightning_pos_Y,2));

and

var lightning_distance = pow((pow(lightning_pos_X,2) + pow(lightning_pos_Y,2)),0.5);

onox commented 8 years ago

Try math.pow and math.sqrt.

gilbertohasnofb commented 8 years ago

Now I get Nasal runtime error: non numeric argument to pow() when using:

var lightning_distance = math.sqrt(math.pow(lightning_pos_X,2) + math.pow(lightning_pos_Y,2));

onox commented 8 years ago

Call debug.dump(lightning_pos_x); on the line before.

gilbertohasnofb commented 8 years ago

It's dumping '/environment/lightning/lightning-pos-x'

onox commented 8 years ago

Is there a :bulb: appearing above your head already? :wink:

gilbertohasnofb commented 8 years ago

Not really. I obviously can spot the interesting 31m, and I guess the whole thing is a string not a float which is why math.pow is complaining. But I have no idea how to get the 31 out of the string in Nasal.

onox commented 8 years ago

[31m is just ASCII code to give the text a red color. What's more important is that it's dumping a string, not a number. math.pow wants to get a number. It can do nothing with strings.

onox commented 8 years ago

I'll give you another hint: look at somewhere around line 73. Then look back at where you define lightning_pos_X.

gilbertohasnofb commented 8 years ago

getprop() ....

onox commented 8 years ago

:clap: :smile:

gilbertohasnofb commented 8 years ago

It works!!! :smile: I am pushing it now. Thanks a lot, onox!

Also, I was thinking about adding a rain sound to our plane, what do you think? Is there a way of telling if it's raining (but not snowing!) in the sim?

What do you think about that?

onox commented 8 years ago

What do you think about that?

Great idea, I think you can use /environment/rain-norm.

gilbertohasnofb commented 8 years ago

:+1:

onox commented 8 years ago

@gilbertohasnofb Have you heard Sounds/thunder.wav in FGData?

gilbertohasnofb commented 8 years ago

Yes I have, but I don't like it, it has so much clipping and it also sounds too artificial to me. Do you like it better than ours?

onox commented 8 years ago

I cannot hear it right now, because FG is running currently. The problem right now is that we're assuming there's only one flash every 20 seconds. If a second flash occurs, the first is interrupted. We basically need n instances of <thunder> if the maximum number of flashes in 20 seconds is n, in order to prevent interrupting.

It's a bit sad the XML sound system doesn't support templates + Nasal API to trigger sounds :disappointed:

onox commented 8 years ago

@gilbertohasnofb It would be nice if you could strip off that half a second of silence in the beginning and remove some of the near-silence at the end of the file, so that the duration become quite a bit less than 20 seconds.

gilbertohasnofb commented 8 years ago

I see, I will remove that silence in the beginning and fade the sound earlier.

gilbertohasnofb commented 8 years ago

Ok, I managed to shorten it to 9 seconds. I will work on the rain sound now.

onox commented 8 years ago

Thanks.

gilbertohasnofb commented 8 years ago

Rain implemented. So now the only thing missing is to change the volume of the thunder (within a certain range) according to the distance, and then we can make a PR for this branch. And then, if one day someone implements dynamic changes in the position of sounds (I asked for it in the mailing list) then we can make thunder strikes directional.

As for the volume according to the distance, how can I do that? I guess I can already do the math inside that Nasal function so that it outputs some variable proportionally to lightning_distance and between 0 and 1, what do you say?

onox commented 8 years ago

Lets first merge the other branch.

gilbertohasnofb commented 8 years ago

Which other branch?

gilbertohasnofb commented 8 years ago

Ah, the louder wind and engine branch, of course.

onox commented 8 years ago

Is the rain to be positional, like the wind?

onox commented 8 years ago

AFAIK (0,0,0) means no position.

gilbertohasnofb commented 8 years ago

Since the rain's sound is constant (doesn't change with speed) I would say let it be non-positional, so any camera will have the sound of rain. What do you think?

onox commented 8 years ago

Yeah, ok, that's is fine.

onox commented 8 years ago

Do you have any more commits or not? If not, then I can rebase this branch onto latest master.

gilbertohasnofb commented 8 years ago

I don't have any other commits, so let's rebase it before working on the volume.

onox commented 8 years ago

Try git pull --force again.

onox commented 8 years ago

Do we also have a light rain sound and a rain sound of rain hitting the wind shield (front window)?

gilbertohasnofb commented 8 years ago

Try git pull --force again.

Ok, seems to be fine (at least my additions to those files are still there). Output from the command:

git pull --force
remote: Counting objects: 15, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 15 (delta 6), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (15/15), done.
From https://github.com/Juanvvc/c172p-detailed
 + 2ad1903...24812b1 bug-443    -> origin/bug-443  (forced update)
   20e29f3..147ca4b  master     -> origin/master
Auto-merging c172-sound.xml
Merge made by the 'recursive' strategy.
 Systems/c172p-sounds.xml |  2 +-
 Systems/sounds.xml       | 82 ++++++++++++++++++++++++++++++++++++++++++++++++
 c172-sound.xml           | 81 ++++++++++++++++++++++++++++++++++++-----------
 c172p-set.xml            |  4 +++
 4 files changed, 150 insertions(+), 19 deletions(-)
 create mode 100644 Systems/sounds.xml

Do we also have a light rain sound and a rain sound of rain hitting the wind shield?

I just have the sound of some medium rain, which should be fine for all conditions. But I found a good sound licensed as CC-BY-3.0: http://freesound.org/people/geodylabs/sounds/122795/

What do you think? Should we use both sounds (external rain + rain on windshield) when inside the cockpit, and only external rain for other views?

onox commented 8 years ago

Hmm... no, git did a merge for you. Can you do git log --oneline and paste me like first 10 commits?

gilbertohasnofb commented 8 years ago
gilberto@gilberto-Inspiron-7520 ~/WorkingFG/c172p/c172p $ git log --oneline
da1b7fd Merge branch 'bug-443' of https://github.com/Juanvvc/c172p-detailed into
24812b1 rain sound and implementation
3b02b08 shorter thunder sound
fde28f4 thunder sound implementation
0000c0b Add thunder sound
ac74795 Add 'delay' parameter to click() function
ec870e2 thunder sound
147ca4b Merge pull request #478 from Juanvvc/bug-117b
2ad1903 rain sound and implementation
ad335b7 shorter thunder sound
0cb2746 thunder sound implementation
5825581 Add thunder sound
a8c06e8 Add 'delay' parameter to click() function
onox commented 8 years ago

Make that first 20.

onox commented 8 years ago

Can you try git merge-base bug-443 master?

gilbertohasnofb commented 8 years ago
gilberto@gilberto-Inspiron-7520 ~/WorkingFG/c172p/c172p $ git log --oneline
da1b7fd Merge branch 'bug-443' of https://github.com/Juanvvc/c172p-detailed into
24812b1 rain sound and implementation
3b02b08 shorter thunder sound
fde28f4 thunder sound implementation
0000c0b Add thunder sound
ac74795 Add 'delay' parameter to click() function
ec870e2 thunder sound
147ca4b Merge pull request #478 from Juanvvc/bug-117b
2ad1903 rain sound and implementation
ad335b7 shorter thunder sound
0cb2746 thunder sound implementation
5825581 Add thunder sound
a8c06e8 Add 'delay' parameter to click() function
09cadff thunder sound
9c94395 Position wind sound
3248f33 Move volume boost logic to <property-rule> file
f47cc56 Attenuate engine and wind volume if one or both doors are closed
20e29f3 Merge pull request #477 from Juanvvc/bug-476
878fa8d Disable mouse zooming for Chase View Without Yaw as well
8257edb Use mouse scroll wheel to zoom in and out for 'lookat' views
310f3bb Merge pull request #475 from Juanvvc/cleanup
0a1a38e new splash screen no. 10
207e3fe Cleanup miscellaneous files.
gilbertohasnofb commented 8 years ago

Sure:

gilberto@gilberto-Inspiron-7520 ~/WorkingFG/c172p/c172p $ git merge-base bug-443 master
20e29f3dd9e0c4f4832011a1bc21ef99bfbc842b
onox commented 8 years ago

Nice, that's what I expected. 20e29f3dd9e0c4f4832011a1bc21ef99bfbc842b

Do this:

git reset --hard 20e29f3dd9e0c4f4832011a1bc21ef99bfbc842b
git pull
gilbertohasnofb commented 8 years ago
gilberto@gilberto-Inspiron-7520 ~/WorkingFG/c172p/c172p $ git reset --hard 20e29f3dd9e0c4f4832011a1bc21ef99bfbc842b
HEAD is now at 20e29f3 Merge pull request #477 from Juanvvc/bug-476
gilberto@gilberto-Inspiron-7520 ~/WorkingFG/c172p/c172p $ git pull
Updating 20e29f3..24812b1
Fast-forward
 Nasal/c172p.nas          |  36 +++++++++++----
 Sounds/rain.wav          | Bin 0 -> 621258 bytes
 Sounds/thunder.wav       | Bin 0 -> 837944 bytes
 Systems/c172p-sounds.xml |   2 +-
 Systems/sounds.xml       |  82 ++++++++++++++++++++++++++++++++++
 c172-sound.xml           | 112 +++++++++++++++++++++++++++++++++++++++--------
 c172p-set.xml            |   4 ++
 7 files changed, 209 insertions(+), 27 deletions(-)
 create mode 100644 Sounds/rain.wav
 create mode 100644 Sounds/thunder.wav
 create mode 100644 Systems/sounds.xml
onox commented 8 years ago

:+1:

gilbertohasnofb commented 8 years ago

Cool! So back to work: what about those two things I proposed?:

onox commented 8 years ago

Yes, first thing you can do is replace <factor>1.0</factor> in <thunder> with <property>/sim/model/c172p/sound/volume-boost-doors</property>.

onox commented 8 years ago

@gilbertohasnofb Make sure you use spaces, I found tabs in your code :worried: