LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Play tone for duration of space bar press

Solved!
Go to solution

I would like to use the key press event to play a tone for the duration of the key press. Does anyone have an example for how I can do this?

0 Kudos
Message 1 of 11
(3,360 Views)

Hi Liddy,

 

LabVIEW comes with several functions to play "tones" aka waveforms on your speakers. Did you play with them?

They are called SoundOutput-Configure, -Play, -Stop:

check.png

The sound stops as soon as the error data arrives at the sequence frame…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 11
(3,352 Views)

Also use an event structure to capture key down and key up events.  I show them in the same event frame but they could just as easily be separated into different frames.  Check the scancode for the equivalent of spacebar and use this to toggle the sound on/off using Gerd's post above. 

Example_VI_BD.png

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
Message 3 of 11
(3,313 Views)

Thank you. I have been reading documentation and trying to use the key down event. I keep getting an error that there is no case for some selector values (fig. 1 below). I tried to start by turning on an LED during a key down event and turning it off when the key is released. If I can get the key down event to work I'm hoping I can move to turning on =audio for the duration of a key press and off when they key is released. I was able to turn on an LED for the duration of a key press (any key) and off with a key release using the structure in Fig. 2. I essentially want to do the same thing with an audio tone with a frequency of 500 Hz.

 

Key Down Event.PNG

Fig. 1

Space Bar LED.PNG

Fig. 2

0 Kudos
Message 4 of 11
(3,298 Views)

Hi Liddy,

 

you have to create a "default" case when you wire numeric values or create only cases for some items of an enum…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 5 of 11
(3,291 Views)

Thank you for your help. It's been three days and I still cannot figure this out.

0 Kudos
Message 6 of 11
(3,272 Views)
Solution
Accepted by topic author Liddy

I would put the audio portion of the code in an action engine so you can call a play or stop function easily within the event structure.  This works with WAV file format.  Each press of the spacebar restarts the audio file from the beginning.  I think it can be restarted from the last position it stopped but that isn't the way I wrote this.  

main.PNG

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
Download All
0 Kudos
Message 7 of 11
(3,267 Views)

Thank you very much!

0 Kudos
Message 8 of 11
(3,264 Views)

Is there a way I can stop the event structure without using a Boolean Stop? I would like to stop the event structure when the For Loop it is within stops.

0 Kudos
Message 9 of 11
(3,255 Views)

Hi Liddy,

 

Is there a way I can stop the event structure without using a Boolean Stop?

You don't "stop" an event structure…

But you can enforce the event structure to execute within a certain time limit using the timeout event…

 

I would like to stop the event structure when the For Loop it is within stops.

Why is the event structure in a FOR loop? Usually it is located within a WHILE loop!
THINK DATAFLOW: the FOR loop will NOT STOP until the event structure has been executed…
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 10 of 11
(3,240 Views)