Jump to content
Sign in to follow this  
n4gix

Starting and stopping a looping sound with DirectX

Recommended Posts

Guest ptwaugh

I'm trying to be able to have a sound loop until certain conditions occur which toggle a var, at which point the sound is stopped.This is what I've tried:case PANEL_SERVICE_PRE_UPDATE: // Load GaugeSound Services DLL MGaugeSound = GetModuleHandle("GaugeSound"); if (MGaugeSound == NULL) {MGaugeSound = LoadLibrary("GaugeSound");} GaugePlaySound = (TGaugePlaySound)GetProcAddress(MGaugeSound,"GaugePlaySound"); GaugeStopSound = (TGaugeStopSound)GetProcAddress(MGaugeSound,"GaugeStopSound"); TerminateSounds = (TTerminateSounds)GetProcAddress(MGaugeSound,"TerminateSounds"); //Show/hide the switch bitmaps by showing and hiding the icons if ( low_rrpm ) { // LOW ROTOR WARNING LIGHT ON SHOW_LISTELEMENT(pgauge->elements_list[0],1); // Audio warning on (if not active) if (WARNING_LRRPM_PLAYING == 0) { //(GaugePlaySound)("soundBenchmark.B206B3LowRotor.Loop.wav", "LRRPM", 1); WARNING_LRRPM_PLAYING = 1; } }else{ // LOW ROTOR WARNING LIGHT OFF HIDE_LISTELEMENT(pgauge->elements_list[0],1); // Audio warning off (GaugeStopSound)("LRRPM"); //(TerminateSounds)(); WARNING_LRRPM_PLAYING = 0; }----------I am unable to stop the sound without stopping all sounds. Someone suggested that perhaps this is because the sound is being started over and over (each time PANEL_SERVICE_PRE_UPDATE is called).So, I tried to use one variable to tell me if the sound should be sounding, and another to avoid toggling it on again if it was on already. That didn't work either.Can anyone make a suggestion?Thanks

Share this post


Link to post
Share on other sites

Nothing wrong with your code. As soon as I tried to compile gauges in C++, I ran into the same problem. I think you are OK as long as you stick to C. I presume there is a problem somewhere with the definition of LPTSTR, or more likely, with GaugePlaySound or GaugeStopSound.If compiling in C is not an option, my xml sound gauge will work equally well with compiled gauges as with xml gauges.Doug Dawson

Share this post


Link to post
Share on other sites
Guest ptwaugh

Doug,Thanks for looking at this, but the DirectSound dll is fully debuged (and not my work), and I'm not sure what LPSTR you are talking about as there isn't one in my code above.I'm thinking the problem is that the dll gets called multiple times per sec, thus starting a bizzillon sound buffers, and that is why I can't shut it down by name. That's why I'm wondering if maybe I have it in the wrong "section".Patrick

Share this post


Link to post
Share on other sites

PatrickThe LPTSTR is the defined format for the parameters of the two functions. You could certainly try taking the definitions out of the PRE_UPDATE case - there's nothing there that needs to be defined any more than once. Try putting them in PANEL_SERVICE_CONNECT_TO_WINDOW, which is only ever executed once in the life of the gauge.Doug

Share this post


Link to post
Share on other sites
Guest Skymed

Heres an XML version using Dougs Sound gauge V3 if it's any help:(L:rotorwarn,bool) (A:Electrical master battery,bool) (L:rotorwarn,bool) 0 == && (A:Rotor rpm pct:1,percent) 90 < && if{ 2 (>L:rotorwarnsnd,enum) } els{ 0 (>L:rotorwarnsnd,enum) } (A:Electrical master battery,bool) 0 == (A:Rotor rpm pct:1,percent) 90 >= || if{ 0 (>L:rotorwarn,bool) } (L:rotorwarn,bool) ! (>L:rotorwarn,bool)

Share this post


Link to post
Share on other sites
Guest ptwaugh

XML is completely different, as you know.Have to determine why the c code isn't doing what it appears it should, so that this can be integrated with otehr systems.

Share this post


Link to post
Share on other sites

Please describe in pseudo-code exactly what it is you want to accomplish. It shouldn't be all that difficult to get working, but from your code above, every time PRE_UPDATE is executed (18 times/sec) you are evaluating the condition if 0, playing the sound, setting the condition to 1, and then setting the condition back to 0... all in the same time slice...If you are wanting to "loop" a sound until a condition is reached, then there is a very specific format that GaugeSound.dll expects. For example:case PANEL_SERVICE_PRE_UPDATE:MGaugeSound = GetModuleHandle("GaugeSound");if (MGaugeSound == NULL) {MGaugeSound = LoadLibrary("GaugeSound");}GaugePlaySound = (TGaugePlaySound)GetProcAddress(MGaugeSound,"GaugePlaySound");GaugeStopSound = (TGaugeStopSound)GetProcAddress(MGaugeSound,"GaugeStopSound");TerminateSounds = (TTerminateSounds)GetProcAddress(MGaugeSound,"TerminateSounds");if ( rrpm == 0 ) { (GaugePlaySound)("soundESDGsiren.wav","stop_loop",1) ; } if ( rrpm == 1 ) { (GaugeStopSound)("stop_loop"); ; } break;In this example, the siren will loop until the control variable rrpm is set to 1, at which time the flag variable "stop_loop" is set. BTW, "stop_loop" does NOT have to be defined anywhere. It can be any convenient name, and is passed automatically to the GaugeSound.dll for you. ;)


Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator

Share this post


Link to post
Share on other sites

Here you go Patrick... this will loop the "siren" until either (a) rpm > 2000 -OR- (:( pilot manually cancels by pushing the button. You can easily adapt this for your precise needs:// Initialize variablesfloat time_count=0;int rpm_low_warn=0;float rotor_rpm=0;int cancel=1;MODULE_VAR TICK18var = {TICK18};MODULE_VAR PROPELLER_1_RPMvar = {PROPELLER_1_RPM};// Will blink warning light & sound alarm until canceled or error correctedcase PANEL_SERVICE_PRE_UPDATE:MGaugeSound = GetModuleHandle("GaugeSound");if (MGaugeSound == NULL) {MGaugeSound = LoadLibrary("GaugeSound");}GaugePlaySound = (TGaugePlaySound)GetProcAddress(MGaugeSound,"GaugePlaySound");GaugeStopSound = (TGaugeStopSound)GetProcAddress(MGaugeSound,"GaugeStopSound");TerminateSounds = (TTerminateSounds)GetProcAddress(MGaugeSound,"TerminateSounds");lookup_var(&TICK18var);lookup_var(&STALL_WARNINGvar);lookup_var(&PROPELLER_1_RPMvar);time_count = TICK18var.var_value.n ; rotor_rpm = PROPELLER_1_RPMvar.var_value.n ;// Sound Siren & Blink Light until error corrected or cancelled if ( rotor_rpm < 2000 && cancel == 1 ) { rpm_low_warn = 1 ; }else { rpm_low_warn = 0 ; }// Reset cancel flag if used if ( rotor_rpm > 2000 && cancel == 0 ) { cancel = 1 ; }// Loop Siren until error cleared or cancelled if ( rpm_low_warn == 1 ) { (GaugePlaySound)("soundESDGsiren.wav","stopsiren",1) ; } else { (GaugeStopSound)("stopsiren"); ; } break;BOOL FSAPI cancel_cb( PPIXPOINT relative_point, FLAGS32 mouse_flags){if ( cancel == 1 ) { cancel = 0 ; (GaugePlaySound)("soundESDGpush.wav","",0) ; }else { cancel = 1 ; (GaugePlaySound)("soundESDGpush.wav","",0) ; } return FALSE;}MOUSE_BEGIN(RotorStallWarning_mouse_rect,0,0,0)MOUSE_TOOLTIP_TEXT_STRING ("Click to Cancel",NULL)MOUSE_CHILD_FUNCT(16,11,167,172,CURSOR_HAND,MOUSE_LEFTSINGLE,cancel_cb)MOUSE_END// I use a "slider element": code for the X value// Y value callback is identical...FLOAT64 FSAPI callback2( PELEMENT_SLIDER pelement){FLOAT64 rwertX=pelement->source_var_x.var_value.n;FLOAT64 rwertY=pelement->source_var_y.var_value.n;rwertX = fmod((float)time_count,18) ; if ( rwertX < 9 && (float)rpm_low_warn == 1 ) { SHOW_IMAGE(pelement) ; LIGHT_IMAGE(pelement) ; } else { HIDE_IMAGE(pelement) ; } return rwertX;}


Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator

Share this post


Link to post
Share on other sites

I tried to send this directly, but your email address is "faulty..."Action: failedStatus: 4.4.7 Unable to contact host for 1 days,Diagnostic-Code: smtp; Persistent Transient Failure: Delivery time expiredLast-Attempt-Date: 13 Feb 2005 16:43:36 +0000Here you go, Patrick. Attached is an "EG" version that can easily be modified to accomplish your goal. Maybe more 'bells and whistles" than you actually want, but the core logic is sound and works as expected.Since I don't fly helicopters, I tested this using propeller rpm on a Cirrus SR20GT model I'm working on... ;)I also included the C source files, as well as the .wav sounds...


Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Tom Allensworth,
    Founder of AVSIM Online


  • Flight Simulation's Premier Resource!

    AVSIM is a free service to the flight simulation community. AVSIM is staffed completely by volunteers and all funds donated to AVSIM go directly back to supporting the community. Your donation here helps to pay our bandwidth costs, emergency funding, and other general costs that crop up from time to time. Thank you for your support!

    Click here for more information and to see all donations year to date.
×
×
  • Create New...